This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.Chapter 10. Knative Eventing
10.1. Using brokers with Knative Eventing Copy linkLink copied to clipboard!
Knative Eventing uses the default broker unless otherwise specified.
If you have cluster administrator permissions, you can create the default broker automatically using namespace annotation.
All other users must create a broker using the manual process as described in this guide.
10.1.1. Creating a broker manually Copy linkLink copied to clipboard!
To create a broker, you must create a service account for each namespace, and give that service account the required RBAC permissions.
Prerequisites
-
Knative Eventing installed, which includes the
ClusterRole.
Procedure
Create the
ServiceAccountobjects.Create the
eventing-broker-ingressobject by entering the following command:oc -n <namespace> create serviceaccount eventing-broker-ingress
$ oc -n <namespace> create serviceaccount eventing-broker-ingressCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create the
eventing-broker-filterobject by entering the following command:oc -n <namespace> create serviceaccount eventing-broker-filter
$ oc -n <namespace> create serviceaccount eventing-broker-filterCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Give the objects that you have created RBAC permissions:
oc -n default create rolebinding eventing-broker-ingress \ --clusterrole=eventing-broker-ingress \ --serviceaccount=default:eventing-broker-ingress
$ oc -n default create rolebinding eventing-broker-ingress \ --clusterrole=eventing-broker-ingress \ --serviceaccount=default:eventing-broker-ingressCopy to Clipboard Copied! Toggle word wrap Toggle overflow oc -n default create rolebinding eventing-broker-filter \ --clusterrole=eventing-broker-filter \ --serviceaccount=default:eventing-broker-filter
$ oc -n default create rolebinding eventing-broker-filter \ --clusterrole=eventing-broker-filter \ --serviceaccount=default:eventing-broker-filterCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a broker by creating and applying a YAML file containing the following:
apiVersion: eventing.knative.dev/v1beta1 kind: Broker metadata: namespace: default name: default
apiVersion: eventing.knative.dev/v1beta1 kind: Broker metadata: namespace: default name: default1 Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- This example uses the name
default, but you can replace this with any other valid name.
10.1.2. Creating a broker automatically using namespace annotation Copy linkLink copied to clipboard!
If you have cluster administrator permissions, you can create a broker automatically by annotating a namespace.
Prerequisites
- Knative Eventing installed.
- Cluster administrator permissions for OpenShift Container Platform.
Procedure
Annotate your namespace by entering the following commands:
oc label namespace default knative-eventing-injection=enabled oc -n default get broker default
$ oc label namespace default knative-eventing-injection=enabled1 $ oc -n default get broker defaultCopy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- Replace
defaultwith the desired namespace.
The line shown in this example will automatically create a broker named
defaultin thedefaultnamespace.
Brokers created due to annotation will not be removed if you remove the annotation. You must manually delete them.
10.1.3. Deleting a broker that was created using namespace annotation Copy linkLink copied to clipboard!
Delete the injected broker from the selected namespace (in this example, the
defaultnamespace):oc -n default delete broker default
$ oc -n default delete broker defaultCopy to Clipboard Copied! Toggle word wrap Toggle overflow
10.2. Using channels Copy linkLink copied to clipboard!
It is possible to sink events from an event source to a Knative Eventing channel. Channels are custom resources (CRs) that define a single event-forwarding and persistence layer. After events have been sent to a channel, these events can be sent to multiple Knative services by using a subscription.
The default configuration for channel instances is defined in the default-ch-webhook ConfigMap. However, developers can still create their own channels directly by instantiating a supported channel object.
10.2.1. Supported channel types Copy linkLink copied to clipboard!
Currently, OpenShift Serverless only supports the use of InMemoryChannel type channels as part of the Knative Eventing Technology Preview.
10.2.2. Using the default InMemoryChannel configuration Copy linkLink copied to clipboard!
InMemoryChannels are for development use only, and should not be used in a production environment.
The following are limitations of InMemoryChannel type channels:
- No event persistence is available. If a Pod goes down, events on that Pod are lost.
- InMemoryChannel type channels do not implement event ordering, so two events that are received in the channel at the same time can be delivered to a subscriber in any order.
-
If a subscriber rejects an event, there are no re-delivery attempts. Instead, the rejected event is sent to a
deadLetterSinkif this sink exists, or is otherwise dropped. For more information about configuring event delivery anddeadLetterSinksettings for a channel, see Using subscriptions to send events from a channel to a sink.
When you install Knative Eventing, the following custom resource definition (CRD) is created automatically:
Creating a channel using the cluster default configuration
Create a generic Channel custom object.
apiVersion: messaging.knative.dev/v1 kind: Channel metadata: name: example-channel namespace: default
apiVersion: messaging.knative.dev/v1 kind: Channel metadata: name: example-channel namespace: defaultCopy to Clipboard Copied! Toggle word wrap Toggle overflow When the Channel object is created, a mutating admission webhook adds a set of
spec.channelTemplateproperties for the Channel object based on the default channel implementation.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
The channel controller then creates the backing channel instance based on that spec.channelTemplate configuration. The spec.channelTemplate properties cannot be changed after creation, because they are set by the default channel mechanism rather than by the user.
When this mechanism is used, two objects are created: a generic channel, and an InMemoryChannel type channel.
The generic channel acts as a proxy that copies its subscriptions to the InMemoryChannel, and sets its status to reflect the status of the backing InMemoryChannel type channel.
Because the channel in this example is created in the default namespace, the channel uses the cluster default, which is InMemoryChannel.
10.3. Using subscriptions to send events from a channel to a sink Copy linkLink copied to clipboard!
Subscriptions deliver events to event sinks from a Channel.
10.3.1. Creating a subscription Copy linkLink copied to clipboard!
You can create a subscription to connect a service or other event sink to a channel.
Knative Eventing is a Technology Preview feature. The InMemoryChannel type is provided for development use only, and should not be used in a production environment.
Prerequisites
- You must have a current installation of OpenShift Serverless, including Knative Serving and Eventing, in your OpenShift Container Platform cluster. This can be installed by a cluster administrator.
- If you do not have an existing sink that you wish to use, create a Service to use as a sink by following the documentation on Creating and managing serverless applications.
- You must have a channel to connect your subscription to. See Using channels with Knative Eventing.
Procedure
Create a Subscription object to connect a channel to a service, by creating a YAML file containing the following:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- Name of the subscription.
- 2
- Configuration settings for the channel that the subscription connects to.
- 3
- Configuration settings for event delivery. This tells the subscription what happens to events that cannot be delivered to the subscriber. When this is configured, events that failed to be consumed are sent to the
deadLetterSink. The event is dropped, no re-delivery of the event is attempted, and an error is logged in the system. ThedeadLetterSinkvalue must be a Destination. - 4
- Configuration settings for the subscriber. This is the event sink that events are delivered to from the channel.
Apply the YAML file by entering:
oc apply -f <FILENAME>
$ oc apply -f <FILENAME>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
10.4. Using triggers Copy linkLink copied to clipboard!
All events which are sent to a channel or broker will be sent to all subscribers of that channel or broker by default.
Using triggers allows you to filter events from a channel or broker, so that subscribers will only receive a subset of events based on your defined criteria.
The Knative CLI provides a set of kn trigger commands that can be used to create and manage triggers.
10.4.1. Prerequisites Copy linkLink copied to clipboard!
Before you can use triggers, you will need:
-
Knative Eventing and
kninstalled. An available broker, either the
defaultbroker or one that you have created.You can create the
defaultbroker either by following the instructions on Using brokers with Knative Eventing, or by using the--inject-brokerflag while creating a trigger. Use of this flag is described in the procedure below.- An available event consumer, for example, a Knative service.
10.4.2. Creating a trigger using kn Copy linkLink copied to clipboard!
Procedure
To create a trigger, enter the following command:
kn trigger create <TRIGGER-NAME> --broker <BROKER-NAME> --filter <KEY=VALUE> --sink <SINK>
$ kn trigger create <TRIGGER-NAME> --broker <BROKER-NAME> --filter <KEY=VALUE> --sink <SINK>
To create a trigger and also create the default broker using broker injection, enter the following command:
kn trigger create <TRIGGER-NAME> --inject-broker --filter <KEY=VALUE> --sink <SINK>
$ kn trigger create <TRIGGER-NAME> --inject-broker --filter <KEY=VALUE> --sink <SINK>
Example trigger YAML:
10.4.3. Listing triggers using kn Copy linkLink copied to clipboard!
The kn trigger list command prints a list of available triggers.
Procedure
To print a list of available triggers, enter the following command:
kn trigger list
$ kn trigger listCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output:
kn trigger list
$ kn trigger list NAME BROKER SINK AGE CONDITIONS READY REASON email default svc:edisplay 4s 5 OK / 5 True ping default svc:edisplay 32s 5 OK / 5 TrueCopy to Clipboard Copied! Toggle word wrap Toggle overflow To print a list of triggers in JSON format, enter the following command:
kn trigger list -o json
$ kn trigger list -o jsonCopy to Clipboard Copied! Toggle word wrap Toggle overflow
10.4.4. Describing a trigger using kn Copy linkLink copied to clipboard!
The kn trigger describe command prints information about a trigger.
Procedure
To print information about a trigger, enter the following command:
kn trigger describe <TRIGGER-NAME>
$ kn trigger describe <TRIGGER-NAME>
Example output:
10.4.5. Deleting a trigger using kn Copy linkLink copied to clipboard!
Procedure
To delete a trigger, enter the following command:
kn trigger delete <TRIGGER-NAME>
$ kn trigger delete <TRIGGER-NAME>
10.4.6. Updating a trigger using kn Copy linkLink copied to clipboard!
You can use the kn trigger update command with certain flags to quickly update attributes of a trigger.
Procedure
To update a trigger, enter the following command:
kn trigger update NAME --filter KEY=VALUE --sink SINK [flags]
$ kn trigger update NAME --filter KEY=VALUE --sink SINK [flags]
You can update a trigger to filter exact event attributes that match incoming events, such as type=knative.dev.event. For example:
kn trigger update mytrigger --filter type=knative.dev.event
$ kn trigger update mytrigger --filter type=knative.dev.event
You can also remove a filter attribute from a trigger. For example, you can remove the filter attribute with key type:
kn trigger update mytrigger --filter type-
$ kn trigger update mytrigger --filter type-
The following example shows how to update the sink of a trigger to svc:new-service:
kn trigger update mytrigger --sink svc:new-service
$ kn trigger update mytrigger --sink svc:new-service
10.4.7. Filtering events using triggers Copy linkLink copied to clipboard!
In the following trigger example, only events with attribute type: dev.knative.samples.helloworld will reach the event consumer.
kn trigger create foo --broker default --filter type=dev.knative.samples.helloworld --sink svc:mysvc
$ kn trigger create foo --broker default --filter type=dev.knative.samples.helloworld --sink svc:mysvc
You can also filter events using multiple attributes. The following example shows how to filter events using the type, source, and extension attributes.
kn trigger create foo --broker default --sink svc:mysvc \ --filter type=dev.knative.samples.helloworld \ --filter source=dev.knative.samples/helloworldsource \ --filter myextension=my-extension-value
$ kn trigger create foo --broker default --sink svc:mysvc \
--filter type=dev.knative.samples.helloworld \
--filter source=dev.knative.samples/helloworldsource \
--filter myextension=my-extension-value
10.5. Using SinkBinding Copy linkLink copied to clipboard!
SinkBinding is used to connect event producers, or event sources, to an event consumer, or event sink, for example, a Knative service or application.
Both of the following procedures require 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.
10.5.1. Using SinkBinding with the Knative CLI (kn) Copy linkLink copied to clipboard!
This guide describes the steps required to create, manage, and delete a SinkBinding instance using kn commands.
Prerequisites
- You have Knative Serving and Eventing installed.
-
You have the
knCLI installed.
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:
kn service create event-display --image quay.io/openshift-knative/knative-eventing-sources-event-display:latest
$ kn service create event-display --image quay.io/openshift-knative/knative-eventing-sources-event-display:latestCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a SinkBinding that directs events to the service:
kn source binding create bind-heartbeat --subject Job:batch/v1:app=heartbeat-cron --sink svc:event-display
$ kn source binding create bind-heartbeat --subject Job:batch/v1:app=heartbeat-cron --sink svc:event-displayCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a CronJob.
Create a file named
heartbeats-cronjob.yamland copy the following sample code into it:Copy to Clipboard Copied! Toggle word wrap Toggle overflow After you have created the
heartbeats-cronjob.yamlfile, apply it by entering:oc apply --filename heartbeats-cronjob.yaml
$ oc apply --filename heartbeats-cronjob.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Check that the controller is mapped correctly by entering the following command and inspecting the output:
kn source binding describe bind-heartbeat
$ kn source binding describe bind-heartbeatCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification steps
You can verify that the Kubernetes events were sent to the Knative event sink by looking at the message dumper function logs.
View the message dumper function logs by entering the following commands:
oc get pods
$ oc get podsCopy to Clipboard Copied! Toggle word wrap Toggle overflow oc logs $(oc get pod -o name | grep event-display) -c user-container
$ oc logs $(oc get pod -o name | grep event-display) -c user-containerCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
10.5.2. Using SinkBinding with the YAML method Copy linkLink copied to clipboard!
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.
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:Copy to Clipboard Copied! Toggle word wrap Toggle overflow After you have created the
service.yamlfile, apply it by entering:oc apply -f service.yaml
$ oc apply -f service.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Create a SinkBinding that directs events to the service.
Create a file named
sinkbinding.yamland copy the following sample code into it:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
- 1
- In this example, any Job with the label
app: heartbeat-cronwill be bound to the event sink.After you have created the
sinkbinding.yamlfile, apply it by entering:oc apply -f sinkbinding.yaml
$ oc apply -f sinkbinding.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Create a CronJob.
Create a file named
heartbeats-cronjob.yamland copy the following sample code into it:Copy to Clipboard Copied! Toggle word wrap Toggle overflow After you have created the
heartbeats-cronjob.yamlfile, apply it by entering:oc apply -f heartbeats-cronjob.yaml
$ oc apply -f heartbeats-cronjob.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Check that the controller is mapped correctly by entering the following command and inspecting the output:
oc get sinkbindings.sources.knative.dev bind-heartbeat -oyaml
$ oc get sinkbindings.sources.knative.dev bind-heartbeat -oyamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification steps
You can verify that the Kubernetes events were sent to the Knative event sink by looking at the message dumper function logs.
View the message dumper function logs by entering the following commands:
oc get pods
$ oc get podsCopy to Clipboard Copied! Toggle word wrap Toggle overflow oc logs $(oc get pod -o name | grep event-display) -c user-container
$ oc logs $(oc get pod -o name | grep event-display) -c user-containerCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow