-
Notifications
You must be signed in to change notification settings - Fork 3.5k
fix(controller): JSON-unmarshal marshaled expression template before evaluating #6285
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
Merged
alexec
merged 27 commits into
argoproj:master
from
crenshaw-dev:fix-escaped-double-quotes
Aug 5, 2021
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
09eb3fc
fix escaped double quotes in expression templates
crenshaw-dev fe7bad8
expand on docs
crenshaw-dev 8d6138a
only curl if file doesn't already exist
crenshaw-dev 75c3155
more docs clarification
crenshaw-dev 5b658a5
handle escapes more generally
crenshaw-dev 7011bcc
reset import ordering, fix typos
crenshaw-dev 4224b0c
Merge remote-tracking branch 'origin/master' into fix-escaped-double-…
crenshaw-dev 6f156fe
Merge remote-tracking branch 'origin/master' into fix-escaped-double-…
crenshaw-dev 49dba00
test newlines, better handle errors, better test for backslashes
crenshaw-dev b4d0414
lint
crenshaw-dev 7b8713c
Merge remote-tracking branch 'origin/master' into fix-escaped-double-…
crenshaw-dev 5b27b9a
re-marshal the result of the template evaluation
crenshaw-dev c336eda
add test for expr output which contains double quotes
crenshaw-dev b45e6b9
Merge remote-tracking branch 'origin/master' into fix-escaped-double-…
crenshaw-dev 660dcbc
better tests
crenshaw-dev 89ccfd6
lint
crenshaw-dev 7b8ccb7
more tests
crenshaw-dev 074ddfd
add boolean test
crenshaw-dev 48ff38b
add another expression example
crenshaw-dev 4dcb442
json everything
crenshaw-dev 3c7ad5b
fix typo
crenshaw-dev 2f5d76f
Merge branch 'master' into fix-escaped-double-quotes
crenshaw-dev cf61b7e
fix some tests
crenshaw-dev 81c55e0
Merge branch 'fix-escaped-double-quotes' of https://github.com/mac941…
crenshaw-dev cb26c89
more jsonification before templating
crenshaw-dev b1bf03e
new fields
crenshaw-dev f64b644
Update examples/expression-reusing-verbose-snippets.yaml
crenshaw-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Expressions do not support variables. Rather than repeating verbose expressions to reuse output, you can map over the | ||
| # expression output and use its value which is aliased as `#`. Then you can place your "variables" in a JSON object to | ||
| # be used elsewhere. | ||
| apiVersion: argoproj.io/v1alpha1 | ||
| kind: Workflow | ||
| metadata: | ||
| generateName: expression-reusing-verbose-snippets- | ||
| spec: | ||
| arguments: | ||
| parameters: | ||
| - name: weather | ||
| # The base64 string is this JSON: {"temps": [34, 27, 15, 57, 46]} | ||
| value: '{"weekWeather": "eyJ0ZW1wcyI6IFszNCwgMjcsIDE1LCA1NywgNDZdfQo="}' | ||
| entrypoint: main | ||
| templates: | ||
| - name: main | ||
| inputs: | ||
| parameters: | ||
| - name: week-temps | ||
| # The line being mapped over is verbose. Rather than repeat it, we use `map` to alias its output as #. | ||
| value: >- | ||
| {{= | ||
| map([ | ||
| jsonpath(sprig.b64dec(jsonpath(workflow.parameters.weather, '$.weekWeather')), '$.temps') | ||
| ], { | ||
| toJson({ | ||
| avg: sprig.add(#[0], #[1], #[2], #[3], #[4]) / 5, | ||
| min: sprig.min(#[0], #[1], #[2], #[3], #[4]), | ||
| max: sprig.max(#[0], #[1], #[2], #[3], #[4]) | ||
| }) | ||
| })[0] | ||
| }} | ||
| script: | ||
| env: | ||
| - name: AVG | ||
| value: "{{=jsonpath(inputs.parameters['week-temps'], '$.avg')}}" | ||
| - name: MIN | ||
| value: "{{=jsonpath(inputs.parameters['week-temps'], '$.min')}}" | ||
| - name: MAX | ||
| value: "{{=jsonpath(inputs.parameters['week-temps'], '$.max')}}" | ||
| image: debian:9.4 | ||
| command: [bash] | ||
| source: | | ||
| echo "The week's average temperature was $AVG with a minimum of $MIN and a maximum of $MAX." |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,29 @@ | ||
| package template | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| ) | ||
|
|
||
| // Replace takes a json-formatted string and performs variable replacement. | ||
| func Replace(s string, replaceMap map[string]string, allowUnresolved bool) (string, error) { | ||
| if !json.Valid([]byte(s)) { | ||
| return "", errors.New("cannot do template replacements with invalid JSON") | ||
| } | ||
|
|
||
| t, err := NewTemplate(s) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return t.Replace(replaceMap, allowUnresolved) | ||
|
|
||
| replacedString, err := t.Replace(replaceMap, allowUnresolved) | ||
| if err != nil { | ||
| return s, err | ||
| } | ||
|
|
||
| if !json.Valid([]byte(replacedString)) { | ||
| return s, errors.New("cannot finish template replacement because the result was invalid JSON") | ||
| } | ||
|
|
||
| return replacedString, nil | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to be very careful with expressions, I've seen many cases of patching specific issues, that just produce new issues
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, there's a lot going on here.
I would like to be more expressive with the change. Basically, it's "grab part of a JSON string, unescape it, evaluate it, and escape the result before re-inserting it in the string".
But the code is much more difficult to read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw, it looks like example workflows can be invoked as e2e tests... if I can figure out that system, I'll add some example workflows which test at least these bugs.