Skip to content

Commit

Permalink
Fixup volumes (#780)
Browse files Browse the repository at this point in the history
* Still use /tmp for script outputs but allow mounting own volume with
`volume_for_outputs` passed to Runner constructor
* Fix examples using volumes incorrectly and simplify where possible

---------

Signed-off-by: Elliot Gunton <[email protected]>
  • Loading branch information
elliotgunton authored Sep 26, 2023
1 parent cd253fb commit 932ba29
Show file tree
Hide file tree
Showing 32 changed files with 965 additions and 60 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
229 changes: 229 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,229 @@
# 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,
EmptyDirVolume,
Parameter,
RunnerScriptConstructor,
Steps,
Workflow,
models as m,
script,
)

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


@script(
constructor=RunnerScriptConstructor(
outputs_directory="/mnt/empty/dir",
volume_for_outputs=EmptyDirVolume(name="my-empty-dir"),
),
)
def output_artifact_empty_dir(
a_number: Annotated[int, Parameter(name="a_number")],
) -> Annotated[int, Artifact(name="successor_out")]:
return a_number + 1


@script(
constructor=RunnerScriptConstructor(), # Has no outputs
)
def use_artifact(successor_in: Annotated[int, Artifact(name="successor_in", loader=ArtifactLoader.json)]):
print(successor_in)


@script(
constructor=RunnerScriptConstructor(outputs_directory="/mnt/here"),
volume_mounts=[
m.VolumeMount(name="my-vol", mount_path="/mnt/here")
], # Mounting volume created outside of this script
)
def output_artifact_existing_vol(
a_number: Annotated[int, Parameter(name="a_number")],
) -> Annotated[int, Artifact(name="successor_out")]:
return a_number + 1


@script(
constructor=RunnerScriptConstructor(), # no outputs
volume_mounts=[
m.VolumeMount(name="my-vol", mount_path="/mnt/here")
], # Mounting volume created outside of this script
)
def use_artifact_existing_vol(
successor_in: Annotated[
int, Artifact(name="successor_in", path="/mnt/here/artifacts/successor_out", loader=ArtifactLoader.json)
],
):
print(successor_in)


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

out_to_my_vol = output_artifact_existing_vol(arguments={"a_number": 3})
use_artifact_existing_vol()
```

=== "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-empty-dir
template: output-artifact-empty-dir
- - arguments:
artifacts:
- from: '{{steps.output-artifact-empty-dir.outputs.artifacts.successor_out}}'
name: successor_in
name: use-artifact
template: use-artifact
- - arguments:
parameters:
- name: a_number
value: '3'
name: output-artifact-existing-vol
template: output-artifact-existing-vol
- - name: use-artifact-existing-vol
template: use-artifact-existing-vol
- inputs:
parameters:
- name: a_number
name: output-artifact-empty-dir
outputs:
artifacts:
- name: successor_out
path: /mnt/empty/dir/artifacts/successor_out
script:
args:
- -m
- hera.workflows.runner
- -e
- examples.workflows.script_annotations_artifact_custom_volume:output_artifact_empty_dir
command:
- python
env:
- name: hera__script_annotations
value: ''
- name: hera__outputs_directory
value: /mnt/empty/dir
image: python:3.8
source: '{{inputs.parameters}}'
volumeMounts:
- mountPath: /mnt/empty/dir
name: my-empty-dir
volumes:
- emptyDir: {}
name: my-empty-dir
- 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}}'
- inputs:
parameters:
- name: a_number
name: output-artifact-existing-vol
outputs:
artifacts:
- name: successor_out
path: /mnt/here/artifacts/successor_out
script:
args:
- -m
- hera.workflows.runner
- -e
- examples.workflows.script_annotations_artifact_custom_volume:output_artifact_existing_vol
command:
- python
env:
- name: hera__script_annotations
value: ''
- name: hera__outputs_directory
value: /mnt/here
image: python:3.8
source: '{{inputs.parameters}}'
volumeMounts:
- mountPath: /mnt/here
name: my-vol
- inputs:
artifacts:
- name: successor_in
path: /mnt/here/artifacts/successor_out
name: use-artifact-existing-vol
script:
args:
- -m
- hera.workflows.runner
- -e
- examples.workflows.script_annotations_artifact_custom_volume:use_artifact_existing_vol
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
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Script Annotations Artifact Outputs Defaults



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,
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", loader=ArtifactLoader.json)]):
print(successor_in)


with Workflow(
generate_name="test-output-annotations-",
entrypoint="my-steps",
volumes=[Volume(name="my-vol", size="1Gi")],
) 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: /tmp/hera/outputs/artifacts/successor_out
script:
args:
- -m
- hera.workflows.runner
- -e
- examples.workflows.script_annotations_artifact_outputs_defaults:output_artifact
command:
- python
env:
- name: hera__script_annotations
value: ''
- name: hera__outputs_directory
value: /tmp/hera/outputs
image: python:3.8
source: '{{inputs.parameters}}'
- inputs:
artifacts:
- name: successor_in
name: use-artifact
script:
args:
- -m
- hera.workflows.runner
- -e
- examples.workflows.script_annotations_artifact_outputs_defaults:use_artifact
command:
- python
env:
- name: hera__script_annotations
value: ''
image: python:3.8
source: '{{inputs.parameters}}'
volumeClaimTemplates:
- metadata:
name: my-vol
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
```

Loading

0 comments on commit 932ba29

Please sign in to comment.