Sink binding

    The 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.

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

    Create a namespace called sinkbinding-example:

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

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

      1. kubectl apply --filename service.yaml

    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:

    2. Apply the file:

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

    Cloning a sample heartbeat cron job

    Knative contains a sample heartbeats event source.

    Prerequisites
    • Ensure that ko publish is set up correctly:
      • 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.20" https://github.com/knative/eventing-contrib.git
      1. $ ko publish knative.dev/eventing-contrib/cmd/heartbeats

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

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

    Create a sink binding:

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

    Delete the sinkbinding-example namespace and all of its resources from your cluster:

      Was this page helpful?

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