Skip to content

Commit

Permalink
Fixup volumes
Browse files Browse the repository at this point in the history
* Do not use /tmp for script outputs
* Allow mounting own volume with use_volume_for_outputs
* Fix examples using volumes incorrectly and simplify where possible
* Fix passing artifacts as argument (remove path), and simplify examples

Signed-off-by: Elliot Gunton <egunton@bloomberg.net>
  • Loading branch information
elliotgunton committed Sep 18, 2023
1 parent bb3c7c0 commit 91196d8
Show file tree
Hide file tree
Showing 30 changed files with 570 additions and 56 deletions.
1 change: 0 additions & 1 deletion docs/examples/workflows/artifact.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ task, consumer, takes this artifact, places it at its own `/file` path, and prin
artifacts:
- from: '{{tasks.writer.outputs.artifacts.out-art}}'
name: in-art
path: /tmp/file
depends: writer
name: consumer
template: consumer
Expand Down
1 change: 0 additions & 1 deletion docs/examples/workflows/artifact_with_fanout.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
artifacts:
- from: '{{tasks.writer.outputs.artifacts.out-art}}'
name: in-art
path: /tmp/file
depends: writer
name: fanout
template: fanout
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Http
# Http



Expand Down
145 changes: 145 additions & 0 deletions docs/examples/workflows/script_annotations_artifact_custom_volume.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Script Annotations Artifact Custom Volume



This example will reuse the outputs volume across script steps.


=== "Hera"

```python linenums="1"
from hera.workflows.artifact import ArtifactLoader
from hera.workflows.volume import Volume

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore


from hera.shared import global_config
from hera.workflows import (
Artifact,
Parameter,
RunnerScriptConstructor,
Steps,
Workflow,
models as m,
script,
)

global_config.experimental_features["script_annotations"] = True
global_config.experimental_features["script_runner"] = True

vol = Volume(name="my-vol", size="1Gi")


@script(
constructor=RunnerScriptConstructor(use_volume_for_outputs=vol.name),
volume_mounts=[m.VolumeMount(name="my-vol", mount_path="/mnt/here")],
)
def output_artifact(
a_number: Annotated[int, Parameter(name="a_number")],
) -> Annotated[int, Artifact(name="successor_out")]:
return a_number + 1


@script(
constructor=RunnerScriptConstructor(use_volume_for_outputs=vol.name),
volume_mounts=[m.VolumeMount(name="my-vol", mount_path="/mnt/here")],
)
def use_artifact(successor_in: Annotated[int, Artifact(name="successor_in", loader=ArtifactLoader.json)]):
print(successor_in)


with Workflow(
generate_name="test-output-annotations-",
entrypoint="my-steps",
volumes=[vol],
) as w:
with Steps(name="my-steps") as s:
out = output_artifact(arguments={"a_number": 3})
use_artifact(arguments=[out.get_artifact("successor_out").as_name("successor_in")])
```

=== "YAML"

```yaml linenums="1"
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: test-output-annotations-
spec:
entrypoint: my-steps
templates:
- name: my-steps
steps:
- - arguments:
parameters:
- name: a_number
value: '3'
name: output-artifact
template: output-artifact
- - arguments:
artifacts:
- from: '{{steps.output-artifact.outputs.artifacts.successor_out}}'
name: successor_in
name: use-artifact
template: use-artifact
- inputs:
parameters:
- name: a_number
name: output-artifact
outputs:
artifacts:
- name: successor_out
path: /hera/outputs/artifacts/successor_out
script:
args:
- -m
- hera.workflows.runner
- -e
- examples.workflows.script_annotations_artifact_custom_volume:output_artifact
command:
- python
env:
- name: hera__script_annotations
value: ''
- name: hera__outputs_directory
value: /hera/outputs
image: python:3.8
source: '{{inputs.parameters}}'
volumeMounts:
- mountPath: /mnt/here
name: my-vol
- inputs:
artifacts:
- name: successor_in
name: use-artifact
script:
args:
- -m
- hera.workflows.runner
- -e
- examples.workflows.script_annotations_artifact_custom_volume:use_artifact
command:
- python
env:
- name: hera__script_annotations
value: ''
image: python:3.8
source: '{{inputs.parameters}}'
volumeMounts:
- mountPath: /mnt/here
name: my-vol
volumeClaimTemplates:
- metadata:
name: my-vol
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
```

125 changes: 125 additions & 0 deletions docs/examples/workflows/script_annotations_artifact_passing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Script Annotations Artifact Passing



This example will reuse the outputs volume across script steps.


=== "Hera"

```python linenums="1"
from hera.workflows.volume import Volume

try:
from typing import Annotated # type: ignore
except ImportError:
from typing_extensions import Annotated # type: ignore


from hera.shared import global_config
from hera.workflows import (
Artifact,
Parameter,
Steps,
Workflow,
models as m,
script,
)

global_config.experimental_features["script_annotations"] = True
global_config.experimental_features["script_runner"] = True


@script(constructor="runner")
def output_artifact(
a_number: Annotated[int, Parameter(name="a_number")],
) -> Annotated[int, Artifact(name="successor_out")]:
return a_number + 1


@script(constructor="runner")
def use_artifact(successor_in: Annotated[int, Artifact(name="successor_in")]):
print(successor_in)


with Workflow(
generate_name="annotations-artifact-passing",
entrypoint="my-steps",
) as w:
with Steps(name="my-steps") as s:
out = output_artifact(arguments={"a_number": 3})
use_artifact(arguments=[out.get_artifact("successor_out").as_name("successor_in")])
```

=== "YAML"

```yaml linenums="1"
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: annotations-artifact-passing
spec:
entrypoint: my-steps
templates:
- name: my-steps
steps:
- - arguments:
parameters:
- name: a_number
value: '3'
name: output-artifact
template: output-artifact
- - arguments:
artifacts:
- from: '{{steps.output-artifact.outputs.artifacts.successor_out}}'
name: successor_in
name: use-artifact
template: use-artifact
- inputs:
parameters:
- name: a_number
name: output-artifact
outputs:
artifacts:
- name: successor_out
path: /hera/outputs/artifacts/successor_out
script:
args:
- -m
- hera.workflows.runner
- -e
- examples.workflows.script_annotations_artifact_passing:output_artifact
command:
- python
env:
- name: hera__script_annotations
value: ''
- name: hera__outputs_directory
value: /hera/outputs
image: python:3.8
source: '{{inputs.parameters}}'
volumeMounts:
- mountPath: /hera/outputs
name: hera-outputs-directory
volumes:
- emptyDir: {}
name: hera-outputs-directory
- inputs:
artifacts:
- name: successor_in
name: use-artifact
script:
args:
- -m
- hera.workflows.runner
- -e
- examples.workflows.script_annotations_artifact_passing:use_artifact
command:
- python
env:
- name: hera__script_annotations
value: ''
image: python:3.8
source: '{{inputs.parameters}}'
```

14 changes: 7 additions & 7 deletions docs/examples/workflows/script_annotations_outputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
global_config.experimental_features["script_annotations"] = True
global_config.experimental_features["script_runner"] = True

global_config.set_class_defaults(RunnerScriptConstructor, outputs_directory="user/chosen/outputs")
global_config.set_class_defaults(RunnerScriptConstructor, outputs_directory="/user/chosen/outputs")


@script(constructor="runner")
Expand Down Expand Up @@ -69,16 +69,16 @@
outputs:
artifacts:
- name: successor2
path: user/chosen/outputs/artifacts/successor2
path: /user/chosen/outputs/artifacts/successor2
- name: successor4
path: user/chosen/outputs/artifacts/successor4
path: /user/chosen/outputs/artifacts/successor4
parameters:
- name: successor
valueFrom:
path: user/chosen/outputs/parameters/successor
path: /user/chosen/outputs/parameters/successor
- name: successor3
valueFrom:
path: user/chosen/outputs/parameters/successor3
path: /user/chosen/outputs/parameters/successor3
script:
args:
- -m
Expand All @@ -91,11 +91,11 @@
- name: hera__script_annotations
value: ''
- name: hera__outputs_directory
value: user/chosen/outputs
value: /user/chosen/outputs
image: python:3.8
source: '{{inputs.parameters}}'
volumeMounts:
- mountPath: user/chosen/outputs
- mountPath: /user/chosen/outputs
name: hera-outputs-directory
volumes:
- emptyDir: {}
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/workflows/script_artifact_passing.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

with Workflow(generate_name="artifact-passing-", entrypoint="artifact-example") as w:
with Steps(name="artifact-example") as s:
whalesay(name="generate-artifact")
whale_step = whalesay(name="generate-artifact")
print_message(
name="consume-artifact",
arguments=[Artifact(name="message", from_="{{steps.generate-artifact.outputs.artifacts.hello-art}}")],
arguments=whale_step.get_artifact("hello-art").as_name("message"),
)
```

Expand Down
4 changes: 2 additions & 2 deletions docs/examples/workflows/upstream/artifact_passing.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ The upstream example can be [found here](https://github.com/argoproj/argo-workfl
)

with Steps(name="artifact-example") as s:
Step(name="generate-artifact", template=whalesay)
gen_step = Step(name="generate-artifact", template=whalesay)
Step(
name="consume-artifact",
template=print_message,
arguments=[Artifact(name="message", from_="{{steps.generate-artifact.outputs.artifacts.hello-art}}")],
arguments=gen_step.get_artifact("hello-art").as_name("message"),
)
```

Expand Down
3 changes: 2 additions & 1 deletion docs/examples/workflows/upstream/volumes_emptydir.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ The upstream example can be [found here](https://github.com/argoproj/argo-workfl
from hera.workflows import (
Container,
Workflow,
EmptyDirVolume,
models as m,
)

with Workflow(
generate_name="volumes-emptydir-",
entrypoint="volumes-emptydir-example",
volumes=[m.Volume(name="workdir", empty_dir=m.EmptyDirVolumeSource())],
volumes=[EmptyDirVolume(name="workdir")],
) as w:
empty_dir = Container(
name="volumes-emptydir-example",
Expand Down
6 changes: 3 additions & 3 deletions docs/examples/workflows/volume_mounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
generate_name="volumes-",
entrypoint="d",
volumes=[
Volume(name="v1", mount_path="/mnt/v1", size="1Gi"),
Volume(name="v2", mount_path="/mnt/v2", size="3Gi"),
Volume(name="v3", mount_path="/mnt/v3", size="5Gi"),
Volume(name="v1", size="1Gi"),
Volume(name="v2", size="3Gi"),
Volume(name="v3", size="5Gi"),
],
) as w:
with DAG(name="d"):
Expand Down
Loading

0 comments on commit 91196d8

Please sign in to comment.