> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# CEL Admission Policies

> Using CEL validating admission policies in Kubernetes to declaratively reject invalid or unsafe custom resource requests before persistence

Sometimes the safest reconcile loop is the one that never has to see a bad object. Validating admission policies let the API server reject unsafe requests before they are persisted, so your operator never has to reconcile invalid input.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/_cG0RWHUHqfjYKx0/images/Kubernetes-Operators/Validation-Webhooks-And-CEL-Policies/CEL-Admission-Policies/rejecting-bad-web-app-request-diagram.jpg?fit=max&auto=format&n=_cG0RWHUHqfjYKx0&q=85&s=2ea8de9eba9d152b3dc4865a6ede0709" alt="The image illustrates the process of rejecting a bad web app request at admission using a validating admission policy before it is stored, ensuring the reconcile loop never sees it." width="1920" height="1080" data-path="images/Kubernetes-Operators/Validation-Webhooks-And-CEL-Policies/CEL-Admission-Policies/rejecting-bad-web-app-request-diagram.jpg" />
</Frame>

Think of admission as the Kubernetes API server's front desk: every create or update request arrives there, gets checked, and only accepted objects proceed to storage and the reconciler. Validating admission policies express those checks using the Common Expression Language (CEL), a compact, safe expression language evaluated by the API server.

<Callout icon="lightbulb" color="#1CB2FE">
  CEL expressions are evaluated inside the API server: they are not compiled Go code and they do not call your operator. You can use CEL to write concise, declarative checks against the incoming object.
</Callout>

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/_cG0RWHUHqfjYKx0/images/Kubernetes-Operators/Validation-Webhooks-And-CEL-Policies/CEL-Admission-Policies/common-expression-language-rule-sheet-explanation.jpg?fit=max&auto=format&n=_cG0RWHUHqfjYKx0&q=85&s=aff188ff28455342e7d03a367891c1c9" alt="The image displays text explaining that the rule sheet uses the Common Expression Language (CEL) and indicates that it is not code, specifically not a Go function or operator call." width="1920" height="1080" data-path="images/Kubernetes-Operators/Validation-Webhooks-And-CEL-Policies/CEL-Admission-Policies/common-expression-language-rule-sheet-explanation.jpg" />
</Frame>

Key objects in a validating admission setup

* Policy: defines match criteria and the CEL expressions (rules) that must evaluate true.
* Binding: enables a policy and chooses the enforcement action (deny, warn, audit).

Keeping Policy and Binding separate allows you to author reusable policies and enable them selectively via bindings.

| Resource | Purpose                                                             | Example                                           |
| -------- | ------------------------------------------------------------------- | ------------------------------------------------- |
| Policy   | Defines which requests to match and the CEL expressions to evaluate | `validatingadmissionpolicy/webapp-rules`          |
| Binding  | Turns a Policy on and selects the enforcement action                | `validatingadmissionpolicybinding/webapp-binding` |

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/_cG0RWHUHqfjYKx0/images/Kubernetes-Operators/Validation-Webhooks-And-CEL-Policies/CEL-Admission-Policies/binding-component-policy-enforcement-deny.jpg?fit=max&auto=format&n=_cG0RWHUHqfjYKx0&q=85&s=d50ddf94852f12f5afb59320233ce16f" alt="The image illustrates a process where a &#x22;Binding&#x22; component with &#x22;Policy on&#x22; and &#x22;Enforcement: Deny&#x22; settings connects to a &#x22;Policy&#x22; component with rules and expressions." width="1920" height="1080" data-path="images/Kubernetes-Operators/Validation-Webhooks-And-CEL-Policies/CEL-Admission-Policies/binding-component-policy-enforcement-deny.jpg" />
</Frame>

Matching CRDs and referencing object fields

* For a custom resource like a webapp, match typically targets API group `webapp.cloudnative.space`, version `v1`, and kind `WebApp` (or `webapp` depending on your CRD).
* Inside a CEL expression, use the built-in `object` variable to read the incoming object (for example `object.spec.image` or `object.spec.replicas`).
* Use `has(...)` to test for optional fields before evaluating them.

Practical rule examples and recommended CEL expressions

| Rule           | Purpose                                                             | CEL expression                                                                                     |
| -------------- | ------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| Image pinning  | Disallow moving tags like `:latest` so deployments are reproducible | `cel\n!object.spec.image.endsWith(':latest')\n`                                                    |
| Replica limits | Allow omitted replicas, but if provided require 1..10               | `cel\n!has(object.spec.replicas) \|\| (object.spec.replicas >= 1 && object.spec.replicas <= 10)\n` |

Examples above show typical, object-local validations that CEL is designed for.

Two short CEL examples:

```cel theme={null}
# Disallow :latest tags
!object.spec.image.endsWith(':latest')
```

```cel theme={null}
# Allow missing replicas, otherwise require 1..10
!has(object.spec.replicas) || (object.spec.replicas >= 1 && object.spec.replicas <= 10)
```

Enforcement modes

* `deny`: blocks requests that fail the CEL check.
* `warn`: allows the request but surfaces a warning to clients.
* `audit`: records violations (useful for monitoring and rollout).

Start with `warn` or `audit` when introducing a new policy to observe cluster impact before switching to `deny`.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/_cG0RWHUHqfjYKx0/images/Kubernetes-Operators/Validation-Webhooks-And-CEL-Policies/CEL-Admission-Policies/softer-modes-warn-audit-description.jpg?fit=max&auto=format&n=_cG0RWHUHqfjYKx0&q=85&s=0551ba6a54e185b1fffa686c5dd3b0f9" alt="The image shows two boxes titled &#x22;Warn&#x22; and &#x22;Audit&#x22; with brief descriptions under a section called &#x22;Softer Modes: Warn and Audit.&#x22; There is also a note about observing violations before fully enforcing." width="1920" height="1080" data-path="images/Kubernetes-Operators/Validation-Webhooks-And-CEL-Policies/CEL-Admission-Policies/softer-modes-warn-audit-description.jpg" />
</Frame>

When to use CEL — and when not to
CEL is a strong fit for concise, object-local rules that can be expressed as pure predicates. It is not a general-purpose programming extension for the API server: do not attempt side effects, external network calls, or large workflows inside CEL.

<Callout icon="warning" color="#FF6B6B">
  Do not use CEL for checks that require external API calls, side effects, or complex business logic. For those cases, implement a custom validating webhook or another admission design that can perform network calls and richer processing.
</Callout>

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/_cG0RWHUHqfjYKx0/images/Kubernetes-Operators/Validation-Webhooks-And-CEL-Policies/CEL-Admission-Policies/cel-boundaries-good-fit-not-extension.jpg?fit=max&auto=format&n=_cG0RWHUHqfjYKx0&q=85&s=1ddbe654d6284972c050fa549939c729" alt="The image outlines the boundaries of CEL, indicating it's a &#x22;Good fit&#x22; for clear expressions and object-local checks, but &#x22;Not a programming extension&#x22; for tasks involving side effects, external calls, or large workflows." width="1920" height="1080" data-path="images/Kubernetes-Operators/Validation-Webhooks-And-CEL-Policies/CEL-Admission-Policies/cel-boundaries-good-fit-not-extension.jpg" />
</Frame>

Quick demo: apply a CEL-based validating admission policy for the webapp CRD

```bash theme={null}
$ kubectl apply -f webapp-policy.yaml
validatingadmissionpolicy/webapp-rules created
```

The objective is to have Kubernetes reject unsafe webapp requests before they are stored in the API server.

References and further reading

* CEL spec: [https://github.com/google/cel-spec](https://github.com/google/cel-spec)
* Kubernetes Admission Controllers overview: [https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/](https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/)
* ValidatingAdmissionPolicy docs: [https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/#validatingadmissionpolicy](https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/#validatingadmissionpolicy)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/kubernetes-operators/module/06ac03ac-518a-4bc6-b2f8-6ed63fcb26d5/lesson/33941c74-e52c-489e-a74d-e752fc4a54cf" />
</CardGroup>
