OnCallReadyStart learning

Security and secrets Lesson 23.25 · Kubernetes Security (CKS) · 4 min read

ValidatingAdmissionPolicy: admission rules in CEL, built in

In plain words

Before, the API server had to phone an outside expert, a webhook, for every rule it wanted checked, and if the expert was unreachable things got stuck. A ValidatingAdmissionPolicy is a rule card the API server keeps at its own desk and checks itself, written in a tiny language called CEL that only asks yes-or-no questions about the object. Simple and quick.

Policy without a webhook

Kyverno and OPA Gatekeeper are extra controllers that the apiserver calls over a webhook. Since Kubernetes 1.30 the apiserver can check simple rules itself: a ValidatingAdmissionPolicy holds rules written in CEL (the Common Expression Language), and a Binding says where they apply and what happens when one fails.

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: lab-registry-only
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups: [""]
      apiVersions: ["v1"]
      operations: ["CREATE", "UPDATE"]
      resources: ["pods"]
  validations:
  - expression: "object.spec.containers.all(c, c.image.startsWith('registry.lab/'))"
    message: "images must come from registry.lab"
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicyBinding
metadata:
  name: lab-registry-only-binding
spec:
  policyName: lab-registry-only
  validationActions: [Deny]          # Warn / Audit: report, do not block
  matchResources:
    namespaceSelector:
      matchLabels:
        policy: strict

CEL in two minutes. object is what is being created; oldObject the version before an update. object.spec.containers.all(c, ...) / .exists(c, ...) test every / any item; has(object.metadata .labels.team) checks a field exists - reading a missing field is an error, not false, so guard optional fields with has(). A failing request reads:

The pods "hub" is invalid: : ValidatingAdmissionPolicy 'lab-registry-only' with binding
'lab-registry-only-binding' denied request: images must come from registry.lab

With Warn the request goes through and kubectl prints Warning: Validation failed for ValidatingAdmissionPolicy '<policy>' with binding '<binding>': <message>; with Audit it goes through and the failure lands in the request's audit event, as the annotation validation.policy.admission.k8s.io/validation_failure. A binding may list several actions.

(simulator) a CEL subset (fields, comparisons, && || !, has, size, string methods, all / exists / filter / map, ?:); no params or matchConditions.

In an interview: "VAP for simple, fast rules in-process; Kyverno or Gatekeeper when you need mutation, image verification or rules across objects".

On the exam: domain "Minimize microservice vulnerabilities": write or fix a policy + binding.

Why it helps

Built-in policies need no extra controller, no webhook to keep alive and no certificates to rotate, so simple rules become cheap and reliable. CEL also appears in CRD validation and other Kubernetes features, so learning to read it pays off beyond this one feature, and the CKS exam expects you to write or fix a policy and its binding. It is quick to test.

FAQ

What does the Binding add?

Where the policy applies, through matchResources such as a namespaceSelector, and what a failure does: Deny blocks the request, Warn returns a warning, Audit only records it. A policy without a binding does nothing at all.

Why use has() in CEL?

Reading a field that does not exist is an error in CEL, not false. has(object.metadata.labels.team) tests whether the field exists, so the expression gives a clean false instead of an error when the label is missing.

What do all and exists do?

They test a condition on every item, or on at least one item, of a list. For example object.spec.containers.all(c, c.image.startsWith('registry.lab/')) is true only when every container uses the lab registry. Both stop early once the answer is known.

What does the denial look like?

The object is reported as invalid, with the policy name, the binding name and the validation's message, or the failed expression when no message was given. That makes it easy to find which rule rejected the request.

When would I still use Kyverno?

For mutation, image signature verification or rules that need to look at other objects. ValidatingAdmissionPolicy is for fast, simple validation of the object in the request, which covers most label, image and field rules. Many teams use both side by side.

In an interview Mid

How would you enforce that every Deployment has a team label without installing anything?

With a ValidatingAdmissionPolicy on apps deployments (CREATE, UPDATE) whose CEL is has(object.metadata.labels) && has(object.metadata.labels.team) && object.metadata.labels.team != '', plus a ValidatingAdmissionPolicyBinding with validationActions [Deny] and a namespaceSelector for the namespaces it should cover. Then prove it by creating a Deployment without the label and watching it fail.

Also asked: Why does a missing field matter in CEL? · What do Warn and Audit actions do? · When is a webhook-based policy engine still the better choice?

Practise this lesson in the terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.