5353
5454type dockerResolver struct {
5555 credentials func (string ) (string , string , error )
56+ host func (string ) (string , error )
5657 plainHTTP bool
5758 client * http.Client
5859 tracker StatusTracker
@@ -65,6 +66,9 @@ type ResolverOptions struct {
6566 // is interpretted as a long lived token.
6667 Credentials func (string ) (string , string , error )
6768
69+ // Host provides the hostname given a namespace.
70+ Host func (string ) (string , error )
71+
6872 // PlainHTTP specifies to use plain http and not https
6973 PlainHTTP bool
7074
@@ -77,14 +81,27 @@ type ResolverOptions struct {
7781 Tracker StatusTracker
7882}
7983
84+ // DefaultHost is the default host function.
85+ func DefaultHost (ns string ) (string , error ) {
86+ if ns == "docker.io" {
87+ return "registry-1.docker.io" , nil
88+ }
89+ return ns , nil
90+ }
91+
8092// NewResolver returns a new resolver to a Docker registry
8193func NewResolver (options ResolverOptions ) remotes.Resolver {
8294 tracker := options .Tracker
8395 if tracker == nil {
8496 tracker = NewInMemoryTracker ()
8597 }
98+ host := options .Host
99+ if host == nil {
100+ host = DefaultHost
101+ }
86102 return & dockerResolver {
87103 credentials : options .Credentials ,
104+ host : host ,
88105 plainHTTP : options .PlainHTTP ,
89106 client : options .Client ,
90107 tracker : tracker ,
@@ -270,18 +287,19 @@ func (r *dockerResolver) base(refspec reference.Spec) (*dockerBase, error) {
270287 )
271288
272289 host := refspec .Hostname ()
273- base .Scheme = "https"
274-
275- if host == "docker.io" {
276- base .Host = "registry-1.docker.io"
277- } else {
278- base .Host = host
279-
280- if r .plainHTTP || strings .HasPrefix (host , "localhost:" ) {
281- base .Scheme = "http"
290+ base .Host = host
291+ if r .host != nil {
292+ base .Host , err = r .host (host )
293+ if err != nil {
294+ return nil , err
282295 }
283296 }
284297
298+ base .Scheme = "https"
299+ if r .plainHTTP || strings .HasPrefix (base .Host , "localhost:" ) {
300+ base .Scheme = "http"
301+ }
302+
285303 if r .credentials != nil {
286304 username , secret , err = r .credentials (base .Host )
287305 if err != nil {
0 commit comments