SinkBinding

version

A SinkBinding provides a framework for injecting K_SINK (destination address) and K_CE_OVERRIDES (JSON cloudevents attributes) environment variables into any Kubernetes resource which has a spec.template that looks like a Pod (aka PodSpecable).

Prerequisites

  • Install ko
  • Set KO_DOCKER_REPO (e.g. gcr.io/[gcloud-project] or docker.io/<username>)
  • Authenticated with your KO_DOCKER_REPO
  • Install docker

Installation

The SinkBinding type is enabled by default when you install Knative Eventing.

Example

This example shows the SinkBinding that injects$K_SINK and $K_CE_OVERRIDES into select Jobs and direct events to the Event Display Service.

Prepare the heartbeats image

Knative event-sources has a sample of heartbeats event source. You could clone the source codes by

  1. git clone -b "release-0.18" https://github.com/knative/eventing-contrib.git

And then build a heartbeats image and publish to your image repo with

  1. ko publish knative.dev/eventing-contrib/cmd/heartbeats

Creating a namespace

Create a new namespace called sinkbinding-example by entering the following command:

  1. kubectl create namespace sinkbinding-example

Creating the Event Display Service

In this step, you create one event consumer, event-display to verify that SinkBinding is properly working.

To deploy the event-display consumer to your cluster, run the following command:

  1. kubectl -n sinkbinding-example apply -f - << EOF
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: event-display
  6. spec:
  7. replicas: 1
  8. selector:
  9. matchLabels: &labels
  10. app: event-display
  11. template:
  12. metadata:
  13. labels: *labels
  14. spec:
  15. containers:
  16. - name: event-display
  17. image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display
  18. ---
  19. kind: Service
  20. apiVersion: v1
  21. metadata:
  22. name: event-display
  23. spec:
  24. selector:
  25. app: event-display
  26. ports:
  27. - protocol: TCP
  28. port: 80
  29. targetPort: 8080
  30. EOF

Creating the SinkBinding

In order to direct events to our Event Display, we will first create a SinkBinding that will inject $K_SINK and $K_CE_OVERRIDES into select Jobs:

  1. kubectl -n sinkbinding-example apply -f - << EOF
  2. apiVersion: sources.knative.dev/v1beta1
  3. kind: SinkBinding
  4. metadata:
  5. name: bind-heartbeat
  6. spec:
  7. subject:
  8. apiVersion: batch/v1
  9. kind: Job
  10. selector:
  11. matchLabels:
  12. app: heartbeat-cron
  13. sink:
  14. ref:
  15. apiVersion: v1
  16. kind: Service
  17. name: event-display
  18. ceOverrides:
  19. extensions:
  20. sink: bound
  21. EOF
  1. kn source binding create bind-heartbeat \
  2. --namespace sinkbinding-example \
  3. --subject "Job:batch/v1:app=heartbeat-cron" \
  4. --sink http://event-display.svc.cluster.local \
  5. --ce-override "sink=bound"

In this case, we will bind any Job with the labels app: heartbeat-cron.

Creating the CronJob

Now we will use the heartbeats container to send events to $K_SINK every time the CronJob runs:

  1. kubectl -n sinkbinding-example apply -f - << EOF
  2. apiVersion: batch/v1beta1
  3. kind: CronJob
  4. metadata:
  5. name: heartbeat-cron
  6. spec:
  7. # Run every minute
  8. schedule: "* * * * *"
  9. jobTemplate:
  10. metadata:
  11. labels:
  12. app: heartbeat-cron
  13. spec:
  14. template:
  15. spec:
  16. restartPolicy: Never
  17. containers:
  18. - name: single-heartbeat
  19. # This corresponds to a heartbeats image uri you build and publish in the previous step
  20. # e.g. gcr.io/[gcloud-project]/knative.dev/eventing-contrib/cmd/heartbeats
  21. image: <heartbeats_image_uri>
  22. args:
  23. - --period=1
  24. env:
  25. - name: ONE_SHOT
  26. value: "true"
  27. - name: POD_NAME
  28. valueFrom:
  29. fieldRef:
  30. fieldPath: metadata.name
  31. - name: POD_NAMESPACE
  32. valueFrom:
  33. fieldRef:
  34. fieldPath: metadata.namespace
  35. EOF

Verify

View the logs for the event-display event consumer by entering the following command:

  1. kubectl -n sinkbinding-example logs -l app=event-display --tail=200

You should see log lines showing the request headers and body of the event message sent by the heartbeats source to the display function:

  1. ☁️ cloudevents.Event
  2. Validation: valid
  3. Context Attributes,
  4. specversion: 1.0
  5. type: dev.knative.eventing.samples.heartbeat
  6. source: https://knative.dev/eventing-contrib/cmd/heartbeats/#default/heartbeat-cron-1582120020-75qrz
  7. id: 5f4122be-ac6f-4349-a94f-4bfc6eb3f687
  8. time: 2020-02-19T13:47:10.41428688Z
  9. datacontenttype: application/json
  10. Extensions,
  11. beats: true
  12. heart: yes
  13. the: 42
  14. Data,
  15. {
  16. "id": 1,
  17. "label": ""
  18. }

Cleanup

Delete the sinkbinding-example namespace and all of its resources from your cluster by entering the following command:

  1. kubectl delete namespace sinkbinding-example

Reference Documentation

See the SinkBinding specification.

Contact

For any inquiries about this source, please reach out on to the Knative users group.