1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/pkg/utils/report/new.go
Charles-Edouard Brétéché e0ab72bb9a
feat: reports v2 implementation (#4608)
This PR refactors the reports generation code.
It removes RCR and CRCR crds and replaces them with AdmissionReport, ClusterAdmissionReport, BackgroundScanReport and ClusterBackgroundScanReport crds.

The new reports system is based on 4 controllers:

Admission reports controller is responsible for cleaning up admission reports and attaching admission reports to their corresponding resource in case of a creation
Background scan reports controller is responsible for creating background scan reports when a resource and/or policy changes
Aggregation controller takes care of aggregation per resource reports into higher level reports (per namespace)
Resources controller is responsible for watching reports that need background scan reports
I added two new flags to disable admission reports and/or background scan reports, the whole reporting system can be disabled if something goes wrong.

I also added a flag to split reports in chunks to avoid creating too large resources.

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>

Signed-off-by: prateekpandey14 <prateek.pandey@nirmata.com>

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
Signed-off-by: prateekpandey14 <prateek.pandey@nirmata.com>
Co-authored-by: prateekpandey14 <prateek.pandey@nirmata.com>
2022-09-28 17:15:16 +05:30

80 lines
2.7 KiB
Go

package report
import (
kyvernov1alpha2 "github.com/kyverno/kyverno/api/kyverno/v1alpha2"
policyreportv1alpha2 "github.com/kyverno/kyverno/api/policyreport/v1alpha2"
"github.com/kyverno/kyverno/pkg/engine/response"
controllerutils "github.com/kyverno/kyverno/pkg/utils/controller"
admissionv1 "k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
)
func NewAdmissionReport(resource unstructured.Unstructured, request *admissionv1.AdmissionRequest, gvk metav1.GroupVersionKind, responses ...*response.EngineResponse) kyvernov1alpha2.ReportInterface {
name := string(request.UID)
namespace := resource.GetNamespace()
owner := resource.GetName()
uid := resource.GetUID()
var report kyvernov1alpha2.ReportInterface
if namespace == "" {
report = &kyvernov1alpha2.ClusterAdmissionReport{
Spec: kyvernov1alpha2.AdmissionReportSpec{
Owner: metav1.OwnerReference{
APIVersion: metav1.GroupVersion{Group: gvk.Group, Version: gvk.Version}.String(),
Kind: gvk.Kind,
Name: owner,
UID: uid,
},
},
}
} else {
report = &kyvernov1alpha2.AdmissionReport{
Spec: kyvernov1alpha2.AdmissionReportSpec{
Owner: metav1.OwnerReference{
APIVersion: metav1.GroupVersion{Group: gvk.Group, Version: gvk.Version}.String(),
Kind: gvk.Kind,
Name: owner,
UID: uid,
},
},
}
}
report.SetName(name)
report.SetNamespace(namespace)
SetResourceLabels(report, namespace, owner, uid)
SetResourceVersionLabels(report, &resource)
SetResponses(report, responses...)
SetManagedByKyvernoLabel(report)
return report
}
func NewBackgroundScanReport(namespace, name string, gvk schema.GroupVersionKind, owner string, uid types.UID) kyvernov1alpha2.ReportInterface {
var report kyvernov1alpha2.ReportInterface
if namespace == "" {
report = &kyvernov1alpha2.ClusterBackgroundScanReport{}
} else {
report = &kyvernov1alpha2.BackgroundScanReport{}
}
report.SetName(name)
report.SetNamespace(namespace)
controllerutils.SetOwner(report, gvk.GroupVersion().String(), gvk.Kind, owner, uid)
SetResourceLabels(report, namespace, owner, uid)
SetManagedByKyvernoLabel(report)
return report
}
func NewPolicyReport(namespace, name string, results ...policyreportv1alpha2.PolicyReportResult) kyvernov1alpha2.ReportInterface {
var report kyvernov1alpha2.ReportInterface
if namespace == "" {
report = &policyreportv1alpha2.ClusterPolicyReport{}
} else {
report = &policyreportv1alpha2.PolicyReport{}
}
report.SetName(name)
report.SetNamespace(namespace)
SetManagedByKyvernoLabel(report)
SetResults(report, results...)
return report
}