kube-cel
Kubernetes CEL extension functions for Rust, built on top of cel.
Implements the Kubernetes-specific CEL libraries defined in k8s.io/apiserver/pkg/cel/library and cel-go/ext, enabling client-side evaluation of CRD validation rules.
Installation
[]
= "0.5"
= "0.13"
Usage
use ;
use register_all;
let mut ctx = default;
register_all;
// String functions
let result = compile
.unwrap.execute.unwrap;
// Quantity comparison
let result = compile
.unwrap.execute.unwrap;
// Semver
let result = compile
.unwrap.execute.unwrap;
CRD Validation Pipeline
With the validation feature, you can compile and evaluate x-kubernetes-validations CEL rules client-side — no API server required.
[]
= { = "0.5", = ["validation"] }
use Validator;
use json;
let schema = json!;
let object = json!;
let validator = new;
let errors = validator.validate;
assert_eq!;
assert_eq!;
assert_eq!;
The validator walks the schema tree, compiles rules at each node, and evaluates them with self bound to the corresponding object value. Transition rules (referencing oldSelf) are supported by passing old_object.
Schema-aware format support
Fields with format: "date-time" or format: "duration" in the schema are automatically converted to CEL Timestamp / Duration values, matching K8s API server behavior:
let schema = json!;
Invalid strings gracefully fall back to Value::String.
Field name escaping
JSON field names that are CEL reserved words or contain special characters are automatically escaped when converting to CEL map keys, matching K8s API server behavior:
| JSON field name | CEL access |
|---|---|
namespace |
self.__namespace__ |
foo-bar |
self.foo__dash__bar |
a.b |
self.a__dot__b |
x/y |
self.x__slash__y |
my_field |
self.my__field |
Schema defaults
Apply schema default values before validation, matching K8s API server behavior:
use apply_defaults;
let schema = json!;
let object = json!;
let defaulted = apply_defaults;
// defaulted = {"replicas": 3, "strategy": "RollingUpdate"}
Or use the convenience method:
let errors = new.validate_with_defaults;
Root-level variables
Bind CRD-level apiVersion, apiGroup, kind variables for root-level rules:
use ;
let root_ctx = RootContext ;
let errors = new.validate_with_context;
ValidatingAdmissionPolicy (VAP)
Evaluate ValidatingAdmissionPolicy CEL expressions client-side — no API server required. Supports all VAP variables except authorizer.
use ;
let evaluator = builder
.object
.request
.params
.build;
let results = evaluator.evaluate;
assert!;
For repeated evaluation, pre-compile expressions:
let compiled = evaluator.compile_expressions;
let results = evaluator.evaluate_compiled; // no re-parsing
Static Analysis
Catch CEL rule issues before deployment — variable scope violations and cost budget warnings:
use ;
use compile_schema;
let compiled = compile_schema;
let warnings = analyze_rule;
// Warns: "list field has no maxItems bound" (may exceed K8s 1M cost budget)
ScopeContext::CrdValidation catches admission-only variables (request, object, etc.) used in CRD rules. ScopeContext::AdmissionPolicy allows the full VAP variable set.
Supported Functions
Strings
charAt, indexOf, lastIndexOf, lowerAscii, upperAscii, replace, split, substring, trim, join, reverse, strings.quote
Lists
isSorted, sum, min, max, indexOf, lastIndexOf, slice, sort, flatten, reverse, distinct, first, last, lists.range
Sets
sets.contains, sets.equivalent, sets.intersects
Regex
find, findAll
URLs
url, isURL, getScheme, getHost, getHostname, getPort, getEscapedPath, getQuery
IP / CIDR
ip, isIP, isIPv4, isIPv6, ip.isCanonical, family, isLoopback, isUnspecified, isLinkLocalMulticast, isLinkLocalUnicast, isGlobalUnicast, <IP>.string(), cidr, isCIDR, isCIDRv4, isCIDRv6, containsIP, containsCIDR, prefixLength, masked, <CIDR>.ip(), <CIDR>.string()
Semver
semver, isSemver, major, minor, patch, isGreaterThan, isLessThan, compareTo
Quantity
quantity, isQuantity, isInteger, asInteger, asApproximateFloat, sign, add, sub, isGreaterThan, isLessThan, compareTo
Format
<string>.format(<list>) with verbs: %s, %d, %f, %e, %b, %o, %x, %X
Named Format Validation
format.dns1123Label, format.dns1123Subdomain, format.dns1035Label, format.dns1035LabelPrefix, format.dns1123LabelPrefix, format.dns1123SubdomainPrefix, format.qualifiedName, format.labelValue, format.uri, format.uuid, format.byte, format.date, format.datetime, format.named, validate
// Returns optional: none = valid, of([...errors]) = invalid
// K8s pattern: !format.<name>().validate(value).hasValue()
let result = compile
.unwrap.execute.unwrap;
// Value::Bool(true)
// Dynamic format lookup
let result = compile
.unwrap.execute.unwrap;
// Value::Bool(true)
Math
math.ceil, math.floor, math.round, math.trunc, math.abs, math.sign, math.sqrt, math.isInf, math.isNaN, math.isFinite, math.bitAnd, math.bitOr, math.bitXor, math.bitNot, math.bitShiftLeft, math.bitShiftRight, math.greatest, math.least
Encoders
base64.decode, base64.encode
JSONPatch
jsonpatch.escapeKey
// RFC 6901: ~ → ~0, / → ~1
let result = compile
.unwrap.execute.unwrap;
// Value::String("k8s.io~1my~0label")
Feature Flags
All features are enabled by default. Disable with default-features = false and pick what you need:
| Feature | Dependencies | Description |
|---|---|---|
strings |
- | String extension functions |
lists |
- | List extension functions |
sets |
- | Set operations |
regex_funcs |
regex |
Regex find/findAll |
urls |
url |
URL parsing and accessors |
ip |
ipnet |
IP/CIDR parsing and operations |
semver_funcs |
semver |
Semantic versioning |
format |
- | String formatting |
quantity |
- | Kubernetes resource quantities |
jsonpatch |
- | JSONPatch key escaping (RFC 6901) |
named_format |
- | Named format validation (format.dns1123Label(), etc.) |
math |
- | Math functions (math.ceil, math.abs, bitwise, etc.) |
encoders |
base64 |
Base64 encode/decode |
validation |
serde_json, serde, chrono |
CRD validation pipeline, VAP evaluation, static analysis, schema defaults |
Known Limitations
| Feature | Reason |
|---|---|
cel.bind(var, init, expr) |
CEL compiler macro — requires cel crate support |
<list>.sortBy(var, expr) |
Lambda evaluation — requires cel crate support |
TwoVarComprehensions (all(i,v,...), transformList, etc.) |
CEL compiler macro — K8s 1.33+ |
| Authz library | Requires API server connection — outside client library scope |
Related
- kube-rs - Rust Kubernetes client and controller runtime
- cel - Rust CEL interpreter
- Kubernetes CEL docs
License
Apache-2.0