Skip to content

Commit b6f3d8e

Browse files
docs: add condition field documentation for CEL-based conditional function execution
- Add condition section to book/04-using-functions with examples and CEL patterns - Update kptfile schema reference to include the condition field Signed-off-by: SurbhiAgarwal1 <agarwalsurbhi1807@gmail.com>
1 parent 19bf57c commit b6f3d8e

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

documentation/content/en/book/04-using-functions/_index.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,68 @@ will merge each function pipeline list as an associative list, using
375375
`name` as the merge key. An unspecified `name` or duplicated names may
376376
result in unexpected merges.
377377

378+
### Specifying `condition`
379+
380+
The `condition` field lets you skip a function based on the current state of the resources in the package.
381+
It takes a [CEL](https://cel.dev/) expression that is evaluated against the resource list. If the expression
382+
returns `true`, the function runs. If it returns `false`, the function is skipped.
383+
384+
The expression receives a variable called `resources`, which is a list of all KRM resources passed to
385+
this function step (after `selectors` and `exclude` have been applied). Each resource is a map with
386+
the standard fields: `apiVersion`, `kind`, `metadata`, `spec`, `status`.
387+
388+
For example, only run the `set-labels` function if a `ConfigMap` named `app-config` exists in the package:
389+
390+
```yaml
391+
# wordpress/Kptfile (Excerpt)
392+
apiVersion: kpt.dev/v1
393+
kind: Kptfile
394+
metadata:
395+
name: wordpress
396+
pipeline:
397+
mutators:
398+
- image: ghcr.io/kptdev/krm-functions-catalog/set-labels:latest
399+
configMap:
400+
app: wordpress
401+
condition: resources.exists(r, r.kind == 'ConfigMap' && r.metadata.name == 'app-config')
402+
```
403+
404+
When you render the package, kpt shows whether the function ran or was skipped:
405+
406+
```shell
407+
$ kpt fn render wordpress
408+
Package "wordpress":
409+
410+
[RUNNING] "ghcr.io/kptdev/krm-functions-catalog/set-labels:latest"
411+
[PASS] "ghcr.io/kptdev/krm-functions-catalog/set-labels:latest"
412+
413+
Successfully executed 1 function(s) in 1 package(s).
414+
```
415+
416+
If the condition is not met:
417+
418+
```shell
419+
$ kpt fn render wordpress
420+
Package "wordpress":
421+
422+
[SKIPPED] "ghcr.io/kptdev/krm-functions-catalog/set-labels:latest" (condition not met)
423+
424+
Successfully executed 1 function(s) in 1 package(s).
425+
```
426+
427+
Some useful CEL expression patterns:
428+
429+
- Check if a resource of a specific kind exists:
430+
`resources.exists(r, r.kind == 'Deployment')`
431+
- Check if a specific resource exists by name:
432+
`resources.exists(r, r.kind == 'ConfigMap' && r.metadata.name == 'my-config')`
433+
- Check the count of resources:
434+
`resources.filter(r, r.kind == 'Deployment').size() > 0`
435+
436+
The `condition` field can be combined with `selectors` and `exclude`. The condition is evaluated
437+
after selectors and exclusions are applied, so `resources` only contains the resources that
438+
passed the selection criteria.
439+
378440
### Specifying `selectors`
379441

380442
In some cases, you want to invoke the function only on a subset of resources based on a

documentation/content/en/reference/schema/kptfile/kptfile.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ definitions:
7171
this is primarily used for merging function declaration with upstream counterparts
7272
type: string
7373
x-go-name: Name
74+
condition:
75+
description: |-
76+
`Condition` is an optional CEL expression that determines whether this
77+
function should be executed. The expression is evaluated against the list
78+
of KRM resources passed to this function step (after `Selectors` and
79+
`Exclude` have been applied) and should return a boolean value.
80+
If omitted or evaluates to true, the function executes normally.
81+
If evaluates to false, the function is skipped.
82+
type: string
83+
x-go-name: Condition
7484
selectors:
7585
description: |-
7686
`Selectors` are used to specify resources on which the function should be executed

0 commit comments

Comments
 (0)