Merge pull request #105421 from brianpursley/kubernetes-105146

Fix bug where using kubectl patch with $deleteFromPrimitiveList on an empty or nonexistent list adds the item to be removed
This commit is contained in:
Kubernetes Prow Robot 2021-11-05 12:44:40 -07:00 committed by GitHub
commit 0b0007ae68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 3 deletions

View File

@ -1328,15 +1328,19 @@ func mergeMap(original, patch map[string]interface{}, schema LookupPatchMeta, me
_, ok := original[k] _, ok := original[k]
if !ok { if !ok {
// If it's not in the original document, just take the patch value. if !isDeleteList {
original[k] = patchV // If it's not in the original document, just take the patch value.
original[k] = patchV
}
continue continue
} }
originalType := reflect.TypeOf(original[k]) originalType := reflect.TypeOf(original[k])
patchType := reflect.TypeOf(patchV) patchType := reflect.TypeOf(patchV)
if originalType != patchType { if originalType != patchType {
original[k] = patchV if !isDeleteList {
original[k] = patchV
}
continue continue
} }
// If they're both maps or lists, recurse into the value. // If they're both maps or lists, recurse into the value.

View File

@ -669,6 +669,57 @@ mergingList:
ExpectedError: "does not contain declared merge key", ExpectedError: "does not contain declared merge key",
}, },
}, },
{
Description: "$deleteFromPrimitiveList of nonexistent item in primitive list should not add the item to the list",
StrategicMergePatchRawTestCaseData: StrategicMergePatchRawTestCaseData{
Original: []byte(`
mergingIntList:
- 1
- 2
`),
TwoWay: []byte(`
$deleteFromPrimitiveList/mergingIntList:
- 3
`),
Modified: []byte(`
mergingIntList:
- 1
- 2
`),
},
},
{
Description: "$deleteFromPrimitiveList on empty primitive list should not add the item to the list",
StrategicMergePatchRawTestCaseData: StrategicMergePatchRawTestCaseData{
Original: []byte(`
mergingIntList:
`),
TwoWay: []byte(`
$deleteFromPrimitiveList/mergingIntList:
- 3
`),
Modified: []byte(`
mergingIntList:
`),
},
},
{
Description: "$deleteFromPrimitiveList on nonexistent primitive list should not add the primitive list",
StrategicMergePatchRawTestCaseData: StrategicMergePatchRawTestCaseData{
Original: []byte(`
foo:
- bar
`),
TwoWay: []byte(`
$deleteFromPrimitiveList/mergingIntList:
- 3
`),
Modified: []byte(`
foo:
- bar
`),
},
},
} }
func TestCustomStrategicMergePatch(t *testing.T) { func TestCustomStrategicMergePatch(t *testing.T) {