Skip to content

Commit

Permalink
fix: aggregated admission report not updated correctly (kyverno#7798) (
Browse files Browse the repository at this point in the history
…kyverno#7799)

* fix: aggregated admission report not updated correctly



* kuttl



---------

Signed-off-by: Charles-Edouard Brétéché <[email protected]>
Co-authored-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
gcp-cherry-pick-bot[bot] and eddycharly authored Jul 10, 2023
1 parent c99fa86 commit fdc962e
Show file tree
Hide file tree
Showing 15 changed files with 175 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/controllers/report/admission/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (c *controller) aggregateReports(ctx context.Context, uid types.UID) (kyver
if aggregated.GetResourceVersion() != "" {
after = reportutils.DeepCopy(aggregated)
}
reportutils.SetResults(aggregated, results...)
reportutils.SetResults(after, results...)
if after.GetResourceVersion() == "" {
if len(results) > 0 {
if _, err := reportutils.CreateReport(ctx, after, c.client); err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- policy.yaml
assert:
- policy-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- file: deployment-fail.yaml
assert:
- deployment-fail-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
assert:
- report-fail-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- file: deployment-pass.yaml
assert:
- deployment-pass-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
assert:
- report-pass-assert.yaml
14 changes: 14 additions & 0 deletions test/conformance/kuttl/reports/admission/update/README.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-dpl-1
status:
observedGeneration: 1
updatedReplicas: 1
readyReplicas: 1
replicas: 1
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-dpl-1
status:
observedGeneration: 2
updatedReplicas: 1
readyReplicas: 1
replicas: 1
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: disallow-latest-tag
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready
20 changes: 20 additions & 0 deletions test/conformance/kuttl/reports/admission/update/policy.yaml
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fdc962e

Please sign in to comment.