Skip to content

Commit

Permalink
Fix annotations.AddAnnotations (if annotations has been nil before)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbueringer authored and Cecile Robert-Michon committed Mar 26, 2021
1 parent 27b750a commit f9e3252
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions util/annotations/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions util/annotations/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f9e3252

Please sign in to comment.