From 4b5248b9efd7eba2aa90422e83445d850d8da883 Mon Sep 17 00:00:00 2001 From: Salah Al Saleh Date: Mon, 29 Jan 2024 16:25:55 -0800 Subject: [PATCH] Fix building kotsadm bundle (#4410) --- .github/workflows/release.yaml | 4 ++++ pkg/image/airgap.go | 22 ++++++++++-------- pkg/imageutil/image.go | 8 +++++++ pkg/imageutil/image_test.go | 41 ++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index c3aa0b0d71..6c9fca24d4 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -186,6 +186,10 @@ jobs: - name: Checkout uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: '^1.20.0' + - name: Run bundle registry run: | mkdir -p kotsadm-bundle/images diff --git a/pkg/image/airgap.go b/pkg/image/airgap.go index 354544badc..afa59d6b0f 100644 --- a/pkg/image/airgap.go +++ b/pkg/image/airgap.go @@ -217,19 +217,23 @@ func PushImagesFromTempRegistry(airgapRootDir string, imageList []string, option return errors.Wrapf(err, "failed to parse source image %s", imageID) } - // if kotsadm tag is set, change the tag of the kotsadm/kotsadm and kotsadm/kotsadm-migrations images - if options.KotsadmTag != "" && strings.HasPrefix(imageID, "kotsadm/kotsadm") { - i, err := imageutil.ChangeImageTag(imageID, options.KotsadmTag) - if err != nil { - return errors.Wrap(err, "failed to change kotsadm dest image tag") - } - imageID = i - } - destImage, err := imageutil.DestImage(options.Registry, imageID) if err != nil { return errors.Wrapf(err, "failed to get destination image for %s", imageID) } + + if options.KotsadmTag != "" { + // kotsadm tag is set, change the tag of the kotsadm and kotsadm-migrations images + imageName := imageutil.GetImageName(destImage) + if imageName == "kotsadm" || imageName == "kotsadm-migrations" { + di, err := imageutil.ChangeImageTag(destImage, options.KotsadmTag) + if err != nil { + return errors.Wrap(err, "failed to change kotsadm dest image tag") + } + destImage = di + } + } + destStr := fmt.Sprintf("docker://%s", destImage) destRef, err := alltransports.ParseImageName(destStr) if err != nil { diff --git a/pkg/imageutil/image.go b/pkg/imageutil/image.go index b26281cdb8..e2e21b5314 100644 --- a/pkg/imageutil/image.go +++ b/pkg/imageutil/image.go @@ -282,6 +282,14 @@ func KustomizeImage(destRegistry registrytypes.RegistryOptions, image string) ([ return rewrittenImages, nil } +// GetImageName returns the name of the image without the tag, digest, or registry +func GetImageName(image string) string { + imageParts := strings.Split(image, "/") + lastPart := imageParts[len(imageParts)-1] + return StripImageTagAndDigest(lastPart) +} + +// ChangeImageTag changes the tag of an image to the provided new tag func ChangeImageTag(image string, newTag string) (string, error) { parsed, err := reference.ParseDockerRef(image) if err != nil { diff --git a/pkg/imageutil/image_test.go b/pkg/imageutil/image_test.go index d577536048..7b4d6a15bc 100644 --- a/pkg/imageutil/image_test.go +++ b/pkg/imageutil/image_test.go @@ -1050,6 +1050,47 @@ func TestGetTag(t *testing.T) { } } +func Test_GetImageName(t *testing.T) { + tests := []struct { + name string + image string + expected string + }{ + { + name: "image with tag", + image: "quay.io/someorg/myimage:0.1", + expected: "myimage", + }, + { + name: "image with digest", + image: "quay.io/someorg/myimage@sha256:1234567890abcdef", + expected: "myimage", + }, + { + name: "image without tag or digest", + image: "quay.io/someorg/myimage", + expected: "myimage", + }, + { + name: "image with tag and digest", + image: "quay.io/someorg/myimage:0.1@sha256:1234567890abcdef", + expected: "myimage", + }, + { + name: "image with registry and port", + image: "myregistry.com:5000/someorg/myimage:0.1", + expected: "myimage", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + result := GetImageName(test.image) + assert.Equal(t, test.expected, result) + }) + } +} + func TestChangeImageTag(t *testing.T) { tests := []struct { name string