diff --git a/util/annotations/helpers.go b/util/annotations/helpers.go index c49d3b7cf9a3..3cddc66995ae 100644 --- a/util/annotations/helpers.go +++ b/util/annotations/helpers.go @@ -52,9 +52,13 @@ func HasWithPrefix(prefix string, annotations map[string]string) bool { // AddAnnotations sets the desired annotations on the object and returns true if the annotations have changed. func AddAnnotations(o metav1.Object, desired map[string]string) bool { + if len(desired) == 0 { + return false + } annotations := o.GetAnnotations() if annotations == nil { annotations = make(map[string]string) + o.SetAnnotations(annotations) } hasChanged := false for k, v := range desired { diff --git a/util/annotations/helpers_test.go b/util/annotations/helpers_test.go index 533d1b282ac6..67376ef0a013 100644 --- a/util/annotations/helpers_test.go +++ b/util/annotations/helpers_test.go @@ -69,6 +69,19 @@ func TestAddAnnotations(t *testing.T) { }, changed: false, }, + { + name: "should do nothing if no annotations are provided and have been nil before", + obj: &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: nil, + }, + Spec: corev1.NodeSpec{}, + Status: corev1.NodeStatus{}, + }, + input: map[string]string{}, + expected: nil, + changed: false, + }, { name: "should return true if annotations are added", obj: &corev1.Node{ @@ -110,6 +123,23 @@ func TestAddAnnotations(t *testing.T) { }, changed: true, }, + { + name: "should return true if annotations are changed and have been nil before", + obj: &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: nil, + }, + Spec: corev1.NodeSpec{}, + Status: corev1.NodeStatus{}, + }, + input: map[string]string{ + "foo": "buzz", + }, + expected: map[string]string{ + "foo": "buzz", + }, + changed: true, + }, } for _, tc := range testcases {