Skip to content

Commit 2668cf5

Browse files
authored
Allow passing in an http.RoundTripper (#1505)
Signed-off-by: Jon Johnson <[email protected]>
1 parent 0b6b8a0 commit 2668cf5

File tree

6 files changed

+30
-1
lines changed

6 files changed

+30
-1
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/google/go-cmp v0.6.0
1111
github.com/google/go-containerregistry v0.20.3
1212
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
13+
github.com/hashicorp/go-cleanhttp v0.5.2
1314
github.com/hashicorp/go-retryablehttp v0.7.7
1415
github.com/invopop/jsonschema v0.13.0
1516
github.com/klauspost/compress v1.17.11
@@ -97,7 +98,6 @@ require (
9798
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
9899
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.1-0.20210315223345-82c243799c99 // indirect
99100
github.com/grpc-ecosystem/grpc-gateway/v2 v2.24.0 // indirect
100-
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
101101
github.com/inconshreveable/mousetrap v1.1.0 // indirect
102102
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
103103
github.com/jedisct1/go-minisign v0.0.0-20230811132847-661be99b8267 // indirect

pkg/apk/apk/implementation.go

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ func New(options ...Option) (*APK, error) {
9696
}
9797

9898
client := retryablehttp.NewClient()
99+
100+
client.HTTPClient = &http.Client{Transport: opt.transport}
99101
client.Logger = clog.FromContext(context.Background())
100102

101103
return &APK{

pkg/apk/apk/options.go

+15
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
package apk
1616

1717
import (
18+
"net/http"
1819
"os"
1920
"path/filepath"
2021
"runtime"
2122

23+
"github.com/hashicorp/go-cleanhttp"
24+
2225
"chainguard.dev/apko/pkg/apk/auth"
2326
apkfs "chainguard.dev/apko/pkg/apk/fs"
2427
)
@@ -33,6 +36,7 @@ type opts struct {
3336
noSignatureIndexes []string
3437
auth auth.Authenticator
3538
ignoreSignatures bool
39+
transport http.RoundTripper
3640
}
3741

3842
type Option func(*opts) error
@@ -130,10 +134,21 @@ func WithAuthenticator(a auth.Authenticator) Option {
130134
}
131135
}
132136

137+
// WithTransport allows explicitly setting the inner HTTP transport.
138+
func WithTransport(t http.RoundTripper) Option {
139+
return func(o *opts) error {
140+
if t != nil {
141+
o.transport = t
142+
}
143+
return nil
144+
}
145+
}
146+
133147
func defaultOpts() *opts {
134148
return &opts{
135149
arch: ArchToAPK(runtime.GOARCH),
136150
ignoreMknodErrors: false,
137151
auth: auth.DefaultAuthenticators,
152+
transport: cleanhttp.DefaultPooledTransport(),
138153
}
139154
}

pkg/build/build.go

+1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ func New(ctx context.Context, fs apkfs.FullFS, opts ...Option) (*Context, error)
246246
apk.WithIgnoreMknodErrors(true),
247247
apk.WithIgnoreIndexSignatures(bc.o.IgnoreSignatures),
248248
apk.WithAuthenticator(bc.o.Auth),
249+
apk.WithTransport(bc.o.Transport),
249250
}
250251
// only try to pass the cache dir if one of the following is true:
251252
// - the user has explicitly set a cache dir

pkg/build/options.go

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
sha2562 "crypto/sha256"
2020
"encoding/base64"
2121
"fmt"
22+
"net/http"
2223
"time"
2324

2425
"chainguard.dev/apko/pkg/apk/apk"
@@ -229,3 +230,11 @@ func WithIgnoreSignatures(ignore bool) Option {
229230
return nil
230231
}
231232
}
233+
234+
// WithTransport allows explicitly setting the inner HTTP transport.
235+
func WithTransport(t http.RoundTripper) Option {
236+
return func(bc *Context) error {
237+
bc.o.Transport = t
238+
return nil
239+
}
240+
}

pkg/options/options.go

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package options
1717
import (
1818
"fmt"
1919
"log"
20+
"net/http"
2021
"os"
2122
"runtime"
2223
"time"
@@ -55,6 +56,7 @@ type Options struct {
5556
Auth auth.Authenticator `json:"-"`
5657
IncludePaths []string `json:"includePaths,omitempty"`
5758
IgnoreSignatures bool `json:"ignoreSignatures,omitempty"`
59+
Transport http.RoundTripper `json:"-"`
5860
}
5961

6062
type Auth struct{ User, Pass string }

0 commit comments

Comments
 (0)