Skip to content

Instantly share code, notes, and snippets.

@dtaniwaki
Last active October 7, 2019 07:29
Show Gist options
  • Save dtaniwaki/c6ccf9e5d9ef04bdd4962c76429cf6db to your computer and use it in GitHub Desktop.
Save dtaniwaki/c6ccf9e5d9ef04bdd4962c76429cf6db to your computer and use it in GitHub Desktop.
# Example workflow to demonstrate parameter aggregation.
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
name: parameter-aggregation-template
spec:
templates:
# odd-or-even accepts a number and returns whether or not that number is odd or even
- name: odd-or-even
inputs:
parameters:
- name: num
container:
image: alpine:latest
command: [sh, -xc]
args:
- |
sleep 1 &&
echo {{inputs.parameters.num}} > /tmp/num &&
if [ $(({{inputs.parameters.num}}%2)) -eq 0 ]; then
echo "even" > /tmp/even;
else
echo "odd" > /tmp/even;
fi
outputs:
parameters:
- name: num
valueFrom:
path: /tmp/num
- name: evenness
valueFrom:
path: /tmp/even
# divide-by-2 divides a number in half
- name: divide-by-2
inputs:
parameters:
- name: num
script:
image: alpine:latest
command: [sh, -x]
source: |
#!/bin/sh
echo ready
sleep 30
echo $(({{inputs.parameters.num}}/2))
# Example workflow to demonstrate parameter aggregation.
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: parameter-aggregation-
spec:
entrypoint: parameter-aggregation
templates:
- name: parameter-aggregation
steps:
# First loop through a list of numbers and determine if the number is odd or even. This is
# captured in output parameter 'evenness'. (e.g. {"num": "4", "evenness": "even"})
- - name: odd-or-even
templateRef:
name: parameter-aggregation-template
template: odd-or-even
arguments:
parameters:
- name: num
value: "{{item}}"
withItems: [1, 2, 3, 4]
# Next, for each even number, divide it by two (using a script template).
# Skip odd numbers using a `when` clause.
- - name: divide-by-2
templateRef:
name: parameter-aggregation-template
template: divide-by-2
arguments:
parameters:
- name: num
value: "{{item.num}}"
withParam: "{{steps.odd-or-even.outputs.parameters}}"
when: "{{item.evenness}} == even"
# Finally, print all numbers processed in the previous step
- - name: print
template: whalesay
arguments:
parameters:
- name: message
value: "{{item}}"
withParam: "{{steps.divide-by-2.outputs.result}}"
# whalesay prints a number using whalesay
- name: whalesay
inputs:
parameters:
- name: message
container:
image: docker/whalesay:latest
command: [cowsay]
args: ["{{inputs.parameters.message}}"]
@dtaniwaki
Copy link
Author

$ argo template create parameter-aggregation-template.yaml
$ argo submit parameter-aggregation.yaml
# Wait for the workflow until divide-by-2 pods emit `ready`.
$ argo delete template parameter-aggregation-template
# Wait for 30 seconds
$ argo get parameter-aggregation-msfrr
Name:                parameter-aggregation-msfrr
Namespace:           default
ServiceAccount:      default
Status:              Error
Message:             workflow template parameter-aggregation-template not found
Created:             Mon Oct 07 14:35:34 +0900 (2 minutes ago)
Started:             Mon Oct 07 14:35:34 +0900 (2 minutes ago)
Finished:            Mon Oct 07 14:36:18 +0900 (1 minute ago)
Duration:            44 seconds

STEP                                                                                    PODNAME                                 DURATION  MESSAGE
 ⚠ parameter-aggregation-msfrr (parameter-aggregation)                                                                                    workflow template parameter-aggregation-template not found
 ├-·-✔ odd-or-even(0:1) (parameter-aggregation-template/odd-or-even)                    parameter-aggregation-msfrr-4106064204  15s
 | ├-✔ odd-or-even(1:2) (parameter-aggregation-template/odd-or-even)                    parameter-aggregation-msfrr-4201720470  18s
 | ├-✔ odd-or-even(2:3) (parameter-aggregation-template/odd-or-even)                    parameter-aggregation-msfrr-2072260708  20s
 | └-✔ odd-or-even(3:4) (parameter-aggregation-template/odd-or-even)                    parameter-aggregation-msfrr-2708307642  23s
 └-·-○ divide-by-2(0:evenness:odd,num:1) (parameter-aggregation-template/divide-by-2)                                                     when 'odd == even' evaluated false
   ├-● divide-by-2(1:evenness:even,num:2) (parameter-aggregation-template/divide-by-2)  parameter-aggregation-msfrr-4023672552  1m
   ├-○ divide-by-2(2:evenness:odd,num:3) (parameter-aggregation-template/divide-by-2)                                                     when 'odd == even' evaluated false
   └-● divide-by-2(3:evenness:even,num:4) (parameter-aggregation-template/divide-by-2)  parameter-aggregation-msfrr-2970660232  1m

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