Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor init_containers building logic in TemplateMixin #1157

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions docs/examples/workflows/misc/init_containers_with_env.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Init Containers With Env



This example showcases how to run a init_containers with env


=== "Hera"

```python linenums="1"
from hera.workflows import Container, Env, SecretEnv, UserContainer, Workflow

with Workflow(generate_name="container-", entrypoint="cowsay") as w:
Container(
name="cowsay",
image="docker/whalesay",
command=["cowsay", "foo"],
init_containers=[
UserContainer(
name="init",
image="busybox",
command=[
"sh",
"-c",
"echo Hello from the init container ($FOO, $SECRET)",
],
env=[
Env(name="FOO", value="bar"),
SecretEnv(name="SECRET", secret_key="password", secret_name="my-secret"),
],
)
],
)
```

=== "YAML"

```yaml linenums="1"
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: container-
spec:
entrypoint: cowsay
templates:
- container:
command:
- cowsay
- foo
image: docker/whalesay
name: cowsay
initContainers:
- name: init
image: busybox
command: [ "sh", "-c", "echo Hello from the init container ($FOO, $SECRET)" ]
env:
- name: FOO
value: "bar"
- name: SECRET
valueFrom:
secretKeyRef:
name: my-secret
key: password
```

25 changes: 25 additions & 0 deletions examples/workflows/misc/init-containers-with-env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: container-
spec:
entrypoint: cowsay
templates:
- container:
command:
- cowsay
- foo
image: docker/whalesay
name: cowsay
initContainers:
- name: init
image: busybox
command: [ "sh", "-c", "echo Hello from the init container ($FOO, $SECRET)" ]
env:
- name: FOO
value: "bar"
- name: SECRET
valueFrom:
secretKeyRef:
name: my-secret
key: password
25 changes: 25 additions & 0 deletions examples/workflows/misc/init_containers_with_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""This example showcases how to run a init_containers with env"""

from hera.workflows import Container, Env, SecretEnv, UserContainer, Workflow

with Workflow(generate_name="container-", entrypoint="cowsay") as w:
Container(
name="cowsay",
image="docker/whalesay",
command=["cowsay", "foo"],
init_containers=[
UserContainer(
name="init",
image="busybox",
command=[
"sh",
"-c",
"echo Hello from the init container ($FOO, $SECRET)",
],
env=[
Env(name="FOO", value="bar"),
SecretEnv(name="SECRET", secret_key="password", secret_name="my-secret"),
],
)
],
)
7 changes: 7 additions & 0 deletions src/hera/workflows/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,13 @@ def _convert_active_deadline_seconds(cls, v) -> Optional[IntOrString]:
return v
return IntOrString(__root__=v)

def _build_init_containers(self) -> Optional[List[ModelUserContainer]]:
"""Builds the `init_containers` field and optionally returns a list of `UserContainer`."""
if self.init_containers is None:
return None

return [i.build() if isinstance(i, UserContainer) else i for i in self.init_containers]

def _build_sidecars(self) -> Optional[List[ModelUserContainer]]:
"""Builds the `sidecars` field and optionally returns a list of `UserContainer`."""
if self.sidecars is None:
Expand Down
2 changes: 1 addition & 1 deletion src/hera/workflows/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _build_template(self) -> _ModelTemplate:
executor=self.executor,
fail_fast=self.fail_fast,
host_aliases=self.host_aliases,
init_containers=self.init_containers,
init_containers=self._build_init_containers(),
inputs=self._build_inputs(),
memoize=self.memoize,
metadata=self._build_metadata(),
Expand Down
2 changes: 1 addition & 1 deletion src/hera/workflows/container_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def _build_template(self) -> _ModelTemplate:
executor=self.executor,
fail_fast=self.fail_fast,
host_aliases=self.host_aliases,
init_containers=self.init_containers,
init_containers=self._build_init_containers(),
inputs=self._build_inputs(),
memoize=self.memoize,
metadata=self._build_metadata(),
Expand Down
2 changes: 1 addition & 1 deletion src/hera/workflows/dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _build_template(self) -> _ModelTemplate:
executor=self.executor,
fail_fast=self.fail_fast,
host_aliases=self.host_aliases,
init_containers=self.init_containers,
init_containers=self._build_init_containers(),
inputs=self._build_inputs(),
memoize=self.memoize,
metadata=self._build_metadata(),
Expand Down
2 changes: 1 addition & 1 deletion src/hera/workflows/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _build_template(self) -> m.Template:
executor=self.executor,
fail_fast=self.fail_fast,
host_aliases=self.host_aliases,
init_containers=self.init_containers,
init_containers=self._build_init_containers(),
inputs=self._build_inputs(),
outputs=self._build_outputs(),
memoize=self.memoize,
Expand Down
2 changes: 1 addition & 1 deletion src/hera/workflows/http_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _build_template(self) -> _ModelTemplate:
fail_fast=self.fail_fast,
host_aliases=self.host_aliases,
http=self._build_http_template(),
init_containers=self.init_containers,
init_containers=self._build_init_containers(),
memoize=self.memoize,
metadata=self._build_metadata(),
inputs=self._build_inputs(),
Expand Down
2 changes: 1 addition & 1 deletion src/hera/workflows/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _build_template(self) -> _ModelTemplate:
executor=self.executor,
fail_fast=self.fail_fast,
host_aliases=self.host_aliases,
init_containers=self.init_containers,
init_containers=self._build_init_containers(),
inputs=self._build_inputs(),
memoize=self.memoize,
metadata=self._build_metadata(),
Expand Down
2 changes: 1 addition & 1 deletion src/hera/workflows/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def _build_template(self) -> _ModelTemplate:
executor=self.executor,
fail_fast=self.fail_fast,
host_aliases=self.host_aliases,
init_containers=self.init_containers,
init_containers=self._build_init_containers(),
inputs=self._build_inputs(),
memoize=self.memoize,
metadata=self._build_metadata(),
Expand Down
2 changes: 1 addition & 1 deletion src/hera/workflows/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def _build_template(self) -> _ModelTemplate:
fail_fast=self.fail_fast,
http=None,
host_aliases=self.host_aliases,
init_containers=self.init_containers,
init_containers=self._build_init_containers(),
inputs=self._build_inputs(),
memoize=self.memoize,
metadata=self._build_metadata(),
Expand Down
2 changes: 1 addition & 1 deletion src/hera/workflows/suspend.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _build_template(self) -> _ModelTemplate:
executor=self.executor,
fail_fast=self.fail_fast,
host_aliases=self.host_aliases,
init_containers=self.init_containers,
init_containers=self._build_init_containers(),
inputs=self._build_inputs(),
memoize=self.memoize,
metadata=self._build_metadata(),
Expand Down