mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-28 02:18:15 +00:00
fix: do not update reports when they are identical (#5056)
* fix: do not update reports when they are identical Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * fix linter Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
parent
c4b3301ab0
commit
749c1bab3b
1 changed files with 33 additions and 2 deletions
|
@ -24,6 +24,7 @@ import (
|
|||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
corev1informers "k8s.io/client-go/informers/core/v1"
|
||||
corev1listers "k8s.io/client-go/listers/core/v1"
|
||||
metadatainformers "k8s.io/client-go/metadata/metadatainformer"
|
||||
|
@ -187,6 +188,36 @@ func (c *controller) fetchPolicies(logger logr.Logger, namespace string) ([]kyve
|
|||
return policies, nil
|
||||
}
|
||||
|
||||
// reportsAreIdentical we expect reports are sorted before comparing them
|
||||
func reportsAreIdentical(before, after kyvernov1alpha2.ReportInterface) bool {
|
||||
bLabels := sets.NewString()
|
||||
aLabels := sets.NewString()
|
||||
for key := range before.GetLabels() {
|
||||
bLabels.Insert(key)
|
||||
}
|
||||
for key := range after.GetLabels() {
|
||||
aLabels.Insert(key)
|
||||
}
|
||||
if !aLabels.Equal(bLabels) {
|
||||
return false
|
||||
}
|
||||
b := before.GetResults()
|
||||
a := after.GetResults()
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i := range a {
|
||||
a := a[i]
|
||||
b := b[i]
|
||||
a.Timestamp = metav1.Timestamp{}
|
||||
b.Timestamp = metav1.Timestamp{}
|
||||
if !reflect.DeepEqual(&a, &b) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *controller) updateReport(ctx context.Context, meta metav1.Object, gvk schema.GroupVersionKind, resource resource.Resource) error {
|
||||
namespace := meta.GetNamespace()
|
||||
labels := meta.GetLabels()
|
||||
|
@ -240,7 +271,7 @@ func (c *controller) updateReport(ctx context.Context, meta metav1.Object, gvk s
|
|||
}
|
||||
}
|
||||
reportutils.SetResponses(report, responses...)
|
||||
if reflect.DeepEqual(before, report) {
|
||||
if reportsAreIdentical(before, report) {
|
||||
return nil
|
||||
}
|
||||
_, err = reportutils.UpdateReport(ctx, report, c.kyvernoClient)
|
||||
|
@ -319,7 +350,7 @@ func (c *controller) updateReport(ctx context.Context, meta metav1.Object, gvk s
|
|||
}
|
||||
}
|
||||
reportutils.SetResults(report, ruleResults...)
|
||||
if reflect.DeepEqual(before, report) {
|
||||
if reportsAreIdentical(before, report) {
|
||||
return nil
|
||||
}
|
||||
_, err = reportutils.UpdateReport(ctx, report, c.kyvernoClient)
|
||||
|
|
Loading…
Add table
Reference in a new issue