Skip to content

feat(expressions): support $name shorthand for bindings.resolve() in CEL - #2769

Open
ivomarino wants to merge 1 commit into
kyverno:mainfrom
ivomarino:cel-binding-shorthand
Open

ivomarino wants to merge 1 commit into
kyverno:mainfrom
ivomarino:cel-binding-shorthand

Conversation

@ivomarino

Copy link
Copy Markdown

Fixes #2156.

CEL statements had to spell out bindings.resolve('name') in full every time a binding value was needed, which gets verbose fast. This adds a preprocessing step in parseExpressionRegex that rewrites a bare $name into the equivalent bindings.resolve('name') call before the statement reaches the CEL compiler.

The rewrite tracks single/double-quoted string literals (with backslash-escape handling) so a literal $ inside a string (e.g. "cost: $5") is left untouched - only a $ followed by a valid identifier outside of a string gets rewritten. Added test coverage in parse_test.go for the happy path, multiple bindings in one expression, string-literal edge cases, and a couple of no-op cases (non-CEL engines, already-explicit resolve calls, bare $ with nothing after it).

Scope, please read before reviewing: this only covers chainsaw's own binding/value templating layer (pkg/expressions.Parse, used by bindings: values, template: true fields, etc.) - not the assertion-tree key/value expressions used in assert/match checks, which compile through kyverno-json's own expression parser directly (v1alpha1.Check.Compile) and aren't touched here. I confirmed this split is real by running both paths end-to-end on a local build: the templating path picks up the shorthand correctly, the assert-tree path does not.

Since the original issue's own example (.exists(...) inside what reads like an assert expression) is likely the assert-tree case, this PR alone probably doesn't cover your exact use case - I wanted to land the self-contained piece first rather than sit on it, and I'm happy to look into the assert-tree/kyverno-json side too if that's wanted, though that may need a separate PR against kyverno-json itself.

…'name') in CEL

CEL statements had to spell out `bindings.resolve('name')` in full every
time a binding value was needed, which gets verbose fast in anything
non-trivial (kyverno#2156). Add a preprocessing step in parseExpressionRegex
that rewrites a bare `$name` into the equivalent bindings.resolve()
call before the statement reaches the CEL compiler.

The rewrite walks the statement tracking single/double-quoted string
literals (with backslash-escape handling) so a literal `$` inside a
string, e.g. "cost: $5", is left untouched - only `$` followed by a
valid identifier outside of a string gets rewritten.

Scope: this only covers chainsaw's own binding/value templating layer
(pkg/expressions.Parse, used by bindings: values, template: true
fields, etc.), not the assertion-tree key/value expressions used in
assert/match checks - those compile through kyverno-json's own
expression parser directly (v1alpha1.Check.Compile), which this PR
doesn't touch. Confirmed this split is real by running both paths
end-to-end: the templating path picks up the shorthand correctly, the
assert-tree path does not. If the assert-tree case (which is what
kyverno#2156's own example is really about) is wanted too, that likely needs
a change in kyverno-json's compiler rather than here - happy to look
into that separately if there's interest.

Signed-off-by: Ivo Marino <ivo.marino@gmail.com>
@ivomarino

Copy link
Copy Markdown
Author

Opened the companion fix for the assertion-tree side: kyverno/kyverno-json#597 - that one is what actually closes #2156 (verified end-to-end with both applied together). This PR still stands on its own for the value-templating case.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] cel use $ to replace bindings.resolve()

1 participant