Skip to content

Commit 9ff3194

Browse files
authored
Merge pull request #772 from TeweiLuo/version-check
Allow using minor version format for GKE
2 parents 0098649 + d2a3b9f commit 9ff3194

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

test/k8s-integration/version.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var (
1515
internalPatchVersion = `(\-[a-zA-Z0-9_.+-]+)`
1616

1717
versionRegex = regexp.MustCompile(`^` + versionNum + `\.` + versionNum + `\.` + versionNum + internalPatchVersion + "?$")
18+
minorVersionRegex = regexp.MustCompile(`^` + versionNum + `\.` + versionNum + `$`)
1819
gkeExtraVersionRegex = regexp.MustCompile(`^(?:gke)\.(0|[1-9][0-9]*)$`)
1920
)
2021

@@ -73,20 +74,35 @@ func parseVersion(vs string) (*version, error) {
7374
vs = vs[1:]
7475
}
7576

76-
submatches := versionRegex.FindStringSubmatch(vs)
77-
if submatches == nil {
77+
var submatches []string
78+
var v version
79+
var lastIndex int
80+
81+
switch {
82+
case versionRegex.MatchString(vs):
83+
submatches = versionRegex.FindStringSubmatch(vs)
84+
lastIndex = 4
85+
case minorVersionRegex.MatchString(vs):
86+
submatches = minorVersionRegex.FindStringSubmatch(vs)
87+
v.version[2] = -1
88+
v.version[3] = -1
89+
lastIndex = 3
90+
default:
7891
return nil, fmt.Errorf("version %q is invalid", vs)
7992
}
8093

81-
var v version
8294
// submatches[0] is the whole match, [1]..[3] are the version bits, [4] is the extra
83-
for i, sm := range submatches[1:4] {
95+
for i, sm := range submatches[1:lastIndex] {
8496
var err error
8597
if v.version[i], err = strconv.Atoi(sm); err != nil {
8698
return nil, fmt.Errorf("submatch %q failed atoi conversion", sm)
8799
}
88100
}
89101

102+
if minorVersionRegex.MatchString(vs) {
103+
return &v, nil
104+
}
105+
90106
// Ensure 1.X.Y < 1.X.Y-gke.0
91107
v.version[3] = -1
92108
if submatches[4] != "" {

test/k8s-integration/version_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ func TestParseVersion(t *testing.T) {
6666
version: [4]int{100, 101, 102, 103},
6767
},
6868
},
69+
{
70+
version: "1.20",
71+
expectedV: version{
72+
version: [4]int{1, 20, -1, -1},
73+
},
74+
},
6975
// Negative test cases
7076
{
7177
version: "1",
@@ -87,14 +93,6 @@ func TestParseVersion(t *testing.T) {
8793
version: "1.18.9-gke.-1",
8894
expectErr: true,
8995
},
90-
{
91-
version: "1.1",
92-
expectErr: true,
93-
},
94-
{
95-
version: "1.18",
96-
expectErr: true,
97-
},
9896
{
9997
version: "1.18.9.1",
10098
expectErr: true,

0 commit comments

Comments
 (0)