Merge pull request #2751 from Charliekenney23/add-useragent-resolver-option

Add custom headers resolver option
This commit is contained in:
Derek McGowan 2018-11-20 10:51:10 -08:00 committed by GitHub
commit b8631c750c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,6 +29,7 @@ import (
"github.com/containerd/containerd/log" "github.com/containerd/containerd/log"
"github.com/containerd/containerd/reference" "github.com/containerd/containerd/reference"
"github.com/containerd/containerd/remotes" "github.com/containerd/containerd/remotes"
"github.com/containerd/containerd/version"
digest "github.com/opencontainers/go-digest" digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1" ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -82,6 +83,9 @@ type ResolverOptions struct {
// Host provides the hostname given a namespace. // Host provides the hostname given a namespace.
Host func(string) (string, error) Host func(string) (string, error)
// Headers are the HTTP request header fields sent by the resolver
Headers http.Header
// PlainHTTP specifies to use plain http and not https // PlainHTTP specifies to use plain http and not https
PlainHTTP bool PlainHTTP bool
@ -105,6 +109,7 @@ func DefaultHost(ns string) (string, error) {
type dockerResolver struct { type dockerResolver struct {
auth Authorizer auth Authorizer
host func(string) (string, error) host func(string) (string, error)
headers http.Header
plainHTTP bool plainHTTP bool
client *http.Client client *http.Client
tracker StatusTracker tracker StatusTracker
@ -118,12 +123,27 @@ func NewResolver(options ResolverOptions) remotes.Resolver {
if options.Host == nil { if options.Host == nil {
options.Host = DefaultHost options.Host = DefaultHost
} }
if options.Headers == nil {
options.Headers = make(http.Header)
}
if _, ok := options.Headers["Accept"]; !ok {
// set headers for all the types we support for resolution.
options.Headers.Set("Accept", strings.Join([]string{
images.MediaTypeDockerSchema2Manifest,
images.MediaTypeDockerSchema2ManifestList,
ocispec.MediaTypeImageManifest,
ocispec.MediaTypeImageIndex, "*"}, ", "))
}
if _, ok := options.Headers["User-Agent"]; !ok {
options.Headers.Set("User-Agent", "containerd/"+version.Version)
}
if options.Authorizer == nil { if options.Authorizer == nil {
options.Authorizer = NewAuthorizer(options.Client, options.Credentials) options.Authorizer = NewAuthorizer(options.Client, options.Credentials)
} }
return &dockerResolver{ return &dockerResolver{
auth: options.Authorizer, auth: options.Authorizer,
host: options.Host, host: options.Host,
headers: options.Headers,
plainHTTP: options.PlainHTTP, plainHTTP: options.PlainHTTP,
client: options.Client, client: options.Client,
tracker: options.Tracker, tracker: options.Tracker,
@ -182,12 +202,7 @@ func (r *dockerResolver) Resolve(ctx context.Context, ref string) (string, ocisp
return "", ocispec.Descriptor{}, err return "", ocispec.Descriptor{}, err
} }
// set headers for all the types we support for resolution. req.Header = r.headers
req.Header.Set("Accept", strings.Join([]string{
images.MediaTypeDockerSchema2Manifest,
images.MediaTypeDockerSchema2ManifestList,
ocispec.MediaTypeImageManifest,
ocispec.MediaTypeImageIndex, "*"}, ", "))
log.G(ctx).Debug("resolving") log.G(ctx).Debug("resolving")
resp, err := fetcher.doRequestWithRetries(ctx, req, nil) resp, err := fetcher.doRequestWithRetries(ctx, req, nil)