Skip to content

feat: Add CEL-based conditional function execution (#4388)#4391

Open
SurbhiAgarwal1 wants to merge 1 commit intokptdev:mainfrom
SurbhiAgarwal1:feat/cel-conditional-execution
Open

feat: Add CEL-based conditional function execution (#4388)#4391
SurbhiAgarwal1 wants to merge 1 commit intokptdev:mainfrom
SurbhiAgarwal1:feat/cel-conditional-execution

Conversation

@SurbhiAgarwal1
Copy link

Implements #4388

Changes

  • Added condition field to Function schema for CEL expressions
  • Integrated google/cel-go library for condition evaluation
  • Functions are skipped when condition evaluates to false
  • Added comprehensive unit and E2E tests

Example Usage

pipeline:
  mutators:
  - image: gcr.io/kpt-fn/set-namespace:v0.4
    condition: "resources.exists(r, r.kind == 'ConfigMap' && r.metadata.name == 'env-config')"
    configMap:
      namespace: production

Copilot AI review requested due to automatic review settings February 14, 2026 17:56
@netlify
Copy link

netlify bot commented Feb 14, 2026

Deploy Preview for kptdocs ready!

Name Link
🔨 Latest commit 3d2bde5
🔍 Latest deploy log https://app.netlify.com/projects/kptdocs/deploys/699197bc2b908e0008177490
😎 Deploy Preview https://deploy-preview-4391--kptdocs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Signed-off-by: Surbhi <agarwalsurbhi1807@gmail.com>
@SurbhiAgarwal1 SurbhiAgarwal1 force-pushed the feat/cel-conditional-execution branch from 32fe3c0 to 3d2bde5 Compare February 15, 2026 09:54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to post these files on the issue rather than on the PR? It looks like AI generated content.

Copy link
Contributor

@nagygergo nagygergo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, good to see a new contributor.

Some general comments:

  1. Clean up AI instructions.
  2. Add e2e tests.
  3. Add documentation.

Some specific comments are inline.


// NewCELEvaluator creates a new CEL evaluator with the standard environment
func NewCELEvaluator() (*CELEvaluator, error) {
env, err := cel.NewEnv(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a totally unprotected cel executor. There should be limitations on the number of CPU cycles it can consume, the amount of characters it can output, the max complexity of the ast.

return NewFunctionRunner(ctx, fltr, pkgPath, fnResult, fnResults, opts)

// Initialize CEL evaluator if a condition is specified
var evaluator *CELEvaluator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to create a new CEL env for each function evaluation? The env can be the same.

pr := printer.FromContextOrDie(fr.ctx)

// Check condition before executing function
if fr.condition != "" && fr.evaluator != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why check if fr.evaluator exists or not. If the function runner was created with a condition appearing for it's Runner, then must have an evaluator. It's ok to run to a panic if it doesn't exist at this point.

"github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

Copy link
Contributor

@nagygergo nagygergo Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a testcase that makes sure that the cel functions can't mutate the resourcelist that is the input. The function signature can allow for it, as it hands over the *yaml.RNode list.


// Create function runner with condition
fnResult := &fnresult.Result{}
fnResults := &fnresult.ResultList{}
Copy link
Contributor

@nagygergo nagygergo Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these needed for testware when initialising it?

// NewCELEvaluator creates a new CEL evaluator with the standard environment
func NewCELEvaluator() (*CELEvaluator, error) {
env, err := cel.NewEnv(
cel.Variable("resources", cel.ListType(cel.DynType)),
Copy link
Contributor

@nagygergo nagygergo Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably advanced strings libraries would be good to include. https://pkg.go.dev/github.com/google/cel-go/ext#Strings

}

// Evaluate the expression
out, _, err := prg.Eval(map[string]interface{}{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a context passed to this to protect against long-hanging operations.

}

// Convert resources to a format suitable for CEL
resourceList, err := e.resourcesToList(resources)
Copy link
Contributor

@nagygergo nagygergo Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is serialising all the yaml.RNode actually needed? As it's a map[string]any type anyways (with no strange subtypes), probably the CEL interpreter can deal with it directly. Serialising the whole package for the cel execution, then not reusing it can cause a significant memory footprint bloat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/fn-runtime KRM function runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants