1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

fix: check resource version on update notification (#5179)

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:
Charles-Edouard Brétéché 2022-11-01 20:49:25 +00:00 committed by GitHub
parent 2dcd48ef6f
commit 05d90afe1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 17 deletions

View file

@ -85,8 +85,8 @@ func NewController(
cbgscanEnqueue: controllerutils.AddDefaultEventHandlers(logger, cbgscanr.Informer(), queue),
metadataCache: metadataCache,
}
controllerutils.AddEventHandlers(polInformer.Informer(), c.addPolicy, c.updatePolicy, c.deletePolicy)
controllerutils.AddEventHandlers(cpolInformer.Informer(), c.addPolicy, c.updatePolicy, c.deletePolicy)
controllerutils.AddEventHandlersT(polInformer.Informer(), c.addPolicy, c.updatePolicy, c.deletePolicy)
controllerutils.AddEventHandlersT(cpolInformer.Informer(), c.addPolicy, c.updatePolicy, c.deletePolicy)
c.metadataCache.AddEventHandler(func(uid types.UID, _ schema.GroupVersionKind, resource resource.Resource) {
selector, err := reportutils.SelectorResourceUidEquals(uid)
if err != nil {
@ -108,8 +108,8 @@ func (c *controller) Run(ctx context.Context, workers int) {
controllerutils.Run(ctx, logger, ControllerName, time.Second, c.queue, workers, maxRetries, c.reconcile)
}
func (c *controller) addPolicy(obj interface{}) {
selector, err := reportutils.SelectorPolicyDoesNotExist(obj.(kyvernov1.PolicyInterface))
func (c *controller) addPolicy(obj kyvernov1.PolicyInterface) {
selector, err := reportutils.SelectorPolicyDoesNotExist(obj)
if err != nil {
logger.Error(err, "failed to create label selector")
}
@ -118,18 +118,20 @@ func (c *controller) addPolicy(obj interface{}) {
}
}
func (c *controller) updatePolicy(_, obj interface{}) {
selector, err := reportutils.SelectorPolicyNotEquals(obj.(kyvernov1.PolicyInterface))
if err != nil {
logger.Error(err, "failed to create label selector")
}
if err := c.enqueue(selector); err != nil {
logger.Error(err, "failed to enqueue")
func (c *controller) updatePolicy(old, obj kyvernov1.PolicyInterface) {
if old.GetResourceVersion() != obj.GetResourceVersion() {
selector, err := reportutils.SelectorPolicyNotEquals(obj)
if err != nil {
logger.Error(err, "failed to create label selector")
}
if err := c.enqueue(selector); err != nil {
logger.Error(err, "failed to enqueue")
}
}
}
func (c *controller) deletePolicy(obj interface{}) {
selector, err := reportutils.SelectorPolicyExists(obj.(kyvernov1.PolicyInterface))
func (c *controller) deletePolicy(obj kyvernov1.PolicyInterface) {
selector, err := reportutils.SelectorPolicyExists(obj)
if err != nil {
logger.Error(err, "failed to create label selector")
}

View file

@ -5,6 +5,7 @@ import (
"time"
"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
@ -118,18 +119,28 @@ func ExplicitKey[K any](parseKey func(K) cache.ExplicitKey) keyFunc {
func AddFunc(logger logr.Logger, enqueue EnqueueFunc) addFunc {
return func(obj interface{}) {
_ = enqueue(obj)
if err := enqueue(obj); err != nil {
logger.Error(err, "failed to enqueue object", "obj", obj)
}
}
}
func UpdateFunc(logger logr.Logger, enqueue EnqueueFunc) updateFunc {
return func(_, obj interface{}) {
_ = enqueue(obj)
return func(old, obj interface{}) {
oldMeta := old.(metav1.Object)
objMeta := obj.(metav1.Object)
if oldMeta.GetResourceVersion() != objMeta.GetResourceVersion() {
if err := enqueue(obj); err != nil {
logger.Error(err, "failed to enqueue object", "obj", obj)
}
}
}
}
func DeleteFunc(logger logr.Logger, enqueue EnqueueFunc) deleteFunc {
return func(obj interface{}) {
_ = enqueue(obj)
if err := enqueue(obj); err != nil {
logger.Error(err, "failed to enqueue object", "obj", obj)
}
}
}