Policy Testing and Audit Mode
What you'll learn: How to safely test policy enforcement using dry-run and audit modes before blocking real workloads.
When rolling out new policies in Kubernetes—especially with tools like OPA Gatekeeper—you don't want to immediately break production deployments. Audit mode and dry-run capabilities let you observe what would be blocked without actually rejecting requests.
Why Test Before Enforcing?
Think of audit mode like a speed camera that only flashes and records violations without issuing tickets. You can gather data about which pods, deployments, or configurations violate your new policies, then fix them proactively before enforcement begins.
Audit Mode in Gatekeeper
When you create a Constraint in Gatekeeper, you can set enforcementAction: dryrun instead of deny. The admission controller evaluates incoming requests against your policy but allows them through even if they violate the constraint. Violations are logged and visible in the constraint's status.
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sRequiredLabels
metadata:
name: must-have-owner
spec:
enforcementAction: dryrun # Test mode
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
Gatekeeper also runs periodic audits of existing resources in the cluster, comparing them against your constraints. Violations appear in the constraint's status.violations field, even for resources created before the policy existed.
Testing Workflow
- Deploy constraints with
enforcementAction: dryrun - Monitor violation reports via
kubectl get constraint <name> -o yaml - Identify and remediate non-compliant resources
- Switch to
enforcementAction: denyonce violations are resolved
Key Takeaway: Audit mode and dry-run let you validate policies against real traffic and existing resources without disrupting workloads, enabling safe, gradual policy rollout.