From 1b4995a6e8000311dc885ea1de92a4313405efa3 Mon Sep 17 00:00:00 2001 From: Sascha Grunert Date: Tue, 14 May 2024 12:08:22 +0200 Subject: [PATCH] Fix version comparison for final version after RC Fixing the very specific use case of the failing semver comparison mentioned in https://github.com/kubernetes/kubernetes/issues/117115 Follow-up on https://github.com/kubernetes/release/pull/3223 and https://github.com/kubernetes/release/pull/3254 Signed-off-by: Sascha Grunert --- pkg/release/publish.go | 22 +++++++++++++++++++- pkg/release/publish_test.go | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/pkg/release/publish.go b/pkg/release/publish.go index 6fafbe255ad..f2205a1f27f 100644 --- a/pkg/release/publish.go +++ b/pkg/release/publish.go @@ -23,6 +23,7 @@ import ( "path/filepath" "strings" + "github.com/blang/semver/v4" "github.com/sirupsen/logrus" "sigs.k8s.io/release-sdk/gcli" @@ -283,7 +284,7 @@ func (p *Publisher) VerifyLatestUpdate( return false, fmt.Errorf("invalid GCS version format %s", gcsVersion) } - if sv.LTE(gcsSemverVersion) { + if IsUpToDate(gcsSemverVersion, sv) { logrus.Infof( "Not updating version, because %s <= %s", version, gcsVersion, ) @@ -294,6 +295,25 @@ func (p *Publisher) VerifyLatestUpdate( return true, nil } +func IsUpToDate(oldVersion, newVersion semver.Version) bool { + oldPre := oldVersion.Pre + newPre := newVersion.Pre + + // Verfy specific use case in our tagging logic: + // 1.30.0-rc.2.10+00000000000000 + // needs to be considered lower than + // 1.30.0-11+00000000000000 + // which is not given by newVersion.LTE(oldVersion) below. + if len(oldPre) == 3 && // [rc 2 10] + oldPre[0].String() == "rc" && + len(newPre) == 1 && // [11] + newPre[0].IsNum { + return false + } + + return newVersion.LTE(oldVersion) +} + // PublishToGcs publishes a release to GCS // publishFile - the GCS location to look in // buildDir - build output directory diff --git a/pkg/release/publish_test.go b/pkg/release/publish_test.go index f76d1709cbd..73660f9e8eb 100644 --- a/pkg/release/publish_test.go +++ b/pkg/release/publish_test.go @@ -21,6 +21,7 @@ import ( "os" "testing" + "github.com/blang/semver/v4" "github.com/stretchr/testify/require" "k8s.io/release/pkg/release" "k8s.io/release/pkg/release/releasefakes" @@ -325,3 +326,42 @@ func TestPublishReleaseNotesIndex(t *testing.T) { } } } + +func TestIsUpToDate(t *testing.T) { + t.Parallel() + + for _, tc := range []struct { + name, oldVersion, newVersion string + }{ + { + name: "Final version after RC", + oldVersion: "1.30.0-rc.2.10+00000000000000", + newVersion: "1.30.0-11+00000000000000", + }, + { + name: "More commits", + oldVersion: "1.30.0-10+00000000000000", + newVersion: "1.30.0-11+00000000000000", + }, + { + name: "Newer release", + oldVersion: "1.29.0-0+00000000000000", + newVersion: "1.29.1-0+00000000000000", + }, + { + name: "Counter reset", + oldVersion: "1.30.0-rc.2.10+00000000000000", + newVersion: "1.30.0-1+00000000000000", + }, + } { + oldVersion := semver.MustParse(tc.oldVersion) + newVersion := semver.MustParse(tc.newVersion) + + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + res := release.IsUpToDate(oldVersion, newVersion) + require.False(t, res) + }) + } +}