diff --git a/pkg/dependenciesdistributor/dependencies_distributor.go b/pkg/dependenciesdistributor/dependencies_distributor.go index 16f44a150..093521e57 100644 --- a/pkg/dependenciesdistributor/dependencies_distributor.go +++ b/pkg/dependenciesdistributor/dependencies_distributor.go @@ -22,6 +22,7 @@ import ( "fmt" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/equality" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -151,7 +152,9 @@ func (d *DependenciesDistributor) OnUpdate(oldObj, newObj interface{}) { klog.V(4).Infof("Ignore update event of object (%s, kind=%s, %s) as specification no change", unstructuredOldObj.GetAPIVersion(), unstructuredOldObj.GetKind(), names.NamespacedKey(unstructuredOldObj.GetNamespace(), unstructuredOldObj.GetName())) return } - d.OnAdd(oldObj) + if !equality.Semantic.DeepEqual(unstructuredOldObj.GetLabels(), unstructuredNewObj.GetLabels()) { + d.OnAdd(oldObj) + } d.OnAdd(newObj) } @@ -214,7 +217,6 @@ func matchesWithBindingDependencies(resourceTemplateKey *LabelsKey, independentB independentBinding.Namespace, independentBinding.Name, dependencies, err) return false } - if len(dependenciesSlice) == 0 { return false } diff --git a/pkg/dependenciesdistributor/dependencies_distributor_test.go b/pkg/dependenciesdistributor/dependencies_distributor_test.go index 31443d69b..63a55592d 100644 --- a/pkg/dependenciesdistributor/dependencies_distributor_test.go +++ b/pkg/dependenciesdistributor/dependencies_distributor_test.go @@ -114,7 +114,7 @@ func Test_OnUpdate(t *testing.T) { }, }, }, - wantQueueSize: 2, + wantQueueSize: 1, }, { name: "do not update the object, no specification changed", @@ -140,6 +140,130 @@ func Test_OnUpdate(t *testing.T) { }, wantQueueSize: 0, }, + { + name: "no specification changed, labels changed", + args: args{ + oldObj: &corev1.Node{ + TypeMeta: metav1.TypeMeta{ + Kind: "Node", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "bar", + Labels: map[string]string{ + "app": "test", + }, + }, + }, + newObj: &corev1.Node{ + TypeMeta: metav1.TypeMeta{ + Kind: "Node", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "bar", + Labels: map[string]string{ + "app": "new-test", + }, + }, + }, + }, + wantQueueSize: 2, + }, + { + name: "specification changed, labels not changed", + args: args{ + oldObj: &corev1.Node{ + TypeMeta: metav1.TypeMeta{ + Kind: "Node", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "bar", + Labels: map[string]string{ + "app": "test", + }, + }, + }, + newObj: &corev1.Node{ + TypeMeta: metav1.TypeMeta{ + Kind: "Node", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Labels: map[string]string{ + "app": "test", + }, + }, + }, + }, + wantQueueSize: 1, + }, + { + name: "specification changed, more than two elements in labels and not changed", + args: args{ + oldObj: &corev1.Node{ + TypeMeta: metav1.TypeMeta{ + Kind: "Node", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "bar", + Labels: map[string]string{ + "app": "test", + "online": "svc", + }, + }, + }, + newObj: &corev1.Node{ + TypeMeta: metav1.TypeMeta{ + Kind: "Node", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Labels: map[string]string{ + "online": "svc", + "app": "test", + }, + }, + }, + }, + wantQueueSize: 1, + }, + { + name: "specification changed, more than two elements in labels and changed", + args: args{ + oldObj: &corev1.Node{ + TypeMeta: metav1.TypeMeta{ + Kind: "Node", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "bar", + Labels: map[string]string{ + "app": "test", + "online": "svc", + }, + }, + }, + newObj: &corev1.Node{ + TypeMeta: metav1.TypeMeta{ + Kind: "Node", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Labels: map[string]string{ + "online": "svc1", + "app": "test", + }, + }, + }, + }, + wantQueueSize: 2, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {