mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-16 23:29:21 +00:00
filter out directives before taking patch value as-is
This commit is contained in:
parent
6de6cd0a65
commit
1b76683967
@ -1182,7 +1182,13 @@ func mergePatchIntoOriginal(original, patch map[string]interface{}, schema Looku
|
|||||||
merged = originalFieldValue
|
merged = originalFieldValue
|
||||||
case !foundOriginal && foundPatch:
|
case !foundOriginal && foundPatch:
|
||||||
// list was added
|
// list was added
|
||||||
merged = patchFieldValue
|
v, keep := removeDirectives(patchFieldValue)
|
||||||
|
if !keep {
|
||||||
|
// Shouldn't be possible since patchFieldValue is a slice
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
merged = v.([]interface{})
|
||||||
case foundOriginal && foundPatch:
|
case foundOriginal && foundPatch:
|
||||||
merged, err = mergeSliceHandler(originalList, patchList, subschema,
|
merged, err = mergeSliceHandler(originalList, patchList, subschema,
|
||||||
patchStrategy, patchMeta.GetPatchMergeKey(), false, mergeOptions)
|
patchStrategy, patchMeta.GetPatchMergeKey(), false, mergeOptions)
|
||||||
@ -1270,6 +1276,42 @@ func partitionMapsByPresentInList(original, partitionBy []interface{}, mergeKey
|
|||||||
return patch, serverOnly, nil
|
return patch, serverOnly, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Removes directives from an object and returns value to use instead and whether
|
||||||
|
// or not the field/index should even be kept
|
||||||
|
// May modify input
|
||||||
|
func removeDirectives(obj interface{}) (interface{}, bool) {
|
||||||
|
if obj == nil {
|
||||||
|
return obj, true
|
||||||
|
} else if typedV, ok := obj.(map[string]interface{}); ok {
|
||||||
|
if _, hasDirective := typedV[directiveMarker]; hasDirective {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range typedV {
|
||||||
|
var keep bool
|
||||||
|
typedV[k], keep = removeDirectives(v)
|
||||||
|
if !keep {
|
||||||
|
delete(typedV, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return typedV, true
|
||||||
|
} else if typedV, ok := obj.([]interface{}); ok {
|
||||||
|
var res []interface{}
|
||||||
|
if typedV != nil {
|
||||||
|
// Make sure res is non-nil if patch is non-nil
|
||||||
|
res = []interface{}{}
|
||||||
|
}
|
||||||
|
for _, v := range typedV {
|
||||||
|
if newV, keep := removeDirectives(v); keep {
|
||||||
|
res = append(res, newV)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res, true
|
||||||
|
} else {
|
||||||
|
return obj, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Merge fields from a patch map into the original map. Note: This may modify
|
// Merge fields from a patch map into the original map. Note: This may modify
|
||||||
// both the original map and the patch because getting a deep copy of a map in
|
// both the original map and the patch because getting a deep copy of a map in
|
||||||
// golang is highly non-trivial.
|
// golang is highly non-trivial.
|
||||||
@ -1333,7 +1375,10 @@ func mergeMap(original, patch map[string]interface{}, schema LookupPatchMeta, me
|
|||||||
if mergeOptions.IgnoreUnmatchedNulls {
|
if mergeOptions.IgnoreUnmatchedNulls {
|
||||||
discardNullValuesFromPatch(patchV)
|
discardNullValuesFromPatch(patchV)
|
||||||
}
|
}
|
||||||
original[k] = patchV
|
original[k], ok = removeDirectives(patchV)
|
||||||
|
if !ok {
|
||||||
|
delete(original, k)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -1345,7 +1390,10 @@ func mergeMap(original, patch map[string]interface{}, schema LookupPatchMeta, me
|
|||||||
if mergeOptions.IgnoreUnmatchedNulls {
|
if mergeOptions.IgnoreUnmatchedNulls {
|
||||||
discardNullValuesFromPatch(patchV)
|
discardNullValuesFromPatch(patchV)
|
||||||
}
|
}
|
||||||
original[k] = patchV
|
original[k], ok = removeDirectives(patchV)
|
||||||
|
if !ok {
|
||||||
|
delete(original, k)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -1372,7 +1420,11 @@ func mergeMap(original, patch map[string]interface{}, schema LookupPatchMeta, me
|
|||||||
}
|
}
|
||||||
original[k], err = mergeSliceHandler(original[k], patchV, subschema, patchStrategy, patchMeta.GetPatchMergeKey(), isDeleteList, mergeOptions)
|
original[k], err = mergeSliceHandler(original[k], patchV, subschema, patchStrategy, patchMeta.GetPatchMergeKey(), isDeleteList, mergeOptions)
|
||||||
default:
|
default:
|
||||||
original[k] = patchV
|
original[k], ok = removeDirectives(patchV)
|
||||||
|
if !ok {
|
||||||
|
// if patchV itself is a directive, then don't keep it
|
||||||
|
delete(original, k)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
Reference in New Issue
Block a user