From 691009b518cbb33e031b938881b7ba06ed99d7dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Min=C3=A1=C5=99?= Date: Wed, 22 Nov 2017 17:15:19 +0100 Subject: [PATCH] Version prefix matters when sorting tags names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make sure that 'v' prefix is taken into account. Sort identical tag names with 'v' prefix at the end. Signed-off-by: Michal Minář --- pkg/image/apis/image/helper.go | 17 +++++++++++++++-- pkg/image/apis/image/helper_test.go | 6 +++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/pkg/image/apis/image/helper.go b/pkg/image/apis/image/helper.go index c9b9933561ac..29095719cb7a 100644 --- a/pkg/image/apis/image/helper.go +++ b/pkg/image/apis/image/helper.go @@ -856,6 +856,7 @@ type prioritizedTag struct { tag string priority tagPriority semver semver.Version + prefix string } func prioritizeTag(tag string) prioritizedTag { @@ -866,7 +867,12 @@ func prioritizeTag(tag string) prioritizedTag { } } - short := strings.TrimLeft(tag, "v") + short := tag + prefix := "" + if strings.HasPrefix(tag, "v") { + prefix = "v" + short = tag[1:] + } // 5.1.3 if v, err := semver.Parse(short); err == nil { @@ -874,6 +880,7 @@ func prioritizeTag(tag string) prioritizedTag { tag: tag, priority: tagPriorityFull, semver: v, + prefix: prefix, } } @@ -884,6 +891,7 @@ func prioritizeTag(tag string) prioritizedTag { tag: tag, priority: tagPriorityMinor, semver: v, + prefix: prefix, } } } @@ -895,6 +903,7 @@ func prioritizeTag(tag string) prioritizedTag { tag: tag, priority: tagPriorityMinor, semver: v, + prefix: prefix, } } } @@ -903,6 +912,7 @@ func prioritizeTag(tag string) prioritizedTag { return prioritizedTag{ tag: tag, priority: tagPriorityOther, + prefix: prefix, } } @@ -920,7 +930,10 @@ func (t prioritizedTags) Less(i, j int) bool { } cmp := t[i].semver.Compare(t[j].semver) - return cmp > 0 // the newer tag has a higher priority + if cmp > 0 { // the newer tag has a higher priority + return true + } + return t[i].prefix < t[j].prefix } // PrioritizeTags orders a set of image tags with a few conventions: diff --git a/pkg/image/apis/image/helper_test.go b/pkg/image/apis/image/helper_test.go index 063f70a7742a..c7e2f9095945 100644 --- a/pkg/image/apis/image/helper_test.go +++ b/pkg/image/apis/image/helper_test.go @@ -1161,7 +1161,7 @@ func TestPrioritizeTags(t *testing.T) { }{ { tags: []string{"other", "latest", "v5.5", "5.2.3", "v5.3.6-bother", "5.3.6-abba", "5.6"}, - expected: []string{"latest", "5.6", "v5.5", "v5.3.6-bother", "5.3.6-abba", "5.2.3", "other"}, + expected: []string{"latest", "5.6", "v5.5", "5.3.6-abba", "v5.3.6-bother", "5.2.3", "other"}, }, { tags: []string{"1.1-beta1", "1.2-rc1", "1.1-rc1", "1.1-beta2", "1.2-beta1", "1.2-alpha1", "1.2-beta4", "latest"}, @@ -1171,6 +1171,10 @@ func TestPrioritizeTags(t *testing.T) { tags: []string{"7.1", "v7.1", "7.1.0"}, expected: []string{"7.1", "v7.1", "7.1.0"}, }, + { + tags: []string{"7.1.0", "v7.1", "7.1"}, + expected: []string{"7.1", "v7.1", "7.1.0"}, + }, } for _, tc := range tests {