From ae0881900767af8220544b9c02692988d0ba1f16 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Thu, 20 Jun 2019 15:39:42 +0200 Subject: [PATCH] Handle eu.gcr.io like gcr.io when replacing default image Fix #2185 Signed-off-by: David Gageot --- pkg/skaffold/util/image.go | 22 +++++++++++----------- pkg/skaffold/util/image_test.go | 6 ++++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/pkg/skaffold/util/image.go b/pkg/skaffold/util/image.go index abefd188cb4..ec0418ec931 100644 --- a/pkg/skaffold/util/image.go +++ b/pkg/skaffold/util/image.go @@ -23,30 +23,30 @@ import ( const maxLength = 255 -const gcr = "gcr.io" -const escapeChars = "[/._:@]" -const prefixRegexStr = "gcr.io/[a-zA-Z0-9-_]+/" - -var escapeRegex = regexp.MustCompile(escapeChars) -var prefixRegex = regexp.MustCompile(prefixRegexStr) +var ( + escapeRegex = regexp.MustCompile(`[/._:@]`) + prefixRegex = regexp.MustCompile(`(.*\.)?gcr.io/[a-zA-Z0-9-_]+/?`) +) func SubstituteDefaultRepoIntoImage(defaultRepo string, originalImage string) string { if defaultRepo == "" { return originalImage } - if strings.HasPrefix(defaultRepo, gcr) { - originalPrefix := prefixRegex.FindString(originalImage) - defaultRepoPrefix := prefixRegex.FindString(defaultRepo) + originalPrefix := prefixRegex.FindString(originalImage) + defaultRepoPrefix := prefixRegex.FindString(defaultRepo) + if originalPrefix != "" && defaultRepoPrefix != "" { + // prefixes match if originalPrefix == defaultRepoPrefix { - // prefixes match return defaultRepo + "/" + originalImage[len(originalPrefix):] - } else if strings.HasPrefix(originalImage, defaultRepo) { + } + if strings.HasPrefix(originalImage, defaultRepo) { return originalImage } // prefixes don't match, concatenate and truncate return truncate(defaultRepo + "/" + originalImage) } + return truncate(defaultRepo + "/" + escapeRegex.ReplaceAllString(originalImage, "_")) } diff --git a/pkg/skaffold/util/image_test.go b/pkg/skaffold/util/image_test.go index 8fa6ad510bb..2e3a74d9137 100644 --- a/pkg/skaffold/util/image_test.go +++ b/pkg/skaffold/util/image_test.go @@ -46,6 +46,12 @@ func TestImageReplaceDefaultRepo(t *testing.T) { defaultRepo: "gcr.io/default", expectedImage: "gcr.io/default/registry", }, + { + description: "provided image has defaultRepo eu prefix", + image: "eu.gcr.io/project/registry", + defaultRepo: "eu.gcr.io/project", + expectedImage: "eu.gcr.io/project/registry", + }, { description: "image has shared prefix with defaultRepo", image: "gcr.io/default/example/registry",