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

Lab 2: Event Sources

Calendar and resource EventSources, filters, multiple dependencies, and data transformation. Assumes lab 1 (EventBus default, operate-workflow-sa).

Calendar

  • Time based events, cron schedule or fixed interval
  • No external trigger needed
  • Event data: {"eventTime": ..., "userPayload": ..., "metadata": {...}}
apiVersion: argoproj.io/v1alpha1
kind: EventSource
metadata:
  name: calendar
  namespace: argo-events
spec:
  calendar:
    every-2min:
      schedule: "*/2 * * * *"
      timezone: UTC
      metadata:
        frequency: high
    hourly:
      schedule: "0 * * * *"
      timezone: UTC
      metadata:
        frequency: low
    every-3min:
      interval: 3m
  • Cron is minute hour day month weekday
  • Default timezone is the pod’s (UTC), set timezone explicitly if you care

Sensor that passes the event time to a workflow:

apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
  name: calendar
  namespace: argo-events
spec:
  template:
    serviceAccountName: operate-workflow-sa
  dependencies:
    - name: tick
      eventSourceName: calendar
      eventName: every-2min
  triggers:
    - template:
        name: scheduled
        k8s:
          operation: create
          source:
            resource:
              apiVersion: argoproj.io/v1alpha1
              kind: Workflow
              metadata:
                generateName: scheduled-
              spec:
                entrypoint: main
                arguments:
                  parameters:
                    - name: time
                      value: placeholder
                templates:
                  - name: main
                    container:
                      image: alpine:3
                      command: [echo, "event time: {{workflow.parameters.time}}"]
          parameters:
            - src:
                dependencyName: tick
                dataKey: eventTime
              dest: spec.arguments.parameters.0.value
kubectl apply -f calendar.yaml
kubectl get workflows -n argo-events -w
kubectl logs -n argo-events -l sensor-name=calendar

Resource

  • Watches Kubernetes objects, emits ADD, UPDATE, DELETE
  • The EventSource pod needs RBAC to list/watch whatever it watches
  • Event data: {"type": "ADD", "body": <the object>, "group", "version", "resource"}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: resource-watcher-sa
  namespace: argo-events
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: resource-watcher
rules:
  - apiGroups: ["", apps]
    resources: [pods, deployments]
    verbs: [get, list, watch]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: resource-watcher
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: resource-watcher
subjects:
  - kind: ServiceAccount
    name: resource-watcher-sa
    namespace: argo-events
---
apiVersion: argoproj.io/v1alpha1
kind: EventSource
metadata:
  name: resource
  namespace: argo-events
spec:
  template:
    serviceAccountName: resource-watcher-sa
  resource:
    pod-events:
      namespace: default
      group: ""
      version: v1
      resource: pods
      eventTypes: [ADD, UPDATE, DELETE]
      filter:
        labels:
          - key: monitored
            operation: "=="
            value: "true"
        afterStart: true     # ignore objects that existed before the event source started

Sensor, only ADD events, pod name into the workflow:

apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
  name: resource
  namespace: argo-events
spec:
  template:
    serviceAccountName: operate-workflow-sa
  dependencies:
    - name: pod-added
      eventSourceName: resource
      eventName: pod-events
      filters:
        data:
          - path: type
            type: string
            value: ["ADD"]
  triggers:
    - template:
        name: pod-added
        k8s:
          operation: create
          source:
            resource:
              apiVersion: argoproj.io/v1alpha1
              kind: Workflow
              metadata:
                generateName: pod-added-
              spec:
                entrypoint: main
                arguments:
                  parameters:
                    - name: pod
                      value: placeholder
                templates:
                  - name: main
                    container:
                      image: alpine:3
                      command: [echo, "new pod: {{workflow.parameters.pod}}"]
          parameters:
            - src:
                dependencyName: pod-added
                dataKey: body.metadata.name
              dest: spec.arguments.parameters.0.value
kubectl run test-pod --image=nginx:alpine -l monitored=true
kubectl get workflows -n argo-events
kubectl logs -n argo-events -l eventsource-name=resource
  • filter also supports fields, prefix, createdBy
  • Same pattern works for deployments (group: apps, resource: deployments). Filtering on body.spec.replicas with comparator works for numbers

Filters

Applied per dependency in the sensor, event is dropped if the filter fails.

  • data: match values at a path in the event (type: string, number, bool). Several data filters are ANDed by default (dataLogicalOperator). Values are regexes for strings
  • context: match event metadata (source, type, subject…)
  • exprs: boolean expression over fields pulled from the event
  • time: only accept events in a time window
  dependencies:
    - name: prod-deploy
      eventSourceName: webhook-multi
      eventName: production
      filters:
        data:
          - path: body.action
            type: string
            value: ["deploy"]
          - path: body.branch
            type: string
            value: ["main", "master"]
        exprs:
          - expr: priority == 'high'
            fields:
              - name: priority
                path: body.priority

EventSource for it (three endpoints on one port):

apiVersion: argoproj.io/v1alpha1
kind: EventSource
metadata:
  name: webhook-multi
  namespace: argo-events
spec:
  service:
    ports:
      - port: 12000
        targetPort: 12000
  webhook:
    production: {port: "12000", endpoint: /production, method: POST}
    staging: {port: "12000", endpoint: /staging, method: POST}
kubectl port-forward -n argo-events svc/webhook-multi-eventsource-svc 12000:12000 &
# triggers (the JSON content type is needed for body.* to be parsed)
curl -X POST localhost:12000/production -H "Content-Type: application/json" \
  -d '{"action":"deploy","branch":"main","priority":"high"}'
# filtered out: wrong branch
curl -X POST localhost:12000/production -H "Content-Type: application/json" \
  -d '{"action":"deploy","branch":"feature","priority":"high"}'

Multiple dependencies

  • Default: ALL dependencies must be satisfied (AND), then the triggers fire
  • OR / custom logic: put conditions on the trigger, an expression over dependency names (the old dependencyGroups/circuit is deprecated)
  • Use names without hyphens so they are valid in the expression
  dependencies:
    - name: prod
      eventSourceName: webhook-multi
      eventName: production
    - name: staging
      eventSourceName: webhook-multi
      eventName: staging
  triggers:
    - template:
        name: either-env
        conditions: "prod || staging"
        k8s:
          # ...
  • AND across sources: webhook ready + calendar hourly. The sensor remembers the first event until the other arrives
  • Each dependency is addressed by dependencyName in parameters

Transform

  • Reshape the event before filters/triggers with transform
  • jq runs on the event data, so it sees .body and .header but not the event context
  • The result replaces the event data, so parameter dataKeys refer to the new fields
  dependencies:
    - name: hook
      eventSourceName: webhook-multi
      eventName: production
      transform:
        jq: '{app: .body.app, version: .body.version, ref: (.body.app + ":" + .body.version)}'
  # parameter:
  #   src: {dependencyName: hook, dataKey: ref}
curl -X POST localhost:12000/production -H "Content-Type: application/json" \
  -d '{"app":"my-app","version":"v1.2.3"}'

Cleanup

kill %1     # port-forward
kubectl delete pod test-pod --ignore-not-found
kubectl delete sensor,eventsource --all -n argo-events
kubectl delete workflows --all -n argo-events