Control plane
Kyverno policies
Who checks your YAML
RBAC decides who may create a Deployment. It has nothing to say about what's inside: which image, which security settings, whether a tag is pinned. Kyverno does. It runs as ordinary Pods, and the API server asks it about objects before saving them. Follow three policies, one that checks, one that changes and one that creates, and then see what happens when Kyverno itself is down.
- Checks
- ValidatingPolicy · allow, warn or deny
- Changes
- MutatingPolicy · patch on the way in
- Creates
- GeneratingPolicy · new objects from a trigger
- Version
- Kyverno 1.20 · policies.kyverno.io/v1
Read the whole lesson as text
1. What RBAC can't ask
A teammate deploys a small internal app, tools, with the image tag latest. That tag can point to a different image tomorrow, so two Pods of the same Deployment can end up running different code. The API server checks who is asking and whether they may create Deployments here. Both answers are yes, so the object is saved and the Pod starts. Nobody looked at the tag, because RBAC only knows verbs and resources.
- A teammate deploys tools with an image tag that can change under them.
- They may create Deployments in default. That's all RBAC is asked.
- The Deployment is saved, tag and all.
- A moment later tools is running on node-1.
2. Install Kyverno
You install Kyverno with Helm. It adds its own resource types to the API and four Deployments in the kyverno namespace. The admission controller answers the API server's questions. The reports controller scans what already exists. The background controller creates and changes objects later. The cleanup controller deletes on a schedule. They are normal Pods on normal nodes: the control plane is not modified, and with no policies yet, Kyverno has nothing to check.
- Helm creates Kyverno's resource types and its four Deployments.
- The admission controller comes up on node-1.
- The reports, background and cleanup controllers come up on node-2.
3. The road to etcd
Zoom into the API server. Every write takes the same road. Authentication: who is this? Authorization: may they? Mutating admission: should the object be changed? Schema validation: are the fields legal? Validating admission: is the object acceptable? Only then is it written to etcd. The two admission stops can call out to a webhook, an HTTPS server somewhere in the cluster. That is where Kyverno plugs in, always in this order: changes first, checks after.
- A write arrives. First stop: who is it?
- Second stop: may they do this? RBAC answers.
- Third stop: anyone registered to change it? Not yet.
- Fourth stop: is it a legal Deployment?
- Last stop: does anyone object? Not yet.
- No objections, so it's written.
4. Write the rule
You write a ValidatingPolicy called disallow-latest-tag and apply it with kubectl. It says which objects it's about, Pods being created or updated, and holds one CEL expression that must be true. Its action is Audit: record violations, block nothing. The API server has Kyverno check the policy, then saves it. Kyverno is watching policies, so it sees the new one and registers a webhook with the API server, for exactly the objects this policy needs. Because Deployments, Jobs and the rest only make Pods, Kyverno also derives the same rule for them.
- You write the rule down as a Kubernetes object.
- Kyverno checks the policy itself first. An expression that doesn't compile is rejected here.
- It compiles, so the API server goes on to create the object you asked for.
- The policy is an object in etcd like any other.
- Kyverno watches policies, so it hears about the new one.
- Kyverno asks the API server to call it for Pods, and for everything that makes Pods.
5. The report
Admission only sees objects as they arrive, and tools arrived before the policy. So the reports controller runs the same rule over what already exists, once when the policy appears and then every hour. The results become PolicyReports, one per object, that you read with kubectl. web passes. tools fails. A scan never blocks or deletes anything. That makes Audit a safe first step: see what would break, fix it, then switch the policy to Deny.
- The reports controller runs the rule on everything that's already there.
- It writes the results down as PolicyReports.
- You list the reports and find the one that fails.
- You pin tools to a version. Its report turns to pass.
- Nothing fails any more, so you switch the policy to Deny.
6. Denied
The policy now says Deny, and someone deploys web with the tag latest. The request passes authentication and RBAC as before, and reaches validating admission. The API server sends the whole object to Kyverno and waits. Kyverno runs the expression, gets false, and answers allowed: false with the policy's message. The API server turns that into an error for the caller. Nothing reaches etcd, so no controller ever hears of it.
- Someone deploys web:latest. It passes every stop up to the last.
- The API server sends the whole object to Kyverno and waits.
- Kyverno runs the expression on the Pod template. It comes out false.
- The answer is no, with the reason you wrote in the policy.
- kubectl prints it. The running Pods never noticed.
7. Two more policies
You add two policies of other kinds. A MutatingPolicy, default-seccomp, changes Pods on their way in. A GeneratingPolicy, default-deny, creates an object whenever a namespace is created. Each takes the route the first one took: kubectl sends it, the API server has Kyverno check it and saves it, and Kyverno sees it appear. For these two it registers two more webhooks: one that may change Pods, and one for namespaces.
- You apply a policy that fills in a seccomp profile.
- Kyverno checks it first, like the one before.
- It's valid, so the API server creates it.
- The MutatingPolicy is saved.
- You apply a policy that creates a NetworkPolicy for each new namespace.
- Kyverno checks this one too.
- Valid again, so the API server creates it.
- The GeneratingPolicy is saved.
- Kyverno's watch delivers both new policies.
- Kyverno registers a webhook for each: one that may change Pods, one for namespaces.
8. Changed on the way in
Some rules are better applied than demanded. The MutatingPolicy default-seccomp sets the seccomp profile RuntimeDefault, which blocks system calls that containers rarely need. Now web goes out with a proper tag, 1.5.0. At mutating admission Kyverno answers allowed, plus a patch, and the API server applies it. The patched object goes through the schema check and then to validating admission, so checks always see the final object. The second answer is yes, and it's saved.
- web goes out again, this time with a real version.
- First question to Kyverno: should this object change?
- Allowed, with a patch. The API server applies it.
- The patched object moves on through the schema check.
- Second question to Kyverno: is the final object acceptable?
- The tag is pinned. Yes.
- What's saved is your Deployment plus one field you didn't write.
9. No way around
The rollout starts, and the ReplicaSet controller creates Pods. Those are API requests as well, so each one takes the same road and the API server asks Kyverno about it. This time there's nothing to change and nothing to refuse. The rule holds no matter who asks: a controller, a CI job or you. Someone who skips the Deployment and starts a Pod by hand, with no tag at all, gets the same refusal.
- The rollout needs a Pod, so the ReplicaSet controller asks for one.
- The API server asks Kyverno about this Pod, like any other object.
- Yes to both, and the rolling update replaces the Pods.
- Someone tries a bare Pod, with no tag on the image.
- Same rule, same refusal.
10. Created for you
The third kind of policy creates things. The GeneratingPolicy default-deny puts a NetworkPolicy in every new namespace, one that blocks incoming traffic until someone allows it. You create the namespace team-a. Kyverno answers the webhook with a plain yes and notes the work to be done in an UpdateRequest. The background controller picks it up and creates the NetworkPolicy through the API, with its own permissions. With synchronize on, deleting it only brings it back.
- You create a namespace for a team.
- The API server tells Kyverno, and gets a plain yes. The creating comes later.
- Kyverno writes down what has to be done.
- The background controller sees the note and evaluates the policy.
- It creates the NetworkPolicy like any other client would.
- Someone deletes it.
- Kyverno puts it back.
11. Kyverno is down
Kyverno's only admission Pod runs out of memory and is killed. Then a web Pod is deleted, and the ReplicaSet controller asks for a replacement. The API server calls the webhook and gets no answer. failurePolicy is Fail, so it refuses the Pod: a rule that can't be checked counts as broken. Everything already running keeps running, but nothing the policies cover can be created. Kyverno's own namespace is exempt, so it can always come back. Run three replicas, and losing one stops nothing.
- The admission Pod runs out of memory. Nobody is behind kyverno-svc now.
- You delete a web Pod. The webhook only covers creates and updates, so that works.
- The ReplicaSet controller asks for a replacement.
- The API server calls Kyverno. Connection refused.
- So the Pod is refused. web carries on with two.
- The kubelet restarts the container. That needs no admission at all.
- The controller's next try gets a yes, and web is back to three.
- You give Kyverno three admission replicas. Any one of them can answer.
Summary
Kyverno is a set of ordinary Pods that the API server consults on the way to etcd. Mutating policies change an object first, validating policies judge the result, and generating policies follow up with objects of their own. Because the check sits in the API server's path, it holds for people and controllers alike. The price is the same fact seen from the other side: if Kyverno can't answer, the request can't go through. Start with Audit, read the reports, then Deny, and run three replicas. But all of this sees only API requests: what a program does inside its container is out of its sight, and that is where the next lesson goes.
Next lesson: Quotas and defaults for a tenant