Sub topic 2
Why policy as code: common goals and risks
This chapter assumes you know how admission webhooks intercept API requests, as covered in the previous chapter. It also assumes you are comfortable with Kubernetes objects, YAML and kubectl.
What policy as code means
Policy as code means writing your cluster rules as machine-readable files. The files live in version control, go through review, and are applied by a tool. A written rule such as "every Pod needs an owner label" depends on people remembering it. A policy file is checked on every request, and the policy engine applies it.
Both engines in this tutorial work this way. Kyverno policies are Kubernetes YAML. Gatekeeper policies are Rego code wrapped in YAML.
Common policy goals
Most clusters start with a handful of rules in a few categories.
| Goal | Example rule |
|---|---|
| Security baseline | Reject privileged containers and host namespaces |
| Supply chain | Allow images only from approved registries |
| Cost and ownership | Require `team` and `cost-center` labels |
| Reliability | Require resource requests and limits, forbid the `latest` tag |
| Consistency | Add default labels or annotations automatically |
Policies fall into three kinds of action. Validation accepts or rejects a request. Mutation changes a request before it is stored. Auditing reports on resources that already exist.
The policy lifecycle
A policy should move through stages before it blocks anyone.
Later chapters cover each stage in detail. The point to take from this one is the order: report first, block second.
Risks to plan for
Policy engines are powerful because they sit in the request path. That also makes them risky.
- Blocked deployments. An over-broad rule can reject legitimate workloads, including system Pods. A bad policy can stop a rollout during an incident.
- Webhook availability. If the engine's webhook is down and its failure policy is
Fail, requests that match it are rejected. If the failure policy isIgnore, requests pass without being checked. - Latency. Every matching request waits for the webhook. Slow or complex policies slow every
kubectl apply. - Mutation surprises. Mutations change objects after the user submitted them. The live object can differ from the manifest in Git, which confuses GitOps diffs.
- Policy sprawl. Rules added without owners or documentation become hard to reason about. Rules also conflict with each other.
- False confidence. Admission control only sees requests. It does not stop an attacker who has already bypassed it, and it does not fix resources that existed before the policy did unless you also run audits.
Principles that reduce risk
- Start with audit or warn behavior and promote to enforce after reviewing results.
- Scope each policy to the resources and namespaces it needs.
- Treat policies like application code: review them, test them and version them.
- Give every policy an owner and a clear error message that tells users how to fix the request.
Exercise
A team deploys a new policy that requires a team label on all Pods and sets it to enforce immediately. The engine's webhook is also configured with failure policy Fail. Name two things that can go wrong.
Show answer
Existing system components and legitimate workloads without the label can be rejected when they are recreated or rescheduled, because the policy was never tested in audit mode. If the engine's webhook becomes unavailable, every matching request is rejected, which can block deployments and cluster operations.