feat: Add CEL-based conditional function execution (#4388)#4469
feat: Add CEL-based conditional function execution (#4388)#4469SurbhiAgarwal1 wants to merge 3 commits intokptdev:mainfrom
Conversation
✅ Deploy Preview for kptdocs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
2037fa3 to
b9a1ab1
Compare
|
Comment from #4391: Closing this PR in favor of a clean rebase. The branch had accumulated 61 commits including upstream commits from other contributors, making it messy to review. All the feedback from this review has been addressed in the new PR which has a single clean commit: Thank you all for the thorough review! |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 31 out of 32 changed files in this pull request and generated 6 comments.
Comments suppressed due to low confidence (1)
thirdparty/kyaml/runfn/runfn.go:1
NewFunctionRunner’s error is discarded andSetConditionis called without validatingopts.CELEnvironment. This can both (a) mask construction failures and (b) silently ignore the condition at runtime whenCELEnvironmentis nil (sinceFilter()only evaluates conditions whencelEnv != nil). Handle theerrfromNewFunctionRunner, and ifr.Conditionis set, return an error whenopts.CELEnvironmentis nil (or makeSetConditionreturn an error and enforce it here).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 29 out of 30 changed files in this pull request and generated 4 comments.
Comments suppressed due to low confidence (1)
documentation/content/en/book/04-using-functions/_index.md:1
- Two doc mismatches with the implementation: (1) the CEL resource map normalization in
resourceToMapguaranteesapiVersion,kind, andmetadatakeys, but notspec/status—either update the docs or ensure those keys are present; (2) the skipped example says “Successfully executed 1 function(s)” but the new behavior and e2e expectations indicate skipped functions should not count as executed (so this should be0).
---
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| fr.fnResult.ExitCode = 0 | ||
| fr.fnResult.Skipped = true | ||
| fr.fnResults.Items = append(fr.fnResults.Items, *fr.fnResult) | ||
| // Return input unchanged - function is skipped | ||
| fr.skipped = true | ||
| return input, nil | ||
| } | ||
| } |
There was a problem hiding this comment.
fr.skipped (and fr.fnResult.Skipped) are only ever set to true and never reset in Filter(). If the same FunctionRunner instance is used for more than one Filter() call, WasSkipped() can remain true and fnResult.Skipped can leak across executions. Please reset fr.skipped and fr.fnResult.Skipped at the beginning of Filter() before evaluating the condition.
|
The tests are failing because we have merged the conditions and renederstatus to kpt recently. I thought that by deleting the The following `diff.patch files work for me on the tests locally on my machine: e2e/testdata/fn-render/condition/condition-met/.expected/diff.patch e2e/testdata/fn-render/condition/condition-not-met/.expected/diff.patch Can you try restoring the two Sorry for mucking with your PR 😢 |
abd89dd to
df1189f
Compare
|
Thank you @liamfallon! I've restored both diff.patch files with exactly the contents you provided in commit df1189f. The tests should pass now. Sorry for the confusion! |
df1189f to
ad63ed9
Compare
298cc5d to
6d4aed1
Compare
6d4aed1 to
26362cc
Compare
|
Hi @liamfallon, I've updated the diff.patch files again using the exact actual output from the CI logs. The tests are still failing with the same diff - it seems the expected diff.patch I have doesn't match what CI produces. The actual diff CI produces for condition-met is:
And for condition-not-met:
I've set the diff.patch files to exactly match this output. Could you check if there's something else causing the mismatch? The CI run is #8920 if you want to look at the full logs. |
Adds support for CEL expressions in Kptfile pipeline functions via a new 'condition' field. Functions with a condition are only executed if the CEL expression evaluates to true against the current resource list. - Add CELEnvironment in pkg/lib/runneroptions/celenv.go - Integrate condition check in FunctionRunner.Filter (runner.go) - Append skipped result to fnResults when condition is not met - Add 'condition' field to kptfile/v1 Function type - Update all callers of InitDefaults to also call InitCELEnvironment - Add e2e testdata for condition-met and condition-not-met cases - Add unit tests for CEL evaluation and condition checking - Update documentation: kptfile schema and book/04-using-functions - Fix go.mod: mark k8s.io/apiserver as direct dependency Resolves kptdev#4388 Signed-off-by: SurbhiAgarwal1 <agarwalsurbhi1807@gmail.com>
26362cc to
8daf88e
Compare
|
Hi @SurbhiAgarwal1 , Have a look at this test PR, I just cloned this PR across so that I could push changes directly. I think the tests are running on that one so if you copy the diff.patch files from that PR into this PR I hope it'll work: |
|
Hi @liamfallon, thank you so much for doing that! I've copied the diff.patch files from PR #4472 into this PR. The tests should pass now. Really appreciate you taking the time to debug this! |
e2e/testdata/fn-render/condition/condition-met/.expected/diff.patch
Outdated
Show resolved
Hide resolved
e2e/testdata/fn-render/condition/condition-met/.expected/diff.patch
Outdated
Show resolved
Hide resolved
|
@SurbhiAgarwal1 All tests passing now! The problems we experienced here are why it is generally not recommended to have text comparison in tests, but to fix all the tests in kpt would be a huge job. |
|
Got it, makes sense. Thanks! |
|
If you have time to look at the copilot comments that would be great. We can move to formally review and merge the PR then. |
Description
This PR adds CEL-based conditional function execution to kpt, implementing #4388.
A new optional
conditionfield is added to theFunctiontype in the Kptfile pipeline. When specified, the CEL expression is evaluated against the current list of KRM resources. If the expression returnsfalse, the function is skipped. If omitted or returnstrue, the function executes normally.Motivation
In many real-world scenarios, you want a pipeline function to run only when certain resources exist or meet specific criteria. Without this feature, users had to maintain separate Kptfiles or manually manage which functions run. The
conditionfield makes pipelines more dynamic and reduces the need for workarounds.Changes
conditionfield toFunctiontype inpkg/api/kptfile/v1/types.goCELEnvironmentinpkg/lib/runneroptions/celenv.gousinggoogle/cel-goFunctionRunner.Filter()ininternal/fnruntime/runner.go[SKIPPED]in CLI outputInitCELEnvironment()method toRunnerOptionsfor proper error handlingInitDefaults()to also callInitCELEnvironment()condition-metandcondition-not-metcasesExample Usage