1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-09 09:26:54 +00:00
kyverno/pkg/controllers/report/aggregate/utils.go
Charles-Edouard Brétéché 7775541b46
fix: reports aggregation (#9697)
* chore: rename admission to ephemeral in reports aggregation controller

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix: reports aggregation

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* second queue

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* cleanup

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* nit

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* flag

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

---------

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2024-02-08 10:36:01 +00:00

64 lines
2.4 KiB
Go

package aggregate
import (
"context"
"errors"
"time"
kyvernov1alpha2 "github.com/kyverno/kyverno/api/kyverno/v1alpha2"
policyreportv1alpha2 "github.com/kyverno/kyverno/api/policyreport/v1alpha2"
"github.com/kyverno/kyverno/pkg/client/clientset/versioned"
controllerutils "github.com/kyverno/kyverno/pkg/utils/controller"
reportutils "github.com/kyverno/kyverno/pkg/utils/report"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
)
func mergeReports(policyMap map[string]policyMapEntry, vapMap sets.Set[string], accumulator map[string]policyreportv1alpha2.PolicyReportResult, uid types.UID, reports ...kyvernov1alpha2.ReportInterface) {
for _, report := range reports {
if report == nil {
continue
}
for _, result := range report.GetResults() {
if result.Source == "ValidatingAdmissionPolicy" {
if vapMap != nil && vapMap.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
}
}
} else {
currentPolicy := policyMap[result.Policy]
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
}
}
}
}
}
}
func deleteReport(ctx context.Context, report kyvernov1alpha2.ReportInterface, client versioned.Interface) error {
if !controllerutils.IsManagedByKyverno(report) {
return errors.New("can't delete report because it is not managed by kyverno")
}
return reportutils.DeleteReport(ctx, report, client)
}
func updateReport(ctx context.Context, report kyvernov1alpha2.ReportInterface, client versioned.Interface) (kyvernov1alpha2.ReportInterface, error) {
if !controllerutils.IsManagedByKyverno(report) {
return nil, errors.New("can't update report because it is not managed by kyverno")
}
return reportutils.UpdateReport(ctx, report, client)
}
func isTooOld(reportMeta *metav1.PartialObjectMetadata) bool {
return reportMeta.GetCreationTimestamp().Add(deletionGrace).Before(time.Now())
}