From 5272ca8601420aa77017d77dd6d260c725a4b701 Mon Sep 17 00:00:00 2001 From: anushkamittal2001 <138426011+anushkamittal2001@users.noreply.github.com> Date: Mon, 14 Aug 2023 19:43:29 +0530 Subject: [PATCH] Remove length restriction in --set (#7984) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Remove length restriction in --set Signed-off-by: anushkamittal2001 * Add variables in unit test for apply_command Signed-off-by: anushkamittal2001 --------- Signed-off-by: anushkamittal2001 Co-authored-by: shuting Co-authored-by: Charles-Edouard Brétéché --- .../kubectl-kyverno/apply/apply_command.go | 3 --- .../apply/apply_command_test.go | 19 ++++++++++++++++ test/cli/apply/policies-set/policy.yaml | 20 +++++++++++++++++ test/cli/apply/resources-set/resources.yaml | 22 +++++++++++++++++++ 4 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 test/cli/apply/policies-set/policy.yaml create mode 100644 test/cli/apply/resources-set/resources.yaml diff --git a/cmd/cli/kubectl-kyverno/apply/apply_command.go b/cmd/cli/kubectl-kyverno/apply/apply_command.go index 0b19463678..a26187ac8e 100644 --- a/cmd/cli/kubectl-kyverno/apply/apply_command.go +++ b/cmd/cli/kubectl-kyverno/apply/apply_command.go @@ -331,9 +331,6 @@ func (c *ApplyCommandConfig) applyCommandHelper() (*common.ResultCounts, []*unst fmt.Printf("Error: failed to load resources\nCause: %s\n", err) osExit(1) } - if (len(resources) > 1 || len(policies) > 1) && c.Variables != nil { - return nil, resources, skipInvalidPolicies, nil, sanitizederror.NewWithError("currently `set` flag supports variable for single policy applied on single resource ", nil) - } // init variables if len(variables) != 0 { variables = common.SetInStoreContext(policies, variables) diff --git a/cmd/cli/kubectl-kyverno/apply/apply_command_test.go b/cmd/cli/kubectl-kyverno/apply/apply_command_test.go index d2f9bdb10e..99f86e49a5 100644 --- a/cmd/cli/kubectl-kyverno/apply/apply_command_test.go +++ b/cmd/cli/kubectl-kyverno/apply/apply_command_test.go @@ -174,6 +174,25 @@ func Test_Apply(t *testing.T) { }, }, }, + { + config: ApplyCommandConfig{ + PolicyPaths: []string{"../../../../test/cli/apply/policies-set"}, + ResourcePaths: []string{"../../../../test/cli/apply/resources-set"}, + Variables: []string{"request.operation=UPDATE"}, + PolicyReport: true, + }, + expectedPolicyReports: []preport.PolicyReport{ + { + Summary: preport.PolicyReportSummary{ + Pass: 2, + Fail: 0, + Skip: 4, + Error: 0, + Warn: 0, + }, + }, + }, + }, { config: ApplyCommandConfig{ PolicyPaths: []string{"../../../../test/cli/test-validating-admission-policy/check-deployments-replica/policy.yaml"}, diff --git a/test/cli/apply/policies-set/policy.yaml b/test/cli/apply/policies-set/policy.yaml new file mode 100644 index 0000000000..4ae9bdacb4 --- /dev/null +++ b/test/cli/apply/policies-set/policy.yaml @@ -0,0 +1,20 @@ +apiVersion: kyverno.io/v1 +kind: ClusterPolicy +metadata: + name: enforce-label +spec: + validationFailureAction: Audit + background: false + rules: + - name: enforce-label + match: + any: + - resources: + kinds: + - Pod + validate: + message: "The foo label must be set." + pattern: + metadata: + labels: + op: "{{ request.operation }}" \ No newline at end of file diff --git a/test/cli/apply/resources-set/resources.yaml b/test/cli/apply/resources-set/resources.yaml new file mode 100644 index 0000000000..7ff99ef621 --- /dev/null +++ b/test/cli/apply/resources-set/resources.yaml @@ -0,0 +1,22 @@ +apiVersion: v1 +kind: Pod +metadata: + name: test-pod-1 + labels: + foo: bar + op: UPDATE +spec: + containers: + - name: nginx + image: nginx:latest +--- +apiVersion: v1 +kind: Pod +metadata: + name: test-pod-2 + labels: + foo: bar + op: UPDATE +spec: + containers: + - name: nginx