Skip to content

Commit 7b02284

Browse files
committed
Extract image normalizing into separate function
1 parent 4c36b75 commit 7b02284

File tree

1 file changed

+45
-42
lines changed

1 file changed

+45
-42
lines changed

pkg/assets/builder.go

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -179,48 +179,8 @@ func (a *AssetBuilder) RemapImage(image string) string {
179179
}
180180
}
181181

182-
if a.AssetsLocation != nil && a.AssetsLocation.ContainerProxy != nil {
183-
containerProxy := strings.TrimSuffix(*a.AssetsLocation.ContainerProxy, "/")
184-
normalized := image
185-
186-
// If the image name contains only a single / we need to determine if the image is located on docker-hub or if it's using a convenient URL,
187-
// like registry.k8s.io/<image-name> or registry.k8s.io/<image-name>
188-
// In case of a hub image it should be sufficient to just prepend the proxy url, producing eg docker-proxy.example.com/weaveworks/weave-kube
189-
if strings.Count(normalized, "/") <= 1 && !strings.ContainsAny(strings.Split(normalized, "/")[0], ".:") {
190-
normalized = containerProxy + "/" + normalized
191-
} else {
192-
re := regexp.MustCompile(`^[^/]+`)
193-
normalized = re.ReplaceAllString(normalized, containerProxy)
194-
}
195-
196-
asset.DownloadLocation = normalized
197-
198-
// Run the new image
199-
image = asset.DownloadLocation
200-
}
201-
202-
if a.AssetsLocation != nil && a.AssetsLocation.ContainerRegistry != nil {
203-
registryMirror := *a.AssetsLocation.ContainerRegistry
204-
normalized := image
205-
206-
// Remove the 'standard' kubernetes image prefixes, just for sanity
207-
normalized = strings.TrimPrefix(normalized, "registry.k8s.io/")
208-
209-
// When assembling the cluster spec, kops may call the option more then once until the config converges
210-
// This means that this function may me called more than once on the same image
211-
// It this is pass is the second one, the image will already have been normalized with the containerRegistry settings
212-
// If this is the case, passing though the process again will re-prepend the container registry again
213-
// and again, causing the spec to never converge and the config build to fail.
214-
if !strings.HasPrefix(normalized, registryMirror+"/") {
215-
// We can't nest arbitrarily
216-
// Some risk of collisions, but also -- and __ in the names appear to be blocked by docker hub
217-
normalized = strings.Replace(normalized, "/", "-", -1)
218-
asset.DownloadLocation = registryMirror + "/" + normalized
219-
}
220-
221-
// Run the new image
222-
image = asset.DownloadLocation
223-
}
182+
normalized := NormalizeImage(a, image)
183+
asset.DownloadLocation = normalized
224184

225185
a.ImageAssets = append(a.ImageAssets, asset)
226186

@@ -378,3 +338,46 @@ func (a *AssetBuilder) remapURL(canonicalURL *url.URL) (*url.URL, error) {
378338

379339
return fileRepo, nil
380340
}
341+
342+
func NormalizeImage(a *AssetBuilder, image string) string {
343+
if a.AssetsLocation != nil && a.AssetsLocation.ContainerProxy != nil {
344+
containerProxy := strings.TrimSuffix(*a.AssetsLocation.ContainerProxy, "/")
345+
normalized := image
346+
347+
// If the image name contains only a single / we need to determine if the image is located on docker-hub or if it's using a convenient URL,
348+
// like registry.k8s.io/<image-name> or registry.k8s.io/<image-name>
349+
// In case of a hub image it should be sufficient to just prepend the proxy url, producing eg docker-proxy.example.com/weaveworks/weave-kube
350+
if strings.Count(normalized, "/") <= 1 && !strings.ContainsAny(strings.Split(normalized, "/")[0], ".:") {
351+
normalized = containerProxy + "/" + normalized
352+
} else {
353+
re := regexp.MustCompile(`^[^/]+`)
354+
normalized = re.ReplaceAllString(normalized, containerProxy)
355+
}
356+
357+
// Run the new image
358+
image = normalized
359+
}
360+
361+
if a.AssetsLocation != nil && a.AssetsLocation.ContainerRegistry != nil {
362+
registryMirror := *a.AssetsLocation.ContainerRegistry
363+
normalized := image
364+
365+
// Remove the 'standard' kubernetes image prefixes, just for sanity
366+
normalized = strings.TrimPrefix(normalized, "registry.k8s.io/")
367+
368+
// When assembling the cluster spec, kops may call the option more then once until the config converges
369+
// This means that this function may me called more than once on the same image
370+
// It this is pass is the second one, the image will already have been normalized with the containerRegistry settings
371+
// If this is the case, passing though the process again will re-prepend the container registry again
372+
// and again, causing the spec to never converge and the config build to fail.
373+
if !strings.HasPrefix(normalized, registryMirror+"/") {
374+
// We can't nest arbitrarily
375+
// Some risk of collisions, but also -- and __ in the names appear to be blocked by docker hub
376+
normalized = strings.Replace(normalized, "/", "-", -1)
377+
normalized = registryMirror + "/" + normalized
378+
}
379+
image = normalized
380+
}
381+
// Run the new image
382+
return image
383+
}

0 commit comments

Comments
 (0)