Skip to content

Commit

Permalink
Fix value-from for config map references (#431)
Browse files Browse the repository at this point in the history
Signed-off-by: Sambhav Kothari <[email protected]>
  • Loading branch information
sambhav authored Feb 16, 2023
1 parent 7e71811 commit 1a53d3c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
32 changes: 29 additions & 3 deletions src/hera/value_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,31 @@
)


@dataclass
class ConfigMapKeyRef:
"""Holds the ConfigMap reference.
Attributes
----------
key: str
The key to select.
name: str
The ConfigMap to select from.
optional: bool
Specify whether the ConfigMap or its key must be defined.
"""
key: str
name: str
optional: bool = False


@dataclass
class ValueFrom:
"""Holds descriptions of where to obtain parameter values from.
Attributes
----------
config_map_key_ref: Optional[str] = None
config_map_key_ref: Optional[ConfigMapKeyRef] = None
A selector that identifies a config map to obtain the value from.
default: Optional[str] = None
Specifies a value to be used if retrieving the value from the specified source fails.
Expand All @@ -40,7 +58,7 @@ class ValueFrom:
See: https://argoproj.github.io/argo-workflows/fields/#valuefrom
"""

config_map_key_ref: Optional[str] = None
config_map_key_ref: Optional[ConfigMapKeyRef] = None
default: Optional[str] = None
event: Optional[str] = None
expression: Optional[str] = None
Expand All @@ -58,7 +76,15 @@ def __post_init__(self):
def build(self) -> IoArgoprojWorkflowV1alpha1ValueFrom:
value_from = IoArgoprojWorkflowV1alpha1ValueFrom()
if self.config_map_key_ref is not None:
setattr(value_from, "config_map_key_ref", ConfigMapKeySelector(self.config_map_key_ref))
setattr(
value_from,
"config_map_key_ref",
ConfigMapKeySelector(
key=self.config_map_key_ref.key,
name=self.config_map_key_ref.name,
optional=self.config_map_key_ref.optional,
),
)
if self.default is not None:
setattr(value_from, "default", self.default)
if self.event is not None:
Expand Down
5 changes: 3 additions & 2 deletions tests/test_value_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
IoArgoprojWorkflowV1alpha1ValueFrom,
)

from hera.value_from import ValueFrom
from hera.value_from import ConfigMapKeyRef, ValueFrom


class TestValueFrom:
Expand All @@ -14,13 +14,14 @@ def test_raises_on_all_none_fields(self):
assert str(e.value) == "At least one fields must be not `None` for `ValueFrom`"

def test_builds_as_expected(self):
vf_ = ValueFrom(config_map_key_ref="abc")
vf_ = ValueFrom(config_map_key_ref=ConfigMapKeyRef(key="abc", name="cm"))
vf = vf_.build()
assert isinstance(vf, IoArgoprojWorkflowV1alpha1ValueFrom)
assert hasattr(vf, "config_map_key_ref")
assert isinstance(vf.config_map_key_ref, ConfigMapKeySelector)
assert hasattr(vf.config_map_key_ref, "key")
assert vf.config_map_key_ref.key == "abc"
assert vf.config_map_key_ref.name == "cm"
assert not hasattr(vf, "default")
assert not hasattr(vf, "event")
assert not hasattr(vf, "expression")
Expand Down

0 comments on commit 1a53d3c

Please sign in to comment.