diff --git a/docs/examples/workflows/global_config.md b/docs/examples/workflows/global_config.md index 88a43c31d..a48e5fb3f 100644 --- a/docs/examples/workflows/global_config.md +++ b/docs/examples/workflows/global_config.md @@ -9,14 +9,22 @@ ```python linenums="1" from hera.shared import global_config - from hera.workflows import Container, Workflow + from hera.workflows import Container, Workflow, script global_config.api_version = "argoproj.io/v0beta9000" global_config.namespace = "argo-namespace" global_config.service_account_name = "argo-account" + global_config.image = "image-say" + + + @script() + def say(): + print("hello") + with Workflow(generate_name="global-config-", entrypoint="whalesay") as w: whalesay = Container(image="docker/whalesay:latest", command=["cowsay"]) + say() ``` === "YAML" @@ -35,5 +43,14 @@ command: - cowsay image: docker/whalesay:latest + - name: 'say' + script: + command: ['python'] + image: 'image-say' + source: | + import os + import sys + sys.path.append(os.getcwd()) + print("hello") ``` diff --git a/examples/workflows/global-config.yaml b/examples/workflows/global-config.yaml index ec67a8dfb..dd7f52e5e 100644 --- a/examples/workflows/global-config.yaml +++ b/examples/workflows/global-config.yaml @@ -11,3 +11,12 @@ spec: command: - cowsay image: docker/whalesay:latest + - name: 'say' + script: + command: ['python'] + image: 'image-say' + source: | + import os + import sys + sys.path.append(os.getcwd()) + print("hello") diff --git a/examples/workflows/global_config.py b/examples/workflows/global_config.py index c3b6a80b6..5d60eff61 100644 --- a/examples/workflows/global_config.py +++ b/examples/workflows/global_config.py @@ -1,9 +1,17 @@ from hera.shared import global_config -from hera.workflows import Container, Workflow +from hera.workflows import Container, Workflow, script global_config.api_version = "argoproj.io/v0beta9000" global_config.namespace = "argo-namespace" global_config.service_account_name = "argo-account" +global_config.image = "image-say" + + +@script() +def say(): + print("hello") + with Workflow(generate_name="global-config-", entrypoint="whalesay") as w: whalesay = Container(image="docker/whalesay:latest", command=["cowsay"]) + say() diff --git a/src/hera/workflows/_mixins.py b/src/hera/workflows/_mixins.py index 1be533d33..25c9426a7 100644 --- a/src/hera/workflows/_mixins.py +++ b/src/hera/workflows/_mixins.py @@ -3,7 +3,7 @@ import inspect from typing import Any, Callable, Dict, List, Optional, TypeVar, Union, cast -from pydantic import root_validator +from pydantic import root_validator, validator from hera.shared import global_config from hera.shared._base_model import BaseMixin @@ -124,7 +124,7 @@ def _add_sub(self, node: Any) -> Any: class ContainerMixin(BaseMixin): - image: str = global_config.image + image: Optional[str] = None image_pull_policy: Optional[Union[str, ImagePullPolicy]] = None liveness_probe: Optional[Probe] = None @@ -142,6 +142,12 @@ def _build_image_pull_policy(self) -> Optional[ImagePullPolicy]: return self.image_pull_policy return ImagePullPolicy[self.image_pull_policy.lower()] + @validator("image", pre=True, always=True) + def _set_image(cls, v): + if v is None: + return global_config.image + return v + class IOMixin(BaseMixin): inputs: InputsT = None