|
5 | 5 | "encoding/base64" |
6 | 6 | "encoding/json" |
7 | 7 | "fmt" |
| 8 | + "net/url" |
8 | 9 | "sort" |
9 | 10 | "strings" |
10 | 11 |
|
@@ -41,7 +42,7 @@ func newMirror(sourceImageLocation, mirrorImageLocation string, resolveTags bool |
41 | 42 | mirrorHost = extractMirrorURL(sourceImageLocation, mirrorImageLocation) |
42 | 43 | } else { |
43 | 44 | // special case if source and mirror are the same. Do not drop the host repo name to avoid an empty host entry |
44 | | - mirrorHost = extractHostname(mirrorImageLocation) |
| 45 | + mirrorHost = extractRegistryHostname(mirrorImageLocation) |
45 | 46 | } |
46 | 47 | return mirror{host: mirrorHost, resolveTags: resolveTags} |
47 | 48 | } |
@@ -82,14 +83,22 @@ func newMirrorSet(srcImage string, mirrorLocations []config.ImageMirror, resolve |
82 | 83 | for _, m := range mirrorLocations { |
83 | 84 | truncatedMirrors = append(truncatedMirrors, newMirror(srcImage, string(m), resolveTags)) |
84 | 85 | } |
85 | | - return mirrorSet{source: extractHostname(srcImage), mirrors: truncatedMirrors, mirrorSourcePolicy: mirrorSourcePolicy} |
| 86 | + return mirrorSet{source: extractRegistryHostname(srcImage), mirrors: truncatedMirrors, mirrorSourcePolicy: mirrorSourcePolicy} |
86 | 87 | } |
87 | 88 |
|
88 | | -// extractHostname extracts just the initial host repo from a full image location |
89 | | -// e.g. mcr.microsoft.com would be extracted from mcr.microsoft.com/oss/kubernetes/pause:3.9 |
90 | | -func extractHostname(fullImage string) string { |
91 | | - parts := strings.Split(fullImage, imagePathSeparator) |
92 | | - return parts[0] |
| 89 | +// extractRegistryHostname extracts just the initial host repo from a full image location, as containerd does not allow |
| 90 | +// registries to exist on a subpath, given an input image `mcr.microsoft.com/oss/kubernetes/pause:3.9`, |
| 91 | +// mcr.microsoft.com would be the determined registry hostname. |
| 92 | +func extractRegistryHostname(fullImage string) string { |
| 93 | + // url.Parse will only work if URL has a scheme (https://) |
| 94 | + if parsedURL, err := url.Parse(fullImage); err == nil && parsedURL.Hostname() != "" { |
| 95 | + if parsedURL.Port() != "" { |
| 96 | + return parsedURL.Hostname() + ":" + parsedURL.Port() |
| 97 | + } |
| 98 | + return parsedURL.Hostname() |
| 99 | + } |
| 100 | + // For URLs without a scheme, just return everything before the first `/` |
| 101 | + return strings.Split(fullImage, imagePathSeparator)[0] |
93 | 102 | } |
94 | 103 |
|
95 | 104 | // getMergedMirrorSets extracts and merges the contents of the given mirror sets. |
@@ -230,7 +239,7 @@ func (ms *mirrorSet) generateConfig(secretsConfig credentialprovider.DockerConfi |
230 | 239 | result += "\n" |
231 | 240 |
|
232 | 241 | // Extract the mirror repo's authorization credentials, if one exists |
233 | | - if entry, ok := secretsConfig.Auths[extractHostname(m.host)]; ok { |
| 242 | + if entry, ok := secretsConfig.Auths[extractRegistryHostname(m.host)]; ok { |
234 | 243 | credentials := entry.Username + ":" + entry.Password |
235 | 244 | token := base64.StdEncoding.EncodeToString([]byte(credentials)) |
236 | 245 |
|
|
0 commit comments