2024-01-31 00:08:47 +01:00
|
|
|
package aggregate
|
2023-09-22 06:01:21 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2024-02-08 11:36:01 +01:00
|
|
|
"time"
|
2023-09-22 06:01:21 +02:00
|
|
|
|
|
|
|
policyreportv1alpha2 "github.com/kyverno/kyverno/api/policyreport/v1alpha2"
|
2024-06-19 10:08:15 +02:00
|
|
|
reportsv1 "github.com/kyverno/kyverno/api/reports/v1"
|
2024-01-30 15:53:37 +01:00
|
|
|
"github.com/kyverno/kyverno/pkg/client/clientset/versioned"
|
2023-09-22 06:01:21 +02:00
|
|
|
controllerutils "github.com/kyverno/kyverno/pkg/utils/controller"
|
2024-01-30 15:53:37 +01:00
|
|
|
reportutils "github.com/kyverno/kyverno/pkg/utils/report"
|
2024-02-08 11:36:01 +01:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2023-09-22 06:01:21 +02:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
|
|
|
)
|
|
|
|
|
2025-02-05 14:21:28 +01:00
|
|
|
type maps struct {
|
|
|
|
pol map[string]policyMapEntry
|
|
|
|
vap sets.Set[string]
|
|
|
|
vpol sets.Set[string]
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeReports(maps maps, accumulator map[string]policyreportv1alpha2.PolicyReportResult, uid types.UID, reports ...reportsv1.ReportInterface) {
|
2023-09-22 06:01:21 +02:00
|
|
|
for _, report := range reports {
|
2024-02-07 08:10:51 +01:00
|
|
|
if report == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, result := range report.GetResults() {
|
2025-02-05 14:21:28 +01:00
|
|
|
switch result.Source {
|
|
|
|
case reportutils.SourceValidatingPolicy:
|
|
|
|
if maps.vpol != nil && maps.vpol.Has(result.Policy) {
|
|
|
|
key := result.Source + "/" + result.Policy + "/" + string(uid)
|
|
|
|
if rule, exists := accumulator[key]; !exists {
|
|
|
|
accumulator[key] = result
|
|
|
|
} else if rule.Timestamp.Seconds < result.Timestamp.Seconds {
|
|
|
|
accumulator[key] = result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reportutils.SourceValidatingAdmissionPolicy:
|
|
|
|
if maps.vap != nil && maps.vap.Has(result.Policy) {
|
2024-02-07 08:10:51 +01:00
|
|
|
key := result.Source + "/" + result.Policy + "/" + string(uid)
|
|
|
|
if rule, exists := accumulator[key]; !exists {
|
|
|
|
accumulator[key] = result
|
|
|
|
} else if rule.Timestamp.Seconds < result.Timestamp.Seconds {
|
|
|
|
accumulator[key] = result
|
2023-09-22 06:01:21 +02:00
|
|
|
}
|
2024-02-07 08:10:51 +01:00
|
|
|
}
|
2025-02-05 14:21:28 +01:00
|
|
|
default:
|
|
|
|
currentPolicy := maps.pol[result.Policy]
|
2024-02-07 08:10:51 +01:00
|
|
|
if currentPolicy.rules != nil && currentPolicy.rules.Has(result.Rule) {
|
|
|
|
key := result.Source + "/" + result.Policy + "/" + result.Rule + "/" + string(uid)
|
|
|
|
if rule, exists := accumulator[key]; !exists {
|
|
|
|
accumulator[key] = result
|
|
|
|
} else if rule.Timestamp.Seconds < result.Timestamp.Seconds {
|
|
|
|
accumulator[key] = result
|
2023-09-22 06:01:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-19 10:08:15 +02:00
|
|
|
func deleteReport(ctx context.Context, report reportsv1.ReportInterface, client versioned.Interface) error {
|
2023-09-22 06:01:21 +02:00
|
|
|
if !controllerutils.IsManagedByKyverno(report) {
|
|
|
|
return errors.New("can't delete report because it is not managed by kyverno")
|
|
|
|
}
|
2024-01-30 15:53:37 +01:00
|
|
|
return reportutils.DeleteReport(ctx, report, client)
|
2023-09-22 06:01:21 +02:00
|
|
|
}
|
|
|
|
|
2024-06-19 10:08:15 +02:00
|
|
|
func updateReport(ctx context.Context, report reportsv1.ReportInterface, client versioned.Interface) (reportsv1.ReportInterface, error) {
|
2023-09-22 06:01:21 +02:00
|
|
|
if !controllerutils.IsManagedByKyverno(report) {
|
|
|
|
return nil, errors.New("can't update report because it is not managed by kyverno")
|
|
|
|
}
|
2024-01-30 15:53:37 +01:00
|
|
|
return reportutils.UpdateReport(ctx, report, client)
|
2023-09-22 06:01:21 +02:00
|
|
|
}
|
2024-02-08 11:36:01 +01:00
|
|
|
|
|
|
|
func isTooOld(reportMeta *metav1.PartialObjectMetadata) bool {
|
|
|
|
return reportMeta.GetCreationTimestamp().Add(deletionGrace).Before(time.Now())
|
|
|
|
}
|