Skip to main content
Security & multi-tenancyLesson 7 of 13

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.

RequestDeployment web
validationActions—
Result—
Existing objects—
Inside the API serverDeveloperkubectl applyAPI serverAuthN · AuthZAdmission policyCEL rulesPolicy bindingScope · actionsParamsparamKindPolicy webhookKyverno · GatekeeperetcdStored objectsOld DeploymentCreated earlierPolicy reportBackground scanCreate Deployment
RequestResponse / reportWatchAppears in a later step

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 runsInside the API serverSeparate Pods, called over HTTPS
If it failsNo extra server to fail; an expression error follows the policy's failurePolicyTimeout, then failurePolicy Fail (reject) or Ignore (skip)
LatencyIn process, no network hopOne network call per matching request, up to timeoutSeconds
Can mutateNo (MutatingAdmissionPolicy is a separate, newer kind)Yes
Existing objectsNot checkedBackground scan or audit reports
ExtrasParameters via paramKindImage signature checks, generating resources, external data
Best forSimple field rules: labels, image tags, limitsRules 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

  1. You created a ValidatingAdmissionPolicy that requires a memory limit, but Deployments without one are still accepted. What should you check first?
  2. The policy webhook's Pods crash and its Service has no endpoints. The webhook has failurePolicy: Fail and matches all Pods. What happens?
  3. You switch a no-:latest rule to Deny. A Deployment created last month with a :latest image is still running. What happens to it?

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