|
| 1 | +# Advanced Template Features |
| 2 | + |
| 3 | +This section exemplifies `template` features found in Argo, but are beyond the scope of the Walk Through. |
| 4 | + |
| 5 | +## Secrets |
| 6 | + |
| 7 | +To access secrets stored in your Kubernetes/Argo cluster, you should use the `env` or `env_from` members of |
| 8 | +`TemplateMixin` (i.e. any standard template). |
| 9 | + |
| 10 | +```py |
| 11 | +from hera.workflows import ( |
| 12 | + ConfigMapEnv, |
| 13 | + ConfigMapEnvFrom, |
| 14 | + Container, |
| 15 | + Env, |
| 16 | + ResourceEnv, |
| 17 | + SecretEnv, |
| 18 | + SecretEnvFrom, |
| 19 | + Workflow, |
| 20 | +) |
| 21 | + |
| 22 | +with Workflow(generate_name="secret-env-from-", entrypoint="whalesay") as w: |
| 23 | + whalesay = Container( |
| 24 | + image="docker/whalesay:latest", |
| 25 | + command=["cowsay"], |
| 26 | + env_from=[ |
| 27 | + SecretEnvFrom(prefix="abc", name="secret", optional=False), |
| 28 | + ConfigMapEnvFrom(prefix="abc", name="configmap", optional=False), |
| 29 | + ], |
| 30 | + env=[ |
| 31 | + Env(name="test", value="1"), |
| 32 | + SecretEnv(name="s1", secret_key="s1", secret_name="abc"), |
| 33 | + ResourceEnv(name="r1", resource="abc"), |
| 34 | + ConfigMapEnv(name="c1", config_map_key="c1", config_map_name="abc"), |
| 35 | + ], |
| 36 | + ) |
| 37 | +``` |
| 38 | + |
| 39 | +## Retrying and Timeouts |
| 40 | + |
| 41 | +You can easily set retries and timeouts on templates through the `retry_strategy` and `timeout` members of `TemplateMixin`. |
| 42 | + |
| 43 | +See the example below for a combination of these features, which retries with a backoff up to 10 times or until the |
| 44 | +timeout, set at 5 minutes. |
| 45 | + |
| 46 | +```py |
| 47 | +from hera.workflows import ( |
| 48 | + Container, |
| 49 | + RetryStrategy, |
| 50 | + Workflow, |
| 51 | + models as m, |
| 52 | +) |
| 53 | + |
| 54 | +with Workflow( |
| 55 | + generate_name="retry-backoff-til-timeout-", |
| 56 | + entrypoint="retry-backoff-til-timeout", |
| 57 | +) as w: |
| 58 | + retry_backoff_til_timeout = Container( |
| 59 | + name="retry-backoff-til-timeout", |
| 60 | + image="python:alpine3.6", |
| 61 | + command=["python", "-c"], |
| 62 | + args=["import random; import sys; exit_code = random.choice([0, 0, 0, 1]); sys.exit(exit_code)"], |
| 63 | + retry_strategy=RetryStrategy( |
| 64 | + limit=10, |
| 65 | + backoff=m.Backoff( |
| 66 | + duration="1", |
| 67 | + factor="2", |
| 68 | + max_duration="1m", |
| 69 | + ), |
| 70 | + ), |
| 71 | + timeout="5m", |
| 72 | + ) |
| 73 | +``` |
| 74 | + |
| 75 | +## Recursion |
| 76 | + |
| 77 | +Individual Steps and Tasks can specify the `Steps` or `DAG` template that it is a part of, allowing recursive Template |
| 78 | +Invocators. You must ensure there is a break condition, and you should *NOT* make the first `Step` or `Task` the parent |
| 79 | +`Steps` or `DAG` without a condition, otherwise the Argo controller will expand the definition indefinitely until it |
| 80 | +crashes. |
| 81 | + |
| 82 | +See the [recursive coinflip](../../examples/workflows/upstream/coinflip-recursive.md) example for a recursive `Steps` |
| 83 | +template. |
0 commit comments