CEL Playground
repository·main·Indexed 41 days ago
https://github.com/undistro/cel-playgroundA WebAssembly-powered interactive environment for experimenting with Common Expression Language (CEL) expressions. It provides tools to evaluate standard CEL and mimic Kubernetes apiserver capabilities, including Validating Admission Policy (VAP) and Webhook modes.
What's inside CEL Playground
- CEL Playground is an interactive environment powered by WebAssembly (Wasm) designed for exploring and experimenting with the Common Expression Language (CEL). It provides a user-friendly interface to write and evaluate CEL expressions directly in the browser.
Available CEL libraries in CEL Playground
mainCEL Playground compiles Go code to WebAssembly and includes several pre-configured libraries. Depending on the mode used, you have access to:
Standard CEL Libraries
- CEL extended string function library
- Kubernetes list library
- Kubernetes regex library
- Kubernetes URL library
- Kubernetes semver library
Kubernetes Policy Modes (Validating Admission Policy and Web Hooks)
When using Kubernetes policy modes, the environment matches what a Kubernetes apiserver exposes, adding the following:
- IP library
- CIDR library
- format library
- Two-variable comprehensions
- CEL list extension (
.sort())
For detailed environment configuration, refer to
eval/eval.go(for CEL mode) andk8s/cel.go(for Kubernetes modes).Build and serve CEL Playground
mainIf you are developing on CEL Playground, use the following commands to build the Wasm binary and serve the static files:
- Build the Wasm binary:
make build- Serve the static files:
make servemake build make serveInitialize the Go WebAssembly runtime
mainThe CEL Playground relies on a compressed WebAssembly (
.wasm) binary to run the CEL engine in the browser. The application automatically handles the fetching, decompression (usingpako), and instantiation of the WASM module.Requirements for manual implementation/integration:
- The WASM file must be available at
assets/main.wasm.gz. - The
Goclass (fromwasm_exec.js) must be available in the global scope. pakomust be available for ungzipping the asset.- A polyfill for
WebAssembly.instantiateStreamingis provided for older browsers like Microsoft Edge 17/18.
// The application performs this lifecycle automatically: const go = new Go(); const buffer = pako.ungzip(await (await fetch("assets/main.wasm.gz")).arrayBuffer()); WebAssembly.instantiate(buffer, go.importObject) .then((result) => { go.run(result.instance); // WASM is now ready to use });- The WASM file must be available at
Execute CEL expressions in the playground
mainThe playground allows you to evaluate Common Expression Language (CEL) expressions using a WebAssembly-powered Go backend. Evaluation is triggered by clicking the 'Run' button or by pressing
Ctrl+Enter(orCmd+Enteron macOS).When an expression is run:
- The current mode (e.g., a specific CEL implementation or configuration) is retrieved via
getCurrentMode(). - The expression and its associated values are passed to an
eval(modeId, values)function. - If the evaluation results in an error, the error message is displayed in red in the output area.
- If successful, the output is parsed. If the result contains a
resultkey, it is stringified and displayed. Otherwise, the output is rendered using accordion components to show structured data. - If the result object contains a
costfield, it is displayed usingsetCost().
// Trigger evaluation via UI // Click the element with id='run' // OR use keyboard shortcut: Ctrl/Cmd + Enter- The current mode (e.g., a specific CEL implementation or configuration) is retrieved via
Use the `eval` function in JavaScript via WebAssembly
mainWhen running the
cel-playgroundWebAssembly module, a globalevalfunction is exposed to the JavaScript environment. This function allows you to execute CEL (Common Expression Language) evaluations or Kubernetes-related policy checks (VAP and Webhooks) directly from JavaScript.Function Signature
eval(mode, argMap)mode(string): The evaluation mode to use. Supported values are:"cel": Standard CEL evaluation."vap": Kubernetes Validating Admission Policy evaluation."webhooks": Kubernetes Webhook evaluation.
argMap(Object): A JavaScript object containing the necessary input data for the selected mode.
Return Value
Returns a JavaScript object with the following shape:
{ "output": "string", "isError": boolean }If an error occurs,
isErrorwill betrueandoutputwill contain the error message string.// Example: Standard CEL evaluation const result = eval("cel", { cel: "request.time < timestamp('2023-01-01T00:00:00Z')", dataInput: "{ 'request': { 'time': '2022-01-01T00:00:00Z' } }" }); if (result.isError) { console.error("Error:", result.output); } else { console.log("Result:", result.output); }Configure arguments for `eval` modes
mainThe
argMapobject passed to theevalfunction must contain specific keys depending on themodeselected.Mode:
celRequires:
cel: The CEL expression string.dataInput: The input data for the expression.
Mode:
vap(Validating Admission Policy)Requires:
vap: The VAP policy string.dataOldObject: The existing object data.dataObject: The new object data.dataNamespace: The namespace string.dataRequest: The admission request data.dataAuthorizer: The authorizer information.
Mode:
webhooksRequires:
webhooks: The webhook configuration/policy string.dataOldObject: The existing object data.dataObject: The new object data.dataRequest: The admission request data.dataAuthorizer: The authorizer information.
// VAP Example eval("vap", { vap: "...policy text...", dataOldObject: "...", dataObject: "...", dataNamespace: "...", dataRequest: "...", dataAuthorizer: "..." }); // Webhook Example eval("webhooks", { webhooks: "...webhook text...", dataOldObject: "...", dataObject: "...", dataRequest: "...", dataAuthorizer: "..." });Configure the CEL Playground server flags
mainThe server accepts the following command-line flags:
-listen: The network address to listen on (e.g.,:8080or127.0.0.1:9000). Defaults to:8080.-dir: The local directory containing the files to be served via the HTTP file server. Defaults to.(the current directory).
-listen string (default ":8080") - listen address -dir string (default ".") - directory to serveRun the CEL Playground server
mainThe CEL Playground server is a simple HTTP file server used to serve the playground application. You can run it using the
cmd/server/main.goentrypoint, specifying the network address to listen on and the directory containing the static files to serve.go run cmd/server/main.go -listen :8080 -dir ./path/to/playground