Skip to content

Commit

Permalink
chore: incorporate review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Kumar Mallikarjuna <[email protected]>
  • Loading branch information
kumar-mallikarjuna committed Oct 14, 2024
1 parent c6a8c0e commit dd9625b
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions keps/0009-cel-assertions.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
kep-number: 9
short-desc: This KEP discusses adding CEL-based assertions to Kuttl
title: CEL Assertions
short-desc: This KEP discusses adding expression-based assertions to Kuttl
title: Expression-Based Assertions
authors:
- "@kumar-mallikarjuna"
owners:
Expand All @@ -12,12 +12,11 @@ last-updated: 2024-09-23
status: provisional
---

# CEL Assertions
# Expression-Based Assertions

## Summary

Add Common Expression Language (CEL) support for Kuttl assertions. This would extend Kuttl's ability to perform
assertions based on complex expressions that are currently not possible with the existing syntax.
Add expression-based assertions to Kuttl. This can be achieved by integrating support for languages such as Common Expression Language (CEL). This would extend Kuttl's ability to perform assertions based on complex expressions that are currently not possible with the existing syntax.

## Motivation

Expand Down Expand Up @@ -46,37 +45,36 @@ evaluation and more flexible assertions.
1. Reading non-Kubernetes resources for assertions.
2. Reading resources from multiple clusters for assertions.

> **Note:** These goals are not in the scope of the first development of this feature but should be targetted in its later phases.
## Proposal

### CRD Changes

```yaml
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
celAssert:
resources:
- apiVersion: apps/v1
resourceRefs:
- apiVersion: apps/v1
kind: Deployment
name: coredns
namespace: kube-system
id: resource1
- apiVersion: apps/v1
ref: coredns_deployment
- apiVersion: apps/v1
kind: Deployment
name: metrics-server
namespace: kube-system
id: resource2

# Success if any expression evaluates to true
any:
- expression: "resource1.status.readyReplicas > 1"
- expression: ...
- ...

# Success only if all expressions evaluate to true
all:
- expression: "resource2.status.readyReplicas > 0"
- expression: ...
- ...
ref: metrics_server
# Success if any expression evaluates to true
assertAny:
- celExpr: "coredns_deployment.status.readyReplicas > 1"
- celExpr: ...
- ...
# Success only if all expressions evaluate to true
assertAll:
- celExpr: "metrics_server.status.readyReplicas > 0"
- celExpr: ...
- ...
```
### Implementation
Expand All @@ -85,7 +83,7 @@ At a high level, we would use the [github.com/google/cel-go/cel](https://github.
```go
func EvaluateExpression(resources...*unstructured.Unstructured)(ref.Val, error) {
env, err: = cel.NewEnv(
env, err := cel.NewEnv(
cel.Variable("<id1>", cel.MapType(cel.StringType, cel.DynType)),
cel.Variable("<id2>", cel.MapType(cel.StringType, cel.DynType)),
...
Expand All @@ -94,17 +92,17 @@ func EvaluateExpression(resources...*unstructured.Unstructured)(ref.Val, error)
return fmt.Errorf("failed to create environment: %w", err)
}

ast, issues: = env.Compile(`<expression>`)
ast, issues := env.Compile(`<expression>`)
if issues != nil && issues.Err() != nil {
return fmt.Errorf("type-check error: %s", issues.Err())
}

prg, err: = env.Program(ast)
prg, err := env.Program(ast)
if err != nil {
return fmt.Errorf("program construction error: %w", err)
}

out, _, err: = prg.Eval(map[string] interface {} {
out, _, err := prg.Eval(map[string] interface {} {
"<id1>": resources[0].Object,
"<id2>": resources[1].Object,
...
Expand Down

0 comments on commit dd9625b

Please sign in to comment.