Skip to content

Commit

Permalink
Avoid assignments to nil maps when coalescing metadata (#210)
Browse files Browse the repository at this point in the history
Initialize the maps when they're nil.

---------

Co-authored-by: Mariano Uvalle <[email protected]>
  • Loading branch information
AYM1607 and Mariano Uvalle authored Oct 2, 2024
1 parent 2731edd commit 1529fc2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
12 changes: 10 additions & 2 deletions internal/controllers/replication/symphony.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,22 @@ func sortSynthesizerRefs(refs []apiv1.SynthesizerRef) {

func coalesceMetadata(variation *apiv1.Variation, existing *apiv1.Composition) bool {
var metaChanged bool

if existing.Labels == nil {
existing.Labels = map[string]string{}
}
for key, val := range variation.Labels {
if existing.Labels == nil || existing.Labels[key] != val {
if existing.Labels[key] != val {
metaChanged = true
}
existing.Labels[key] = val
}

if existing.Annotations == nil {
existing.Annotations = map[string]string{}
}
for key, val := range variation.Annotations {
if existing.Annotations == nil || existing.Annotations[key] != val {
if existing.Annotations[key] != val {
metaChanged = true
}
existing.Annotations[key] = val
Expand Down
10 changes: 2 additions & 8 deletions internal/controllers/replication/symphony_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,7 @@ func TestCoalesceMetadata(t *testing.T) {
Annotations: map[string]string{},
},
existing: &apiv1.Composition{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{},
Annotations: map[string]string{},
},
ObjectMeta: metav1.ObjectMeta{},
},
expectedLabels: map[string]string{
"label1": "value1",
Expand Down Expand Up @@ -356,10 +353,7 @@ func TestCoalesceMetadata(t *testing.T) {
},
},
existing: &apiv1.Composition{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{},
Annotations: map[string]string{},
},
ObjectMeta: metav1.ObjectMeta{},
},
expectedLabels: map[string]string{},
expectedAnnos: map[string]string{
Expand Down

0 comments on commit 1529fc2

Please sign in to comment.