From 9f6b0e8017da7c2e5254dea3277488cdac8e3304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Wed, 2 Nov 2022 09:06:44 +0000 Subject: [PATCH] refactor: remove policyreport package (#5174) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché Signed-off-by: Charles-Edouard Brétéché --- cmd/cli/kubectl-kyverno/apply/apply_command.go | 5 ++--- cmd/cli/kubectl-kyverno/apply/report.go | 6 +++--- cmd/cli/kubectl-kyverno/apply/report_test.go | 6 +++--- cmd/cli/kubectl-kyverno/test/test_command.go | 5 ++--- cmd/cli/kubectl-kyverno/utils/common/common.go | 17 ++++++++--------- .../cli/kubectl-kyverno/utils/common}/info.go | 2 +- 6 files changed, 19 insertions(+), 22 deletions(-) rename {pkg/policyreport => cmd/cli/kubectl-kyverno/utils/common}/info.go (95%) diff --git a/cmd/cli/kubectl-kyverno/apply/apply_command.go b/cmd/cli/kubectl-kyverno/apply/apply_command.go index 157a5e7e6f..15b30b7108 100644 --- a/cmd/cli/kubectl-kyverno/apply/apply_command.go +++ b/cmd/cli/kubectl-kyverno/apply/apply_command.go @@ -17,7 +17,6 @@ import ( "github.com/kyverno/kyverno/pkg/config" "github.com/kyverno/kyverno/pkg/openapi" policy2 "github.com/kyverno/kyverno/pkg/policy" - "github.com/kyverno/kyverno/pkg/policyreport" "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/client-go/kubernetes" @@ -167,7 +166,7 @@ func Command() *cobra.Command { return cmd } -func (c *ApplyCommandConfig) applyCommandHelper() (rc *common.ResultCounts, resources []*unstructured.Unstructured, skipInvalidPolicies SkippedInvalidPolicies, pvInfos []policyreport.Info, err error) { +func (c *ApplyCommandConfig) applyCommandHelper() (rc *common.ResultCounts, resources []*unstructured.Unstructured, skipInvalidPolicies SkippedInvalidPolicies, pvInfos []common.Info, err error) { store.SetMock(true) store.SetRegistryAccess(c.RegistryAccess) if c.Cluster { @@ -396,7 +395,7 @@ func checkMutateLogPath(mutateLogPath string) (mutateLogPathIsDir bool, err erro } // PrintReportOrViolation - printing policy report/violations -func PrintReportOrViolation(policyReport bool, rc *common.ResultCounts, resourcePaths []string, resourcesLen int, skipInvalidPolicies SkippedInvalidPolicies, stdin bool, pvInfos []policyreport.Info) { +func PrintReportOrViolation(policyReport bool, rc *common.ResultCounts, resourcePaths []string, resourcesLen int, skipInvalidPolicies SkippedInvalidPolicies, stdin bool, pvInfos []common.Info) { divider := "----------------------------------------------------------------------" if len(skipInvalidPolicies.skipped) > 0 { diff --git a/cmd/cli/kubectl-kyverno/apply/report.go b/cmd/cli/kubectl-kyverno/apply/report.go index 1449a0c1d3..14a16ac3bf 100644 --- a/cmd/cli/kubectl-kyverno/apply/report.go +++ b/cmd/cli/kubectl-kyverno/apply/report.go @@ -8,9 +8,9 @@ import ( kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1" policyreportv1alpha2 "github.com/kyverno/kyverno/api/policyreport/v1alpha2" + "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/common" "github.com/kyverno/kyverno/pkg/engine/response" engineutils "github.com/kyverno/kyverno/pkg/engine/utils" - "github.com/kyverno/kyverno/pkg/policyreport" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -21,7 +21,7 @@ import ( const clusterpolicyreport = "clusterpolicyreport" // resps is the engine responses generated for a single policy -func buildPolicyReports(pvInfos []policyreport.Info) (res []*unstructured.Unstructured) { +func buildPolicyReports(pvInfos []common.Info) (res []*unstructured.Unstructured) { var raw []byte var err error @@ -74,7 +74,7 @@ func buildPolicyReports(pvInfos []policyreport.Info) (res []*unstructured.Unstru // buildPolicyResults returns a string-PolicyReportResult map // the key of the map is one of "clusterpolicyreport", "policyreport-ns-" -func buildPolicyResults(infos []policyreport.Info) map[string][]policyreportv1alpha2.PolicyReportResult { +func buildPolicyResults(infos []common.Info) map[string][]policyreportv1alpha2.PolicyReportResult { results := make(map[string][]policyreportv1alpha2.PolicyReportResult) now := metav1.Timestamp{Seconds: time.Now().Unix()} diff --git a/cmd/cli/kubectl-kyverno/apply/report_test.go b/cmd/cli/kubectl-kyverno/apply/report_test.go index 2c6d1c0b22..94cd3ea9be 100644 --- a/cmd/cli/kubectl-kyverno/apply/report_test.go +++ b/cmd/cli/kubectl-kyverno/apply/report_test.go @@ -6,9 +6,9 @@ import ( kyverno "github.com/kyverno/kyverno/api/kyverno/v1" preport "github.com/kyverno/kyverno/api/policyreport/v1alpha2" + "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/common" kyvCommon "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/common" "github.com/kyverno/kyverno/pkg/engine/response" - "github.com/kyverno/kyverno/pkg/policyreport" "gotest.tools/assert" v1 "k8s.io/api/core/v1" ) @@ -87,7 +87,7 @@ var rawEngRes = []byte(`{"PatchedResource":{"apiVersion":"v1","kind":"Pod","meta func Test_buildPolicyReports(t *testing.T) { rc := &kyvCommon.ResultCounts{} - var pvInfos []policyreport.Info + var pvInfos []common.Info var policy kyverno.ClusterPolicy err := json.Unmarshal(rawPolicy, &policy) assert.NilError(t, err) @@ -123,7 +123,7 @@ func Test_buildPolicyReports(t *testing.T) { func Test_buildPolicyResults(t *testing.T) { rc := &kyvCommon.ResultCounts{} - var pvInfos []policyreport.Info + var pvInfos []common.Info var policy kyverno.ClusterPolicy err := json.Unmarshal(rawPolicy, &policy) assert.NilError(t, err) diff --git a/cmd/cli/kubectl-kyverno/test/test_command.go b/cmd/cli/kubectl-kyverno/test/test_command.go index dcfbf558a9..ecb3b2a4b6 100644 --- a/cmd/cli/kubectl-kyverno/test/test_command.go +++ b/cmd/cli/kubectl-kyverno/test/test_command.go @@ -29,7 +29,6 @@ import ( "github.com/kyverno/kyverno/pkg/engine/response" "github.com/kyverno/kyverno/pkg/openapi" policy2 "github.com/kyverno/kyverno/pkg/policy" - "github.com/kyverno/kyverno/pkg/policyreport" util "github.com/kyverno/kyverno/pkg/utils" "github.com/lensesio/tableprinter" "github.com/spf13/cobra" @@ -514,7 +513,7 @@ func getLocalDirTestFiles(fs billy.Filesystem, path, fileName string, rc *result return errors } -func buildPolicyResults(engineResponses []*response.EngineResponse, testResults []TestResults, infos []policyreport.Info, policyResourcePath string, fs billy.Filesystem, isGit bool) (map[string]policyreportv1alpha2.PolicyReportResult, []TestResults) { +func buildPolicyResults(engineResponses []*response.EngineResponse, testResults []TestResults, infos []common.Info, policyResourcePath string, fs billy.Filesystem, isGit bool) (map[string]policyreportv1alpha2.PolicyReportResult, []TestResults) { results := make(map[string]policyreportv1alpha2.PolicyReportResult) now := metav1.Timestamp{Seconds: time.Now().Unix()} @@ -824,7 +823,7 @@ func applyPoliciesFromPath(fs billy.Filesystem, policyBytes []byte, isGit bool, var dClient dclient.Interface values := &Test{} var variablesString string - var pvInfos []policyreport.Info + var pvInfos []common.Info var resultCounts common.ResultCounts store.SetMock(true) diff --git a/cmd/cli/kubectl-kyverno/utils/common/common.go b/cmd/cli/kubectl-kyverno/utils/common/common.go index 1b045c4001..168971ebdf 100644 --- a/cmd/cli/kubectl-kyverno/utils/common/common.go +++ b/cmd/cli/kubectl-kyverno/utils/common/common.go @@ -26,7 +26,6 @@ import ( "github.com/kyverno/kyverno/pkg/engine/response" ut "github.com/kyverno/kyverno/pkg/engine/utils" "github.com/kyverno/kyverno/pkg/engine/variables" - "github.com/kyverno/kyverno/pkg/policyreport" yamlutils "github.com/kyverno/kyverno/pkg/utils/yaml" yamlv2 "gopkg.in/yaml.v2" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -355,7 +354,7 @@ func GetVariable(variablesString, valuesFile string, fs billy.Filesystem, isGit } // ApplyPolicyOnResource - function to apply policy on resource -func ApplyPolicyOnResource(c ApplyPolicyConfig) ([]*response.EngineResponse, policyreport.Info, error) { +func ApplyPolicyOnResource(c ApplyPolicyConfig) ([]*response.EngineResponse, Info, error) { var engineResponses []*response.EngineResponse namespaceLabels := make(map[string]string) operationIsDelete := false @@ -402,7 +401,7 @@ OuterLoop: resourceNamespace := c.Resource.GetNamespace() namespaceLabels = c.NamespaceSelectorMap[c.Resource.GetNamespace()] if resourceNamespace != "default" && len(namespaceLabels) < 1 { - return engineResponses, policyreport.Info{}, sanitizederror.NewWithError(fmt.Sprintf("failed to get namespace labels for resource %s. use --values-file flag to pass the namespace labels", c.Resource.GetName()), nil) + return engineResponses, Info{}, sanitizederror.NewWithError(fmt.Sprintf("failed to get namespace labels for resource %s. use --values-file flag to pass the namespace labels", c.Resource.GetName()), nil) } } @@ -464,7 +463,7 @@ OuterLoop: err = processMutateEngineResponse(c, mutateResponse, resPath) if err != nil { if !sanitizederror.IsErrorSanitized(err) { - return engineResponses, policyreport.Info{}, sanitizederror.NewWithError("failed to print mutated result", err) + return engineResponses, Info{}, sanitizederror.NewWithError("failed to print mutated result", err) } } @@ -477,7 +476,7 @@ OuterLoop: policyContext.NewResource = mutateResponse.PatchedResource - var info policyreport.Info + var info Info var validateResponse *response.EngineResponse if policyHasValidate { validateResponse = engine.Validate(policyContext) @@ -674,7 +673,7 @@ func GetResourceAccordingToResourcePath(fs billy.Filesystem, resourcePaths []str return resources, err } -func ProcessValidateEngineResponse(policy kyvernov1.PolicyInterface, validateResponse *response.EngineResponse, resPath string, rc *ResultCounts, policyReport bool) policyreport.Info { +func ProcessValidateEngineResponse(policy kyvernov1.PolicyInterface, validateResponse *response.EngineResponse, resPath string, rc *ResultCounts, policyReport bool) Info { var violatedRules []kyvernov1.ViolatedRule printCount := 0 @@ -750,11 +749,11 @@ func ProcessValidateEngineResponse(policy kyvernov1.PolicyInterface, validateRes return buildPVInfo(validateResponse, violatedRules) } -func buildPVInfo(er *response.EngineResponse, violatedRules []kyvernov1.ViolatedRule) policyreport.Info { - info := policyreport.Info{ +func buildPVInfo(er *response.EngineResponse, violatedRules []kyvernov1.ViolatedRule) Info { + info := Info{ PolicyName: er.PolicyResponse.Policy.Name, Namespace: er.PatchedResource.GetNamespace(), - Results: []policyreport.EngineResponseResult{ + Results: []EngineResponseResult{ { Resource: er.GetResourceSpec(), Rules: violatedRules, diff --git a/pkg/policyreport/info.go b/cmd/cli/kubectl-kyverno/utils/common/info.go similarity index 95% rename from pkg/policyreport/info.go rename to cmd/cli/kubectl-kyverno/utils/common/info.go index 4cf45f388f..47d64a7aa4 100644 --- a/pkg/policyreport/info.go +++ b/cmd/cli/kubectl-kyverno/utils/common/info.go @@ -1,4 +1,4 @@ -package policyreport +package common import ( kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"