mirror of
https://github.com/openshift/openshift-docs.git
synced 2026-02-06 06:46:26 +01:00
232 lines
5.0 KiB
Plaintext
232 lines
5.0 KiB
Plaintext
// Module included in the following assemblies:
|
|
//
|
|
// serverless/event_workflows/serverless-sinkbinding.adoc
|
|
|
|
[id="serverless-sinkbinding-yaml_{context}"]
|
|
= Using SinkBinding with the YAML method
|
|
|
|
This guide describes the steps required to create, manage, and delete a SinkBinding instance using YAML files.
|
|
|
|
.Prerequisites
|
|
|
|
* You have Knative Serving and Eventing installed.
|
|
|
|
[NOTE]
|
|
====
|
|
The following procedure requires you to create YAML files.
|
|
|
|
If you change the names of the YAML files from those used in the examples, you must ensure that you also update the corresponding CLI commands.
|
|
====
|
|
|
|
[IMPORTANT]
|
|
====
|
|
Before developers can use a SinkBinding, cluster administrators must label the namespace that will be configured in the SinkBinding with `bindings.knative.dev/include:"true"`:
|
|
|
|
[source,terminal]
|
|
----
|
|
$ oc label namespace <namespace> bindings.knative.dev/include=true
|
|
----
|
|
====
|
|
|
|
.Procedure
|
|
|
|
. To check that SinkBinding is set up correctly, create a Knative event display service, or event sink, that dumps incoming messages to its log.
|
|
.. Copy the following sample YAML into a file named `service.yaml`:
|
|
+
|
|
|
|
[source,yaml]
|
|
----
|
|
apiVersion: serving.knative.dev/v1
|
|
kind: Service
|
|
metadata:
|
|
name: event-display
|
|
spec:
|
|
template:
|
|
spec:
|
|
containers:
|
|
- image: quay.io/openshift-knative/knative-eventing-sources-event-display:latest
|
|
----
|
|
|
|
.. After you have created the `service.yaml` file, apply it by entering:
|
|
+
|
|
|
|
[source,terminal]
|
|
----
|
|
$ oc apply -f service.yaml
|
|
----
|
|
|
|
. Create a SinkBinding that directs events to the service.
|
|
.. Create a file named `sinkbinding.yaml` and copy the following sample code into it:
|
|
+
|
|
|
|
[source,yaml]
|
|
----
|
|
apiVersion: sources.knative.dev/v1alpha1
|
|
kind: SinkBinding
|
|
metadata:
|
|
name: bind-heartbeat
|
|
spec:
|
|
subject:
|
|
apiVersion: batch/v1
|
|
kind: Job <1>
|
|
selector:
|
|
matchLabels:
|
|
app: heartbeat-cron
|
|
|
|
sink:
|
|
ref:
|
|
apiVersion: serving.knative.dev/v1
|
|
kind: Service
|
|
name: event-display
|
|
----
|
|
<1> In this example, any Job with the label `app: heartbeat-cron` will be bound to the event sink.
|
|
|
|
.. After you have created the `sinkbinding.yaml` file, apply it by entering:
|
|
+
|
|
|
|
[source,terminal]
|
|
----
|
|
$ oc apply -f sinkbinding.yaml
|
|
----
|
|
|
|
. Create a CronJob.
|
|
.. Create a file named `heartbeats-cronjob.yaml` and copy the following sample code into it:
|
|
+
|
|
|
|
[source,yaml]
|
|
----
|
|
apiVersion: batch/v1beta1
|
|
kind: CronJob
|
|
metadata:
|
|
name: heartbeat-cron
|
|
spec:
|
|
spec:
|
|
# Run every minute
|
|
schedule: "* * * * *"
|
|
jobTemplate:
|
|
metadata:
|
|
labels:
|
|
app: heartbeat-cron
|
|
bindings.knative.dev/include: "true"
|
|
spec:
|
|
template:
|
|
spec:
|
|
restartPolicy: Never
|
|
containers:
|
|
- name: single-heartbeat
|
|
image: quay.io/openshift-knative/knative-eventing-sources-heartbeats:latest
|
|
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
|
|
----
|
|
|
|
+
|
|
[IMPORTANT]
|
|
====
|
|
To use SinkBinding, you must manually add a `bindings.knative.dev/include=true` label to your Knative resources.
|
|
|
|
For example, to add this label to a CronJob instance, add the following lines to the Job resource YAML definition:
|
|
|
|
[source,yaml]
|
|
----
|
|
jobTemplate:
|
|
metadata:
|
|
labels:
|
|
app: heartbeat-cron
|
|
bindings.knative.dev/include: "true"
|
|
----
|
|
|
|
====
|
|
|
|
+
|
|
.. After you have created the `heartbeats-cronjob.yaml` file, apply it by entering:
|
|
+
|
|
|
|
[source,terminal]
|
|
----
|
|
$ oc apply -f heartbeats-cronjob.yaml
|
|
----
|
|
|
|
. Check that the controller is mapped correctly by entering the following command and inspecting the output:
|
|
+
|
|
|
|
[source,terminal]
|
|
----
|
|
$ oc get sinkbindings.sources.knative.dev bind-heartbeat -oyaml
|
|
----
|
|
|
|
+
|
|
.Example output
|
|
[source,yaml]
|
|
----
|
|
spec:
|
|
sink:
|
|
ref:
|
|
apiVersion: serving.knative.dev/v1
|
|
kind: Service
|
|
name: event-display
|
|
namespace: default
|
|
subject:
|
|
apiVersion: batch/v1
|
|
kind: Job
|
|
namespace: default
|
|
selector:
|
|
matchLabels:
|
|
app: heartbeat-cron
|
|
----
|
|
|
|
.Verification steps
|
|
|
|
You can verify that the Kubernetes events were sent to the Knative event sink by looking at the message dumper function logs.
|
|
|
|
. Enter the command:
|
|
+
|
|
|
|
[source,terminal]
|
|
----
|
|
$ oc get pods
|
|
----
|
|
|
|
. Enter the command:
|
|
+
|
|
|
|
[source,terminal]
|
|
----
|
|
$ oc logs $(oc get pod -o name | grep event-display) -c user-container
|
|
----
|
|
|
|
+
|
|
.Example output
|
|
|
|
[source,terminal]
|
|
----
|
|
☁️ cloudevents.Event
|
|
Validation: valid
|
|
Context Attributes,
|
|
specversion: 1.0
|
|
type: dev.knative.eventing.samples.heartbeat
|
|
source: https://knative.dev/eventing-contrib/cmd/heartbeats/#event-test/mypod
|
|
id: 2b72d7bf-c38f-4a98-a433-608fbcdd2596
|
|
time: 2019-10-18T15:23:20.809775386Z
|
|
contenttype: application/json
|
|
Extensions,
|
|
beats: true
|
|
heart: yes
|
|
the: 42
|
|
Data,
|
|
{
|
|
"id": 1,
|
|
"label": ""
|
|
}
|
|
----
|