Policy as code

Kyverno/OPA Gatekeeper via admission control: block :latest tags, require limits and probes, enforce labels — guardrails that teach (message with the fix) instead of tickets that block.

About 25 min read3-question knowledge check

The mental model

There are two ways to keep bad code out of production. One is a gate: a human who reviews every request, says no a lot, and becomes a queue everyone hates and routes around. The other is a spellchecker: it catches the mistake the instant you make it, tells you exactly what is wrong and how to fix it, and gets out of your way — so you never even think of it as a gate. It just makes the wrong thing hard to do and the right thing automatic.

Policy as code is the spellchecker for your cluster. Instead of a wiki page that says "please always set resource limits" and a platform engineer who catches violations in code review three days later, an **admission controller** intercepts every kubectl apply at the API server and evaluates it against rules the moment it arrives. A :latest image tag, a missing liveness probe, a pod without resource limits, a deployment with no owner label — rejected at the door, with a message that names the fix. No ticket, no Slack, no waiting for you.

The distinction that separates a good platform engineer from a mediocre one in interviews: **a guardrail that teaches beats a guardrail that blocks.** A raw rejection ("denied by policy") generates a support ticket. A rejection that says "image app:latest uses a mutable tag; pin a digest or a semver tag like app:1.4.2 — see go/image-policy" teaches the developer to never make the mistake again and never contacts you. And the best guardrail of all is one the developer never notices, because instead of rejecting a missing label, the policy mutates the resource and adds it. The goal is not to say no more efficiently — it is to make the compliant path the only path, invisibly where possible.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision