Skip to content

Commit 6bb351b

Browse files
committed
Fix BuildImageName for digest mode
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]>
1 parent 6e26edd commit 6bb351b

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

builder/build.go

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func BuildImage(image string, handler string, functionName string, language stri
6868
}
6969

7070
imageName := schema.BuildImageName(tagFormat, image, version, branch)
71+
7172
fmt.Printf("Building: %s with %s template. Please wait..\n", imageName, language)
7273

7374
if remoteBuilder != "" {

schema/image.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (i *BuildFormat) Set(value string) error {
7171
func BuildImageName(format BuildFormat, image string, version string, branch string) string {
7272
imageVal := image
7373
splitImage := strings.Split(image, "/")
74-
if strings.Contains(splitImage[len(splitImage)-1], ":") == false {
74+
if !strings.Contains(splitImage[len(splitImage)-1], ":") {
7575
imageVal += ":latest"
7676
}
7777

@@ -85,13 +85,13 @@ func BuildImageName(format BuildFormat, image string, version string, branch str
8585
// the describe describe value
8686
return imageVal + "-" + version
8787
case DigestFormat:
88-
baseImage, _, found := strings.Cut(imageVal, ":")
89-
if !found {
88+
89+
if lastIndex := strings.LastIndex(imageVal, ":"); lastIndex > -1 {
90+
baseImage := imageVal[:lastIndex]
91+
return fmt.Sprintf("%s:%s", baseImage, version)
92+
} else {
9093
return imageVal + "-" + version
9194
}
92-
93-
return fmt.Sprintf("%s:%s", baseImage, version)
94-
9595
default:
9696
return imageVal
9797
}

schema/image_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ func Test_BuildImageName_DefaultFormat_WithCustomServerPort(t *testing.T) {
2020
}
2121
}
2222

23+
func Test_BuildImageName_Localhost_SingleLevelDigestFormat(t *testing.T) {
24+
25+
want := "localhost:5001/pydict:851a77095527c1c703905ddb5f94780a"
26+
got := BuildImageName(DigestFormat, "localhost:5001/pydict:latest", "851a77095527c1c703905ddb5f94780a", "")
27+
28+
if got != want {
29+
t.Errorf("BuildImageName want: \"%s\", got: \"%s\"", want, got)
30+
}
31+
}
32+
2333
func Test_BuildImageName_SHAFormat(t *testing.T) {
2434
want := "img:latest-ef384"
2535
got := BuildImageName(SHAFormat, "img", "ef384", "master")

0 commit comments

Comments
 (0)