-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(controller): JSON-unmarshal marshaled expression template before …
…evaluating (#6285) Signed-off-by: Michael Crenshaw <michael@crenshaw.dev>
- Loading branch information
1 parent
7bf825b
commit 2a2ecc9
Showing
8 changed files
with
270 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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.