Skip to content

Commit

Permalink
fix: Improve change detection based on generation
Browse files Browse the repository at this point in the history
  • Loading branch information
NoUseFreak committed Dec 14, 2022
1 parent 922880d commit b9427e3
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions internal/pkg/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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():
Expand All @@ -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)) {
Expand Down

0 comments on commit b9427e3

Please sign in to comment.