Skip to content

Commit

Permalink
Merge pull request #2300 from dgageot/fix-2185
Browse files Browse the repository at this point in the history
Handle `eu.gcr.io` like `gcr.io` when replacing default image
  • Loading branch information
balopat authored Jun 20, 2019
2 parents ce51164 + ae08819 commit f07a695
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
22 changes: 11 additions & 11 deletions pkg/skaffold/util/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "_"))
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/skaffold/util/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f07a695

Please sign in to comment.