From 6cfa71cdff22f83d6846ce8e8a8a86518bf7f2a1 Mon Sep 17 00:00:00 2001 From: Frank Jogeleit Date: Fri, 19 Jan 2024 16:58:27 +0100 Subject: [PATCH] fix resource details in compliance report Signed-off-by: Frank Jogeleit --- pkg/reporting/policy_report.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/reporting/policy_report.go b/pkg/reporting/policy_report.go index d1d2699..f613810 100644 --- a/pkg/reporting/policy_report.go +++ b/pkg/reporting/policy_report.go @@ -5,6 +5,7 @@ import ( "strings" "golang.org/x/exp/slices" + corev1 "k8s.io/api/core/v1" v1 "github.com/kyverno/policy-reporter-kyverno-plugin/pkg/crd/api/kyverno/v1" "github.com/kyverno/policy-reporter-kyverno-plugin/pkg/crd/api/policyreport/v1alpha2" @@ -115,6 +116,8 @@ func (g *policyReportGenerator) PerPolicyData(ctx context.Context, filter Filter if len(result.Resources) > 0 { val.Groups[polr.GetNamespace()].Rules[rule].Resources = append(val.Groups[polr.GetNamespace()].Rules[rule].Resources, mapResource(result)) + } else if polr.GetScope() != nil { + val.Groups[polr.GetNamespace()].Rules[rule].Resources = append(val.Groups[polr.GetNamespace()].Rules[rule].Resources, mapScope(result.Result, polr.GetScope())) } increaseSummary(result.Result, val.Groups[polr.GetNamespace()].Rules[rule].Summary) @@ -238,11 +241,12 @@ func (g *policyReportGenerator) PerNamespaceData(ctx context.Context, filter Fil if len(result.Resources) > 0 { ruleObj.Resources = append(ruleObj.Resources, mapResource(result)) + } else if polr.GetScope() != nil { + ruleObj.Resources = append(ruleObj.Resources, mapScope(result.Result, polr.GetScope())) } increaseSummary(result.Result, ruleObj.Summary) increaseSummary(result.Result, val.Groups[result.Policy].Summary) - } } @@ -319,6 +323,15 @@ func mapResource(result v1alpha2.PolicyReportResult) *Resource { } } +func mapScope(result v1alpha2.PolicyResult, resource *corev1.ObjectReference) *Resource { + return &Resource{ + Kind: resource.Kind, + APIVersion: resource.APIVersion, + Name: resource.Name, + Status: string(result), + } +} + func mapPolicy(pol v1.Policy) *Policy { v := &Policy{ Title: pol.Annotations[titleAnnotation],