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

fix: aggregated admission report not updated correctly (#7798)

* fix: aggregated admission report not updated correctly

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:
Charles-Edouard Brétéché 2023-07-10 14:25:32 +02:00 committed by GitHub
parent e20745b01d
commit e166d7897b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 175 additions and 1 deletions

View file

@ -249,7 +249,7 @@ func (c *controller) aggregateReports(ctx context.Context, uid types.UID) (kyver
if aggregated.GetResourceVersion() != "" { if aggregated.GetResourceVersion() != "" {
after = reportutils.DeepCopy(aggregated) after = reportutils.DeepCopy(aggregated)
} }
reportutils.SetResults(aggregated, results...) reportutils.SetResults(after, results...)
if after.GetResourceVersion() == "" { if after.GetResourceVersion() == "" {
if len(results) > 0 { if len(results) > 0 {
if _, err := reportutils.CreateReport(ctx, after, c.client); err != nil { if _, err := reportutils.CreateReport(ctx, after, c.client); err != nil {

View file

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

View file

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

View file

@ -0,0 +1,4 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
assert:
- report-fail-assert.yaml

View file

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

View file

@ -0,0 +1,4 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
assert:
- report-pass-assert.yaml

View file

@ -0,0 +1,14 @@
## Description
This test verifies that aggregated admission report is correctly updated when a resource changes.
A policy in Audit mode is created.
A deployment is created, the deployment violates the policy and we assert the admission report contains a `fail` result.
The deployment is then updated to not violate the policy anymore and we assert the admission report changes to contain `pass` result.
## Expected result
When the resource does not violate the policy anymore, the result in the admission report should change from `fail` to `pass`.
## Related issue(s)
- https://github.com/kyverno/kyverno/issues/7793

View file

@ -0,0 +1,9 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-dpl-1
status:
observedGeneration: 1
updatedReplicas: 1
readyReplicas: 1
replicas: 1

View file

@ -0,0 +1,19 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-dpl-1
spec:
selector:
matchLabels:
app: test-dpl-1
template:
metadata:
labels:
app: test-dpl-1
spec:
securityContext:
seccompProfile:
type: RuntimeDefault
containers:
- name: test-container
image: nginx:latest

View file

@ -0,0 +1,9 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-dpl-1
status:
observedGeneration: 2
updatedReplicas: 1
readyReplicas: 1
replicas: 1

View file

@ -0,0 +1,19 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-dpl-1
spec:
selector:
matchLabels:
app: test-dpl-1
template:
metadata:
labels:
app: test-dpl-1
spec:
securityContext:
seccompProfile:
type: RuntimeDefault
containers:
- name: test-container
image: nginx:1.25.1

View file

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

View file

@ -0,0 +1,20 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: disallow-latest-tag
spec:
validationFailureAction: Audit
background: true
rules:
- name: validate-image-tag-pod
match:
any:
- resources:
kinds:
- Pod
validate:
message: "Using a mutable image tag e.g. 'latest' is not allowed."
pattern:
spec:
containers:
- image: "!*:latest"

View file

@ -0,0 +1,25 @@
apiVersion: kyverno.io/v1alpha2
kind: AdmissionReport
metadata:
ownerReferences:
- apiVersion: apps/v1
kind: Deployment
name: test-dpl-1
spec:
results:
- message: 'validation error: Using a mutable image tag e.g. ''latest'' is not allowed.
rule autogen-validate-image-tag-pod failed at path /spec/template/spec/containers/0/image/'
policy: disallow-latest-tag
resources:
- apiVersion: apps/v1
kind: Deployment
name: test-dpl-1
result: fail
rule: autogen-validate-image-tag-pod
source: kyverno
summary:
error: 0
fail: 1
pass: 0
skip: 0
warn: 0

View file

@ -0,0 +1,24 @@
apiVersion: kyverno.io/v1alpha2
kind: AdmissionReport
metadata:
ownerReferences:
- apiVersion: apps/v1
kind: Deployment
name: test-dpl-1
spec:
results:
- message: validation rule 'autogen-validate-image-tag-pod' passed.
policy: disallow-latest-tag
resources:
- apiVersion: apps/v1
kind: Deployment
name: test-dpl-1
result: pass
rule: autogen-validate-image-tag-pod
source: kyverno
summary:
error: 0
fail: 0
pass: 1
skip: 0
warn: 0