From b2e2dd8a0fa18b02eacd78b4fbcbe6574e1feafc Mon Sep 17 00:00:00 2001 From: shravan Date: Fri, 24 Jan 2020 20:22:33 +0530 Subject: [PATCH] 522 now supports all possible kinds --- pkg/policy/validation.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/pkg/policy/validation.go b/pkg/policy/validation.go index 128908b198..c21623e0a8 100644 --- a/pkg/policy/validation.go +++ b/pkg/policy/validation.go @@ -25,10 +25,11 @@ import ( ) var validationGlobalState struct { - document *openapi_v2.Document - definitions map[string]*openapi_v2.Schema - models proto.Models - isSet bool + document *openapi_v2.Document + definitions map[string]*openapi_v2.Schema + kindToDefinitionName map[string]string + models proto.Models + isSet bool } func init() { @@ -58,7 +59,7 @@ func ValidatePolicyMutation(policy v1.ClusterPolicy) error { newPolicy := policy newPolicy.Spec.Rules = rules - resource, _ := generateEmptyResource(validationGlobalState.definitions["io.k8s.api.core.v1."+kind]).(map[string]interface{}) + resource, _ := generateEmptyResource(validationGlobalState.definitions[validationGlobalState.kindToDefinitionName[kind]]).(map[string]interface{}) newResource := unstructured.Unstructured{Object: resource} newResource.SetKind(kind) policyContext := engine.PolicyContext{ @@ -91,7 +92,7 @@ func ValidateResource(patchedResource interface{}, kind string) error { return nil } - kind = "io.k8s.api.core.v1." + kind + kind = validationGlobalState.kindToDefinitionName[kind] schema := validationGlobalState.models.LookupModel(kind) if schema == nil { @@ -119,9 +120,11 @@ func setValidationGlobalState() error { } validationGlobalState.definitions = make(map[string]*openapi_v2.Schema) - + validationGlobalState.kindToDefinitionName = make(map[string]string) for _, definition := range validationGlobalState.document.GetDefinitions().AdditionalProperties { validationGlobalState.definitions[definition.GetName()] = definition.GetValue() + path := strings.Split(definition.GetName(), ".") + validationGlobalState.kindToDefinitionName[path[len(path)-1]] = definition.GetName() } validationGlobalState.models, err = proto.NewOpenAPIData(validationGlobalState.document)