From 7e4b58460b0f4a10997019a12db88d8051bd9b1c Mon Sep 17 00:00:00 2001 From: Andres Date: Thu, 7 Jun 2018 15:12:12 +0200 Subject: [PATCH] Avoid queuing an item that has no relevant changes (#31) * Avoid queuing an item that has no relevant changes * Minor review * Fix syntax --- cmd/controller/controller.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/cmd/controller/controller.go b/cmd/controller/controller.go index 29085e8..3404c5e 100644 --- a/cmd/controller/controller.go +++ b/cmd/controller/controller.go @@ -9,6 +9,7 @@ import ( "time" "google.golang.org/grpc" + apiequality "k8s.io/apimachinery/pkg/api/equality" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" utilruntime "k8s.io/apimachinery/pkg/util/runtime" @@ -66,7 +67,13 @@ func NewController(clientset helmClientset.Interface, kubeClient kubernetes.Inte UpdateFunc: func(oldObj, newObj interface{}) { key, err := cache.MetaNamespaceKeyFunc(newObj) if err == nil { - queue.Add(key) + newReleaseObj := newObj.(*helmCrdV1.HelmRelease) + oldReleaseObj := oldObj.(*helmCrdV1.HelmRelease) + if releaseObjChanged(oldReleaseObj, newReleaseObj) { + queue.Add(key) + } else { + log.Printf("Ignoring update event on unchanged object %v", newReleaseObj) + } } }, DeleteFunc: func(obj interface{}) { @@ -194,6 +201,14 @@ func removeIndex(i int, s []string) []string { return s[:lastIdx] } +func releaseObjChanged(old, new *helmCrdV1.HelmRelease) bool { + // If the object deletion timestamp is set, then process + if old.DeletionTimestamp != new.DeletionTimestamp { + return true + } + return !apiequality.Semantic.DeepEqual(old.Spec, new.Spec) +} + // remove item from slice without keeping order func remove(item string, s []string) ([]string, error) { index := findIndex(item, s)