Skip to main content
Validation is the API-server gatekeeper for writes to the cluster. In this lesson we add two WebApp validation rules at that gate using a ValidatingAdmissionPolicy backed by CEL expressions:
  • Deny images that use the floating :latest tag.
  • Deny explicit replica counts outside the supported range (1–10). The replicas field is optional for the WebApp API, so omission remains allowed.
These checks are implemented in two pieces:
  1. A ValidatingAdmissionPolicy that describes which resources to match and contains the CEL expressions.
  2. A ValidatingAdmissionPolicyBinding that attaches the policy and selects the enforcement action. Here the enforcement action is Deny, so any failed expression blocks the write before the object is stored.
A policy without a binding is like a rule written down but not posted at the door — it won’t be enforced until it is bound.

What this policy enforces

  • Image rule: prevents images that explicitly end with :latest. The expression allows the image field to be omitted, or if present requires it not to end with :latest.
  • Replicas rule: allows omitting replicas, or if present requires its value to be an integer between 1 and 10 inclusive.
Summary table

ValidatingAdmissionPolicy

This resource matches webapps.webapp.kodekloud.com/v1 on CREATE and UPDATE and contains the two CEL validations shown below:
Notes:
  • failurePolicy: Fail means the admission request is rejected on policy evaluation errors.
  • The CEL has() check lets the field be optional; only present values are evaluated against the constraint.
Ensure your cluster supports ValidatingAdmissionPolicy resources in admissionregistration.k8s.io/v1 before applying these manifests. Cluster feature availability can vary by Kubernetes distribution and version.

ValidatingAdmissionPolicyBinding

Bind the policy and set enforcement to Deny so failed validations block writes:

Apply the policy and confirm

Apply the policy and binding, then verify both exist:
These commands confirm both halves are in place and the binding is active.

Test resources

Use the following example manifests to validate policy behavior.
  1. Test: image with :latest should be rejected
The denial happens at the API server before any controller reconciles the object.
  1. Test: replicas outside allowed range should be rejected
  1. Test: valid WebApp should be accepted
This demonstrates the policy blocks unsafe cases while allowing valid objects.

Watch Video