1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

allow text after patch versions (#1230)

This commit is contained in:
Jim Bugwadia 2020-11-02 22:14:36 -08:00 committed by GitHub
parent 7e9b08baeb
commit 48b98bd17b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 33 deletions

View file

@ -60,9 +60,7 @@ func main() {
}
// Exit for unsupported version of kubernetes cluster
// https://github.com/kyverno/kyverno/issues/700
// - supported from v1.12.7+
if !utils.HigherThanKubernetesVersion(client, log.Log, 1, 12, 7) {
if !utils.HigherThanKubernetesVersion(client, log.Log, 1, 14, 0) {
os.Exit(1)
}

View file

@ -19,6 +19,9 @@ import (
"k8s.io/client-go/rest"
)
var regexVersion = regexp.MustCompile(`v(\d+).(\d+).(\d+)\.*`)
//Contains Check if strint is contained in a list of string
func contains(list []string, element string, fn func(string, string) bool) bool {
for _, e := range list {
@ -56,14 +59,6 @@ func NewKubeClient(config *rest.Config) (kubernetes.Interface, error) {
return kclient, nil
}
//Btoi converts boolean to int
func Btoi(b bool) int {
if b {
return 1
}
return 0
}
//CRDInstalled to check if the CRD is installed or not
func CRDInstalled(discovery client.IDiscovery, log logr.Logger) bool {
logger := log.WithName("CRDInstalled")
@ -87,7 +82,7 @@ func CRDInstalled(discovery client.IDiscovery, log logr.Logger) bool {
func CleanupOldCrd(client *dclient.Client, log logr.Logger) {
logger := log.WithName("CleanupOldCrd")
gvr := client.DiscoveryClient.GetGVRFromKind("NamespacedPolicyViolation")
if !reflect.DeepEqual(gvr, (schema.GroupVersionResource{})) {
if !reflect.DeepEqual(gvr, schema.GroupVersionResource{}) {
if err := client.DeleteResource("", "CustomResourceDefinition", "", "namespacedpolicyviolations.kyverno.io", false); err != nil {
logger.Error(err, "Failed to remove prevous CRD", "kind", "namespacedpolicyviolation")
}
@ -137,41 +132,50 @@ func ConvertResource(raw []byte, group, version, kind, namespace string) (unstru
return *obj, nil
}
// HigherThanKubernetesVersion compare kuberneates client version to user given version
func HigherThanKubernetesVersion(client *client.Client, log logr.Logger, k8smajor, k8sminor, k8ssub int) bool {
// HigherThanKubernetesVersion compare Kubernetes client version to user given version
func HigherThanKubernetesVersion(client *client.Client, log logr.Logger, major, minor, patch int) bool {
logger := log.WithName("CompareKubernetesVersion")
serverVersion, err := client.DiscoveryClient.GetServerVersion()
if err != nil {
logger.Error(err, "Failed to get kubernetes server version")
return false
}
exp := regexp.MustCompile(`v(\d*).(\d*).(\d*)`)
groups := exp.FindAllStringSubmatch(serverVersion.String(), -1)
b, err := isVersionHigher(serverVersion.String(), major, minor, patch)
if err != nil {
logger.Error(err, "serverVersion", serverVersion)
return false
}
return b
}
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 {
logger.Error(err, "Failed to extract kubernetes server version", "serverVersion", serverVersion)
return false
return false, fmt.Errorf("invalid version %s. Expected {major}.{minor}.{patch}", version)
}
// convert string to int
// assuming the version are always intergers
major, err := strconv.Atoi(groups[0][1])
currentMajor, err := strconv.Atoi(groups[0][1])
if err != nil {
logger.Error(err, "Failed to extract kubernetes major server version", "serverVersion", serverVersion)
return false
return false, fmt.Errorf("failed to extract major version from %s", version)
}
minor, err := strconv.Atoi(groups[0][2])
currentMinor, err := strconv.Atoi(groups[0][2])
if err != nil {
logger.Error(err, "Failed to extract kubernetes minor server version", "serverVersion", serverVersion)
return false
return false, fmt.Errorf("failed to extract minor version from %s", version)
}
sub, err := strconv.Atoi(groups[0][3])
currentPatch, err := strconv.Atoi(groups[0][3])
if err != nil {
logger.Error(err, "Failed to extract kubernetes sub minor server version", "serverVersion", serverVersion)
return false
return false, fmt.Errorf("failed to extract minor version from %s", version)
}
if major <= k8smajor && minor <= k8sminor && sub < k8ssub {
return false
if currentMajor <= major && currentMinor <= minor && currentPatch <= patch {
return false, nil
}
return true
return true, nil
}
func SliceContains(slice []string, values ...string) bool {

View file

@ -65,5 +65,31 @@ func Test_containsNs(t *testing.T) {
patterns = []string{}
res = ContainsNamepace(patterns, "test")
assert.Assert(t, res == false)
}
func Test_higherVersion(t *testing.T) {
v, err := isVersionHigher("invalid.version", 1, 1, 1)
assert.Assert(t, v == false && err != nil)
v, err = isVersionHigher("invalid-version", 0, 0, 0)
assert.Assert(t, v == false && err != nil)
v, err = isVersionHigher("v1.1.1", 1, 1, 1)
assert.Assert(t, v == false && err == nil)
v, err = isVersionHigher("v1.0.0", 1, 1, 1)
assert.Assert(t, v == false && err == nil)
v, err = isVersionHigher("v1.5.9", 1, 5, 8)
assert.Assert(t, v == true && err == nil)
v, err = isVersionHigher("v1.5.9+distro", 1, 5, 8)
assert.Assert(t, v == true && err == nil)
v, err = isVersionHigher("v1.5.9+distro", 1, 5, 8)
assert.Assert(t, v == true && err == nil)
v, err = isVersionHigher("v1.5.9-rc2", 1, 5, 9)
assert.Assert(t, v == false && err == nil)
}