Admission Policies with CEL and Webhooks
Pod Security Standards check only security fields. Most teams also have their own rules, such as "every Deployment has a team label", "no :latest images" and "every container has a memory limit". Follow how these rules are enforced at admission, first inside the API server with CEL, then with a webhook engine, and see what each one costs when it fails.
What you will learn
- A ValidatingAdmissionPolicy runs CEL inside the API server
- A policy does nothing until a binding gives it a scope and an action
- Roll out with Warn and Audit before Deny
- Webhook engines and what failurePolicy Fail or Ignore means when they are down
- Admission checks writes only, so old objects need a scan
CEL policy or webhook?
| ValidatingAdmissionPolicy (CEL) | Webhook engine (Kyverno, Gatekeeper) | |
|---|---|---|
| Where it runs | Inside the API server | Separate Pods, called over HTTPS |
| If it fails | No extra server to fail; an expression error follows the policy's failurePolicy | Timeout, then failurePolicy Fail (reject) or Ignore (skip) |
| Latency | In process, no network hop | One network call per matching request, up to timeoutSeconds |
| Can mutate | No (MutatingAdmissionPolicy is a separate, newer kind) | Yes |
| Existing objects | Not checked | Background scan or audit reports |
| Extras | Parameters via paramKind | Image signature checks, generating resources, external data |
| Best for | Simple field rules: labels, image tags, limits | Rules CEL cannot express, or when you need reports |
Example CEL expressions for a Deployment (object is the incoming object):
validations:
- expression: "has(object.metadata.labels) && 'team' in object.metadata.labels"
message: "metadata.labels.team is required"
- expression: "object.spec.template.spec.containers.all(c, !c.image.endsWith(':latest'))"
message: "Do not use the :latest tag; pin a version or a digest"
- expression: "object.spec.template.spec.containers.all(c, has(c.resources.limits) && has(c.resources.limits.memory))"
message: "Every container needs resources.limits.memory"
The :latest check is simplified: an image with no tag at all also means latest, and a registry port (registry:5000/app) contains a colon. A real rule should handle both.
Try it yourself
kubectl get validatingadmissionpolicies # Policies and their match rules
kubectl get validatingadmissionpolicybindings # Which policy, which action, which scope
kubectl apply -f policy.yaml -f binding.yaml # Policy with validationActions: [Warn, Audit] first
kubectl create deployment web --image=nginx:latest --dry-run=server # See the warning or denial without storing anything
kubectl get validatingwebhookconfigurations # Webhook engines registered on this cluster
kubectl get validatingwebhookconfigurations -o custom-columns=NAME:.metadata.name,POLICY:.webhooks[*].failurePolicy,TIMEOUT:.webhooks[*].timeoutSeconds
Check yourself
Further reading: Validating Admission Policy · Mutating Admission Policy · Dynamic Admission Control (webhooks) · CEL in Kubernetes · Kyverno · OPA Gatekeeper · How an API Request Gets Stored · Pod Security Standards and securityContext