mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
Implemented logic "Add if not exist"
This commit is contained in:
parent
9002a27ddb
commit
1a08ab4bda
3 changed files with 64 additions and 5 deletions
pkg/engine
|
@ -113,10 +113,11 @@ func applyOverlayToMap(resourceMap, overlayMap map[string]interface{}, path stri
|
|||
continue
|
||||
}
|
||||
|
||||
currentPath := path + key + "/"
|
||||
resourcePart, ok := resourceMap[key]
|
||||
noAnchorKey := removeAnchor(key)
|
||||
currentPath := path + noAnchorKey + "/"
|
||||
resourcePart, ok := resourceMap[noAnchorKey]
|
||||
|
||||
if ok {
|
||||
if ok && !isAddingAnchor(key) {
|
||||
// Key exists - go down through the overlay and resource trees
|
||||
patches, res := applyOverlay(resourcePart, value, currentPath)
|
||||
overlayResult.MergeWith(&res)
|
||||
|
@ -124,7 +125,9 @@ func applyOverlayToMap(resourceMap, overlayMap map[string]interface{}, path stri
|
|||
if result.Success == overlayResult.GetReason() {
|
||||
appliedPatches = append(appliedPatches, patches...)
|
||||
}
|
||||
} else {
|
||||
}
|
||||
|
||||
if !ok {
|
||||
// Key does not exist - insert entire overlay subtree
|
||||
patch, res := insertSubtree(value, currentPath)
|
||||
overlayResult.MergeWith(&res)
|
||||
|
|
|
@ -430,3 +430,46 @@ func TestApplyOverlay_ImagePullPolicy(t *testing.T) {
|
|||
|
||||
compareJsonAsMap(t, expectedResult, doc)
|
||||
}
|
||||
|
||||
func TestApplyOverlay_AddingAnchor(t *testing.T) {
|
||||
overlayRaw := []byte(`{
|
||||
"metadata": {
|
||||
"name": "nginx-deployment",
|
||||
"labels": {
|
||||
"+(app)": "should-not-be-here",
|
||||
"+(key1)": "value1"
|
||||
}
|
||||
}
|
||||
}`)
|
||||
resourceRaw := []byte(`{
|
||||
"metadata": {
|
||||
"name": "nginx-deployment",
|
||||
"labels": {
|
||||
"app": "nginx"
|
||||
}
|
||||
}
|
||||
}`)
|
||||
|
||||
var resource, overlay interface{}
|
||||
|
||||
json.Unmarshal(resourceRaw, &resource)
|
||||
json.Unmarshal(overlayRaw, &overlay)
|
||||
|
||||
patches, res := applyOverlay(resource, overlay, "/")
|
||||
assert.NilError(t, res.ToError())
|
||||
assert.Assert(t, len(patches) != 0)
|
||||
|
||||
doc, err := ApplyPatches(resourceRaw, patches)
|
||||
assert.NilError(t, err)
|
||||
expectedResult := []byte(`{
|
||||
"metadata":{
|
||||
"labels":{
|
||||
"app":"nginx",
|
||||
"key1":"value1"
|
||||
},
|
||||
"name":"nginx-deployment"
|
||||
}
|
||||
}`)
|
||||
|
||||
compareJsonAsMap(t, expectedResult, doc)
|
||||
}
|
||||
|
|
|
@ -138,6 +138,17 @@ func wrappedWithParentheses(str string) bool {
|
|||
return (str[0] == '(' && str[len(str)-1] == ')')
|
||||
}
|
||||
|
||||
func isAddingAnchor(key string) bool {
|
||||
const left = "+("
|
||||
const right = ")"
|
||||
|
||||
if len(key) < len(left)+len(right) {
|
||||
return false
|
||||
}
|
||||
|
||||
return left == key[:len(left)] && right == key[len(key)-len(right):]
|
||||
}
|
||||
|
||||
// Checks if array object matches anchors. If not - skip - return true
|
||||
func skipArrayObject(object, anchors map[string]interface{}) bool {
|
||||
for key, pattern := range anchors {
|
||||
|
@ -162,7 +173,9 @@ func removeAnchor(key string) string {
|
|||
return key[1 : len(key)-1]
|
||||
}
|
||||
|
||||
// TODO: Add logic for other anchors here
|
||||
if isAddingAnchor(key) {
|
||||
return key[2 : len(key)-1]
|
||||
}
|
||||
|
||||
return key
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue