Skip to content

Commit

Permalink
Fix BuildImageName for digest mode
Browse files Browse the repository at this point in the history
Fixes an issue where a custom port was used in the image name
and the wrong part of the image name was replaced.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed May 14, 2024
1 parent 6e26edd commit 6bb351b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func BuildImage(image string, handler string, functionName string, language stri
}

imageName := schema.BuildImageName(tagFormat, image, version, branch)

fmt.Printf("Building: %s with %s template. Please wait..\n", imageName, language)

if remoteBuilder != "" {
Expand Down
12 changes: 6 additions & 6 deletions schema/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (i *BuildFormat) Set(value string) error {
func BuildImageName(format BuildFormat, image string, version string, branch string) string {
imageVal := image
splitImage := strings.Split(image, "/")
if strings.Contains(splitImage[len(splitImage)-1], ":") == false {
if !strings.Contains(splitImage[len(splitImage)-1], ":") {
imageVal += ":latest"
}

Expand All @@ -85,13 +85,13 @@ func BuildImageName(format BuildFormat, image string, version string, branch str
// the describe describe value
return imageVal + "-" + version
case DigestFormat:
baseImage, _, found := strings.Cut(imageVal, ":")
if !found {

if lastIndex := strings.LastIndex(imageVal, ":"); lastIndex > -1 {
baseImage := imageVal[:lastIndex]
return fmt.Sprintf("%s:%s", baseImage, version)
} else {
return imageVal + "-" + version
}

return fmt.Sprintf("%s:%s", baseImage, version)

default:
return imageVal
}
Expand Down
10 changes: 10 additions & 0 deletions schema/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ func Test_BuildImageName_DefaultFormat_WithCustomServerPort(t *testing.T) {
}
}

func Test_BuildImageName_Localhost_SingleLevelDigestFormat(t *testing.T) {

want := "localhost:5001/pydict:851a77095527c1c703905ddb5f94780a"
got := BuildImageName(DigestFormat, "localhost:5001/pydict:latest", "851a77095527c1c703905ddb5f94780a", "")

if got != want {
t.Errorf("BuildImageName want: \"%s\", got: \"%s\"", want, got)
}
}

func Test_BuildImageName_SHAFormat(t *testing.T) {
want := "img:latest-ef384"
got := BuildImageName(SHAFormat, "img", "ef384", "master")
Expand Down

0 comments on commit 6bb351b

Please sign in to comment.