Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Please clarify the use of double curly braces for jmesPath in external variables #1388

Open
jsalatiel opened this issue Oct 15, 2024 · 7 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@jsalatiel
Copy link

Description

It would be really nice if the docs could clearly explain the difference/possibility of using double curly braces in jmespath for external variables.
I tried to create a policy that would set a variable based in one optional annotation or fallback to another annotation ( mandatory ) , in this case prefixed by some path.

The only way that worked ( thanks to @realshuting ) was to mix part of the request without double curly brace and part of it with double curly brace like this.

    - name: mountpath
      variable:
        jmesPath: request.object.metadata.annotations.optional || '/etc/ssl/{{request.object.metadata.annotations.mandatory"}}'

It would be nice to have a better understanding of that in the docs. Is that only for concatenation? What else?

Thanks!

Slack discussion

https://kubernetes.slack.com/archives/CLGR9BJU9/p1728944204996649

@jsalatiel jsalatiel added the enhancement New feature or request label Oct 15, 2024
Copy link

welcome bot commented Oct 15, 2024

Thanks for opening your first issue here! Be sure to follow the issue template!

@realshuting
Copy link
Member

Adding working samples, playground.

@realshuting
Copy link
Member

By default for a plain JMESPath variable, no double curly brackets are needed in variable.jmesPath, for example:

    - name: mountpath
      variable:
        jmesPath: request.object.metadata.annotations.optional

Nested variables need to be wrapped with {{}}, for example:

    - name: mountpath
      variable:
        jmesPath: request.object.metadata.annotations.optional || '/custom/string/{{request.object.metadata.annotations.mandatory"}}'

@realshuting realshuting added the good first issue Good for newcomers label Oct 15, 2024
@A-5ingh
Copy link
Contributor

A-5ingh commented Oct 25, 2024

/assign

@cerebro1
Copy link
Contributor

cerebro1 commented Dec 20, 2024

@realshuting Please /assign
Also quick question:
I was doing a bit of research on where this should go and i think this seems like the right place: https://kyverno.io/docs/writing-policies/jmespath/#basics

We already have a paragraph here mentioning the following:

JMESPath expressions in most places in Kyverno must be enclosed in double curly braces like {{request.namespace}}. If an expression is used as the value of a field and contains nothing else, the expression needs to be wrapped in quotes: appns: "{{request.namespace}}". If the value field contains other text outside of the expression, then it can be unquoted and treated as a string but this isn’t strictly required: message: The namespace name is {{request.namespace}}.

I think we can update the above text.

Or do you think it should go in Variables over here: https://kyverno.io/docs/writing-policies/variables/
Since we do not necessarily need {{}} to reference the plain JMESPath variable. We need it for the nested variables.

Or we can mention both places and add the example you have shared above in the Variables doc.

Let me know what you guys think.

@cerebro1
Copy link
Contributor

@JimBugwadia @realshuting
Please /assign this

@realshuting
Copy link
Member

Or do you think it should go in Variables over here: https://kyverno.io/docs/writing-policies/variables/
Since we do not necessarily need {{}} to reference the plain JMESPath variable. We need it for the nested variables.

This makes sense to me! Since we have jmesPath under each attribute of a rule context, we can clarify the same on the variables page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
Status: No status
Development

No branches or pull requests

4 participants