Event Sources and Sensors
Event-driven automation for kubernetes: events come in, get filtered, and trigger actions.
Architecture
- EventSource - captures events from an external system and publishes them to the event bus
- Webhooks
- AWS S3
- AWS SQS/SNS
- Kafka
- Calendar
- etc…
- EventBus - transport between sources and sensors
- NATS (JetStream is the current implementation, legacy NATS Streaming)
- To connect to an existing NATS EventBus, use
exotic:apiVersion: argoproj.io/v1alpha1 kind: EventBus metadata: name: default spec: nats: exotic: url: nats://xxxxx:xxx clusterID: cluster-id auth: token accessSecret: name: my-secret-name key: secret-key
- To connect to an existing NATS EventBus, use
- Created as a CRD named
defaultin the namespace, sources and sensors use it automatically
- NATS (JetStream is the current implementation, legacy NATS Streaming)
- Sensor - subscribes to the bus, evaluates dependencies and filters, fires triggers
- Trigger - what gets executed
- Kubernetes Objects
- Argo Workflows
- AWS Lambda
- Slack Notifications
- etc
- Flow: EventSource -> EventBus -> Sensor -> trigger (Workflow, k8s resource, HTTP, …)
apiVersion: argoproj.io/v1alpha1
kind: EventBus
metadata:
name: default
spec:
jetstream:
version: latest
EventSource
- One
EventSourcecan define many events of a type; each key under the type is the eventName serviceexposes ports for sources that receive traffic (webhook, github)
apiVersion: argoproj.io/v1alpha1
kind: EventSource
metadata:
name: webhook
spec:
service:
ports:
- port: 12000
targetPort: 12000
webhook:
example: # eventName
port: '12000'
endpoint: /example
method: POST
- Test:
curl -d '{"message":"hi"}' -H "Content-Type: application/json" http://webhook-eventsource-svc:12000/example - The Service is named
<eventsource-name>-eventsource-svc
Types
- webhook - HTTP endpoint
- calendar - cron
scheduleorinterval, plustimezone - resource - watches k8s resources
- github / gitlab - repo webhooks
- kafka, nats, amqp, redis, sns, sqs, pubsub, eventhubs, slack, file, hdfs, etc.
spec:
calendar:
daily-backup:
schedule: '0 2 * * *'
timezone: UTC
spec:
resource:
deployment-events:
namespace: production
group: apps
version: v1
resource: deployments
eventTypes:
- ADD
- UPDATE
filter:
labels:
- key: monitor
operation: '=='
value: 'true'
spec:
github:
example:
owner: myorg
repository: myrepo
events:
- push
- pull_request
webhook:
endpoint: /push
port: '12000'
method: POST
url: https://webhook.example.com
apiToken:
name: github-access
key: token
webhookSecret:
name: github-access
key: secret
- GitHub
webhook.urlmust be reachable from GitHub
spec:
kafka:
example:
url: kafka-broker.default.svc:9092
topic: mytopic
partition: '0'
Sensor
dependencies- events to wait for (eventSourceName+eventName)triggers- actions to run when dependencies are satisfied
apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
name: webhook-sensor
spec:
dependencies:
- name: test-dep
eventSourceName: webhook
eventName: example
triggers:
- template:
name: workflow-trigger
argoWorkflow:
operation: submit
source:
resource:
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: webhook-workflow-
spec:
entrypoint: main
arguments:
parameters:
- name: message
value: placeholder
templates:
- name: main
inputs:
parameters:
- name: message
container:
image: alpine:latest
command: [echo]
args: ['{{inputs.parameters.message}}']
parameters:
- src:
dependencyName: test-dep
dataKey: body.message
dest: spec.arguments.parameters.0.value
dependencyNameinsrcmust match the dependencyname- Sensor needs a ServiceAccount with RBAC to create the triggered resource (
serviceAccountNamein spec)
Multiple dependencies
- By default a trigger fires when all dependencies are satisfied (AND)
- Use trigger
conditionsfor other combos, referencing dependency names
dependencies:
- name: github-push
eventSourceName: github
eventName: push
- name: approval
eventSourceName: webhook
eventName: approved
triggers:
- template:
name: deploy
conditions: 'github-push && approval'
- OR:
conditions: "dep-a || dep-b"
Filters
- On a dependency, before the trigger sees the event
- data - payload (
path,type,value) - context - event metadata (source, subject, time)
- exprs - expressions over multiple fields
- time - only within a time window
dependencies:
- name: push-main
eventSourceName: github
eventName: push
filters:
data:
- path: body.ref
type: string
value:
- 'refs/heads/main'
Transform
- Reshape the event before it’s used, with
jq
dependencies:
- name: webhook-dep
eventSourceName: webhook
eventName: example
transform:
jq: '{repository: .body.repository.name, branch: (.body.ref | split("/") | .[-1])}'
Event payload
- Event delivered as JSON (CloudEvents format):
context+data - For webhook,
data.bodyis the request body anddata.headerthe headers - Reference with
dataKey: body.fieldin trigger parameters