Merge pull request #2450 from Random-Liu/support-host-in-resolver

Support specifying host in resolver.
This commit is contained in:
Derek McGowan 2018-07-10 18:29:49 -07:00 committed by GitHub
commit b382b6fe0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -53,6 +53,7 @@ var (
type dockerResolver struct { type dockerResolver struct {
credentials func(string) (string, string, error) credentials func(string) (string, string, error)
host func(string) (string, error)
plainHTTP bool plainHTTP bool
client *http.Client client *http.Client
tracker StatusTracker tracker StatusTracker
@ -65,6 +66,9 @@ type ResolverOptions struct {
// is interpretted as a long lived token. // is interpretted as a long lived token.
Credentials func(string) (string, string, error) Credentials func(string) (string, string, error)
// Host provides the hostname given a namespace.
Host func(string) (string, error)
// PlainHTTP specifies to use plain http and not https // PlainHTTP specifies to use plain http and not https
PlainHTTP bool PlainHTTP bool
@ -77,14 +81,27 @@ type ResolverOptions struct {
Tracker StatusTracker Tracker StatusTracker
} }
// DefaultHost is the default host function.
func DefaultHost(ns string) (string, error) {
if ns == "docker.io" {
return "registry-1.docker.io", nil
}
return ns, nil
}
// NewResolver returns a new resolver to a Docker registry // NewResolver returns a new resolver to a Docker registry
func NewResolver(options ResolverOptions) remotes.Resolver { func NewResolver(options ResolverOptions) remotes.Resolver {
tracker := options.Tracker tracker := options.Tracker
if tracker == nil { if tracker == nil {
tracker = NewInMemoryTracker() tracker = NewInMemoryTracker()
} }
host := options.Host
if host == nil {
host = DefaultHost
}
return &dockerResolver{ return &dockerResolver{
credentials: options.Credentials, credentials: options.Credentials,
host: host,
plainHTTP: options.PlainHTTP, plainHTTP: options.PlainHTTP,
client: options.Client, client: options.Client,
tracker: tracker, tracker: tracker,
@ -270,18 +287,19 @@ func (r *dockerResolver) base(refspec reference.Spec) (*dockerBase, error) {
) )
host := refspec.Hostname() host := refspec.Hostname()
base.Scheme = "https" base.Host = host
if r.host != nil {
if host == "docker.io" { base.Host, err = r.host(host)
base.Host = "registry-1.docker.io" if err != nil {
} else { return nil, err
base.Host = host
if r.plainHTTP || strings.HasPrefix(host, "localhost:") {
base.Scheme = "http"
} }
} }
base.Scheme = "https"
if r.plainHTTP || strings.HasPrefix(base.Host, "localhost:") {
base.Scheme = "http"
}
if r.credentials != nil { if r.credentials != nil {
username, secret, err = r.credentials(base.Host) username, secret, err = r.credentials(base.Host)
if err != nil { if err != nil {