You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Browse filesBrowse the repository at this point in the historyBrowse files
SurbhiAgarwal1
committed
feat: Add CEL-based conditional function execution (#4388)
Add CEL-based conditional function execution to kpt pipelines.
A new optional 'when' field is added to the Function type in the Kptfile pipeline. When specified, the CEL expression is evaluated against the current list of KRM resources. If the expression returns false, the function is skipped. If omitted or returns true, the function executes normally.
Changes:
- Added 'when' (CelCondition) field to Function type in api/kptfile/v1/types.go
- Added CELEnvironment in pkg/lib/runneroptions/celenv.go using google/cel-go
- Integrated condition check in FunctionRunner.Filter() in pkg/fn/runtime/runner.go
- Functions skipped due to condition show [SKIPPED] in CLI output
- Added 'when' and 'skipped' fields to PipelineStepResult for render status tracking
- CEL limits (CelCheckFrequency, CelCostLimit) are configurable on RunnerOptions
- Added InitCELEnvironment() method to RunnerOptions for proper error handling
- Updated all callers of InitDefaults() to also call InitCELEnvironment()
- Added E2E testdata for condition-met and condition-not-met cases
- Added unit tests for CEL evaluation covering builtin, exec, and container runtimes
- Updated documentation: kptfile schema reference and book/04-using-functions
- Windows test skips use runtime.GOOS == 'windows' check
Signed-off-by: SurbhiAgarwal1 <surbhi.agarwal@example.com>
Copy file name to clipboardExpand all lines: documentation/content/en/book/01-getting-started/_index.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,7 +43,7 @@ documents for [`kpt fn render`](../../reference/cli/fn/render/) and [`kpt fn eva
43
43
44
44
### Kubernetes cluster
45
45
46
-
To deploy the examples, you need a Kubernetes cluster and a configured kubectl context.
46
+
To deploy the examples, you need a Kubernetes cluster and a configured kubeconfig context.
47
47
48
48
For testing purposes, the [kind](https://kind.sigs.k8s.io/docs/user/quick-start/) tool is useful for running an ephemeral Kubernetes
49
49
cluster on your local host.
@@ -106,7 +106,7 @@ vim deployment.yaml
106
106
#### Automating one-time edits with functions
107
107
108
108
The [`kpt fn`](../../reference/cli/fn/) set of commands enables you to execute programs called _kpt functions_. These programs are
109
-
packaged as containers and take YAML files as input, mutate or validate them, and then output YAML.
109
+
packaged as containers and take in YAML files, mutate or validate them, and then output YAML.
110
110
111
111
For example, you can use a function (`ghcr.io/kptdev/krm-functions-catalog/search-replace:latest`) to search for and replace all the occurrences of the `app` key, in the `spec` section of the YAML document (`spec.**.app`), and set the value to `my-nginx`.
Copy file name to clipboardExpand all lines: documentation/content/en/book/04-using-functions/_index.md
+63Lines changed: 63 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -346,6 +346,69 @@ pipeline:
346
346
347
347
It is recommended to use unique function names for all the functions in the Kptfile function pipeline. If the `name` is specified, then the `kpt pkg update` will merge each function pipeline list as an associative list, using `name` as the merge key. An unspecified `name`, or duplicated names, may result in unexpected merges.
348
348
349
+
### Specifying `when`
350
+
351
+
The `when` field lets you skip a function based on the current state of the resources in the package.
352
+
It takes a [CEL](https://cel.dev/) expression that is evaluated against the resource list. If the expression
353
+
returns `true`, the function runs. If it returns `false`, the function is skipped.
354
+
355
+
The expression receives a variable called `resources`, which is a list of all KRM resources passed to
356
+
this function step (after `selectors` and `exclude` have been applied). Each resource is a map with
357
+
the standard fields `apiVersion`, `kind`, and `metadata`. Depending on the resource, fields such as
358
+
`spec`and `status` may also be present.
359
+
360
+
For example, only run the `set-labels` function if a `ConfigMap` named `app-config` exists in the package:
The `when` field can be combined with `selectors` and `exclude`. The condition is evaluated
409
+
after selectors and exclusions are applied, so `resources` only contains the resources that
410
+
passed the selection criteria.
411
+
349
412
### Specifying `selectors`
350
413
351
414
In some cases, it is necessary to invoke the function only on a subset of resources based on certain selection criteria. This can be accomplished using selectors. At a high level, the selectors work as follows:
0 commit comments