Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Commit

Permalink
make the http client for the oci client configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Schrodi committed Feb 19, 2021
1 parent 53171f2 commit 2fa53c7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 9 additions & 3 deletions ociclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type client struct {
resolver Resolver
cache cache.Cache
allowPlainHttp bool
httpClient *http.Client

knownMediaTypes sets.String
}
Expand Down Expand Up @@ -72,9 +73,14 @@ func NewClient(log logr.Logger, opts ...Option) (Client, error) {
options.Cache = c
}

if options.HTTPClient == nil {
options.HTTPClient = http.DefaultClient
}

return &client{
log: log,
allowPlainHttp: options.AllowPlainHttp,
httpClient: options.HTTPClient,
resolver: options.Resolver,
cache: options.Cache,
knownMediaTypes: DefaultKnownMediaTypes.Union(options.CustomMediaTypes),
Expand All @@ -87,7 +93,7 @@ func (c *client) InjectCache(cache cache.Cache) error {
}

func (c *client) GetManifest(ctx context.Context, ref string) (*ocispecv1.Manifest, error) {
resolver, err := c.resolver.Resolver(ctx, ref, http.DefaultClient, c.allowPlainHttp)
resolver, err := c.resolver.Resolver(ctx, ref, c.httpClient, c.allowPlainHttp)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -136,7 +142,7 @@ func (c *client) getFetchReader(ctx context.Context, ref string, desc ocispecv1.
}
}

resolver, err := c.resolver.Resolver(context.Background(), ref, http.DefaultClient, c.allowPlainHttp)
resolver, err := c.resolver.Resolver(context.Background(), ref, c.httpClient, c.allowPlainHttp)
if err != nil {
return nil, err
}
Expand All @@ -161,7 +167,7 @@ func (c *client) getFetchReader(ctx context.Context, ref string, desc ocispecv1.
}

func (c *client) PushManifest(ctx context.Context, ref string, manifest *ocispecv1.Manifest) error {
resolver, err := c.resolver.Resolver(context.Background(), ref, http.DefaultClient, c.allowPlainHttp)
resolver, err := c.resolver.Resolver(context.Background(), ref, c.httpClient, c.allowPlainHttp)
if err != nil {
return err
}
Expand Down
10 changes: 10 additions & 0 deletions ociclient/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type Options struct {

// CustomMediaTypes defines the custom known media types
CustomMediaTypes sets.String

HTTPClient *http.Client
}

// Option is the interface to specify different cache options
Expand Down Expand Up @@ -126,3 +128,11 @@ type AllowPlainHttp bool
func (c AllowPlainHttp) ApplyOption(options *Options) {
options.AllowPlainHttp = bool(c)
}

// WithHTTPClient configures the http client.
type WithHTTPClient http.Client

func (c WithHTTPClient) ApplyOption(options *Options) {
client := http.Client(c)
options.HTTPClient = &client
}

0 comments on commit 2fa53c7

Please sign in to comment.