Lab 05: Workflow Templates
Reusable definitions: WorkflowTemplate (namespaced), ClusterWorkflowTemplate (cluster-wide). A template defines the work once, a Workflow runs it with its own arguments.
WorkflowTemplate
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
name: greeting-template
namespace: argo
spec:
serviceAccountName: argo
entrypoint: greet
arguments:
parameters:
- name: greeting
value: Hello
- name: name
value: World
templates:
- name: greet
inputs:
parameters:
- name: greeting
- name: name
container:
image: alpine:3.20
command: [sh, -c]
args: ["echo '{{inputs.parameters.greeting}}, {{inputs.parameters.name}}!'"]
kubectl apply -f greeting-template.yaml
argo template list -n argo
argo submit -n argo --from workflowtemplate/greeting-template --watch
argo submit -n argo --from workflowtemplate/greeting-template -p greeting="Good morning" -p name=DevOps --watch
argumentson the template are defaults,-poverridesargo template create/get/deletealso work,kubectlis fine too
Run it from a Workflow instead of the CLI:
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: from-template-
spec:
arguments:
parameters:
- name: name
value: CAPA
workflowTemplateRef:
name: greeting-template
kubectl create -n argo -f from-template.yaml
- the Workflow takes the whole spec from the template (
workflowTemplateRef), onlyargumentsand a few other fields can be set on it - the template is referenced at runtime, editing it later affects new workflows only (the resolved spec is stored in
status.storedWorkflowTemplateSpec)
Multiple entrypoints
spec:
entrypoint: full-pipeline
templates:
- name: full-pipeline
steps:
- - {name: build, template: build-step}
- - {name: test, template: test-step}
- - {name: deploy, template: deploy-step}
- name: quick-test
steps:
- - {name: test, template: test-step}
...
argo submit -n argo --from workflowtemplate/multi-entry-template --entrypoint quick-test --watch
--entrypointoverridesspec.entrypoint
ClusterWorkflowTemplate
apiVersion: argoproj.io/v1alpha1
kind: ClusterWorkflowTemplate
metadata:
name: common-build-template
spec:
entrypoint: build-pipeline
arguments:
parameters:
- name: branch
value: main
templates:
- name: build-pipeline
inputs:
parameters:
- name: branch
container:
image: alpine:3.20
command: [sh, -c]
args: ["echo building {{inputs.parameters.branch}}"]
kubectl apply -f clusterworkflowtemplate.yaml
argo cluster-template list
argo submit -n argo --from clusterworkflowtemplate/common-build-template -p branch=develop --watch
- no namespace; the workflow runs in the namespace you submit to, so the SA must exist there
- from a Workflow:
workflowTemplateRef: {name: common-build-template, clusterScope: true} - the controller needs cluster-level read access to these (cluster install has it)
Template library with templateRef
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
name: common-tasks
namespace: argo
spec:
templates:
- name: notify
inputs:
parameters:
- name: message
- name: channel
value: "#general"
container:
image: alpine:3.20
command: [sh, -c]
args: ["echo 'to {{inputs.parameters.channel}}: {{inputs.parameters.message}}'"]
Use a single template from another workflow:
spec:
entrypoint: deploy
arguments:
parameters:
- name: environment
value: staging
templates:
- name: deploy
steps:
- - name: deploy-app
template: deploy-step
- - name: notify
templateRef:
name: common-tasks
template: notify
arguments:
parameters:
- name: message
value: "deployed to {{workflow.parameters.environment}}"
- name: channel
value: "#deployments"
templateRef= one template out of another WorkflowTemplate (clusterScope: truefor ClusterWorkflowTemplates)workflowTemplateRef= the whole workflow spec- a library template has no
entrypoint, it canโt be submitted by itself
Conditionals and suspend
- name: main
steps:
- - {name: build, template: build-step}
- - name: test
template: test-step
when: "{{workflow.parameters.run-tests}} == true"
- - name: approval
template: approval-step
when: "{{workflow.parameters.auto-approve}} == false"
- - {name: deploy, template: deploy-step}
- name: approval-step
suspend: {}
argo submit -n argo --from workflowtemplate/conditional-deploy -p auto-approve=true --watch
argo resume -n argo @latest
- a
suspendtemplate pauses untilargo resume(or UI),suspend: {duration: 30s}resumes by itself - inside a steps template, reference workflow-level values with
{{workflow.parameters.x}},{{inputs.parameters.x}}only works if the template declares that input
Versioning
- templates have no built-in versions, use a name suffix and/or labels
kubectl apply -f app-deploy-v1.yaml -f app-deploy-v2.yaml
argo template list -n argo -l version=v2
argo submit -n argo --from workflowtemplate/app-deploy-v1 --watch
workflowTemplateRefalways uses the current object, pin by referencing the versioned name- keep templates in git and roll out via ArgoCD
CronWorkflow from a template
apiVersion: argoproj.io/v1alpha1
kind: CronWorkflow
metadata:
name: hello-cron
namespace: argo
spec:
schedule: "*/2 * * * *"
concurrencyPolicy: Replace
workflowSpec:
workflowTemplateRef:
name: greeting-template
- details in lab 07
- a health-check cron that alerts on failure needs
continueOn: {failed: true}on the check step, otherwise the workflow stops there and the alert step never runs
Cleanup
argo delete -n argo --all
argo cron delete --all -n argo
argo template delete --all -n argo
argo cluster-template delete --all