Creating a SinkBinding object
This topic describes how to create a SinkBinding object. SinkBinding resolves a sink as a URI, sets the URI in the environment variable , and adds the URI to a subject using K_SINK
. If the URI changes, SinkBinding updates the value of K_SINK
.
In the following examples, the sink is a Knative Service and the subject is a CronJob. If you have an existing subject and sink, you can replace the examples with your own values.
Before you can create a SinkBinding object:
- You must have Knative Eventing installed on your cluster.
- Optional: If you want to use
kn
commands with SinkBinding, install thekn
CLI.
Optional: Choose SinkBinding namespace selection behavior
The SinkBinding object operates in one of two modes: exclusion
or inclusion
.
The default mode is exclusion
. In exclusion mode, SinkBinding behavior is enabled for the namespace by default. To disallow a namespace from being evaluated for mutation you must exclude it using the label bindings.knative.dev/exclude: true
.
In inclusion mode, SinkBinding behavior is not enabled for the namespace. Before a namespace can be evaluated for mutation, you must explicitly include it using the label bindings.knative.dev/include: true
.
To set the SinkBinding object to inclusion mode:
Change the value of
SINK_BINDING_SELECTION_MODE
fromexclusion
toinclusion
by running:To verify that
SINK_BINDING_SELECTION_MODE
is set as desired, run:kubectl -n knative-eventing set env deployments eventing-webhook --containers="eventing-webhook" --list | grep SINK_BINDING
If you do not have an existing namespace, create a namespace for the SinkBinding object:
kubectl create namespace <namespace>
Where <namespace>
is the namespace that you want your SinkBinding to use. For example, sinkbinding-example
.
Note
If you have selected inclusion mode, you must add the bindings.knative.dev/include: true
label to the namespace to enable SinkBinding behavior.
Create a sink
The sink can be any addressable Kubernetes object that can receive events.
If you do not have an existing sink that you want to connect to the SinkBinding object, create a Knative service.
To create a Knative service you must have Knative Serving installed on your cluster.
kn
Create a Knative service by running:
kn service create <app-name> --image <image-url>
Where:
<app-name>
is the name of the application.<image-url>
is the URL of the image container.
For example:
$ kn service create event-display --image gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display
YAML
Create a YAML file for the Knative service using the following template:
Where:
<app-name>
is the name of the application. For example,event-display
.<image-url>
is the URL of the image container. For example,gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display
.
Apply the YAML file by running the command:
kubectl apply -f <filename>.yaml
Where
<filename>
is the name of the file you created in the previous step.
The subject must be a PodSpecable resource. You can use any PodSpecable resource in your cluster, for example:
Deployment
Job
DaemonSet
StatefulSet
Service.serving.knative.dev
If you do not have an existing PodSpecable subject that you want to use, you can use the following sample to create a CronJob object as the subject. The following CronJob makes a single cloud event that targets K_SINK
and adds any extra overrides given by CE_OVERRIDES
.
Create a YAML file for the CronJob using the following example:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: heartbeat-cron
spec:
schedule: "*/1 * * * *"
jobTemplate:
metadata:
labels:
app: heartbeat-cron
spec:
template:
spec:
restartPolicy: Never
containers:
- name: single-heartbeat
image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/heartbeats
args:
- --period=1
env:
- name: ONE_SHOT
value: "true"
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
Apply the YAML file by running the command:
kubectl apply -f <filename>.yaml
Where
<filename>
is the name of the file you created in the previous step.
Create a SinkBinding object
kn
Create a SinkBinding
object by running:
kn source binding create <name> \
--namespace <namespace> \
--subject "<subject>" \
--sink <sink> \
--ce-override "<cloudevent-overrides>"
Where:
<name>
is the name of the SinkBinding object you want to create.<namespace>
is the namespace you created for your SinkBinding to use.<sink>
is the sink to connect. For examplehttp://event-display.svc.cluster.local
.- Optional:
<cloudevent-overrides>
in the formkey=value
. Cloud Event overrides control the output format and modifications of the event sent to the sink and are applied before sending the event. You can provide this flag multiple times.
For example:
YAML
Create a YAML file for the
SinkBinding
object using the following template:apiVersion: sources.knative.dev/v1
kind: SinkBinding
metadata:
name: <name>
spec:
subject:
apiVersion: <api-version>
kind: <kind>
selector:
matchLabels:
<label-key>: <label-value>
sink:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: <sink>
Where:
<name>
is the name of the SinkBinding object you want to create. For example,bind-heartbeat
.<api-version>
is the API version of the subject. For examplebatch/v1
.<kind>
is the Kind of your subject. For exampleJob
.<label-key>: <label-value>
is a map of key-value pairs to select subjects that have a matching label. For example,app: heartbeat-cron
selects any subject with the labelapp=heartbeat-cron
.<sink>
is the sink to connect. For exampleevent-display
.
For more information about the fields you can configure for the SinkBinding object, see .
Apply the YAML file by running the command:
kubectl apply -f <filename>.yaml
Where
<filename>
is the name of the file you created in the previous step.
Verify that a message was sent to the Knative eventing system by looking at the service logs for your sink:
kubectl logs -l <sink> -c <container> --since=10m
Where:
<sink>
is the name of your sink.<container>
is the name of the container your sink is running in.
For example:
$ kubectl logs -l serving.knative.dev/service=event-display -c user-container --since=10m
Cleanup
To delete the SinkBinding object and all of the related resources in the namespace, delete the namespace by running: