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 18, 2024
1 parent 8526311 commit 99f59b4
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:
c: upd
annotations:
c: upd`)

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{
"c": "upd",
}))

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

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

Expand Down

0 comments on commit 99f59b4

Please sign in to comment.