Skip to content

Commit

Permalink
fix incorrect tag selection in arkade chart upgrade
Browse files Browse the repository at this point in the history
Signed-off-by: Nana Kwadwo <[email protected]>
  • Loading branch information
bxffour committed Jun 20, 2023
1 parent 4a2c818 commit e5c3a67
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cmd/chart/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"path"
"regexp"
"sort"
"strings"

Expand Down Expand Up @@ -92,8 +93,15 @@ Otherwise, it returns a non-zero exit code and the updated values.yaml file.`,
return errors.New("unable to list tags for " + imageName)
}

suffix := getTagSuffix(tag)

var vs []*semver.Version
for _, r := range ref {
refSuffix := getTagSuffix(r)
if suffix != refSuffix {
continue
}

v, err := semver.NewVersion(r)
if err == nil {
vs = append(vs, v)
Expand Down Expand Up @@ -144,3 +152,20 @@ func splitImageName(reposName string) (string, string) {
nameParts := strings.SplitN(reposName, ":", 2)
return nameParts[0], nameParts[1]
}

func getTagSuffix(tag string) string {
var suffix string
tagParts := strings.SplitN(tag, "-", 2)

if len(tagParts) > 1 {
suffix = tagParts[1]
suffixRegex := regexp.MustCompile(`^(.*?)(?:\.\d+)?(?:\d+)?$`)
matches := suffixRegex.FindStringSubmatch(suffix)
if len(matches) > 1 {
suffix = matches[1]
}
return suffix
}

return suffix
}
29 changes: 29 additions & 0 deletions cmd/chart/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package chart

import (
"testing"
)

func Test_GetTagSuffix(t *testing.T) {
tt := []struct {
name string
tag string
expectedSuffix string
}{
{name: "test version tag", tag: "2.33.0-rootless", expectedSuffix: "rootless"},
{name: "test prerelease version", tag: "2.33.0-rc.0", expectedSuffix: "rc"},
{name: "test no prerelease dot separator", tag: "2.33.0-rc1", expectedSuffix: "rc"},
{name: "test numerical prerelease tag", tag: "2.33.0-1", expectedSuffix: ""},
{name: "test mixed alphanumeric tag", tag: "2.33.0-alpha.beta.1", expectedSuffix: "alpha.beta"},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
suffix := getTagSuffix(tc.tag)

if suffix != tc.expectedSuffix {
t.Fatalf("expected: %q, got: %q\n", tc.expectedSuffix, suffix)
}
})
}
}

0 comments on commit e5c3a67

Please sign in to comment.