Skip to content

Commit

Permalink
Merge pull request #5364 from ykakarap/version-compare-utils
Browse files Browse the repository at this point in the history
🌱 add version.Compare with CompareOptions
  • Loading branch information
k8s-ci-robot committed Oct 6, 2021
2 parents 33e9f42 + f4380fc commit 18ecf48
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
2 changes: 1 addition & 1 deletion api/v1beta1/cluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (c *Cluster) validateTopology(old *Cluster) field.ErrorList {
),
)
}
if inVersion.NE(semver.Version{}) && oldVersion.NE(semver.Version{}) && version.CompareWithBuildIdentifiers(inVersion, oldVersion) == -1 {
if inVersion.NE(semver.Version{}) && oldVersion.NE(semver.Version{}) && version.Compare(inVersion, oldVersion, version.WithBuildTags()) == -1 {
allErrs = append(
allErrs,
field.Invalid(
Expand Down
2 changes: 1 addition & 1 deletion controllers/topology/internal/contract/controlplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (c *ControlPlaneContract) IsUpgrading(obj *unstructured.Unstructured) (bool
return false, errors.Wrap(err, "failed to parse control plane status version")
}

return version.CompareWithBuildIdentifiers(specV, statusV) == 1, nil
return version.Compare(specV, statusV, version.WithBuildTags()) == 1, nil
}

// IsScaling returns true if the control plane is in the middle of a scale operation, false otherwise.
Expand Down
2 changes: 1 addition & 1 deletion util/collections/machine_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (v machinesByVersion) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
func (v machinesByVersion) Less(i, j int) bool {
vi, _ := semver.ParseTolerant(*v[i].Spec.Version)
vj, _ := semver.ParseTolerant(*v[j].Spec.Version)
comp := version.CompareWithBuildIdentifiers(vi, vj)
comp := version.Compare(vi, vj, version.WithBuildTags())
if comp == 0 {
return v[i].Name < v[j].Name
}
Expand Down
40 changes: 39 additions & 1 deletion util/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,13 @@ func (v buildIdentifier) compare(o buildIdentifier) int {
}
}

// CompareWithBuildIdentifiers compares 2 version a and b.
// CompareWithBuildIdentifiers compares two versions a and b.
// Perfoms a standard version compare between a and b. If the versions
// are equal, build identifiers will be used to compare further.
// -1 == a is less than b.
// 0 == a is equal to b.
// 1 == a is greater than b.
// Deprecated: Use Compare(a, b, WithBuildTags()) instead.
func CompareWithBuildIdentifiers(a semver.Version, b semver.Version) int {
if comp := a.Compare(b); comp != 0 {
return comp
Expand All @@ -187,3 +188,40 @@ func CompareWithBuildIdentifiers(a semver.Version, b semver.Version) int {
biB := newBuildIdentifiers(b.Build)
return biA.compare(biB)
}

type comparer struct {
buildTags bool
}

// CompareOption is a configuration option for Compare.
type CompareOption func(*comparer)

// WithBuildTags modifies the version comparison to also consider build tags
// when comparing versions.
// Performs a standard version compare between a and b. If the versions
// are equal, build identifiers will be used to compare further.
// -1 == a is less than b.
// 0 == a is equal to b.
// 1 == a is greater than b.
func WithBuildTags() CompareOption {
return func(c *comparer) {
c.buildTags = true
}
}

// Compare 2 semver versions.
// Defaults to doing the standard semver comparison when no options are specified.
// The comparison logic can be modified by passing additional compare options.
// Example: using the WithBuildTags() option modifies the compare logic to also
// consider build tags when comparing versions.
func Compare(a, b semver.Version, options ...CompareOption) int {
c := &comparer{}
for _, o := range options {
o(c)
}

if c.buildTags {
return CompareWithBuildIdentifiers(a, b)
}
return a.Compare(b)
}
37 changes: 37 additions & 0 deletions util/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,40 @@ func TestCompareWithBuildIdentifiers(t *testing.T) {
})
}
}

func TestCompare(t *testing.T) {
tests := []struct {
name string
aVersion semver.Version
bVersion semver.Version
options []CompareOption
want int
}{
{
name: "comparing with no options should perform standard compare",
aVersion: semver.MustParse("1.2.3"),
bVersion: semver.MustParse("1.3.1"),
want: -1,
},
{
name: "comparing with no options should perform standard compare - equal versions",
aVersion: semver.MustParse("1.2.3+xyz.1"),
bVersion: semver.MustParse("1.2.3+xyz.2"),
want: 0,
},
{
name: "compare with build tags using the WithBuildTags option",
aVersion: semver.MustParse("1.2.3+xyz.1"),
bVersion: semver.MustParse("1.2.3+xyz.2"),
options: []CompareOption{WithBuildTags()},
want: -1,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
g.Expect(Compare(tt.aVersion, tt.bVersion, tt.options...)).To(Equal(tt.want))
})
}
}

0 comments on commit 18ecf48

Please sign in to comment.