Skip to content

Commit ebf1a53

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request kubernetes#38342 from ymqytw/make_SPatch_delete_all_duplicates
Automatic merge from submit-queue (batch tested with PRs 34488, 39511, 39619, 38342, 39491) Make StrategicPatch delete all matching maps in a merging list fixes kubernetes#38332 ```release-note NONE ``` cc: @lavalamp @pwittrock
2 parents 609e3e3 + 03081a0 commit ebf1a53

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

pkg/util/strategicpatch/patch.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -656,12 +656,16 @@ func mergeSlice(original, patch []interface{}, elemType reflect.Type, mergeKey s
656656
if patchType == deleteDirective {
657657
mergeValue, ok := typedV[mergeKey]
658658
if ok {
659-
_, originalKey, found, err := findMapInSliceBasedOnKeyValue(original, mergeKey, mergeValue)
660-
if err != nil {
661-
return nil, err
662-
}
663-
664-
if found {
659+
// delete all matching entries (based on merge key) from a merging list
660+
for {
661+
_, originalKey, found, err := findMapInSliceBasedOnKeyValue(original, mergeKey, mergeValue)
662+
if err != nil {
663+
return nil, err
664+
}
665+
666+
if !found {
667+
break
668+
}
665669
// Delete the element at originalKey.
666670
original = append(original[:originalKey], original[originalKey+1:]...)
667671
}

pkg/util/strategicpatch/patch_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,25 @@ testCases:
308308
$patch: replace
309309
modified:
310310
other: a
311+
- description: delete all duplicate entries in a merging list
312+
original:
313+
mergingList:
314+
- name: 1
315+
- name: 1
316+
- name: 2
317+
value: a
318+
- name: 3
319+
- name: 3
320+
twoWay:
321+
mergingList:
322+
- name: 1
323+
$patch: delete
324+
- name: 3
325+
$patch: delete
326+
modified:
327+
mergingList:
328+
- name: 2
329+
value: a
311330
`)
312331

313332
func TestCustomStrategicMergePatch(t *testing.T) {

0 commit comments

Comments
 (0)