mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-14 11:57:48 +00:00
Fix: kyverno-pre panic when checking kubernetes version (#2614)
Signed-off-by: Shaohui Liu <liushaohui@xiaomi.com>
This commit is contained in:
parent
f74f7c841a
commit
c90df17356
2 changed files with 18 additions and 7 deletions
|
@ -160,7 +160,7 @@ func HigherThanKubernetesVersion(client *client.Client, log logr.Logger, major,
|
|||
|
||||
b, err := isVersionHigher(serverVersion.String(), major, minor, patch)
|
||||
if err != nil {
|
||||
logger.Error(err, "serverVersion", serverVersion)
|
||||
logger.Error(err, "serverVersion", serverVersion.String())
|
||||
return false
|
||||
}
|
||||
|
||||
|
@ -168,27 +168,29 @@ func HigherThanKubernetesVersion(client *client.Client, log logr.Logger, major,
|
|||
}
|
||||
|
||||
func isVersionHigher(version string, major int, minor int, patch int) (bool, error) {
|
||||
groups := regexVersion.FindAllStringSubmatch(version, -1)
|
||||
if len(groups) != 1 || len(groups[0]) != 4 {
|
||||
groups := regexVersion.FindStringSubmatch(version)
|
||||
if len(groups) != 4 {
|
||||
return false, fmt.Errorf("invalid version %s. Expected {major}.{minor}.{patch}", version)
|
||||
}
|
||||
|
||||
currentMajor, err := strconv.Atoi(groups[0][1])
|
||||
currentMajor, err := strconv.Atoi(groups[1])
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to extract major version from %s", version)
|
||||
}
|
||||
|
||||
currentMinor, err := strconv.Atoi(groups[0][2])
|
||||
currentMinor, err := strconv.Atoi(groups[2])
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to extract minor version from %s", version)
|
||||
}
|
||||
|
||||
currentPatch, err := strconv.Atoi(groups[0][3])
|
||||
currentPatch, err := strconv.Atoi(groups[3])
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to extract minor version from %s", version)
|
||||
}
|
||||
|
||||
if currentMajor <= major && currentMinor <= minor && currentPatch <= patch {
|
||||
if currentMajor < major ||
|
||||
(currentMajor == major && currentMinor < minor) ||
|
||||
(currentMajor == major && currentMinor == minor && currentPatch <= patch) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -91,6 +91,15 @@ func Test_higherVersion(t *testing.T) {
|
|||
|
||||
v, err = isVersionHigher("v1.5.9-rc2", 1, 5, 9)
|
||||
assert.Assert(t, v == false && err == nil)
|
||||
|
||||
v, err = isVersionHigher("v1.5.9", 2, 1, 0)
|
||||
assert.Assert(t, v == false && err == nil)
|
||||
|
||||
v, err = isVersionHigher("v2.1.0", 1, 5, 9)
|
||||
assert.Assert(t, v == true && err == nil)
|
||||
|
||||
v, err = isVersionHigher("v1.5.9-x-v1.5.9.x", 1, 5, 8)
|
||||
assert.Assert(t, v == true && err == nil)
|
||||
}
|
||||
|
||||
func Test_ConvertResource(t *testing.T) {
|
||||
|
|
Loading…
Reference in a new issue