From bbeefb955b74b5195f0b0ee7209b033efcdbb8f9 Mon Sep 17 00:00:00 2001 From: shravan Date: Wed, 1 Apr 2020 19:06:13 +0530 Subject: [PATCH] 778 tested prototype --- pkg/openapi/validation.go | 7 +++++-- pkg/openapi/validation_test.go | 2 +- pkg/policy/validate.go | 10 ++++++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/openapi/validation.go b/pkg/openapi/validation.go index f8f9ece2c9..f25edd428d 100644 --- a/pkg/openapi/validation.go +++ b/pkg/openapi/validation.go @@ -70,7 +70,7 @@ func (o *Controller) ValidatePolicyFields(policyRaw []byte) error { return err } - return o.validatePolicyMutation(policy) + return o.ValidatePolicyMutation(policy) } func (o *Controller) ValidateResource(patchedResource unstructured.Unstructured, kind string) error { @@ -107,7 +107,10 @@ func (o *Controller) GetDefinitionNameFromKind(kind string) string { return o.kindToDefinitionName[kind] } -func (o *Controller) validatePolicyMutation(policy v1.ClusterPolicy) error { +func (o *Controller) ValidatePolicyMutation(policy v1.ClusterPolicy) error { + o.mutex.RLock() + defer o.mutex.RUnlock() + var kindToRules = make(map[string][]v1.Rule) for _, rule := range policy.Spec.Rules { if rule.HasMutate() { diff --git a/pkg/openapi/validation_test.go b/pkg/openapi/validation_test.go index ba4c5327f2..3472810554 100644 --- a/pkg/openapi/validation_test.go +++ b/pkg/openapi/validation_test.go @@ -54,7 +54,7 @@ func Test_ValidateMutationPolicy(t *testing.T) { _ = json.Unmarshal(tc.policy, &policy) var errMessage string - err := o.validatePolicyMutation(policy) + err := o.ValidatePolicyMutation(policy) if err != nil { errMessage = err.Error() } diff --git a/pkg/policy/validate.go b/pkg/policy/validate.go index 1816fa60fa..96efd07cce 100644 --- a/pkg/policy/validate.go +++ b/pkg/policy/validate.go @@ -75,8 +75,14 @@ func Validate(policyRaw []byte, client *dclient.Client, mock bool, openAPIContro } } - if err := openAPIController.ValidatePolicyFields(policyRaw); err != nil { - return err + if !mock { + if err := openAPIController.ValidatePolicyFields(policyRaw); err != nil { + return err + } + } else { + if err := openAPIController.ValidatePolicyMutation(p); err != nil { + return err + } } return nil