Sub topic 13
Enforcement modes and audit of existing resources
This chapter assumes you can read Kyverno ClusterPolicies and Gatekeeper Constraints, and that you know admission webhooks only see requests at the moment a resource is created or updated. Policies are easiest to adopt when you can observe their effect before they block anything. Both engines support this, with different settings.
Why enforcement and audit are separate
Admission control only checks new requests. Resources that already exist in the cluster were never evaluated against a policy you add later. A new policy therefore has two jobs. It blocks or flags bad requests going forward, and it reports on what is already running.
Teams usually roll out in three steps: report only, fix the violations, then block.
Kyverno: validationFailureAction and background scans
Kyverno sets the mode with spec.validationFailureAction.
Enforcerejects a non-compliant request.Auditallows the request and records the violation in a policy report.
The background: true setting makes Kyverno also scan existing resources periodically and write results to PolicyReport and ClusterPolicyReport objects.
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-team-label
spec:
validationFailureAction: Audit
background: true
rules:
- name: check-team-label
match:
any:
- resources:
kinds:
- Pod
validate:
message: "The label 'team' is required."
pattern:
metadata:
labels:
team: "?*"Read the results with kubectl:
kubectl get policyreport -A
kubectl get clusterpolicyreportWhen the failing list is empty or understood, change Audit to Enforce.
Gatekeeper: enforcementAction and the audit controller
Gatekeeper sets the mode on each Constraint with spec.enforcementAction.
| Value | Effect |
|---|---|
| `deny` | Rejects the request. This is the default. |
| `dryrun` | Allows the request. The audit controller still records violations. |
| `warn` | Allows the request and returns a warning to the client. |
Gatekeeper runs a separate audit controller. It lists existing resources on an interval (60 seconds by default) and writes violations to the status.violations field of each Constraint.
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sRequiredLabels
metadata:
name: pods-must-have-team
spec:
enforcementAction: dryrun
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
parameters:
labels: ["team"]kubectl get k8srequiredlabels pods-must-have-team -o yamlLook at status.totalViolations and the status.violations list. By default the list holds only the first 20 entries, so the total can be larger than what you see.
Comparing the two
Common errors
- Setting
Enforcefirst, then discovering that system components or Deployments fail to roll out. - Forgetting
background: truein Kyverno, so no report covers existing resources. Some policies that use admission-only data, such as the requesting user, cannot run in background mode. - Reading only the truncated Gatekeeper violation list and underestimating the total.
- Expecting
warnto be visible in CI logs. The warning appears only in the client that made the request.
Exercise
You add a Gatekeeper Constraint with enforcementAction: dryrun. A Deployment without a team label is created afterward. What happens to the request, and where do you find the violation?
Show answer
The request is admitted because dryrun does not deny. The audit controller later records the violation in the Constraint's status.violations field, and status.totalViolations shows the full count.