diff --git a/cmd/initContainer/main.go b/cmd/initContainer/main.go index eaf642e1e0..d2284e66f8 100644 --- a/cmd/initContainer/main.go +++ b/cmd/initContainer/main.go @@ -6,6 +6,8 @@ package main import ( "flag" "os" + "regexp" + "strconv" "sync" "time" @@ -44,6 +46,11 @@ func main() { glog.Fatalf("Error creating client: %v\n", err) } + // Exit for unsupported version of kubernetes cluster + // https://github.com/nirmata/kyverno/issues/700 + // - supported from v1.12.7+ + isVersionSupported(client) + requests := []request{ // Resource {validatingWebhookConfigKind, config.ValidatingWebhookConfigurationName}, @@ -206,3 +213,32 @@ func merge(done <-chan struct{}, stopCh <-chan struct{}, processes ...<-chan err }() return out } + +func isVersionSupported(client *client.Client) { + serverVersion, err := client.DiscoveryClient.GetServerVersion() + if err != nil { + glog.Fatalf("Failed to get kubernetes server version: %v\n", err) + } + exp := regexp.MustCompile(`v(\d*).(\d*).(\d*)`) + groups := exp.FindAllStringSubmatch(serverVersion.String(), -1) + if len(groups) != 1 || len(groups[0]) != 4 { + glog.Fatalf("Failed to extract kubernetes server version: %v\n", serverVersion, err) + } + // convert string to int + // assuming the version are always intergers + major, err := strconv.Atoi(groups[0][1]) + if err != nil { + glog.Fatalf("Failed to extract kubernetes major server version: %v\n", serverVersion, err) + } + minor, err := strconv.Atoi(groups[0][2]) + if err != nil { + glog.Fatalf("Failed to extract kubernetes minor server version: %v\n", serverVersion, err) + } + sub, err := strconv.Atoi(groups[0][3]) + if err != nil { + glog.Fatalf("Failed to extract kubernetes sub minor server version: %v\n", serverVersion, err) + } + if major <= 1 && minor <= 12 && sub < 7 { + glog.Fatalf("Unsupported kubernetes server version %s. Kyverno is supported from version v1.12.7+", serverVersion) + } +} diff --git a/pkg/dclient/client.go b/pkg/dclient/client.go index b2834949e5..9bb212369e 100644 --- a/pkg/dclient/client.go +++ b/pkg/dclient/client.go @@ -16,6 +16,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" patchTypes "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/version" "k8s.io/client-go/discovery" "k8s.io/client-go/discovery/cached/memory" "k8s.io/client-go/dynamic" @@ -213,6 +214,7 @@ func convertToCSR(obj *unstructured.Unstructured) (*certificates.CertificateSign //IDiscovery provides interface to mange Kind and GVR mapping type IDiscovery interface { GetGVRFromKind(kind string) schema.GroupVersionResource + GetServerVersion() (*version.Info, error) } // SetDiscovery sets the discovery client implementation @@ -265,6 +267,11 @@ func (c ServerPreferredResources) GetGVRFromKind(kind string) schema.GroupVersio return gvr } +//GetServerVersion returns the server version of the cluster +func (c ServerPreferredResources) GetServerVersion() (*version.Info, error) { + return c.cachedClient.ServerVersion() +} + func loadServerResources(k string, cdi discovery.CachedDiscoveryInterface) (schema.GroupVersionResource, error) { serverresources, err := cdi.ServerPreferredResources() emptyGVR := schema.GroupVersionResource{}