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 thekn
CLI.
Procedure
Create a Knative service:
Copy the sample YAML into a
service.yaml
file:apiVersion: serving.knative.dev/v1
metadata:
name: event-display
spec:
template:
spec:
containers:
- image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display
Apply the file:
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:
Copy the sample YAML into a
cronjob.yaml
file:Apply the file:
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]
ordocker.io/<username>
. - You must have authenticated with your
KO_DOCKER_REPO
.
- must be set. For example,
Procedure
Clone the
event-contib
repository:$ git clone -b "release-0.20" https://github.com/knative/eventing-contrib.git
-
$ 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:
Copy the sample YAML into a
cronjob.yaml
file:apiVersion: sources.knative.dev/v1alpha1
kind: SinkBinding
metadata:
name: bind-heartbeat
spec:
subject:
apiVersion: batch/v1
kind: Job
selector:
matchLabels:
app: heartbeat-cron
sink:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: event-display
Apply the file:
kubectl apply --filename heartbeats-source.yaml
Verify that a message was sent to the Knative eventing system by looking at the
event-display
service logs:kubectl logs -l serving.knative.dev/service=event-display -c user-container --since=10m
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.