From a4897304e8abf64a6c86487eaf62c87fd9f9992b Mon Sep 17 00:00:00 2001 From: Matthias Bertschy Date: Mon, 14 Apr 2025 15:36:34 +0200 Subject: [PATCH] merge labels and annotations in RetryOnConflict Signed-off-by: Matthias Bertschy --- httphandler/storage/apiserver.go | 15 ++++++--- httphandler/storage/apiserver_test.go | 48 ++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/httphandler/storage/apiserver.go b/httphandler/storage/apiserver.go index 4b365544..f9d736ea 100644 --- a/httphandler/storage/apiserver.go +++ b/httphandler/storage/apiserver.go @@ -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 + } +} diff --git a/httphandler/storage/apiserver_test.go b/httphandler/storage/apiserver_test.go index 19e507f9..0a4b3664 100644 --- a/httphandler/storage/apiserver_test.go +++ b/httphandler/storage/apiserver_test.go @@ -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) + }) + } }