Skip to content

Commit 18ecf48

Browse files
authored
Merge pull request #5364 from ykakarap/version-compare-utils
🌱 add version.Compare with CompareOptions
2 parents 33e9f42 + f4380fc commit 18ecf48

File tree

5 files changed

+79
-4
lines changed

5 files changed

+79
-4
lines changed

api/v1beta1/cluster_webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func (c *Cluster) validateTopology(old *Cluster) field.ErrorList {
233233
),
234234
)
235235
}
236-
if inVersion.NE(semver.Version{}) && oldVersion.NE(semver.Version{}) && version.CompareWithBuildIdentifiers(inVersion, oldVersion) == -1 {
236+
if inVersion.NE(semver.Version{}) && oldVersion.NE(semver.Version{}) && version.Compare(inVersion, oldVersion, version.WithBuildTags()) == -1 {
237237
allErrs = append(
238238
allErrs,
239239
field.Invalid(

controllers/topology/internal/contract/controlplane.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (c *ControlPlaneContract) IsUpgrading(obj *unstructured.Unstructured) (bool
124124
return false, errors.Wrap(err, "failed to parse control plane status version")
125125
}
126126

127-
return version.CompareWithBuildIdentifiers(specV, statusV) == 1, nil
127+
return version.Compare(specV, statusV, version.WithBuildTags()) == 1, nil
128128
}
129129

130130
// IsScaling returns true if the control plane is in the middle of a scale operation, false otherwise.

util/collections/machine_collection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (v machinesByVersion) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
4949
func (v machinesByVersion) Less(i, j int) bool {
5050
vi, _ := semver.ParseTolerant(*v[i].Spec.Version)
5151
vj, _ := semver.ParseTolerant(*v[j].Spec.Version)
52-
comp := version.CompareWithBuildIdentifiers(vi, vj)
52+
comp := version.Compare(vi, vj, version.WithBuildTags())
5353
if comp == 0 {
5454
return v[i].Name < v[j].Name
5555
}

util/version/version.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,13 @@ func (v buildIdentifier) compare(o buildIdentifier) int {
173173
}
174174
}
175175

176-
// CompareWithBuildIdentifiers compares 2 version a and b.
176+
// CompareWithBuildIdentifiers compares two versions a and b.
177177
// Perfoms a standard version compare between a and b. If the versions
178178
// are equal, build identifiers will be used to compare further.
179179
// -1 == a is less than b.
180180
// 0 == a is equal to b.
181181
// 1 == a is greater than b.
182+
// Deprecated: Use Compare(a, b, WithBuildTags()) instead.
182183
func CompareWithBuildIdentifiers(a semver.Version, b semver.Version) int {
183184
if comp := a.Compare(b); comp != 0 {
184185
return comp
@@ -187,3 +188,40 @@ func CompareWithBuildIdentifiers(a semver.Version, b semver.Version) int {
187188
biB := newBuildIdentifiers(b.Build)
188189
return biA.compare(biB)
189190
}
191+
192+
type comparer struct {
193+
buildTags bool
194+
}
195+
196+
// CompareOption is a configuration option for Compare.
197+
type CompareOption func(*comparer)
198+
199+
// WithBuildTags modifies the version comparison to also consider build tags
200+
// when comparing versions.
201+
// Performs a standard version compare between a and b. If the versions
202+
// are equal, build identifiers will be used to compare further.
203+
// -1 == a is less than b.
204+
// 0 == a is equal to b.
205+
// 1 == a is greater than b.
206+
func WithBuildTags() CompareOption {
207+
return func(c *comparer) {
208+
c.buildTags = true
209+
}
210+
}
211+
212+
// Compare 2 semver versions.
213+
// Defaults to doing the standard semver comparison when no options are specified.
214+
// The comparison logic can be modified by passing additional compare options.
215+
// Example: using the WithBuildTags() option modifies the compare logic to also
216+
// consider build tags when comparing versions.
217+
func Compare(a, b semver.Version, options ...CompareOption) int {
218+
c := &comparer{}
219+
for _, o := range options {
220+
o(c)
221+
}
222+
223+
if c.buildTags {
224+
return CompareWithBuildIdentifiers(a, b)
225+
}
226+
return a.Compare(b)
227+
}

util/version/version_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,40 @@ func TestCompareWithBuildIdentifiers(t *testing.T) {
272272
})
273273
}
274274
}
275+
276+
func TestCompare(t *testing.T) {
277+
tests := []struct {
278+
name string
279+
aVersion semver.Version
280+
bVersion semver.Version
281+
options []CompareOption
282+
want int
283+
}{
284+
{
285+
name: "comparing with no options should perform standard compare",
286+
aVersion: semver.MustParse("1.2.3"),
287+
bVersion: semver.MustParse("1.3.1"),
288+
want: -1,
289+
},
290+
{
291+
name: "comparing with no options should perform standard compare - equal versions",
292+
aVersion: semver.MustParse("1.2.3+xyz.1"),
293+
bVersion: semver.MustParse("1.2.3+xyz.2"),
294+
want: 0,
295+
},
296+
{
297+
name: "compare with build tags using the WithBuildTags option",
298+
aVersion: semver.MustParse("1.2.3+xyz.1"),
299+
bVersion: semver.MustParse("1.2.3+xyz.2"),
300+
options: []CompareOption{WithBuildTags()},
301+
want: -1,
302+
},
303+
}
304+
305+
for _, tt := range tests {
306+
t.Run(tt.name, func(t *testing.T) {
307+
g := NewWithT(t)
308+
g.Expect(Compare(tt.aVersion, tt.bVersion, tt.options...)).To(Equal(tt.want))
309+
})
310+
}
311+
}

0 commit comments

Comments
 (0)