diff --git a/pkg/util/strategicpatch/patch.go b/pkg/util/strategicpatch/patch.go index 66a33f7ad08..f715d8c197f 100644 --- a/pkg/util/strategicpatch/patch.go +++ b/pkg/util/strategicpatch/patch.go @@ -656,12 +656,16 @@ func mergeSlice(original, patch []interface{}, elemType reflect.Type, mergeKey s if patchType == deleteDirective { mergeValue, ok := typedV[mergeKey] if ok { - _, originalKey, found, err := findMapInSliceBasedOnKeyValue(original, mergeKey, mergeValue) - if err != nil { - return nil, err - } + // delete all matching entries (based on merge key) from a merging list + for { + _, originalKey, found, err := findMapInSliceBasedOnKeyValue(original, mergeKey, mergeValue) + if err != nil { + return nil, err + } - if found { + if !found { + break + } // Delete the element at originalKey. original = append(original[:originalKey], original[originalKey+1:]...) } diff --git a/pkg/util/strategicpatch/patch_test.go b/pkg/util/strategicpatch/patch_test.go index 0aa7c37cb22..811ffd62c98 100644 --- a/pkg/util/strategicpatch/patch_test.go +++ b/pkg/util/strategicpatch/patch_test.go @@ -308,6 +308,25 @@ testCases: $patch: replace modified: other: a + - description: delete all duplicate entries in a merging list + original: + mergingList: + - name: 1 + - name: 1 + - name: 2 + value: a + - name: 3 + - name: 3 + twoWay: + mergingList: + - name: 1 + $patch: delete + - name: 3 + $patch: delete + modified: + mergingList: + - name: 2 + value: a `) func TestCustomStrategicMergePatch(t *testing.T) {