1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

fix: exceptions not considered on delete (#7433)

* fix: helm template for cleanup jobs image

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix: exceptions not considered on delete

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* kuttl test

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:
Charles-Edouard Brétéché 2023-06-06 16:15:10 +02:00 committed by GitHub
parent c11db51836
commit 9078acb92a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 116 additions and 1 deletions

View file

@ -48,9 +48,13 @@ func matchesException(
return nil, err return nil, err
} }
gvk, subresource := policyContext.ResourceKind() gvk, subresource := policyContext.ResourceKind()
resource := policyContext.NewResource()
if resource.Object == nil {
resource = policyContext.OldResource()
}
for _, candidate := range candidates { for _, candidate := range candidates {
err := matched.CheckMatchesResources( err := matched.CheckMatchesResources(
policyContext.NewResource(), resource,
candidate.Spec.Match, candidate.Spec.Match,
policyContext.NamespaceLabels(), policyContext.NamespaceLabels(),
policyContext.AdmissionInfo(), policyContext.AdmissionInfo(),

View file

@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- policy.yaml
assert:
- policy-assert.yaml

View file

@ -0,0 +1,4 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- exception.yaml

View file

@ -0,0 +1,4 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- namespace.yaml

View file

@ -0,0 +1,4 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- file: deployment.yaml

View file

@ -0,0 +1,7 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
delete:
- apiVersion: apps/v1
kind: Deployment
name: test-dpl1
namespace: reza-dev

View file

@ -0,0 +1,13 @@
## Description
This test creates a policy, a policy exception and tries to create a deployment violating the policy.
The deployment is then deleted.
## Expected Behavior
Both creation and deletion should be accepted, the exception applies to all operations.
## Reference Issue(s)
7423

View file

@ -0,0 +1,23 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-dpl1
namespace: reza-dev
spec:
selector:
matchLabels:
app: test-dpl1
template:
metadata:
labels:
app: test-dpl1
spec:
securityContext:
seccompProfile:
type: Unconfined
containers:
- name: test-dpl1
image: busybox:1.35.0
command:
- sleep
- "infinity"

View file

@ -0,0 +1,19 @@
apiVersion: kyverno.io/v2alpha1
kind: PolicyException
metadata:
name: delta-exception
namespace: kyverno
spec:
exceptions:
- policyName: psa
ruleNames:
- "*"
match:
any:
- resources:
kinds:
- Deployment
namespaces:
- reza-dev
names:
- test-dpl1*

View file

@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: reza-dev

View file

@ -0,0 +1,9 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: psa
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready

View file

@ -0,0 +1,18 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: psa
spec:
validationFailureAction: Enforce
background: true
rules:
- name: restricted
match:
any:
- resources:
kinds:
- Pod
validate:
podSecurity:
level: restricted
version: v1.25