SinkBinding

A SinkBinding provides a framework for injecting (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).

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

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

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. ko publish knative.dev/eventing-contrib/cmd/heartbeats

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:

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. kind: SinkBinding
  3. name: bind-heartbeat
  4. spec:
  5. subject:
  6. apiVersion: batch/v1
  7. kind: Job
  8. selector:
  9. matchLabels:
  10. app: heartbeat-cron
  11. sink:
  12. ref:
  13. apiVersion: v1
  14. kind: Service
  15. name: event-display
  16. ceOverrides:
  17. extensions:
  18. sink: bound
  19. EOF
  1. kn source binding create bind-heartbeat \
  2. --subject "Job:batch/v1:app=heartbeat-cron" \
  3. --sink http://event-display.svc.cluster.local \

Creating the CronJob

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

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": ""

Cleanup

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

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