mirror of
https://github.com/kubescape/kubescape.git
synced 2026-02-14 09:59:54 +00:00
merge labels and annotations in RetryOnConflict
Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user