Skip to content

Commit

Permalink
Fix building kotsadm bundle (#4410)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgalsaleh authored Jan 30, 2024
1 parent 1980b7f commit 4b5248b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 13 additions & 9 deletions pkg/image/airgap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions pkg/imageutil/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
41 changes: 41 additions & 0 deletions pkg/imageutil/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4b5248b

Please sign in to comment.