Skip to content

Commit da8cb15

Browse files
committed
Fix AddAnnotations for unstructured.Unstructured
The previous implementation worked well for most Object implementations (definitely all embedding metav1.ObjectMeta) because it relies on: * The fact that GetAnnotations doesn't return a copy of the annotations but the underlying map storing them. * When calling set annotations, the input map is used for storage in the Object implementation, hence when adding entries to that map from the outside, it changes the content of the Object's annotations. This is not necessarily part of the contract specified by metav1.Object so we can't really guarantee all implementations will satisfy these 2 conditions. It turns out, unstructured.Unstructured doesn't.
1 parent 3a37397 commit da8cb15

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

util/annotations/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ func AddAnnotations(o metav1.Object, desired map[string]string) bool {
7171
annotations := o.GetAnnotations()
7272
if annotations == nil {
7373
annotations = make(map[string]string)
74-
o.SetAnnotations(annotations)
7574
}
7675
hasChanged := false
7776
for k, v := range desired {
@@ -80,6 +79,7 @@ func AddAnnotations(o metav1.Object, desired map[string]string) bool {
8079
hasChanged = true
8180
}
8281
}
82+
o.SetAnnotations(annotations)
8383
return hasChanged
8484
}
8585

util/annotations/helpers_test.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ import (
2222
. "github.com/onsi/gomega"
2323
corev1 "k8s.io/api/core/v1"
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2526
)
2627

2728
func TestAddAnnotations(t *testing.T) {
2829
g := NewWithT(t)
2930

30-
var testcases = []struct {
31+
testcases := []struct {
3132
name string
3233
obj metav1.Object
3334
input map[string]string
@@ -141,6 +142,39 @@ func TestAddAnnotations(t *testing.T) {
141142
},
142143
changed: true,
143144
},
145+
{
146+
name: "should add annotations to an empty unstructured",
147+
obj: &unstructured.Unstructured{},
148+
input: map[string]string{
149+
"foo": "buzz",
150+
},
151+
expected: map[string]string{
152+
"foo": "buzz",
153+
},
154+
changed: true,
155+
},
156+
{
157+
name: "should add annotations to a non empty unstructured",
158+
obj: &unstructured.Unstructured{
159+
Object: map[string]interface{}{
160+
"metadata": map[string]interface{}{
161+
"annotations": map[string]interface{}{
162+
"foo": "bar",
163+
},
164+
},
165+
},
166+
},
167+
input: map[string]string{
168+
"thing1": "thing2",
169+
"buzz": "blah",
170+
},
171+
expected: map[string]string{
172+
"foo": "bar",
173+
"thing1": "thing2",
174+
"buzz": "blah",
175+
},
176+
changed: true,
177+
},
144178
}
145179

146180
for _, tc := range testcases {

0 commit comments

Comments
 (0)