From b9427e36a3bc1c90163865585167d073d9bcf2dc Mon Sep 17 00:00:00 2001 From: Dries De Peuter Date: Wed, 14 Dec 2022 15:07:16 +0100 Subject: [PATCH] fix: Improve change detection based on generation --- internal/pkg/kubernetes/client.go | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/internal/pkg/kubernetes/client.go b/internal/pkg/kubernetes/client.go index 90fbcf7..d7482d7 100644 --- a/internal/pkg/kubernetes/client.go +++ b/internal/pkg/kubernetes/client.go @@ -61,6 +61,9 @@ func (c *Client) WatchDeploymentEvents(ctx context.Context, namespace string, no if !ok { continue } + if deployment.Generation == deployment.Status.ObservedGeneration { + continue + } if ev := processObject(e, deployment.ObjectMeta, deployment.Spec.Template); ev != nil { notifyFunc(ev) } @@ -87,11 +90,14 @@ func (c *Client) WatchStatefulsetEvents(ctx context.Context, namespace string, n if e.Object == nil { return } - deployment, ok := e.Object.(*v1apps.StatefulSet) + statefulset, ok := e.Object.(*v1apps.StatefulSet) if !ok { continue } - if ev := processObject(e, deployment.ObjectMeta, deployment.Spec.Template); ev != nil { + if statefulset.Generation == statefulset.Status.ObservedGeneration { + continue + } + if ev := processObject(e, statefulset.ObjectMeta, statefulset.Spec.Template); ev != nil { notifyFunc(ev) } case <-ctx.Done(): @@ -104,20 +110,7 @@ func (c *Client) WatchStatefulsetEvents(ctx context.Context, namespace string, n return nil } -var cache = map[string]time.Time{} - func processObject(e watch.Event, objectMeta metav1.ObjectMeta, podTemplate v1.PodTemplateSpec) []Event { - val, ok := cache[string(objectMeta.UID)] - if ok && val.After(time.Now().Add(-10*time.Second)) { - // Skip processing if the object was processed in the last 10 seconds. - logrus.WithFields(logrus.Fields{ - "namespace": objectMeta.Namespace, - "name": objectMeta.Name, - }).Trace("Skipping, recently processed") - return nil - } - cache[string(objectMeta.UID)] = time.Now() - switch e.Type { case watch.Added: if objectMeta.CreationTimestamp.Time.Before(time.Now().Add(-1 * time.Minute)) {