Skip to content

Commit f2bf6e2

Browse files
Fix ineffective GlobalConfig image settings in script (#538)
Fixes image were not being applied in ContainerMixin and mixed in classes. Signed-off-by: Pierre Brunelle <[email protected]> Co-authored-by: Pierre Brunelle <[email protected]>
1 parent c2080f5 commit f2bf6e2

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

docs/examples/workflows/global_config.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@
99

1010
```python linenums="1"
1111
from hera.shared import global_config
12-
from hera.workflows import Container, Workflow
12+
from hera.workflows import Container, Workflow, script
1313

1414
global_config.api_version = "argoproj.io/v0beta9000"
1515
global_config.namespace = "argo-namespace"
1616
global_config.service_account_name = "argo-account"
17+
global_config.image = "image-say"
18+
19+
20+
@script()
21+
def say():
22+
print("hello")
23+
1724

1825
with Workflow(generate_name="global-config-", entrypoint="whalesay") as w:
1926
whalesay = Container(image="docker/whalesay:latest", command=["cowsay"])
27+
say()
2028
```
2129

2230
=== "YAML"
@@ -35,5 +43,14 @@
3543
command:
3644
- cowsay
3745
image: docker/whalesay:latest
46+
- name: 'say'
47+
script:
48+
command: ['python']
49+
image: 'image-say'
50+
source: |
51+
import os
52+
import sys
53+
sys.path.append(os.getcwd())
54+
print("hello")
3855
```
3956

examples/workflows/global-config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ spec:
1111
command:
1212
- cowsay
1313
image: docker/whalesay:latest
14+
- name: 'say'
15+
script:
16+
command: ['python']
17+
image: 'image-say'
18+
source: |
19+
import os
20+
import sys
21+
sys.path.append(os.getcwd())
22+
print("hello")
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
from hera.shared import global_config
2-
from hera.workflows import Container, Workflow
2+
from hera.workflows import Container, Workflow, script
33

44
global_config.api_version = "argoproj.io/v0beta9000"
55
global_config.namespace = "argo-namespace"
66
global_config.service_account_name = "argo-account"
7+
global_config.image = "image-say"
8+
9+
10+
@script()
11+
def say():
12+
print("hello")
13+
714

815
with Workflow(generate_name="global-config-", entrypoint="whalesay") as w:
916
whalesay = Container(image="docker/whalesay:latest", command=["cowsay"])
17+
say()

src/hera/workflows/_mixins.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import inspect
44
from typing import Any, Callable, Dict, List, Optional, TypeVar, Union, cast
55

6-
from pydantic import root_validator
6+
from pydantic import root_validator, validator
77

88
from hera.shared import global_config
99
from hera.shared._base_model import BaseMixin
@@ -124,7 +124,7 @@ def _add_sub(self, node: Any) -> Any:
124124

125125

126126
class ContainerMixin(BaseMixin):
127-
image: str = global_config.image
127+
image: Optional[str] = None
128128
image_pull_policy: Optional[Union[str, ImagePullPolicy]] = None
129129

130130
liveness_probe: Optional[Probe] = None
@@ -142,6 +142,12 @@ def _build_image_pull_policy(self) -> Optional[ImagePullPolicy]:
142142
return self.image_pull_policy
143143
return ImagePullPolicy[self.image_pull_policy.lower()]
144144

145+
@validator("image", pre=True, always=True)
146+
def _set_image(cls, v):
147+
if v is None:
148+
return global_config.image
149+
return v
150+
145151

146152
class IOMixin(BaseMixin):
147153
inputs: InputsT = None

0 commit comments

Comments
 (0)