mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-28 02:18:15 +00:00
fix mutate targets variable (#5862)
Signed-off-by: ShutingZhao <shuting@nirmata.com> Signed-off-by: ShutingZhao <shuting@nirmata.com>
This commit is contained in:
parent
18455b4d21
commit
f59f17c05c
10 changed files with 161 additions and 2 deletions
|
@ -56,8 +56,14 @@ func Mutate(rule *kyvernov1.Rule, ctx context.Interface, resource unstructured.U
|
|||
return NewResponse(response.RuleStatusSkip, resource, nil, "no patches applied")
|
||||
}
|
||||
|
||||
if err := ctx.AddResource(patchedResource.Object); err != nil {
|
||||
return NewErrorResponse("failed to update patched resource in the JSON context", err)
|
||||
if rule.IsMutateExisting() {
|
||||
if err := ctx.AddTargetResource(patchedResource.Object); err != nil {
|
||||
return NewErrorResponse("failed to update patched resource in the JSON context", err)
|
||||
}
|
||||
} else {
|
||||
if err := ctx.AddResource(patchedResource.Object); err != nil {
|
||||
return NewErrorResponse("failed to update patched resource in the JSON context", err)
|
||||
}
|
||||
}
|
||||
|
||||
return NewResponse(response.RuleStatusPass, patchedResource, resp.Patches, resp.Message)
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
apiVersion: kyverno.io/v1
|
||||
kind: ClusterPolicy
|
||||
metadata:
|
||||
name: reload
|
||||
status:
|
||||
conditions:
|
||||
- reason: Succeeded
|
||||
status: "True"
|
||||
type: Ready
|
|
@ -0,0 +1,34 @@
|
|||
apiVersion: kyverno.io/v1
|
||||
kind: ClusterPolicy
|
||||
metadata:
|
||||
name: reload
|
||||
spec:
|
||||
mutateExistingOnPolicyUpdate: false
|
||||
rules:
|
||||
- name: trigger
|
||||
match:
|
||||
any:
|
||||
- resources:
|
||||
kinds:
|
||||
- ConfigMap
|
||||
selector:
|
||||
matchLabels:
|
||||
kyverno.io/watch: "true"
|
||||
preconditions:
|
||||
all:
|
||||
- key: "{{ request.operation }}"
|
||||
operator: Equals
|
||||
value: UPDATE
|
||||
mutate:
|
||||
targets:
|
||||
- apiVersion: v1
|
||||
kind: Pod
|
||||
namespace: "{{ request.namespace }}"
|
||||
patchStrategicMerge:
|
||||
metadata:
|
||||
annotations:
|
||||
corp.org/random: "{{ request.object.data.fookey }}"
|
||||
spec:
|
||||
volumes:
|
||||
- configMap:
|
||||
<(name): "{{ request.object.metadata.name }}"
|
|
@ -0,0 +1,15 @@
|
|||
apiVersion: v1
|
||||
data:
|
||||
fookey: fakeval
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: mycm
|
||||
namespace: foo
|
||||
labels:
|
||||
kyverno.io/watch: "true"
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: mypod
|
||||
namespace: foo
|
|
@ -0,0 +1,58 @@
|
|||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: foo
|
||||
---
|
||||
apiVersion: v1
|
||||
data:
|
||||
fookey: fakeval
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: mycm
|
||||
namespace: foo
|
||||
labels:
|
||||
kyverno.io/watch: "true"
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: mypod
|
||||
namespace: foo
|
||||
spec:
|
||||
containers:
|
||||
- name: busybox
|
||||
image: busybox:1.35
|
||||
command: ["sleep", "1d"]
|
||||
volumeMounts:
|
||||
- name: mycm
|
||||
mountPath: /etc/mycm
|
||||
volumes:
|
||||
- name: mycm
|
||||
configMap:
|
||||
name: mycm
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: unwatched
|
||||
namespace: foo
|
||||
spec:
|
||||
containers:
|
||||
- name: busybox
|
||||
image: busybox:1.35
|
||||
command: ["sleep", "1d"]
|
||||
volumeMounts:
|
||||
- name: othercm
|
||||
mountPath: /etc/fooconfig
|
||||
volumes:
|
||||
- name: othercm
|
||||
configMap:
|
||||
name: othercm
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: othercm
|
||||
namespace: foo
|
||||
data:
|
||||
foo: bar
|
|
@ -0,0 +1,6 @@
|
|||
apiVersion: kuttl.dev/v1beta1
|
||||
kind: TestStep
|
||||
apply:
|
||||
- update-mycm.yaml
|
||||
assert:
|
||||
- update-mycm.yaml
|
|
@ -0,0 +1,7 @@
|
|||
apiVersion: v1
|
||||
data:
|
||||
fookey: bar
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: mycm
|
||||
namespace: foo
|
|
@ -0,0 +1,4 @@
|
|||
apiVersion: kuttl.dev/v1beta1
|
||||
kind: TestStep
|
||||
commands:
|
||||
- command: kubectl delete -f 01-policy.yaml,02-resources.yaml --force --wait=true --ignore-not-found=true
|
|
@ -0,0 +1,11 @@
|
|||
## Description
|
||||
|
||||
This test checks that variable `request.object` always references the admission request object data in mutateExisting policies.
|
||||
|
||||
## Expected Behavior
|
||||
|
||||
With the mutateExisting policy, the variable `request.object` should always be substituted to the matching configmap's name `mycm`, not any pod's name. When the test finishes, the annotation `corp.org/random=bar` should be added to the pod `foo/mypod`.
|
||||
|
||||
## Reference Issue(s)
|
||||
|
||||
https://github.com/kyverno/kyverno/issues/5820
|
|
@ -0,0 +1,9 @@
|
|||
apiVersion: v1
|
||||
data:
|
||||
fookey: bar
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: mycm
|
||||
namespace: foo
|
||||
labels:
|
||||
kyverno.io/watch: "true"
|
Loading…
Add table
Reference in a new issue