Skip to content

Commit c2080f5

Browse files
authored
Fix ineffective GlobalConfig Workflow settings (#537)
Fixes that the following global settings were not being applied in `Workflow` and derivative classes: - `GlobalConfig.apiVersion` - `GlobalConfig.service_account_name` Signed-off-by: Helge Willum Thingvad <[email protected]> Signed-off-by: Sambhav Kothari <[email protected]>
1 parent 7ad187d commit c2080f5

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Global Config
2+
3+
4+
5+
6+
7+
8+
=== "Hera"
9+
10+
```python linenums="1"
11+
from hera.shared import global_config
12+
from hera.workflows import Container, Workflow
13+
14+
global_config.api_version = "argoproj.io/v0beta9000"
15+
global_config.namespace = "argo-namespace"
16+
global_config.service_account_name = "argo-account"
17+
18+
with Workflow(generate_name="global-config-", entrypoint="whalesay") as w:
19+
whalesay = Container(image="docker/whalesay:latest", command=["cowsay"])
20+
```
21+
22+
=== "YAML"
23+
24+
```yaml linenums="1"
25+
apiVersion: argoproj.io/v0beta9000
26+
kind: Workflow
27+
metadata:
28+
generateName: global-config-
29+
namespace: argo-namespace
30+
spec:
31+
entrypoint: whalesay
32+
serviceAccountName: argo-account
33+
templates:
34+
- container:
35+
command:
36+
- cowsay
37+
image: docker/whalesay:latest
38+
```
39+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: argoproj.io/v0beta9000
2+
kind: Workflow
3+
metadata:
4+
generateName: global-config-
5+
namespace: argo-namespace
6+
spec:
7+
entrypoint: whalesay
8+
serviceAccountName: argo-account
9+
templates:
10+
- container:
11+
command:
12+
- cowsay
13+
image: docker/whalesay:latest
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from hera.shared import global_config
2+
from hera.workflows import Container, Workflow
3+
4+
global_config.api_version = "argoproj.io/v0beta9000"
5+
global_config.namespace = "argo-namespace"
6+
global_config.service_account_name = "argo-account"
7+
8+
with Workflow(generate_name="global-config-", entrypoint="whalesay") as w:
9+
whalesay = Container(image="docker/whalesay:latest", command=["cowsay"])

src/hera/workflows/workflow.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class Workflow(
8080
"""
8181

8282
# Workflow fields - https://argoproj.github.io/argo-workflows/fields/#workflow
83-
api_version: Optional[str] = global_config.api_version
83+
api_version: Optional[str] = None
8484
kind: Optional[str] = None
8585
status: Optional[WorkflowStatus] = None
8686

@@ -147,6 +147,12 @@ class Workflow(
147147
# Hera-specific fields
148148
workflows_service: Optional[WorkflowsService] = None
149149

150+
@validator("api_version", pre=True, always=True)
151+
def _set_api_version(cls, v):
152+
if v is None:
153+
return global_config.api_version
154+
return v
155+
150156
@validator("workflows_service", pre=True, always=True)
151157
def _set_workflows_service(cls, v):
152158
if v is None:
@@ -165,6 +171,12 @@ def _set_namespace(cls, v):
165171
return global_config.namespace
166172
return v
167173

174+
@validator("service_account_name", pre=True, always=True)
175+
def _set_service_account_name(cls, v):
176+
if v is None:
177+
return global_config.service_account_name
178+
return v
179+
168180
@validator("image_pull_secrets", pre=True, always=True)
169181
def _set_image_pull_secrets(cls, v):
170182
if v is None:

tests/test_workflow.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import examples.workflows as hera_examples
1212
import examples.workflows.upstream as hera_upstream_examples
13+
from hera.shared import global_config
1314
from hera.workflows import (
1415
CronWorkflow as HeraCronWorkflow,
1516
Workflow as HeraWorkflow,
@@ -61,6 +62,7 @@ def _transform_cron_workflow(obj):
6162
)
6263
def test_hera_output(module_name):
6364
# GIVEN
65+
global_config.reset()
6466
workflow = importlib.import_module(f"examples.workflows.{module_name}").w
6567
yaml_path = Path(hera_examples.__file__).parent / f"{module_name.replace('_', '-')}.yaml"
6668

@@ -77,6 +79,7 @@ def test_hera_output(module_name):
7779
@pytest.mark.parametrize("module_name", [name for _, name, _ in pkgutil.iter_modules(hera_upstream_examples.__path__)])
7880
def test_hera_output_upstream(module_name):
7981
# GIVEN
82+
global_config.reset()
8083
workflow = importlib.import_module(f"examples.workflows.upstream.{module_name}").w
8184
yaml_path = Path(hera_upstream_examples.__file__).parent / f"{module_name.replace('_', '-')}.yaml"
8285
upstream_yaml_path = (

0 commit comments

Comments
 (0)