From c26a41418240ebd57201f4253817849ffb22f42a Mon Sep 17 00:00:00 2001 From: NoSkillGirl Date: Sat, 11 Jul 2020 17:52:52 +0530 Subject: [PATCH] auto generating policy rules --- pkg/kyverno/apply/command.go | 6 +++++- pkg/policymutation/policymutation.go | 19 ++++--------------- pkg/webhooks/policymutation.go | 2 -- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/pkg/kyverno/apply/command.go b/pkg/kyverno/apply/command.go index ca85326e54..fcfd205087 100644 --- a/pkg/kyverno/apply/command.go +++ b/pkg/kyverno/apply/command.go @@ -99,8 +99,12 @@ func Command() *cobra.Command { newPolicies := make([]*v1.ClusterPolicy, 0) + logger := log.Log.WithName("apply") + for _, policy := range policies { - patches, _ := policymutation.GenerateJSONPatchesForDefaults(policy, nil) + patches, updateMsgs := policymutation.GenerateJSONPatchesForDefaults(policy, logger) + + fmt.Println(updateMsgs) type jsonPatch struct { Path string `json:"path"` diff --git a/pkg/policymutation/policymutation.go b/pkg/policymutation/policymutation.go index eda032c6bc..aa6b267589 100644 --- a/pkg/policymutation/policymutation.go +++ b/pkg/policymutation/policymutation.go @@ -37,11 +37,7 @@ func GenerateJSONPatchesForDefaults(policy *kyverno.ClusterPolicy, log logr.Logg var errMsgs []string for _, err := range errs { errMsgs = append(errMsgs, err.Error()) - if log != nil { - log.Error(err, "failed to generate pod controller rule") - } else { - fmt.Println(err, " failed to generate pod controller rule") - } + log.Error(err, "failed to generate pod controller rule") } updateMsgs = append(updateMsgs, strings.Join(errMsgs, ";")) } @@ -91,9 +87,7 @@ func defaultvalidationFailureAction(policy *kyverno.ClusterPolicy, log logr.Logg // set ValidationFailureAction to "audit" if not specified Audit := common.Audit if policy.Spec.ValidationFailureAction == "" { - if log != nil { - log.V(4).Info("setting defautl value", "spec.validationFailureAction", Audit) - } + log.V(4).Info("setting defautl value", "spec.validationFailureAction", Audit) jsonPatch := struct { Path string `json:"path"` @@ -107,16 +101,11 @@ func defaultvalidationFailureAction(policy *kyverno.ClusterPolicy, log logr.Logg patchByte, err := json.Marshal(jsonPatch) if err != nil { - if log != nil { - log.Error(err, "failed to default value", "spec.validationFailureAction", Audit) - } - fmt.Println(err, "failed to default value", "spec.validationFailureAction", Audit) + log.Error(err, "failed to default value", "spec.validationFailureAction", Audit) return nil, "" } - if log != nil { - log.V(3).Info("generated JSON Patch to set default", "spec.validationFailureAction", Audit) - } + log.V(3).Info("generated JSON Patch to set default", "spec.validationFailureAction", Audit) return patchByte, fmt.Sprintf("default 'ValidationFailureAction' to '%s'", Audit) } diff --git a/pkg/webhooks/policymutation.go b/pkg/webhooks/policymutation.go index 426a386e72..995490f4c1 100644 --- a/pkg/webhooks/policymutation.go +++ b/pkg/webhooks/policymutation.go @@ -43,5 +43,3 @@ func (ws *WebhookServer) policyMutation(request *v1beta1.AdmissionRequest) *v1be Allowed: true, } } - -// func GenerateJSONPatchesForDefaults(policy *kyverno.ClusterPolicy, log logr.Logger) ([]byte, []string) {