Sink binding

version

The SinkBinding custom object supports decoupling event production from delivery addressing.

You can use sink binding to connect Kubernetes resources that embed a PodSpec and want to produce events, such as an event source, to an addressable Kubernetes object that can receive events, also known as an event sink.

Sink binding can be used to create new event sources using any of the familiar compute objects that Kubernetes makes available. For example, Deployment, Job, DaemonSet, or StatefulSet objects, or Knative abstractions, such as Service or Configuration objects, can be used.

Sink binding injects environment variables into the PodTemplateSpec of the event sink, so that the application code does not need to interact directly with the Kubernetes API to locate the event destination.

Sink binding operates in one of two modes; Inclusion or Exclusion. You can set the mode by modifying the SINK_BINDING_SELECTION_MODE of the eventing-webhook deployment accordingly. The mode determines the default scope of the webhook.

By default, the webhook is set to exclusion mode, which means that any namespace that does not have the label bindings.knative.dev/exclude: true will be subject to mutation evalutation.

If SINK_BINDING_SELECTION_MODE is set to inclusion, only the resources in a namespace labelled with bindings.knative.dev/include: true will be considered. In inclusion mode, any SinkBinding resource created will automatically label the subject namespace with bindings.knative.dev/include: true for inclusion in the potential environment variable inclusions.

Getting started

The following procedures show how you can create a sink binding and connect it to a service and event source in your cluster.

Creating a namespace

Create a namespace called sinkbinding-example:

  1. kubectl create namespace sinkbinding-example

Creating a Knative service

Create a Knative service if you do not have an existing event sink that you want to connect to the sink binding.

Prerequisites

  • You must have Knative Serving installed on your cluster.
  • Optional: If you want to use kn commands with sink binding, you must install the kn CLI.

Procedure

Create a Knative service:

  1. kn service create hello --image gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display --env RESPONSE="Hello Serverless!"
  1. Copy the sample YAML into a service.yaml file:

    1. apiVersion: serving.knative.dev/v1
    2. kind: Service
    3. metadata:
    4. name: event-display
    5. spec:
    6. template:
    7. spec:
    8. containers:
    9. - image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display
  2. Apply the file:

    1. kubectl apply --filename service.yaml

Creating a cron job

Create a cron job if you do not have an existing event source that you want to connect to the sink binding.

Create a CronJob object:

  1. Copy the sample YAML into a cronjob.yaml file:

    1. apiVersion: batch/v1beta1
    2. kind: CronJob
    3. metadata:
    4. name: heartbeat-cron
    5. spec:
    6. # Run every minute
    7. schedule: "*/1 * * * *"
    8. jobTemplate:
    9. metadata:
    10. labels:
    11. app: heartbeat-cron
    12. spec:
    13. template:
    14. spec:
    15. restartPolicy: Never
    16. containers:
    17. - name: single-heartbeat
    18. image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/heartbeats
    19. args:
    20. - --period=1
    21. env:
    22. - name: ONE_SHOT
    23. value: "true"
    24. - name: POD_NAME
    25. valueFrom:
    26. fieldRef:
    27. fieldPath: metadata.name
    28. - name: POD_NAMESPACE
    29. valueFrom:
    30. fieldRef:
    31. fieldPath: metadata.namespace
  2. Apply the file:

    1. kubectl apply --filename heartbeats-source.yaml

Cloning a sample heartbeat cron job

Knative event-contrib contains a sample heartbeats event source.

Prerequisites
  • Ensure that ko publish is set up correctly:
    • KO_DOCKER_REPO must be set. For example, gcr.io/[gcloud-project] or docker.io/<username>.
    • You must have authenticated with your KO_DOCKER_REPO.
Procedure
  1. Clone the event-contib repository:

    1. $ git clone -b "release-0.21" https://github.com/knative/eventing-contrib.git
  2. Build a heartbeats image, and publish the image to your image repository:

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

Creating a SinkBinding object

Create a SinkBinding object that directs events from your cron job to the event sink.

Prerequisites

  • You must have Knative Eventing installed on your cluster.
  • Optional: If you want to use kn commands with sink binding, you must install the kn CLI.

Procedure

Create a sink binding:

  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"
  1. Copy the sample YAML into a cronjob.yaml file:

    1. apiVersion: sources.knative.dev/v1alpha1
    2. kind: SinkBinding
    3. metadata:
    4. name: bind-heartbeat
    5. spec:
    6. subject:
    7. apiVersion: batch/v1
    8. kind: Job
    9. selector:
    10. matchLabels:
    11. app: heartbeat-cron
    12. sink:
    13. ref:
    14. apiVersion: serving.knative.dev/v1
    15. kind: Service
    16. name: event-display
  2. Apply the file:

    1. kubectl apply --filename heartbeats-source.yaml

Verification steps

  1. Verify that a message was sent to the Knative eventing system by looking at the event-display service logs:

    1. kubectl logs -l serving.knative.dev/service=event-display -c user-container --since=10m
  2. Observe the 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:

  1. kubectl delete namespace sinkbinding-example

Feedback

Was this page helpful?

Glad to hear it! Please tell us how we can improve.

Sorry to hear that. Please tell us how we can improve.