Interview's MateKubernetes
Interview's Mate

Kubernetes

K8s · Workloads · GitOps · Cloud

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Kubernetes topics
Lesson 109/ 160
06. Security, RBAC & Governance

Policy-as-Code with Kyverno

Write Kubernetes-native policies that validate, mutate, and generate resources.

IntermediateKyverno · Policy · Validate/Mutate3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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.yaml and 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

Loading interactive 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.

Verify Policy, not only object creation

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.

Key takeaway: Kyverno is implemented only when this invariant remains observable: 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.

Recover from Policy drift

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.

Key takeaway: Accept the trade-off consciously: Kubernetes-shaped rules are approachable and can generate companion resources, but complex policies need test matrices and clear conflict ordering.

How it works, step by step

Step 1 of 4
Identity owner

1. 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.

Kyverno contract → owned state
Key numbers at this step
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.yaml in 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.yaml first: 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

Admission and least privilege
Best for: Write Kubernetes-native policies that validate, mutate, and generate resources. A concrete incident check is 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.

Runtime-only detection
Best for: Quickly testing the Policy assumption with 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.

Hosted security service
Best for: Teams that need Validate/Mutate integrated with guardrails; the evidence path is 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.yaml without 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?
Interview tip: Interview sequence: state the invariant, run 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

Loading quiz

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.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Seccomp & AppArmor Profiles in Pod Specs
Next topic
Open Policy Agent (OPA) & Gatekeeper Constraints