Overview
CEL expressions are used in:- Resource selectors — Match resources to environments
- Policy selectors — Target policies to specific releases
- Relationship rules — Define how entities connect
Basic Syntax
Accessing Fields
Comparison Operators
| Operator | Description | Example |
|---|---|---|
== | Equal | resource.kind == "KubernetesCluster" |
!= | Not equal | resource.metadata["env"] != "dev" |
< | Less than | resource.metadata["priority"] < "5" |
> | Greater than | resource.metadata["replicas"] > "1" |
<= | Less or equal | version.metadata["build"] <= "100" |
>= | Greater or equal | resource.metadata["tier"] >= "2" |
Logical Operators
| Operator | Description | Example |
|---|---|---|
&& | Logical AND | a == "x" && b == "y" |
|| | Logical OR | a == "x" || a == "y" |
! | Logical NOT | !resource.metadata["deprecated"] |
Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
+ | Addition / Concatenation | "prefix-" + resource.name |
- | Subtraction | int(a) - int(b) |
* | Multiplication | int(a) * 2 |
/ | Division | int(a) / 2 |
% | Modulo | int(a) % 2 == 0 |
Available Variables
Resource Context
When writing resource selectors:| Variable | Type | Description |
|---|---|---|
resource.id | string | Unique resource ID |
resource.name | string | Resource display name |
resource.kind | string | Resource type (e.g., KubernetesCluster) |
resource.identifier | string | External identifier |
resource.version | string | Resource version |
resource.metadata | map | Key-value metadata |
resource.config | map | Resource configuration |
Environment Context
When writing environment selectors:| Variable | Type | Description |
|---|---|---|
environment.id | string | Environment ID |
environment.name | string | Environment name |
environment.metadata | map | Environment metadata |
Deployment Context
When writing deployment selectors:| Variable | Type | Description |
|---|---|---|
deployment.id | string | Deployment ID |
deployment.name | string | Deployment name |
deployment.metadata | map | Deployment metadata |
Version Context
When writing version selectors:| Variable | Type | Description |
|---|---|---|
version.id | string | Version ID |
version.tag | string | Version tag |
version.name | string | Version name |
version.metadata | map | Version metadata |
String Functions
contains
Check if a string contains a substring:startsWith
Check if a string starts with a prefix:endsWith
Check if a string ends with a suffix:matches
Regular expression matching:size
Get string length:toLowerCase / toUpperCase
Case conversion:List Operations
in
Check if a value is in a list:size
Get list length:exists
Check if any element matches:all
Check if all elements match:Map Operations
has
Check if a key exists in a map:Key Access
Access map values:Conditional Expressions
Ternary Operator
Null-Safe Access
Usehas() for optional fields:
Common Patterns
Production Resources
Multi-Region Targeting
Kind Filtering
Team-Based Selection
Exclude Deprecated
Critical Tier in Production
US Regions Only
Canary Resources
Complex Multi-Condition
Policy Selector Examples
Target Production Environment
Target Critical Deployments
Target Specific Environment + Deployment
Version Filtering
Relationship Rule Examples
Match by Region
Match by Account
Type Coercion
CEL is strongly typed. Use these functions to convert types:String to Integer
Integer to String
Type Checking
Error Handling
Safe Field Access
Always usehas() for optional fields to avoid runtime errors:
Default Values
Provide defaults for optional fields:Performance Tips
Prefer Simple Expressions
Short-Circuit Evaluation
CEL uses short-circuit evaluation. Put cheap checks first:Avoid Redundant Checks
Debugging
Test Expressions
Use the API to test your CEL expressions:Common Errors
| Error | Cause | Fix |
|---|---|---|
no such key | Accessing missing map key | Use has() check |
type mismatch | Comparing different types | Use type coercion |
syntax error | Invalid CEL syntax | Check quotes, brackets |
undeclared reference | Unknown variable | Check variable names |
Escaping Quotes
In YAML, escape quotes properly:Reference
Reserved Keywords
These words are reserved in CEL:true,false,nullinasbreak,const,continue,else,for,function,if,import,let,loop,package,namespace,return,var,void,while
Operator Precedence
From highest to lowest:()- Grouping.[]- Member access-!- Unary*/%- Multiplicative+-- Additive<<=>>=in- Relational==!=- Equality&&- Logical AND||- Logical OR?:- Conditional