From 410e53bd9fe091f84c5af7932142648760fde8ef Mon Sep 17 00:00:00 2001 From: shravan Date: Thu, 2 Apr 2020 21:13:39 +0530 Subject: [PATCH 1/2] 787 tested prototype --- pkg/kyverno/apply/command.go | 13 +++++++++++++ pkg/policy/generate/fake.go | 2 ++ 2 files changed, 15 insertions(+) diff --git a/pkg/kyverno/apply/command.go b/pkg/kyverno/apply/command.go index ce07f367c1..a99cff4646 100644 --- a/pkg/kyverno/apply/command.go +++ b/pkg/kyverno/apply/command.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "path/filepath" + "regexp" "github.com/nirmata/kyverno/pkg/utils" @@ -81,6 +82,9 @@ func Command() *cobra.Command { if err != nil { return sanitizedError.New(fmt.Sprintf("Policy %v is not valid", policy.Name)) } + if policyHasVariables(*policy) { + return sanitizedError.New(fmt.Sprintf("Policy %v is not valid - 'apply' does not support policies with variables", policy.Name)) + } } var dClient discovery.CachedDiscoveryInterface @@ -387,3 +391,12 @@ func applyPolicyOnResource(policy *v1.ClusterPolicy, resource *unstructured.Unst return nil } + +func policyHasVariables(policy v1.ClusterPolicy) bool { + policyRaw, _ := json.Marshal(policy) + regex := regexp.MustCompile(`\{\{([^{}]*)\}\}`) + if len(regex.FindAllStringSubmatch(string(policyRaw), -1)) > 0 { + return true + } + return false +} diff --git a/pkg/policy/generate/fake.go b/pkg/policy/generate/fake.go index 0d561c7a94..22fcd408e9 100644 --- a/pkg/policy/generate/fake.go +++ b/pkg/policy/generate/fake.go @@ -3,6 +3,7 @@ package generate import ( kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1" "github.com/nirmata/kyverno/pkg/policy/generate/fake" + "sigs.k8s.io/controller-runtime/pkg/log" ) //FakeGenerate provides implementation for generate rule processing @@ -17,5 +18,6 @@ func NewFakeGenerate(rule kyverno.Generation) *FakeGenerate { g := FakeGenerate{} g.rule = rule g.authCheck = fake.NewFakeAuth() + g.log = log.Log return &g } From 7ce0c8d68a7d006454f80e88b362a3e05b7192b4 Mon Sep 17 00:00:00 2001 From: shravan Date: Thu, 2 Apr 2020 21:19:51 +0530 Subject: [PATCH 2/2] 787 circle ci changes --- pkg/kyverno/apply/command.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pkg/kyverno/apply/command.go b/pkg/kyverno/apply/command.go index a99cff4646..75ee885986 100644 --- a/pkg/kyverno/apply/command.go +++ b/pkg/kyverno/apply/command.go @@ -395,8 +395,5 @@ func applyPolicyOnResource(policy *v1.ClusterPolicy, resource *unstructured.Unst func policyHasVariables(policy v1.ClusterPolicy) bool { policyRaw, _ := json.Marshal(policy) regex := regexp.MustCompile(`\{\{([^{}]*)\}\}`) - if len(regex.FindAllStringSubmatch(string(policyRaw), -1)) > 0 { - return true - } - return false + return len(regex.FindAllStringSubmatch(string(policyRaw), -1)) > 0 }