From 68e4b9de12a6ccb0454d559b1beb1ab0e9f53881 Mon Sep 17 00:00:00 2001 From: Marc Brugger Date: Wed, 22 Mar 2023 19:12:34 +0100 Subject: [PATCH] fix: correct cli test duplicate check (#6630) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: correct cli test dublicate check Signed-off-by: bakito * warn when resources are duplicated Signed-off-by: bakito --------- Signed-off-by: bakito Co-authored-by: Charles-Edouard Brétéché --- cmd/cli/kubectl-kyverno/test/test_command.go | 76 +++++++++++--------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/cmd/cli/kubectl-kyverno/test/test_command.go b/cmd/cli/kubectl-kyverno/test/test_command.go index a6452176fe..1bc835504d 100644 --- a/cmd/cli/kubectl-kyverno/test/test_command.go +++ b/cmd/cli/kubectl-kyverno/test/test_command.go @@ -856,37 +856,7 @@ func applyPoliciesFromPath(fs billy.Filesystem, policyBytes []byte, isGit bool, os.Exit(1) } - filteredResources := []*unstructured.Unstructured{} - for _, r := range resources { - for _, res := range values.Results { - for _, testr := range res.Resources { - if r.GetName() == testr { - filteredResources = append(filteredResources, r) - } - } - if r.GetName() == res.Resource { - filteredResources = append(filteredResources, r) - break - } - } - } - resources = filteredResources - - noDuplicateResources := []*unstructured.Unstructured{} - - for _, resource := range resources { - duplicate := false - for _, unique := range noDuplicateResources { - if resource.GetKind() == unique.GetKind() && resource.GetName() == unique.GetName() && resource.GetNamespace() == unique.GetNamespace() { - duplicate = true - fmt.Println("skipping duplicate resource, resource :", resource) - break - } - } - if !duplicate { - noDuplicateResources = append(noDuplicateResources, resource) - } - } + checkableResources := selectResourcesForCheck(resources, values) msgPolicies := "1 policy" if len(policies) > 1 { @@ -894,11 +864,11 @@ func applyPoliciesFromPath(fs billy.Filesystem, policyBytes []byte, isGit bool, } msgResources := "1 resource" - if len(noDuplicateResources) > 1 { - msgResources = fmt.Sprintf("%d resources", len(noDuplicateResources)) + if len(checkableResources) > 1 { + msgResources = fmt.Sprintf("%d resources", len(checkableResources)) } - if len(policies) > 0 && len(noDuplicateResources) > 0 { + if len(policies) > 0 && len(checkableResources) > 0 { fmt.Printf("\napplying %s to %s... \n", msgPolicies, msgResources) } @@ -923,7 +893,7 @@ func applyPoliciesFromPath(fs billy.Filesystem, policyBytes []byte, isGit bool, kindOnwhichPolicyIsApplied := common.GetKindsFromPolicy(policy, subresources, dClient) - for _, resource := range noDuplicateResources { + for _, resource := range checkableResources { thisPolicyResourceValues, err := common.CheckVariableForPolicy(valuesMap, globalValMap, policy.GetName(), resource.GetName(), resource.GetKind(), variables, kindOnwhichPolicyIsApplied, variable) if err != nil { return sanitizederror.NewWithError(fmt.Sprintf("policy `%s` have variables. pass the values for the variables for resource `%s` using set/values_file flag", policy.GetName(), resource.GetName()), err) @@ -958,6 +928,42 @@ func applyPoliciesFromPath(fs billy.Filesystem, policyBytes []byte, isGit bool, return } +func selectResourcesForCheck(resources []*unstructured.Unstructured, values *api.Test) []*unstructured.Unstructured { + uniqResources := make(map[string]*unstructured.Unstructured) + + for i := range resources { + r := resources[i] + key := fmt.Sprintf("%s/%s/%s", r.GetKind(), r.GetName(), r.GetNamespace()) + if _, ok := uniqResources[key]; ok { + fmt.Println("skipping duplicate resource, resource :", r) + } else { + uniqResources[key] = r + } + } + + selectedResources := map[string]*unstructured.Unstructured{} + for key := range uniqResources { + r := uniqResources[key] + for _, res := range values.Results { + for _, testr := range res.Resources { + if r.GetName() == testr { + selectedResources[key] = r + } + } + if r.GetName() == res.Resource { + selectedResources[key] = r + } + } + } + + var checkableResources []*unstructured.Unstructured + + for key := range selectedResources { + checkableResources = append(checkableResources, selectedResources[key]) + } + return checkableResources +} + func printTestResult(resps map[string]policyreportv1alpha2.PolicyReportResult, testResults []api.TestResults, rc *resultCounts, failOnly, removeColor bool) error { printer := newTablePrinter(removeColor) table := []Table{}