From 03081a00036d1fbeb9907071df1195f258130375 Mon Sep 17 00:00:00 2001 From: ymqytw Date: Wed, 7 Dec 2016 16:44:36 -0800 Subject: [PATCH] make StrategicPatch delete all matching maps in a merging list --- pkg/util/strategicpatch/patch.go | 14 +++++++++----- pkg/util/strategicpatch/patch_test.go | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/pkg/util/strategicpatch/patch.go b/pkg/util/strategicpatch/patch.go index 66a33f7ad0..f715d8c197 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 0aa7c37cb2..811ffd62c9 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) {