Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

🏠 Back to Blog

Triggers and Actions

  • Triggers live in the Sensor and run when dependencies are satisfied
  • Each entry under triggers has a template (the action) and optional parameters, policy, retryStrategy, rateLimit
triggers:
- template:
    name: trigger-name
    k8s: {}        # or argoWorkflow, http, slack, kafka, ...

Types

  • argoWorkflow - submit / resubmit / resume / retry / suspend / terminate / stop workflows
  • k8s - create, update, patch, delete any resource
  • http - call an external API
  • awsLambda, openWhisk
  • kafka, nats, azureEventHubs - publish a message
  • slack
  • log - debugging
  • custom

Parameterization

  • Event data is injected with parameters: src picks data from a dependency, dest is the field path to set in the trigger resource
  • Not templated inline in the resource, that is a common mistake
  • dataKey is a path in the event (body.repository.name), dataTemplate is a Go template, value is a fallback default
  • Parameters can be set on the template (affects the trigger spec) or under source for the resource
triggers:
- template:
    name: build
    argoWorkflow:
      operation: submit
      source:
        resource:
          apiVersion: argoproj.io/v1alpha1
          kind: Workflow
          metadata:
            generateName: build-
          spec:
            entrypoint: main
            arguments:
              parameters:
              - name: repo-url
              - name: commit-sha
            # templates...
      parameters:
      - src:
          dependencyName: github-push
          dataKey: body.repository.clone_url
        dest: spec.arguments.parameters.0.value
      - src:
          dependencyName: github-push
          dataKey: body.after
        dest: spec.arguments.parameters.1.value
  • Use a workflowTemplateRef instead of an inline spec to reuse a WorkflowTemplate:
spec:
  workflowTemplateRef:
    name: build-deploy-template

k8s trigger

k8s:
  operation: create
  source:
    resource:
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        generateName: app-
        namespace: default
      # spec...
  parameters:
  - src:
      dependencyName: webhook-dep
      dataKey: body.image
    dest: spec.template.spec.containers.0.image
  • patch: set patchStrategy (application/merge-patch+json, application/strategic-merge-patch+json, application/json-patch+json) and use the source resource as the patch
  • generateName avoids name collisions on create

http trigger

http:
  url: https://api.example.com/webhook
  method: POST
  headers:
    Content-Type: application/json
  secureHeaders:
  - name: Authorization
    valueFrom:
      secretKeyRef:
        name: api-secret
        key: token
  payload:
  - src:
      dependencyName: event-dep
      dataKey: body
    dest: data
  • payload picks what goes in the request body

Messaging and chat

kafka:
  url: kafka-broker.default.svc:9092
  topic: events
  partition: 0
  payload:
  - src:
      dependencyName: event-dep
      dataKey: body
    dest: message
nats:
  url: nats://nats.default.svc:4222
  subject: events.completed
  payload:
  - src:
      dependencyName: event-dep
      dataKey: body
    dest: message
slack:
  slackToken:
    name: slack-secret
    key: token
  channel: "#deployments"
  message: deployment triggered

Conditions

  • conditions on the template is a boolean expression over dependency names, not event data
  • Event data selection is done with dependency filters
- template:
    name: prod-deploy
    conditions: "manager-approval && security-approval"
  • conditionsReset can reset partially satisfied conditions (e.g. by time)

Policy, retry, rate limit

triggers:
- template:
    name: http-with-retry
    http:
      url: https://api.example.com/endpoint
      method: POST
  retryStrategy:
    steps: 3
    duration: 10s
    factor: 2
    jitter: 0.1
  rateLimit:
    unit: Second
    requestsPerUnit: 10
  policy:
    k8s:
      backoff:
        steps: 3
        duration: 1s
      errorOnBackoffTimeout: true
      # also checks resource status for success/failure (labels)

Multiple triggers

  • Several entries under triggers fire for the same event (workflow + slack + http status update)

Patterns

  • CI/CD - github push with data filter on refs/heads/main, argoWorkflow submit with commit sha as a parameter
  • Resource watching - resource EventSource on ConfigMap, k8s patch on Deployment (annotation) to trigger a restart
  • Scheduled - calendar EventSource + argoWorkflow (alternative to CronWorkflow, but also able to fan out and use filters)
  • Approvals - two webhook dependencies + conditions: "a && b"

Tips

  • Filter early at the dependency level
  • Use secrets for tokens and webhook secrets
  • Use the log trigger to debug event flow
  • Check sensor and event source pod logs when nothing fires