mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
Merge commit 'f1330ede8234eb4d449eb9ec72b41c627488350d' into 518_pod_controller
This commit is contained in:
commit
1f0187e8ea
2 changed files with 91 additions and 10 deletions
|
@ -289,7 +289,7 @@ func applyOverlayToArrayOfMaps(resource, overlay []interface{}, path string) ([]
|
|||
|
||||
if len(anchors) > 0 {
|
||||
// If we have anchors - choose corresponding resource element and mutate it
|
||||
patches, err := applyOverlayWithAnchors(resource, overlayElement, anchors, path)
|
||||
patches, err := applyOverlayWithAnchors(resource, overlayElement, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -320,21 +320,17 @@ func applyOverlayToArrayOfMaps(resource, overlay []interface{}, path string) ([]
|
|||
return appliedPatches, nil
|
||||
}
|
||||
|
||||
func applyOverlayWithAnchors(resource []interface{}, overlay interface{}, anchors map[string]interface{}, path string) ([][]byte, error) {
|
||||
func applyOverlayWithAnchors(resource []interface{}, overlay interface{}, path string) ([][]byte, error) {
|
||||
var appliedPatches [][]byte
|
||||
|
||||
for i, resourceElement := range resource {
|
||||
typedResource := resourceElement.(map[string]interface{})
|
||||
|
||||
currentPath := path + strconv.Itoa(i) + "/"
|
||||
// currentPath example: /spec/template/spec/containers/3/
|
||||
if !skipArrayObject(typedResource, anchors) {
|
||||
patches, err := applyOverlay(resourceElement, overlay, currentPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
appliedPatches = append(appliedPatches, patches...)
|
||||
patches, err := applyOverlay(resourceElement, overlay, currentPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
appliedPatches = append(appliedPatches, patches...)
|
||||
}
|
||||
|
||||
return appliedPatches, nil
|
||||
|
|
|
@ -1040,3 +1040,88 @@ func TestProcessOverlayPatches_InsertIfNotPresentWithConditions(t *testing.T) {
|
|||
|
||||
compareJSONAsMap(t, expectedResult, doc)
|
||||
}
|
||||
|
||||
func TestApplyOverlay_ConditionOnArray(t *testing.T) {
|
||||
resourceRaw := []byte(`
|
||||
{
|
||||
"apiVersion": "v1",
|
||||
"kind": "Pod",
|
||||
"metadata": {
|
||||
"name": "myapp-pod",
|
||||
"labels": {
|
||||
"app": "myapp",
|
||||
"dedicated": "spark"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"containers": [
|
||||
{
|
||||
"name": "myapp-container",
|
||||
"image": "busybox",
|
||||
"command": [
|
||||
"sh",
|
||||
"-c",
|
||||
"echo Hello Kubernetes! && sleep 3600"
|
||||
]
|
||||
}
|
||||
],
|
||||
"affinity": {
|
||||
"nodeAffinity": {
|
||||
"a": {
|
||||
"b": [
|
||||
{
|
||||
"matchExpressions": [
|
||||
{
|
||||
"key": "dedicated",
|
||||
"operator": "NotIn",
|
||||
"values": [
|
||||
"spark"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`)
|
||||
|
||||
overlayRaw := []byte(`
|
||||
{
|
||||
"spec": {
|
||||
"affinity": {
|
||||
"nodeAffinity": {
|
||||
"a": {
|
||||
"b": [
|
||||
{
|
||||
"matchExpressions": [
|
||||
{
|
||||
"(key)": "dedicated",
|
||||
"operator": "In",
|
||||
"(values)": [
|
||||
"spark"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`)
|
||||
var resource, overlay interface{}
|
||||
|
||||
assert.NilError(t, json.Unmarshal(resourceRaw, &resource))
|
||||
assert.NilError(t, json.Unmarshal(overlayRaw, &overlay))
|
||||
|
||||
expectedPatches := []byte(`[
|
||||
{ "op": "replace", "path": "/spec/affinity/nodeAffinity/a/b/0/matchExpressions/0/operator", "value": "In" }
|
||||
]`)
|
||||
p, err := applyOverlay(resource, overlay, "/")
|
||||
assert.NilError(t, err)
|
||||
assert.Assert(t, string(JoinPatches(p)) == string(expectedPatches))
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue