Lab 3: Triggers
Trigger types: argoWorkflow, k8s, http, log. Also parameters, conditions, retries. Assumes labs 1 and 2.
Setup
One webhook EventSource, reused by all the sensors below. Each sensor filters on body.action.
apiVersion: argoproj.io/v1alpha1
kind: EventSource
metadata:
name: trigger-source
namespace: argo-events
spec:
service:
ports:
- port: 12000
targetPort: 12000
webhook:
events:
port: "12000"
endpoint: /events
method: POST
kubectl port-forward -n argo-events svc/trigger-source-eventsource-svc 12000:12000 &
Helper for the examples:
send() { curl -s -X POST localhost:12000/events -H "Content-Type: application/json" -d "$1"; }
argoWorkflow trigger
- Workflow specific:
operationissubmit,resubmit,resume,retry,suspend,terminate,stop - Can submit from an inline
source.resourceor reference aWorkflowTemplate parameters:srcpicks event data,destis the path in the resource.valueinsrcis the default whendataKeyis missing
apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
name: workflow-param
namespace: argo-events
spec:
template:
serviceAccountName: operate-workflow-sa
dependencies:
- name: run
eventSourceName: trigger-source
eventName: events
filters:
data:
- path: body.action
type: string
value: ["run"]
triggers:
- template:
name: param-workflow
argoWorkflow:
operation: submit
source:
resource:
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: param-
spec:
entrypoint: main
arguments:
parameters:
- name: image
value: alpine:3
- name: message
value: hello
templates:
- name: main
container:
image: "{{workflow.parameters.image}}"
command: [echo, "{{workflow.parameters.message}}"]
parameters:
- src:
dependencyName: run
dataKey: body.image
value: alpine:3
dest: spec.arguments.parameters.0.value
- src:
dependencyName: run
dataKey: body.message
value: hello
dest: spec.arguments.parameters.1.value
send '{"action":"run"}'
send '{"action":"run","image":"busybox:1","message":"custom"}'
argo list -n argo-events
argo logs -n argo-events @latest
dataTemplatebuilds a value with Go templates instead of a single key:dataTemplate: "{{ .Input.body.repo }}:{{ .Input.body.tag }}"
k8s trigger
- Creates/updates/patches/deletes any resource:
operation: create|update|patch|delete - The sensor SA needs RBAC for whatever it touches
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: sensor-k8s
rules:
- apiGroups: ["", apps]
resources: [deployments, services, configmaps]
verbs: [create, get, list, patch, update, delete]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: sensor-k8s
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: sensor-k8s
subjects:
- kind: ServiceAccount
name: operate-workflow-sa
namespace: argo-events
Deployment from an event (name, image from the payload):
apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
name: k8s-deploy
namespace: argo-events
spec:
template:
serviceAccountName: operate-workflow-sa
dependencies:
- name: deploy
eventSourceName: trigger-source
eventName: events
filters:
data:
- path: body.action
type: string
value: ["deploy"]
triggers:
- template:
name: create-deployment
k8s:
operation: create
source:
resource:
apiVersion: apps/v1
kind: Deployment
metadata:
name: placeholder
namespace: default
labels:
managed-by: argo-events
spec:
replicas: 1
selector:
matchLabels:
app: placeholder
template:
metadata:
labels:
app: placeholder
spec:
containers:
- name: app
image: placeholder
parameters:
- src: {dependencyName: deploy, dataKey: body.appName}
dest: metadata.name
- src: {dependencyName: deploy, dataKey: body.appName}
dest: spec.selector.matchLabels.app
- src: {dependencyName: deploy, dataKey: body.appName}
dest: spec.template.metadata.labels.app
- src: {dependencyName: deploy, dataKey: body.image}
dest: spec.template.spec.containers.0.image
send '{"action":"deploy","appName":"nginx-app","image":"nginx:alpine"}'
kubectl get deploy -n default -l managed-by=argo-events
- Multiple objects = multiple triggers in the same sensor (e.g. a Deployment and a Service)
patchalso needspatchStrategy(application/merge-patch+jsonetc.)
http trigger
- Calls an external URL.
payloadbuilds the JSON body:dataKey/dataTemplatefrom the event,destis the key in the body
kubectl create deployment echo --image=ealen/echo-server --port=80
kubectl expose deployment echo --port=80
triggers:
- template:
name: notify
http:
url: http://echo.default.svc.cluster.local
method: POST
headers:
Content-Type: application/json
timeout: 10
payload:
- src: {dependencyName: deploy, dataKey: body.appName}
dest: app
- src: {dependencyName: deploy, dataTemplate: "{{ .Input.body.image }}:notified"}
dest: note
kubectl logs -n argo-events -l sensor-name=<sensor>
kubectl logs -l app=echo
- Auth via
basicAuth/secureHeadersreferencing Secrets, donโt inline tokens
log trigger
- Just logs the event in the sensor pod, useful while building a sensor
triggers:
- template:
name: debug
log:
intervalSeconds: 1
Conditions
conditionson a trigger is an expression over dependency names, not event fields- To branch on payload content, make one filtered dependency per case
dependencies:
- name: prod
eventSourceName: trigger-source
eventName: events
filters:
data:
- {path: body.environment, type: string, value: ["production"]}
- name: dev
eventSourceName: trigger-source
eventName: events
filters:
data:
- {path: body.environment, type: string, value: ["dev", "development"]}
- name: urgent
eventSourceName: trigger-source
eventName: events
filters:
data:
- {path: body.priority, type: string, value: ["high"]}
triggers:
- template:
name: prod-deploy
conditions: "prod"
log: {}
- template:
name: dev-deploy
conditions: "dev"
log: {}
- template:
name: notify
conditions: "urgent || prod"
log: {}
send '{"environment":"production","priority":"normal"}' # prod-deploy, notify
send '{"environment":"dev","priority":"high"}' # dev-deploy, notify
- Without
conditionsa trigger waits for all dependencies (AND)
Retries and policy
retryStrategyis on the trigger (sibling oftemplate), it retries the trigger execution, not the workflowpolicy.k8s.statusmakes a trigger succeed only when the created resource reaches a given status (e.g. workflowSucceeded), with its ownbackoffrateLimitcaps trigger executions per unit of time- Workflow level
retryStrategyon the workflow/template is separate, handled by Argo Workflows
triggers:
- template:
name: workflow-with-retry
k8s:
operation: create
source:
resource: {} # workflow here
retryStrategy:
steps: 3
duration: 2s
rateLimit:
unit: Minute
requestsPerUnit: 10
Multiple triggers
- A sensor can have any mix of triggers, they all fire when the dependencies are met
- Triggers run independently, one failing does not stop the others (unless you use
policy/conditions)
triggers:
- template: {name: build, argoWorkflow: {}} # workflow
- template: {name: deploy, k8s: {}} # deployment
- template: {name: notify, http: {}} # http
- template: {name: debug, log: {}}
Cleanup
kill %1 # port-forward
kubectl delete sensor,eventsource --all -n argo-events
kubectl delete workflows --all -n argo-events
kubectl delete deploy nginx-app -n default --ignore-not-found
kubectl delete deploy,svc echo --ignore-not-found
kubectl delete clusterrole,clusterrolebinding sensor-k8s