Skip to content

Commit af27524

Browse files
authored
Fix templateRef and clean up examples (#496)
Signed-off-by: GitHub <[email protected]>
1 parent 506e960 commit af27524

28 files changed

+566
-81
lines changed

docs/examples/workflows/coinflip.md renamed to docs/examples/workflows/upstream/coinflip.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Coinflip
22

3-
3+
> Note: This example is a replication of an Argo Workflow example in Hera. The upstream example can be [found here](https://github.com/argoproj/argo-workflows/blob/master/examples/coinflip.yaml).
44
55

66

@@ -24,25 +24,27 @@ with Workflow(
2424
"This is an example of coin flip defined as a sequence of conditional steps."
2525
),
2626
},
27+
entrypoint="coinflip",
2728
) as w:
2829
heads = Container(
2930
name="heads",
3031
image="alpine:3.6",
3132
command=["sh", "-c"],
32-
args=["echo 'it was heads'"],
33+
args=['echo "it was heads"'],
3334
)
3435
tails = Container(
3536
name="tails",
3637
image="alpine:3.6",
3738
command=["sh", "-c"],
38-
args=["echo 'it was tails'"],
39+
args=['echo "it was tails"'],
3940
)
4041

4142
flip_coin = Script(
4243
name="flip-coin",
4344
image="python:alpine3.6",
4445
command=["python"],
4546
source=flip_coin_func,
47+
add_cwd_to_sys_path=False,
4648
)
4749

4850
with Steps(name="coinflip") as s:
@@ -64,18 +66,19 @@ metadata:
6466
a sequence of conditional steps.
6567
generateName: coinflip-
6668
spec:
69+
entrypoint: coinflip
6770
templates:
6871
- container:
6972
args:
70-
- echo 'it was heads'
73+
- echo "it was heads"
7174
command:
7275
- sh
7376
- -c
7477
image: alpine:3.6
7578
name: heads
7679
- container:
7780
args:
78-
- echo 'it was tails'
81+
- echo "it was tails"
7982
command:
8083
- sh
8184
- -c
@@ -86,13 +89,7 @@ spec:
8689
command:
8790
- python
8891
image: python:alpine3.6
89-
source: 'import os
90-
91-
import sys
92-
93-
sys.path.append(os.getcwd())
94-
95-
import random
92+
source: 'import random
9693
9794
9895
result = "heads" if random.randint(0, 1) == 0 else "tails"

docs/examples/workflows/upstream/cron_workflow.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ from hera.workflows import Container, CronWorkflow
1212
with CronWorkflow(
1313
name="hello-world",
1414
entrypoint="whalesay",
15-
annotations={
16-
"workflows.argoproj.io/description": ("This example demonstrates running a DAG with inline templates."),
17-
"workflows.argoproj.io/version": ">= 3.2.0",
18-
},
1915
schedule="* * * * *",
2016
timezone="America/Los_Angeles",
2117
starting_deadline_seconds=0,
@@ -38,10 +34,6 @@ with CronWorkflow(
3834
apiVersion: argoproj.io/v1alpha1
3935
kind: CronWorkflow
4036
metadata:
41-
annotations:
42-
workflows.argoproj.io/description: This example demonstrates running a DAG with
43-
inline templates.
44-
workflows.argoproj.io/version: '>= 3.2.0'
4537
name: hello-world
4638
spec:
4739
concurrencyPolicy: Replace
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Steps Inline Workflow
2+
3+
> Note: This example is a replication of an Argo Workflow example in Hera. The upstream example can be [found here](https://github.com/argoproj/argo-workflows/blob/master/examples/steps-inline-workflow.yaml).
4+
5+
6+
7+
## Hera
8+
9+
```python
10+
from hera.workflows import Container, Step, Steps, Workflow
11+
12+
container = Container(image="argoproj/argosay:v2")
13+
14+
with Workflow(
15+
generate_name="steps-inline-",
16+
entrypoint="main",
17+
annotations={
18+
"workflows.argoproj.io/description": ("This workflow demonstrates running a steps with inline templates."),
19+
"workflows.argoproj.io/version": ">= 3.2.0",
20+
},
21+
) as w:
22+
with Steps(name="main"):
23+
Step(name="a", inline=container)
24+
```
25+
26+
## YAML
27+
28+
```yaml
29+
apiVersion: argoproj.io/v1alpha1
30+
kind: Workflow
31+
metadata:
32+
annotations:
33+
workflows.argoproj.io/description: This workflow demonstrates running a steps
34+
with inline templates.
35+
workflows.argoproj.io/version: '>= 3.2.0'
36+
generateName: steps-inline-
37+
spec:
38+
entrypoint: main
39+
templates:
40+
- name: main
41+
steps:
42+
- - inline:
43+
container:
44+
image: argoproj/argosay:v2
45+
name: a
46+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Workflow Template Dag
2+
3+
> Note: This example is a replication of an Argo Workflow example in Hera. The upstream example can be [found here](https://github.com/argoproj/argo-workflows/blob/master/examples/workflow-template/dag.yaml).
4+
5+
6+
7+
## Hera
8+
9+
```python
10+
from hera.workflows import DAG, Task, Workflow
11+
from hera.workflows.models import TemplateRef
12+
13+
with Workflow(
14+
generate_name="workflow-template-dag-diamond-",
15+
entrypoint="diamond",
16+
) as w:
17+
whalesay_template_ref = TemplateRef(name="workflow-template-whalesay-template", template="whalesay-template")
18+
inner_template_ref = TemplateRef(name="workflow-template-inner-dag", template="inner-diamond")
19+
with DAG(name="diamond"):
20+
A = Task(name="A", template_ref=whalesay_template_ref, arguments={"message": "A"})
21+
B = Task(name="B", template_ref=whalesay_template_ref, arguments={"message": "B"})
22+
C = Task(name="C", template_ref=inner_template_ref)
23+
D = Task(name="D", template_ref=whalesay_template_ref, arguments={"message": "D"})
24+
25+
A >> [B, C] >> D
26+
```
27+
28+
## YAML
29+
30+
```yaml
31+
apiVersion: argoproj.io/v1alpha1
32+
kind: Workflow
33+
metadata:
34+
generateName: workflow-template-dag-diamond-
35+
spec:
36+
entrypoint: diamond
37+
templates:
38+
- dag:
39+
tasks:
40+
- arguments:
41+
parameters:
42+
- name: message
43+
value: A
44+
name: A
45+
templateRef:
46+
name: workflow-template-whalesay-template
47+
template: whalesay-template
48+
- arguments:
49+
parameters:
50+
- name: message
51+
value: B
52+
depends: A
53+
name: B
54+
templateRef:
55+
name: workflow-template-whalesay-template
56+
template: whalesay-template
57+
- depends: A
58+
name: C
59+
templateRef:
60+
name: workflow-template-inner-dag
61+
template: inner-diamond
62+
- arguments:
63+
parameters:
64+
- name: message
65+
value: D
66+
depends: B && C
67+
name: D
68+
templateRef:
69+
name: workflow-template-whalesay-template
70+
template: whalesay-template
71+
name: diamond
72+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Workflow Template Hello World
2+
3+
> Note: This example is a replication of an Argo Workflow example in Hera. The upstream example can be [found here](https://github.com/argoproj/argo-workflows/blob/master/examples/workflow-template/hello-world.yaml).
4+
5+
6+
7+
## Hera
8+
9+
```python
10+
from hera.workflows import Step, Steps, Workflow
11+
from hera.workflows.models import TemplateRef
12+
13+
with Workflow(
14+
generate_name="workflow-template-hello-world-",
15+
entrypoint="whalesay",
16+
) as w:
17+
whalesay_template_ref = TemplateRef(
18+
name="workflow-template-whalesay-template",
19+
template="whalesay-template",
20+
)
21+
with Steps(name="whalesay"):
22+
Step(
23+
name="call-whalesay-template",
24+
template_ref=whalesay_template_ref,
25+
arguments={"message": "hello world"},
26+
)
27+
```
28+
29+
## YAML
30+
31+
```yaml
32+
apiVersion: argoproj.io/v1alpha1
33+
kind: Workflow
34+
metadata:
35+
generateName: workflow-template-hello-world-
36+
spec:
37+
entrypoint: whalesay
38+
templates:
39+
- name: whalesay
40+
steps:
41+
- - arguments:
42+
parameters:
43+
- name: message
44+
value: hello world
45+
name: call-whalesay-template
46+
templateRef:
47+
name: workflow-template-whalesay-template
48+
template: whalesay-template
49+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Workflow Template Workflow Template Ref
2+
3+
> Note: This example is a replication of an Argo Workflow example in Hera. The upstream example can be [found here](https://github.com/argoproj/argo-workflows/blob/master/examples/workflow-template/workflow-template-ref.yaml).
4+
5+
6+
7+
## Hera
8+
9+
```python
10+
from hera.workflows import Workflow
11+
from hera.workflows.models import WorkflowTemplateRef
12+
13+
wt_ref = WorkflowTemplateRef(name="workflow-template-submittable")
14+
15+
with Workflow(
16+
generate_name="workflow-template-hello-world-",
17+
workflow_template_ref=wt_ref,
18+
) as w:
19+
pass
20+
```
21+
22+
## YAML
23+
24+
```yaml
25+
apiVersion: argoproj.io/v1alpha1
26+
kind: Workflow
27+
metadata:
28+
generateName: workflow-template-hello-world-
29+
spec:
30+
workflowTemplateRef:
31+
name: workflow-template-submittable
32+
```

examples/workflows/coinflip.py renamed to examples/workflows/upstream/coinflip.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,27 @@ def flip_coin_func() -> None:
1515
"This is an example of coin flip defined as a sequence of conditional steps."
1616
),
1717
},
18+
entrypoint="coinflip",
1819
) as w:
1920
heads = Container(
2021
name="heads",
2122
image="alpine:3.6",
2223
command=["sh", "-c"],
23-
args=["echo 'it was heads'"],
24+
args=['echo "it was heads"'],
2425
)
2526
tails = Container(
2627
name="tails",
2728
image="alpine:3.6",
2829
command=["sh", "-c"],
29-
args=["echo 'it was tails'"],
30+
args=['echo "it was tails"'],
3031
)
3132

3233
flip_coin = Script(
3334
name="flip-coin",
3435
image="python:alpine3.6",
3536
command=["python"],
3637
source=flip_coin_func,
38+
add_cwd_to_sys_path=False,
3739
)
3840

3941
with Steps(name="coinflip") as s:
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# The coinflip example combines the use of a script result,
2+
# along with conditionals, to take a dynamic path in the
3+
# workflow. In this example, depending on the result of the
4+
# first step, 'flip-coin', the template will either run the
5+
# 'heads' step or the 'tails' step.
6+
apiVersion: argoproj.io/v1alpha1
7+
kind: Workflow
8+
metadata:
9+
generateName: coinflip-
10+
annotations:
11+
workflows.argoproj.io/description: |
12+
This is an example of coin flip defined as a sequence of conditional steps.
13+
You can also run it in Python: https://couler-proj.github.io/couler/examples/#coin-flip
14+
spec:
15+
entrypoint: coinflip
16+
templates:
17+
- name: coinflip
18+
steps:
19+
- - name: flip-coin
20+
template: flip-coin
21+
- - name: heads
22+
template: heads
23+
when: "{{steps.flip-coin.outputs.result}} == heads"
24+
- name: tails
25+
template: tails
26+
when: "{{steps.flip-coin.outputs.result}} == tails"
27+
28+
- name: flip-coin
29+
script:
30+
image: python:alpine3.6
31+
command: [python]
32+
source: |
33+
import random
34+
result = "heads" if random.randint(0,1) == 0 else "tails"
35+
print(result)
36+
37+
- name: heads
38+
container:
39+
image: alpine:3.6
40+
command: [sh, -c]
41+
args: ["echo \"it was heads\""]
42+
43+
- name: tails
44+
container:
45+
image: alpine:3.6
46+
command: [sh, -c]
47+
args: ["echo \"it was tails\""]

0 commit comments

Comments
 (0)