merge labels and annotations in RetryOnConflict

Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
This commit is contained in:
Matthias Bertschy
2025-04-14 15:36:34 +02:00
parent 80d1165e2c
commit a4897304e8
2 changed files with 58 additions and 5 deletions

View File

@@ -189,8 +189,8 @@ func (a *APIServerStore) StoreWorkloadConfigurationScanResult(ctx context.Contex
return getErr
}
// update the workload configuration scan manifest
result.Annotations = manifest.Annotations
result.Labels = manifest.Labels
mergeMaps(result.Annotations, manifest.Annotations)
mergeMaps(result.Labels, manifest.Labels)
result.Spec = mergeWorkloadConfigurationScanSpec(result.Spec, manifest.Spec)
// try to send the updated workload configuration scan manifest
_, updateErr := a.StorageClient.WorkloadConfigurationScans(namespace).Update(context.Background(), result, metav1.UpdateOptions{})
@@ -288,8 +288,8 @@ func (a *APIServerStore) StoreWorkloadConfigurationScanResultSummary(ctx context
return getErr
}
// update the manifest
result.Annotations = manifest.Annotations
result.Labels = manifest.Labels
mergeMaps(result.Annotations, manifest.Annotations)
mergeMaps(result.Labels, manifest.Labels)
result.Spec = mergeWorkloadConfigurationScanSummarySpec(result.Spec, manifest.Spec)
// try to send the updated manifest
_, updateErr := a.StorageClient.WorkloadConfigurationScanSummaries(namespace).Update(context.Background(), result, metav1.UpdateOptions{})
@@ -532,3 +532,10 @@ func parseWorkloadScanRelatedObjectList(relatedObjects []workloadinterface.IMeta
}
return r
}
// mergeMaps merges new into existing, overwriting existing keys with new values
func mergeMaps(existing, new map[string]string) {
for k, v := range new {
existing[k] = v
}
}

View File

@@ -484,5 +484,51 @@ func Test_RoleBindingResourceTripletToSlug(t *testing.T) {
assert.ElementsMatch(t, tt.expectedSlugs, slugs)
})
}
}
func TestMergeMaps(t *testing.T) {
tests := []struct {
name string
existing map[string]string
new map[string]string
expected map[string]string
}{
{
name: "merge with no conflicts",
existing: map[string]string{"key1": "value1"},
new: map[string]string{"key2": "value2"},
expected: map[string]string{"key1": "value1", "key2": "value2"},
},
{
name: "merge with conflicts",
existing: map[string]string{"key1": "value1"},
new: map[string]string{"key1": "newValue1", "key2": "value2"},
expected: map[string]string{"key1": "newValue1", "key2": "value2"},
},
{
name: "merge with empty new map",
existing: map[string]string{"key1": "value1"},
new: map[string]string{},
expected: map[string]string{"key1": "value1"},
},
{
name: "merge with empty existing map",
existing: map[string]string{},
new: map[string]string{"key1": "value1"},
expected: map[string]string{"key1": "value1"},
},
{
name: "merge with both maps empty",
existing: map[string]string{},
new: map[string]string{},
expected: map[string]string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mergeMaps(tt.existing, tt.new)
assert.Equal(t, tt.expected, tt.existing)
})
}
}