Skip to content

Commit f7e861d

Browse files
Add version mapping in ComponentGlobalsRegistry.
Signed-off-by: Siyuan Zhang <sizhang@google.com> Kubernetes-commit: 4352c4ad2762ce49ce30e62381f8ceb24723fbcc
1 parent cc42a15 commit f7e861d

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

pkg/util/version/version.go

+47-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"regexp"
2424
"strconv"
2525
"strings"
26+
27+
apimachineryversion "k8s.io/apimachinery/pkg/version"
2628
)
2729

2830
// Version is an opaque representation of a version number
@@ -31,6 +33,7 @@ type Version struct {
3133
semver bool
3234
preRelease string
3335
buildMetadata string
36+
info apimachineryversion.Info
3437
}
3538

3639
var (
@@ -252,19 +255,30 @@ func (v *Version) WithMinor(minor uint) *Version {
252255
return &result
253256
}
254257

255-
// SubtractMinor returns the version diff minor versions back, with the same major and no patch.
256-
// If diff >= current minor, the minor would be 0.
257-
func (v *Version) SubtractMinor(diff uint) *Version {
258+
// SubtractMinor returns the version with offset from the original minor, with the same major and no patch.
259+
// If -offset >= current minor, the minor would be 0.
260+
func (v *Version) OffsetMinor(offset int) *Version {
258261
var minor uint
259-
if diff < v.Minor() {
260-
minor = v.Minor() - diff
262+
if offset >= 0 {
263+
minor = v.Minor() + uint(offset)
264+
} else {
265+
diff := uint(-offset)
266+
if diff < v.Minor() {
267+
minor = v.Minor() - diff
268+
}
261269
}
262270
return MajorMinor(v.Major(), minor)
263271
}
264272

273+
// SubtractMinor returns the version diff minor versions back, with the same major and no patch.
274+
// If diff >= current minor, the minor would be 0.
275+
func (v *Version) SubtractMinor(diff uint) *Version {
276+
return v.OffsetMinor(-int(diff))
277+
}
278+
265279
// AddMinor returns the version diff minor versions forward, with the same major and no patch.
266280
func (v *Version) AddMinor(diff uint) *Version {
267-
return MajorMinor(v.Major(), v.Minor()+diff)
281+
return v.OffsetMinor(int(diff))
268282
}
269283

270284
// WithPatch returns copy of the version object with requested patch number
@@ -441,3 +455,30 @@ func (v *Version) Compare(other string) (int, error) {
441455
}
442456
return v.compareInternal(ov), nil
443457
}
458+
459+
// WithInfo returns copy of the version object with requested info
460+
func (v *Version) WithInfo(info apimachineryversion.Info) *Version {
461+
result := *v
462+
result.info = info
463+
return &result
464+
}
465+
466+
func (v *Version) Info() *apimachineryversion.Info {
467+
if v == nil {
468+
return nil
469+
}
470+
// in case info is empty, or the major and minor in info is different from the actual major and minor
471+
v.info.Major = itoa(v.Major())
472+
v.info.Minor = itoa(v.Minor())
473+
if v.info.GitVersion == "" {
474+
v.info.GitVersion = v.String()
475+
}
476+
return &v.info
477+
}
478+
479+
func itoa(i uint) string {
480+
if i == 0 {
481+
return ""
482+
}
483+
return strconv.Itoa(int(i))
484+
}

pkg/util/version/version_test.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -453,37 +453,42 @@ func TestHighestSupportedVersion(t *testing.T) {
453453
}
454454
}
455455

456-
func TestSubtractMinor(t *testing.T) {
456+
func TestOffsetMinor(t *testing.T) {
457457
var tests = []struct {
458458
version string
459-
diff uint
459+
diff int
460460
expectedComponents []uint
461461
}{
462462
{
463463
version: "1.0.2",
464-
diff: 3,
464+
diff: -3,
465465
expectedComponents: []uint{1, 0},
466466
},
467467
{
468468
version: "1.3.2-alpha+001",
469-
diff: 2,
469+
diff: -2,
470470
expectedComponents: []uint{1, 1},
471471
},
472472
{
473473
version: "1.3.2-alpha+001",
474-
diff: 3,
474+
diff: -3,
475475
expectedComponents: []uint{1, 0},
476476
},
477477
{
478478
version: "1.20",
479-
diff: 5,
479+
diff: -5,
480480
expectedComponents: []uint{1, 15},
481481
},
482+
{
483+
version: "1.20",
484+
diff: 5,
485+
expectedComponents: []uint{1, 25},
486+
},
482487
}
483488

484489
for _, test := range tests {
485490
version, _ := ParseGeneric(test.version)
486-
if !reflect.DeepEqual(test.expectedComponents, version.SubtractMinor(test.diff).Components()) {
491+
if !reflect.DeepEqual(test.expectedComponents, version.OffsetMinor(test.diff).Components()) {
487492
t.Error("parse returned un'expected components")
488493
}
489494
}

0 commit comments

Comments
 (0)