diff --git a/util/annotations/helpers.go b/util/annotations/helpers.go index 0ec9ef9388ac..47dc7fc6b77b 100644 --- a/util/annotations/helpers.go +++ b/util/annotations/helpers.go @@ -71,7 +71,6 @@ func AddAnnotations(o metav1.Object, desired map[string]string) bool { annotations := o.GetAnnotations() if annotations == nil { annotations = make(map[string]string) - o.SetAnnotations(annotations) } hasChanged := false for k, v := range desired { @@ -80,6 +79,7 @@ func AddAnnotations(o metav1.Object, desired map[string]string) bool { hasChanged = true } } + o.SetAnnotations(annotations) return hasChanged } diff --git a/util/annotations/helpers_test.go b/util/annotations/helpers_test.go index 9793fcf87369..de973cfb9c14 100644 --- a/util/annotations/helpers_test.go +++ b/util/annotations/helpers_test.go @@ -22,12 +22,13 @@ import ( . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" ) func TestAddAnnotations(t *testing.T) { g := NewWithT(t) - var testcases = []struct { + testcases := []struct { name string obj metav1.Object input map[string]string @@ -141,6 +142,39 @@ func TestAddAnnotations(t *testing.T) { }, changed: true, }, + { + name: "should add annotations to an empty unstructured", + obj: &unstructured.Unstructured{}, + input: map[string]string{ + "foo": "buzz", + }, + expected: map[string]string{ + "foo": "buzz", + }, + changed: true, + }, + { + name: "should add annotations to a non empty unstructured", + obj: &unstructured.Unstructured{ + Object: map[string]interface{}{ + "metadata": map[string]interface{}{ + "annotations": map[string]interface{}{ + "foo": "bar", + }, + }, + }, + }, + input: map[string]string{ + "thing1": "thing2", + "buzz": "blah", + }, + expected: map[string]string{ + "foo": "bar", + "thing1": "thing2", + "buzz": "blah", + }, + changed: true, + }, } for _, tc := range testcases {