From 6bb351bade10f1269de19dcaef5e19e679df22f0 Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Tue, 14 May 2024 11:02:06 +0100 Subject: [PATCH] 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) --- builder/build.go | 1 + schema/image.go | 12 ++++++------ schema/image_test.go | 10 ++++++++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/builder/build.go b/builder/build.go index f28fa9c9..c9486178 100644 --- a/builder/build.go +++ b/builder/build.go @@ -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 != "" { diff --git a/schema/image.go b/schema/image.go index 5e5712c9..3d2f8993 100644 --- a/schema/image.go +++ b/schema/image.go @@ -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" } @@ -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 } diff --git a/schema/image_test.go b/schema/image_test.go index 2486b366..156162cf 100644 --- a/schema/image_test.go +++ b/schema/image_test.go @@ -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")