Policy-as-Code with Kyverno
Write Kubernetes-native policies that validate, mutate, and generate resources.
In plain words
Kyverno is a resident policy desk that can stamp forms, reject them, or generate a missing companion pass using familiar Kubernetes paperwork. The simple Kubernetes rule is: Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes.
Think of it like…
Kyverno is a resident policy desk that can stamp forms, reject them, or generate a missing companion pass using familiar Kubernetes paperwork.
Where you’ve seen it
During an access review, the security engineer runs kubectl get clusterpolicy,policyreport,clusterpolicyreport; kubectl apply -f disallow-latest.yaml. Expected evidence: Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes. If the run instead shows this symptom, stop and restore the last known state: An audit-only policy reports drift without preventing it, background mode can lag, and a broad mutate rule can create unexpected labels or defaults on critical objects.
Words you’ll hear
- Kyverno
- For this lesson, Kyverno is the controlling idea: Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes.
- Policy
- Policy is the checkpoint; run
kubectl get clusterpolicy,policyreport,clusterpolicyreport; kubectl apply -f disallow-latest.yamland inspect the owner chain. An audit-only policy reports drift without preventing it, background mode can lag, and a broad mutate rule can create unexpected labels or defaults on critical objects. - Validate/Mutate
- Validate/Mutate is the outcome to preserve; the cost is explicit: Kubernetes-shaped rules are approachable and can generate companion resources, but complex policies need test matrices and clear conflict ordering.
- Verification evidence
- The evidence command is
kubectl get clusterpolicy,policyreport,clusterpolicyreport; kubectl apply -f disallow-latest.yaml. It connects the declaration to this behavior: Write Kubernetes-native policies that validate, mutate, and generate resources.
Visual diagram
Theory & deep dive
How Kyverno reaches the intended state
Write Kubernetes-native policies that validate, mutate, and generate resources. Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes.
The concrete checkpoint is kubectl get clusterpolicy,policyreport,clusterpolicyreport; kubectl apply -f disallow-latest.yaml; compare the Kyverno owner, Policy state, and Validate/Mutate outcome instead of treating YAML acceptance as proof.
Run kubectl get clusterpolicy,policyreport,clusterpolicyreport; kubectl apply -f disallow-latest.yaml after the change and correlate the result with Events and the owning object. Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes.
Policy: failure boundary and production cost
An audit-only policy reports drift without preventing it, background mode can lag, and a broad mutate rule can create unexpected labels or defaults on critical objects. The prerequisite contract is concrete: Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes.
Kubernetes-shaped rules are approachable and can generate companion resources, but complex policies need test matrices and clear conflict ordering. A safe design limits blast radius, records the last good configuration, and uses kubectl get clusterpolicy,policyreport,clusterpolicyreport; kubectl apply -f disallow-latest.yaml as a deterministic rollback checkpoint.
Capture the current state with kubectl get clusterpolicy,policyreport,clusterpolicyreport; kubectl apply -f disallow-latest.yaml, then correct the controlling manifest rather than patching a child object; the relevant divergence signal is specific: An audit-only policy reports drift without preventing it, background mode can lag, and a broad mutate rule can create unexpected labels or defaults on critical objects.
How it works, step by step
Step 1 of 41. Model the trust boundary
Name the identity, action, resource, and trust boundary before choosing a control: Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes.
- Goal
- Write Kubernetes-native policies that validate, mutate, and generate resources.
- Owner
- Kyverno
When to use it & trade-offs
What it is
Write Kubernetes-native policies that validate, mutate, and generate resources. Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes.
Why it matters: An audit-only policy reports drift without preventing it, background mode can lag, and a broad mutate rule can create unexpected labels or defaults on critical objects. The invariant at risk is specific: Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes. The quickest fact check before changing the design is kubectl get clusterpolicy,policyreport,clusterpolicyreport; kubectl apply -f disallow-latest.yaml.
When to reach for it
- Use Kyverno to enforce this invariant: Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes.
- Use Policy evidence from
kubectl get clusterpolicy,policyreport,clusterpolicyreport; kubectl apply -f disallow-latest.yamlin reviews and incident timelines. Write Kubernetes-native policies that validate, mutate, and generate resources. - Choose the hosted security service route only when this cost is justified by governance or scale: Kubernetes-shaped rules are approachable and can generate companion resources, but complex policies need test matrices and clear conflict ordering.
When to avoid or be careful
- Avoid assuming Kyverno is complete after YAML submission; the required behavior is concrete: Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes.
- Avoid treating the next symptom as random; check the owner and state with
kubectl get clusterpolicy,policyreport,clusterpolicyreport; kubectl apply -f disallow-latest.yamlfirst: An audit-only policy reports drift without preventing it, background mode can lag, and a broad mutate rule can create unexpected labels or defaults on critical objects. - Avoid adopting a heavier platform wrapper before measuring its cost: Kubernetes-shaped rules are approachable and can generate companion resources, but complex policies need test matrices and clear conflict ordering.
Native, manual, and managed paths for Kyverno
kubectl get clusterpolicy,policyreport,clusterpolicyreport; kubectl apply -f disallow-latest.yaml.Strengths: Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes.
Watch out: Kubernetes-shaped rules are approachable and can generate companion resources, but complex policies need test matrices and clear conflict ordering. Recovery must also recognize this signal: An audit-only policy reports drift without preventing it, background mode can lag, and a broad mutate rule can create unexpected labels or defaults on critical objects.
kubectl get clusterpolicy,policyreport,clusterpolicyreport; kubectl apply -f disallow-latest.yaml; the expected invariant is: Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes.Strengths: kubectl get clusterpolicy,policyreport,clusterpolicyreport; kubectl apply -f disallow-latest.yaml gives immediate feedback without first building a platform layer.
Watch out: A manual change can hide drift, and unlike Kyverno, it does not make this symptom self-documenting: An audit-only policy reports drift without preventing it, background mode can lag, and a broad mutate rule can create unexpected labels or defaults on critical objects.
kubectl get clusterpolicy,policyreport,clusterpolicyreport; kubectl apply -f disallow-latest.yaml.Strengths: Platform controls can wrap this exact behavior in policy or dashboards: Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes.
Watch out: The added platform ownership buys value at this cost: Kubernetes-shaped rules are approachable and can generate companion resources, but complex policies need test matrices and clear conflict ordering.
Interview signals & numbers to know
When this topic comes up
- Interview anchor: Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes.
- Trace Kyverno to Validate/Mutate through Policy; the invariant is: Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes.
- Recognize the symptom: An audit-only policy reports drift without preventing it, background mode can lag, and a broad mutate rule can create unexpected labels or defaults on critical objects.
- Justify the cost: Kubernetes-shaped rules are approachable and can generate companion resources, but complex policies need test matrices and clear conflict ordering.
- Verify with kubectl get clusterpolicy,policyreport,clusterpolicyreport; kubectl apply -f disallow-latest.yaml instead of only inspecting YAML.
Benchmarks & rules of thumb
- 3
- rule actions commonly used: validate, mutate, and generate
- 2
- validation enforcement modes include Enforce and Audit
- 0
- policy report entries are not the same as admission rejection
Common mistakes & follow-ups
Common interview mistakes
- Debugging only the child object misses the Kyverno owner contract: Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes.
- Ignoring this telltale condition leads to unrelated changes: An audit-only policy reports drift without preventing it, background mode can lag, and a broad mutate rule can create unexpected labels or defaults on critical objects.
- Repeating
kubectl get clusterpolicy,policyreport,clusterpolicyreport; kubectl apply -f disallow-latest.yamlwithout an owner for remediation wastes retry cost. Kubernetes-shaped rules are approachable and can generate companion resources, but complex policies need test matrices and clear conflict ordering.
Likely interviewer follow-ups
- How would API output and runtime evidence prove this behavior: Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes?
- Which owner object, logs, and Events would you inspect for this symptom-An audit-only policy reports drift without preventing it, background mode can lag, and a broad mutate rule can create unexpected labels or defaults on critical objects-?
- At what threshold would this cost become unacceptable: Kubernetes-shaped rules are approachable and can generate companion resources, but complex policies need test matrices and clear conflict ordering?
kubectl get clusterpolicy,policyreport,clusterpolicyreport; kubectl apply -f disallow-latest.yaml, name the failure, then justify the cost. Invariant: Kyverno uses Kubernetes-native YAML to validate, mutate, or generate resources and can run policy in admission and background reporting modes. Failure: An audit-only policy reports drift without preventing it, background mode can lag, and a broad mutate rule can create unexpected labels or defaults on critical objects. Cost: Kubernetes-shaped rules are approachable and can generate companion resources, but complex policies need test matrices and clear conflict ordering.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Policy-as-Code with Kyverno with generative AI interview Q&A and hands-on practice.