Skip to content

Commit

Permalink
Fix templateRef and clean up examples (#496)
Browse files Browse the repository at this point in the history
Signed-off-by: GitHub <[email protected]>
  • Loading branch information
elliotgunton authored Mar 21, 2023
1 parent 506e960 commit af27524
Show file tree
Hide file tree
Showing 28 changed files with 566 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Coinflip


> 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).


Expand All @@ -24,25 +24,27 @@ with Workflow(
"This is an example of coin flip defined as a sequence of conditional steps."
),
},
entrypoint="coinflip",
) as w:
heads = Container(
name="heads",
image="alpine:3.6",
command=["sh", "-c"],
args=["echo 'it was heads'"],
args=['echo "it was heads"'],
)
tails = Container(
name="tails",
image="alpine:3.6",
command=["sh", "-c"],
args=["echo 'it was tails'"],
args=['echo "it was tails"'],
)

flip_coin = Script(
name="flip-coin",
image="python:alpine3.6",
command=["python"],
source=flip_coin_func,
add_cwd_to_sys_path=False,
)

with Steps(name="coinflip") as s:
Expand All @@ -64,18 +66,19 @@ metadata:
a sequence of conditional steps.
generateName: coinflip-
spec:
entrypoint: coinflip
templates:
- container:
args:
- echo 'it was heads'
- echo "it was heads"
command:
- sh
- -c
image: alpine:3.6
name: heads
- container:
args:
- echo 'it was tails'
- echo "it was tails"
command:
- sh
- -c
Expand All @@ -86,13 +89,7 @@ spec:
command:
- python
image: python:alpine3.6
source: 'import os
import sys
sys.path.append(os.getcwd())
import random
source: 'import random
result = "heads" if random.randint(0, 1) == 0 else "tails"
Expand Down
8 changes: 0 additions & 8 deletions docs/examples/workflows/upstream/cron_workflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ from hera.workflows import Container, CronWorkflow
with CronWorkflow(
name="hello-world",
entrypoint="whalesay",
annotations={
"workflows.argoproj.io/description": ("This example demonstrates running a DAG with inline templates."),
"workflows.argoproj.io/version": ">= 3.2.0",
},
schedule="* * * * *",
timezone="America/Los_Angeles",
starting_deadline_seconds=0,
Expand All @@ -38,10 +34,6 @@ with CronWorkflow(
apiVersion: argoproj.io/v1alpha1
kind: CronWorkflow
metadata:
annotations:
workflows.argoproj.io/description: This example demonstrates running a DAG with
inline templates.
workflows.argoproj.io/version: '>= 3.2.0'
name: hello-world
spec:
concurrencyPolicy: Replace
Expand Down
46 changes: 46 additions & 0 deletions docs/examples/workflows/upstream/steps_inline_workflow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Steps Inline Workflow

> 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).


## Hera

```python
from hera.workflows import Container, Step, Steps, Workflow

container = Container(image="argoproj/argosay:v2")

with Workflow(
generate_name="steps-inline-",
entrypoint="main",
annotations={
"workflows.argoproj.io/description": ("This workflow demonstrates running a steps with inline templates."),
"workflows.argoproj.io/version": ">= 3.2.0",
},
) as w:
with Steps(name="main"):
Step(name="a", inline=container)
```

## YAML

```yaml
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
annotations:
workflows.argoproj.io/description: This workflow demonstrates running a steps
with inline templates.
workflows.argoproj.io/version: '>= 3.2.0'
generateName: steps-inline-
spec:
entrypoint: main
templates:
- name: main
steps:
- - inline:
container:
image: argoproj/argosay:v2
name: a
```
72 changes: 72 additions & 0 deletions docs/examples/workflows/upstream/workflow_template__dag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Workflow Template Dag

> 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).


## Hera

```python
from hera.workflows import DAG, Task, Workflow
from hera.workflows.models import TemplateRef

with Workflow(
generate_name="workflow-template-dag-diamond-",
entrypoint="diamond",
) as w:
whalesay_template_ref = TemplateRef(name="workflow-template-whalesay-template", template="whalesay-template")
inner_template_ref = TemplateRef(name="workflow-template-inner-dag", template="inner-diamond")
with DAG(name="diamond"):
A = Task(name="A", template_ref=whalesay_template_ref, arguments={"message": "A"})
B = Task(name="B", template_ref=whalesay_template_ref, arguments={"message": "B"})
C = Task(name="C", template_ref=inner_template_ref)
D = Task(name="D", template_ref=whalesay_template_ref, arguments={"message": "D"})

A >> [B, C] >> D
```

## YAML

```yaml
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: workflow-template-dag-diamond-
spec:
entrypoint: diamond
templates:
- dag:
tasks:
- arguments:
parameters:
- name: message
value: A
name: A
templateRef:
name: workflow-template-whalesay-template
template: whalesay-template
- arguments:
parameters:
- name: message
value: B
depends: A
name: B
templateRef:
name: workflow-template-whalesay-template
template: whalesay-template
- depends: A
name: C
templateRef:
name: workflow-template-inner-dag
template: inner-diamond
- arguments:
parameters:
- name: message
value: D
depends: B && C
name: D
templateRef:
name: workflow-template-whalesay-template
template: whalesay-template
name: diamond
```
49 changes: 49 additions & 0 deletions docs/examples/workflows/upstream/workflow_template__hello_world.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Workflow Template Hello World

> 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).


## Hera

```python
from hera.workflows import Step, Steps, Workflow
from hera.workflows.models import TemplateRef

with Workflow(
generate_name="workflow-template-hello-world-",
entrypoint="whalesay",
) as w:
whalesay_template_ref = TemplateRef(
name="workflow-template-whalesay-template",
template="whalesay-template",
)
with Steps(name="whalesay"):
Step(
name="call-whalesay-template",
template_ref=whalesay_template_ref,
arguments={"message": "hello world"},
)
```

## YAML

```yaml
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: workflow-template-hello-world-
spec:
entrypoint: whalesay
templates:
- name: whalesay
steps:
- - arguments:
parameters:
- name: message
value: hello world
name: call-whalesay-template
templateRef:
name: workflow-template-whalesay-template
template: whalesay-template
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Workflow Template Workflow Template Ref

> 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).


## Hera

```python
from hera.workflows import Workflow
from hera.workflows.models import WorkflowTemplateRef

wt_ref = WorkflowTemplateRef(name="workflow-template-submittable")

with Workflow(
generate_name="workflow-template-hello-world-",
workflow_template_ref=wt_ref,
) as w:
pass
```

## YAML

```yaml
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: workflow-template-hello-world-
spec:
workflowTemplateRef:
name: workflow-template-submittable
```
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@ def flip_coin_func() -> None:
"This is an example of coin flip defined as a sequence of conditional steps."
),
},
entrypoint="coinflip",
) as w:
heads = Container(
name="heads",
image="alpine:3.6",
command=["sh", "-c"],
args=["echo 'it was heads'"],
args=['echo "it was heads"'],
)
tails = Container(
name="tails",
image="alpine:3.6",
command=["sh", "-c"],
args=["echo 'it was tails'"],
args=['echo "it was tails"'],
)

flip_coin = Script(
name="flip-coin",
image="python:alpine3.6",
command=["python"],
source=flip_coin_func,
add_cwd_to_sys_path=False,
)

with Steps(name="coinflip") as s:
Expand Down
47 changes: 47 additions & 0 deletions examples/workflows/upstream/coinflip.upstream.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# The coinflip example combines the use of a script result,
# along with conditionals, to take a dynamic path in the
# workflow. In this example, depending on the result of the
# first step, 'flip-coin', the template will either run the
# 'heads' step or the 'tails' step.
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: coinflip-
annotations:
workflows.argoproj.io/description: |
This is an example of coin flip defined as a sequence of conditional steps.
You can also run it in Python: https://couler-proj.github.io/couler/examples/#coin-flip
spec:
entrypoint: coinflip
templates:
- name: coinflip
steps:
- - name: flip-coin
template: flip-coin
- - name: heads
template: heads
when: "{{steps.flip-coin.outputs.result}} == heads"
- name: tails
template: tails
when: "{{steps.flip-coin.outputs.result}} == tails"

- name: flip-coin
script:
image: python:alpine3.6
command: [python]
source: |
import random
result = "heads" if random.randint(0,1) == 0 else "tails"
print(result)
- name: heads
container:
image: alpine:3.6
command: [sh, -c]
args: ["echo \"it was heads\""]

- name: tails
container:
image: alpine:3.6
command: [sh, -c]
args: ["echo \"it was tails\""]
Loading

0 comments on commit af27524

Please sign in to comment.