Repository navigation
Conversation
…'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>
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 inparseExpressionRegexthat rewrites a bare$nameinto the equivalentbindings.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 inparse_test.gofor the happy path, multiple bindings in one expression, string-literal edge cases, and a couple of no-op cases (non-CEL engines, already-explicitresolvecalls, bare$with nothing after it).Scope, please read before reviewing: this only covers chainsaw's own binding/value templating layer (
pkg/expressions.Parse, used bybindings:values,template: truefields, etc.) - not the assertion-tree key/value expressions used inassert/matchchecks, 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.