mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-14 11:57:48 +00:00
fix: disable autogen in foreach mutation with json patches (#6996)
* fix: disable autogen in foreach mutation with json patches Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * kuttl Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> --------- Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
parent
6d7f19688a
commit
f2cd3791ea
6 changed files with 75 additions and 0 deletions
|
@ -73,6 +73,11 @@ func CanAutoGen(spec *kyvernov1.Spec) (applyAutoGen bool, controllers string) {
|
|||
if rule.Mutation.PatchesJSON6902 != "" || rule.HasGenerate() {
|
||||
return false, "none"
|
||||
}
|
||||
for _, foreach := range rule.Mutation.ForEachMutation {
|
||||
if foreach.PatchesJSON6902 != "" {
|
||||
return false, "none"
|
||||
}
|
||||
}
|
||||
match, exclude := rule.MatchResources, rule.ExcludeResources
|
||||
if !checkAutogenSupport(&needed, match.ResourceDescription, exclude.ResourceDescription) {
|
||||
debug.Info("skip generating rule on pod controllers: Name / Selector in resource description may not be applicable.", "rule", rule.Name)
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
apiVersion: kuttl.dev/v1beta1
|
||||
kind: TestStep
|
||||
apply:
|
||||
- policy.yaml
|
||||
assert:
|
||||
- policy-assert.yaml
|
||||
error:
|
||||
- policy-error.yaml
|
11
test/conformance/kuttl/autogen/foreach-jsonpatch/README.md
Normal file
11
test/conformance/kuttl/autogen/foreach-jsonpatch/README.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
## Description
|
||||
|
||||
This test creates a cluster policy with a mutation rule containing a foreach and json patch.
|
||||
|
||||
## Expected Behavior
|
||||
|
||||
No autogen rules should be present in the status as json patches are supposed to disable autogen.
|
||||
|
||||
## Reference Issue(s)
|
||||
|
||||
- https://github.com/kyverno/kyverno/issues/4731
|
|
@ -0,0 +1,10 @@
|
|||
apiVersion: kyverno.io/v1
|
||||
kind: ClusterPolicy
|
||||
metadata:
|
||||
name: mutate-pod-require-non-root-user
|
||||
status:
|
||||
conditions:
|
||||
- reason: Succeeded
|
||||
status: "True"
|
||||
type: Ready
|
||||
autogen: {}
|
|
@ -0,0 +1,13 @@
|
|||
apiVersion: kyverno.io/v1
|
||||
kind: ClusterPolicy
|
||||
metadata:
|
||||
name: mutate-pod-require-non-root-user
|
||||
status:
|
||||
conditions:
|
||||
- reason: Succeeded
|
||||
status: "True"
|
||||
type: Ready
|
||||
autogen:
|
||||
rules:
|
||||
- name: autogen-require-non-root-user
|
||||
- name: autogen-cronjob-require-non-root-user
|
28
test/conformance/kuttl/autogen/foreach-jsonpatch/policy.yaml
Normal file
28
test/conformance/kuttl/autogen/foreach-jsonpatch/policy.yaml
Normal file
|
@ -0,0 +1,28 @@
|
|||
apiVersion: kyverno.io/v1
|
||||
kind: ClusterPolicy
|
||||
metadata:
|
||||
name: mutate-pod-require-non-root-user
|
||||
spec:
|
||||
schemaValidation: false
|
||||
rules:
|
||||
- name: require-non-root-user
|
||||
match:
|
||||
all:
|
||||
- resources:
|
||||
kinds:
|
||||
- Pod
|
||||
mutate:
|
||||
foreach:
|
||||
- list: request.object.spec.containers
|
||||
preconditions:
|
||||
all:
|
||||
# skip images that are exempt (allowed to run as a root user);
|
||||
# escape quotes where the replaced value may contain hyphens
|
||||
- key: "{{images.containers.\"{{element.name}}\".path}}"
|
||||
operator: AnyNotIn
|
||||
value:
|
||||
- myorg/exempt-image-name
|
||||
patchesJson6902: |-
|
||||
- path: /spec/containers/{{elementIndex}}/securityContext/runAsNonRoot
|
||||
op: add
|
||||
value: true
|
Loading…
Reference in a new issue