Skip to content

Commit

Permalink
Fix merge annotation function
Browse files Browse the repository at this point in the history
if the current obj as annotation and the updated doesn't we still want
to add the ones from the current object

Signed-off-by: Sebastian Sch <[email protected]>
  • Loading branch information
SchSeba committed Sep 19, 2024
1 parent 8526311 commit 6aedb8c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/apply/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func mergeAnnotations(current, updated *uns.Unstructured) {
for k, v := range updatedAnnotations {
curAnnotations[k] = v
}
if len(curAnnotations) > 1 {
if len(curAnnotations) > 0 {
updated.SetAnnotations(curAnnotations)
}
}
Expand All @@ -238,7 +238,7 @@ func mergeLabels(current, updated *uns.Unstructured) {
for k, v := range updatedLabels {
curLabels[k] = v
}
if len(curLabels) > 1 {
if len(curLabels) > 0 {
updated.SetLabels(curLabels)
}
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/apply/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,38 @@ metadata:
}))
}

func TestMergeOne(t *testing.T) {
g := NewGomegaWithT(t)

cur := UnstructuredFromYaml(t, `
apiVersion: apps/v1
kind: Deployment
metadata:
name: d1
labels:
label-c: cur
annotations:
annotation-c: cur`)

upd := UnstructuredFromYaml(t, `
apiVersion: apps/v1
kind: Deployment
metadata:
name: d1`)

// this mutates updated
err := MergeObjectForUpdate(cur, upd)
g.Expect(err).NotTo(HaveOccurred())

g.Expect(upd.GetLabels()).To(Equal(map[string]string{
"label-c": "cur",
}))

g.Expect(upd.GetAnnotations()).To(Equal(map[string]string{
"annotation-c": "cur",
}))
}

func TestMergeNilCur(t *testing.T) {
g := NewGomegaWithT(t)

Expand Down

0 comments on commit 6aedb8c

Please sign in to comment.