53
53
54
54
type dockerResolver struct {
55
55
credentials func (string ) (string , string , error )
56
+ host func (string ) (string , error )
56
57
plainHTTP bool
57
58
client * http.Client
58
59
tracker StatusTracker
@@ -65,6 +66,9 @@ type ResolverOptions struct {
65
66
// is interpretted as a long lived token.
66
67
Credentials func (string ) (string , string , error )
67
68
69
+ // Host provides the hostname given a namespace.
70
+ Host func (string ) (string , error )
71
+
68
72
// PlainHTTP specifies to use plain http and not https
69
73
PlainHTTP bool
70
74
@@ -77,14 +81,27 @@ type ResolverOptions struct {
77
81
Tracker StatusTracker
78
82
}
79
83
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
+
80
92
// NewResolver returns a new resolver to a Docker registry
81
93
func NewResolver (options ResolverOptions ) remotes.Resolver {
82
94
tracker := options .Tracker
83
95
if tracker == nil {
84
96
tracker = NewInMemoryTracker ()
85
97
}
98
+ host := options .Host
99
+ if host == nil {
100
+ host = DefaultHost
101
+ }
86
102
return & dockerResolver {
87
103
credentials : options .Credentials ,
104
+ host : host ,
88
105
plainHTTP : options .PlainHTTP ,
89
106
client : options .Client ,
90
107
tracker : tracker ,
@@ -270,18 +287,19 @@ func (r *dockerResolver) base(refspec reference.Spec) (*dockerBase, error) {
270
287
)
271
288
272
289
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
282
295
}
283
296
}
284
297
298
+ base .Scheme = "https"
299
+ if r .plainHTTP || strings .HasPrefix (base .Host , "localhost:" ) {
300
+ base .Scheme = "http"
301
+ }
302
+
285
303
if r .credentials != nil {
286
304
username , secret , err = r .credentials (base .Host )
287
305
if err != nil {
0 commit comments