@@ -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
376376result 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
380442In some cases, you want to invoke the function only on a subset of resources based on a
0 commit comments