diff --git a/pkg/apis/rook.io/v1/annotations.go b/pkg/apis/rook.io/v1/annotations.go index 794378845..774152139 100644 --- a/pkg/apis/rook.io/v1/annotations.go +++ b/pkg/apis/rook.io/v1/annotations.go @@ -24,7 +24,7 @@ func (a AnnotationsSpec) All() Annotations { return a[KeyAll] } -// ApplyToObjectMeta adds or overwrites if exists annotations to object meta. +// ApplyToObjectMeta adds annotations to object meta unless the keys are already defined. func (a Annotations) ApplyToObjectMeta(t *metav1.ObjectMeta) { if t.Annotations == nil { t.Annotations = map[string]string{} diff --git a/pkg/apis/rook.io/v1/labels.go b/pkg/apis/rook.io/v1/labels.go index 17d6d974b..b68223b4c 100644 --- a/pkg/apis/rook.io/v1/labels.go +++ b/pkg/apis/rook.io/v1/labels.go @@ -24,7 +24,7 @@ func (a LabelsSpec) All() Labels { return a[KeyAll] } -// ApplyToObjectMeta adds or overwrites if exists Labels to object meta. +// ApplyToObjectMeta adds labels to object meta unless the keys are already defined. func (a Labels) ApplyToObjectMeta(t *metav1.ObjectMeta) { if t.Labels == nil { t.Labels = map[string]string{} diff --git a/pkg/apis/rook.io/v1/labels_test.go b/pkg/apis/rook.io/v1/labels_test.go index b5e2b294a..cd84a0c6c 100644 --- a/pkg/apis/rook.io/v1/labels_test.go +++ b/pkg/apis/rook.io/v1/labels_test.go @@ -54,17 +54,57 @@ mon: } func TestLabelsApply(t *testing.T) { - objMeta := &metav1.ObjectMeta{} - testLabels := Labels{ - "foo": "bar", - "hello": "world", + tcs := []struct { + name string + target *metav1.ObjectMeta + input Labels + expected Labels + }{ + { + name: "it should be able to update meta with no label", + target: &metav1.ObjectMeta{}, + input: Labels{ + "foo": "bar", + }, + expected: Labels{ + "foo": "bar", + }, + }, + { + name: "it should keep the original labels when new labels are set", + target: &metav1.ObjectMeta{ + Labels: Labels{ + "foo": "bar", + }, + }, + input: Labels{ + "hello": "world", + }, + expected: Labels{ + "foo": "bar", + "hello": "world", + }, + }, + { + name: "it should NOT overwrite the existing keys", + target: &metav1.ObjectMeta{ + Labels: Labels{ + "foo": "bar", + }, + }, + input: Labels{ + "foo": "baz", + }, + expected: Labels{ + "foo": "bar", + }, + }, } - testLabels.ApplyToObjectMeta(objMeta) - assert.Equal(t, testLabels.getMapStringString(), objMeta.Labels) - testLabels["isthisatest"] = "test" - testLabels.ApplyToObjectMeta(objMeta) - assert.Equal(t, testLabels.getMapStringString(), objMeta.Labels) + for _, tc := range tcs { + tc.input.ApplyToObjectMeta(tc.target) + assert.Equal(t, tc.expected.getMapStringString(), tc.target.Labels) + } } func TestLabelsMerge(t *testing.T) {