Skip to content

Commit

Permalink
[#687] Adjust image pull policy inference (#689)
Browse files Browse the repository at this point in the history
**Pull Request Checklist**
- [x] Fixes #687
- [ ] Tests added
- [x] Documentation/examples added
- [x] [Good commit messages](https://cbea.ms/git-commit/) and/or PR
title

**Description of PR**

This PR adds a small mapping of possible user inputs to map to
`ImagePullPolicy` objects as a QOL improvement/bug fix

---------

Signed-off-by: Flaviu Vadan <[email protected]>
  • Loading branch information
flaviuvadan authored Jun 20, 2023
1 parent 854a612 commit b10dfa5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
23 changes: 19 additions & 4 deletions src/hera/workflows/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
from hera.workflows.steps import Step
from hera.workflows.task import Task


_yaml: Optional[ModuleType] = None
try:
import yaml
Expand Down Expand Up @@ -176,12 +175,28 @@ class ContainerMixin(BaseMixin):
termination_message_policy: Optional[TerminationMessagePolicy] = None
tty: Optional[bool] = None

def _build_image_pull_policy(self) -> Optional[str]:
def _build_image_pull_policy(self) -> Optional[ImagePullPolicy]:
if self.image_pull_policy is None:
return None
elif isinstance(self.image_pull_policy, ImagePullPolicy):
return self.image_pull_policy.value
return ImagePullPolicy[self.image_pull_policy.lower()].value
return self.image_pull_policy

# this helps map image pull policy values as a convenience
policy_mapper = {
# the following 2 are "normal" entries
**{ipp.name: ipp for ipp in ImagePullPolicy},
**{ipp.value: ipp for ipp in ImagePullPolicy},
# some users might submit the policy without underscores
**{ipp.value.lower().replace("_", ""): ipp for ipp in ImagePullPolicy},
# some users might submit the policy in lowercase
**{ipp.name.lower(): ipp for ipp in ImagePullPolicy},
}
try:
return ImagePullPolicy[policy_mapper[self.image_pull_policy].name]
except KeyError as e:
raise KeyError(
f"Supplied image policy {self.image_pull_policy} is not valid. Use one of {ImagePullPolicy.__members__}"
) from e

@validator("image", pre=True, always=True)
def _set_image(cls, v):
Expand Down
5 changes: 4 additions & 1 deletion src/hera/workflows/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ def _build_template(self) -> _ModelTemplate:

def _build_script(self) -> _ModelScriptTemplate:
assert isinstance(self.constructor, ScriptConstructor)
image_pull_policy = self._build_image_pull_policy()

return self.constructor.transform_script_template_post_build(
self,
_ModelScriptTemplate(
Expand All @@ -202,7 +204,8 @@ def _build_script(self) -> _ModelScriptTemplate:
env=self._build_env(),
env_from=self._build_env_from(),
image=self.image,
image_pull_policy=self._build_image_pull_policy(),
# `image_pull_policy` in script wants a string not an `ImagePullPolicy` object
image_pull_policy=None if image_pull_policy is None else image_pull_policy.value,
lifecycle=self.lifecycle,
liveness_probe=self.liveness_probe,
name=self.container_name,
Expand Down

0 comments on commit b10dfa5

Please sign in to comment.