All guides

ValidatingAdmissionPolicy: admission rules without a webhook

ValidatingAdmissionPolicy is a declarative, in-process alternative to validating admission webhooks. The rule is a CEL expression that the API server evaluates. The Kubernetes project recommends CEL-based admission control when possible.

Lines of code on a screen, coloured by syntax

Rules inside the API server

A validating admission policy is declarative and runs in-process, and it is an alternative to a validating admission webhook. Their validation rules are declared in the Common Expression Language (CEL). The feature is stable since Kubernetes v1.30, and the documented examples use apiVersion: admissionregistration.k8s.io/v1.

A policy is made up of as many as three resources:

  • A ValidatingAdmissionPolicy describes the abstract logic of the policy.
  • A ValidatingAdmissionPolicyBinding links the resources together and provides scoping.
  • A parameter resource provides information that makes the policy a concrete statement. Its schema comes from a native type, ConfigMap for example, or from a CRD.

A policy takes effect only when a policy and a binding that corresponds to it are both defined.

One policy example

This is the basic example from the Kubernetes documentation:

YAML

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: "demo-policy.example.com"
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups:   ["apps"]
      apiVersions: ["v1"]
      operations:  ["CREATE", "UPDATE"]
      resources:   ["deployments"]
  validations:
    - expression: "object.spec.replicas <= 5"

spec.matchConstraints specifies which resources the policy validates. spec.validations contains the CEL expressions that validate the request.

failurePolicy defines how misconfigurations and expressions that evaluate to an error are handled. With Fail, the API request is rejected. With Ignore, the error is ignored and the request continues. The default is Fail.

An expression has access to these variables:

  • object is the object from the incoming request. It is null for DELETE requests.
  • oldObject is the existing object. It is null for CREATE requests.
  • request holds the attributes of the admission request.
  • params is the parameter resource referred to by the binding.
  • namespaceObject is the namespace that the incoming object belongs to.
  • authorizer performs authorization checks for the user who made the request.

The binding and validationActions

The documented binding for that policy:

YAML

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicyBinding
metadata:
  name: "demo-binding-test.example.com"
spec:
  policyName: "demo-policy.example.com"
  validationActions: [Deny]
  matchResources:
    namespaceSelector:
      matchLabels:
        environment: test

With both objects in place, a Deployment whose replicas do not satisfy the expression gets the documented error. It is one line, wrapped here for width:

Text

ValidatingAdmissionPolicy 'demo-policy.example.com' with binding
'demo-binding-test.example.com' denied request: failed expression:
object.spec.replicas <= 5

Each binding must specify one or more validationActions:

  • Deny: a validation failure results in a denied request.
  • Warn: the request client receives a warning about the validation failure.
  • Audit: the audit event for the API request records the validation failure, under the annotation validation.policy.admission.k8s.io/validation_failure.

Deny and Warn may not be used together. To warn clients and audit the failures at once, the documentation uses validationActions: [Warn, Audit].

An admission policy may have multiple bindings. A request is admitted only after it passes every evaluation.

Type checking and messages

When a policy is created or updated, the validation process parses its expressions and rejects the definition if it finds syntax errors. It then checks the referred variables for type errors against the types matched by spec.matchConstraints. The result is in status.typeChecking. An empty status.typeChecking means that no errors were detected.

The documentation lists the limits of type checking:

  • Wildcards are not checked.
  • At most 10 types may be matched.
  • It does not apply to CRDs.
  • It does not affect the policy's behavior. The policy continues to evaluate even if type checking detects errors.

For a more helpful denial, set spec.validations[i].messageExpression. It is a CEL expression that must evaluate to a string, and it takes precedence over the static message.

Parameters and match conditions

A policy names the kind of its parameter resource in spec.paramKind. A binding ties the policy to a particular parameter resource with paramRef. The documented example limits Deployment replicas with a ReplicaLimit custom resource: one binding gives the test environment a maximum of 3, and a second binding gives all other environments 100.

paramRef has a required field, parameterNotFoundAction. With Allow, the absence of matched parameters counts as a successful validation. With Deny, it is subject to the policy's failurePolicy.

matchConditions are CEL expressions for fine-grained request filtering. The resource is evaluated only when every one of them evaluates to true.

When a webhook engine is still needed

The Kubernetes documentation compares the mechanisms. Its recommendation for simpler logic is the built-in, CEL-based admission control, which spares you the overhead of a webhook server. For logic you want to declare or configure in an extensible way, its recommendation is webhook admission control. Its table gives enforcing complex policy logic as a use case for validating webhooks.

Changing a request is a separate kind, MutatingAdmissionPolicy. It is stable since Kubernetes v1.36.

Kyverno's documentation compares its ValidatingPolicy type with ValidatingAdmissionPolicy. Among the features it lists for ValidatingPolicy alone are external data, background scans, exceptions, reporting and auto-generation for pod controllers. The same page says Kyverno can generate a ValidatingAdmissionPolicy from a ValidatingPolicy for the API server to execute.

Checks before you rely on it

  • The documentation calls Validating Admission Policy part of the cluster control plane and says to write and deploy policies with great caution.
  • Confirm that the cluster runs v1.30 or later, the release in which the feature became stable.
  • Give every policy a binding. A policy has no effect without one.
  • Choose failurePolicy on purpose. Fail rejects requests when an expression errors, and Ignore lets them continue.
  • Control who can write policies and bindings. These kinds are exempt from admission policies created through the REST API.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides