admissionregistration.k8s.io/v1, which is the Kubernetes API group that defines validating admission policies.
Using
failurePolicy: Fail ensures that if the policy cannot be evaluated, or a validation fails, the API server will block the matching request rather than allowing it to proceed silently.matchConstraints.resourceRules— scopes the policy so it only evaluates requests for thewebappsresource in thewebapp.kodekloud.com/v1API group duringCREATEandUPDATEoperations.validations— an array of CEL expressions that must evaluate to true for the request to be allowed. The incoming object is available in CEL asobject.- First validation:
- Expression:
!object.spec.image.endsWith(':latest')
This denies requests wherespec.imageends with:latest, enforcing pinned image tags.
- Expression:
- Second validation:
- Expression:
!has(object.spec.replicas) || (object.spec.replicas >= 1 && object.spec.replicas <= 10)
Becausereplicasis optional in the CRD, this expression allows missingreplicasbut requires present values to be between 1 and 10. messageExpressionconstructs a rejection message that embeds the actual invalid value for clearer errors.
- Expression:
- First validation:
Next, create a ValidatingAdmissionPolicyBinding to activate the policy. The binding attaches the policy to evaluation and sets the validation action to
Deny.
- Apply a WebApp that uses an unpinned image (
:latest):
- Apply a WebApp with an acceptable image but invalid replicas (50):
messageExpression.
- Apply a WebApp that satisfies both rules (pinned image and valid replicas):
- Good for centralized, fast checks on object-local properties: image tag pinning, numeric bounds, required labels/annotations, simple string patterns.
- Not intended for complex business logic that requires cross-resource checks or external calls — those belong in controllers or external admission webhooks.
- Validating Admission Policy API (admissionregistration.k8s.io)
- Kubernetes Admission Controllers
- Common Expression Language (CEL) — specification