Brokers can be used in combination with triggers to deliver events from an event source to an event sink.
Events can be sent from an event source to a broker as an HTTP POST request.
After events have entered the broker, they can be filtered by CloudEvent attributes using triggers, and sent as an HTTP POST request to an event sink.
OpenShift Serverless provides a default
Knative broker that can be created by using the Knative CLI.
You can also create the default
broker by adding the eventing.knative.dev/injection=enabled
label to a namespace if you are a cluster administrator, or by adding the eventing.knative.dev/injection: enabled
annotation to a trigger if you are a developer.
Although both developers and cluster administrators can add a broker by injection, only cluster administrators can permanently delete brokers that were created using this method. |
The OpenShift Serverless Operator and Knative Eventing are installed on your OpenShift Container Platform cluster.
You have installed the kn
CLI.
Create the default
broker:
$ kn broker create default
Use the kn
command to list all existing brokers:
$ kn broker list
NAME URL AGE CONDITIONS READY REASON
default http://broker-ingress.knative-eventing.svc.cluster.local/test/default 45s 5 OK / 5 True
Optional: If you are using the OpenShift Container Platform web console, you can navigate to the Topology view in the Developer perspective, and observe that the broker exists:
You can create a broker by adding the eventing.knative.dev/injection: enabled
annotation to a Trigger
object.
If you create a broker by using the |
The OpenShift Serverless Operator and Knative Eventing are installed on your OpenShift Container Platform cluster.
Create a Trigger
object as a .yaml
file that has the eventing.knative.dev/injection: enabled
annotation:
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
annotations:
eventing.knative.dev/injection: enabled
name: <trigger-name>
spec:
broker: default
subscriber: (1)
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: <service-name>
1 | Specify details about the event sink, or subscriber, that the trigger sends events to. |
Apply the .yaml
file:
$ oc apply -f <filename>
You can verify that the broker has been created successfully by using the oc
CLI, or by observing it in the Topology view in the web console.
Use the oc
command to get the broker:
$ oc -n <namespace> get broker default
NAME READY REASON URL AGE
default True http://broker-ingress.knative-eventing.svc.cluster.local/test/default 3m56s
Navigate to the Topology view in the web console, and observe that the broker exists:
If you have cluster administrator permissions, you can create the default
broker automatically by labeling a namespace.
Brokers created using this method will not be removed if you remove the label. You must manually delete them. |
The OpenShift Serverless Operator and Knative Eventing are installed on your OpenShift Container Platform cluster.
You have cluster administrator permissions for OpenShift Container Platform.
Label a namespace with eventing.knative.dev/injection=enabled
:
$ oc label namespace <namespace> eventing.knative.dev/injection=enabled
You can verify that the broker has been created successfully by using the oc
CLI, or by observing it in the Topology view in the web console.
Use the oc
command to get the broker:
$ oc -n <namespace> get broker <broker_name>
$ oc -n default get broker default
NAME READY REASON URL AGE
default True http://broker-ingress.knative-eventing.svc.cluster.local/test/default 3m56s
Navigate to the Topology view in the web console, and observe that the broker exists:
The kn
CLI provides commands that can be used to list, describe, update, and delete brokers.
Cluster administrators can also permanently delete a broker that was created using injection.
The OpenShift Serverless Operator, Knative Serving and Knative Eventing are installed on your OpenShift Container Platform cluster.
You have installed the kn
CLI.
List all existing brokers:
$ kn broker list
NAME URL AGE CONDITIONS READY REASON
default http://broker-ingress.knative-eventing.svc.cluster.local/test/default 45s 5 OK / 5 True
The OpenShift Serverless Operator, Knative Serving and Knative Eventing are installed on your OpenShift Container Platform cluster.
You have installed the kn
CLI.
Describe an existing broker:
$ kn broker describe <broker_name>
$ kn broker describe default
Name: default
Namespace: default
Annotations: eventing.knative.dev/broker.class=MTChannelBasedBroker, eventing.knative.dev/creato ...
Age: 22s
Address:
URL: http://broker-ingress.knative-eventing.svc.cluster.local/default/default
Conditions:
OK TYPE AGE REASON
++ Ready 22s
++ Addressable 22s
++ FilterReady 22s
++ IngressReady 22s
++ TriggerChannelReady 22s
Brokers created by injection, by using a namespace label or trigger annotation, are not deleted permanently if a developer removes the label or annotation. A user with cluster administrator permissions must manually delete these brokers.
Remove the eventing.knative.dev/injection=enabled
label from the namespace:
$ oc label namespace <namespace> eventing.knative.dev/injection-
Removing the annotation prevents Knative from recreating the broker after you delete it.
Delete the broker from the selected namespace:
$ oc -n <namespace> delete broker <broker_name>
Use the oc
command to get the broker:
$ oc -n <namespace> get broker <broker_name>
$ oc -n default get broker default
No resources found.
Error from server (NotFound): brokers.eventing.knative.dev "default" not found
Using triggers enables you to filter events from the broker for delivery to event sinks.
Before you can use triggers, you will need:
Knative Eventing and kn
installed.
An available broker, either the default
broker or one that you have created.
You can create the default
broker either by following the instructions on Using brokers with Knative Eventing, or by using the --inject-broker
flag while creating a trigger. Use of this flag is described later in this section.
An available event consumer, such as a Knative service.
After you have created a broker, you can create a trigger in the web console Developer perspective.
The OpenShift Serverless Operator, Knative Serving, and Knative Eventing are installed on your OpenShift Container Platform cluster.
You have logged in to the web console.
You are in the Developer perspective.
You have created a project or have access to a project with the appropriate roles and permissions to create applications and other workloads in OpenShift Container Platform.
You have created a broker and a Knative service or other event sink to connect to the trigger.
In the Developer perspective, navigate to the Topology page.
Hover over the broker that you want to create a trigger for, and drag the arrow. The Add Trigger option is displayed.
Click Add Trigger.
Select your sink as a Subscriber from the drop-down list.
Click Add.
After the subscription has been created, it is represented as a line that connects the broker to the service in the Topology view:
You can delete triggers in the web console Developer perspective.
To delete a trigger using the Developer perspective, ensure that you have logged in to the web console.
In the Developer perspective, navigate to the Topology page.
Click on the trigger that you want to delete.
In the Actions context menu, select Delete Trigger.
You can create a trigger by using the kn trigger create
command.
Create a trigger:
$ kn trigger create <trigger_name> --broker <broker_name> --filter <key=value> --sink <sink_name>
Alternatively, you can create a trigger and simultaneously create the default
broker using broker injection:
$ kn trigger create <trigger_name> --inject-broker --filter <key=value> --sink <sink_name>
By default, triggers forward all events sent to a broker to sinks that are subscribed to that broker.
Using the --filter
attribute for triggers allows you to filter events from a broker, so that subscribers will only receive a subset of events based on your defined criteria.
The kn trigger list
command prints a list of available triggers.
To print a list of available triggers, enter the following command:
$ kn trigger list
NAME BROKER SINK AGE CONDITIONS READY REASON
email default ksvc:edisplay 4s 5 OK / 5 True
ping default ksvc:edisplay 32s 5 OK / 5 True
Optional: Print a list of triggers in JSON format:
$ kn trigger list -o json
You can use the kn trigger describe
command to print information about a trigger.
To print information about a trigger, enter the following command:
$ kn trigger describe <trigger_name>
Name: ping
Namespace: default
Labels: eventing.knative.dev/broker=default
Annotations: eventing.knative.dev/creator=kube:admin, eventing.knative.dev/lastModifier=kube:admin
Age: 2m
Broker: default
Filter:
type: dev.knative.event
Sink:
Name: edisplay
Namespace: default
Resource: Service (serving.knative.dev/v1)
Conditions:
OK TYPE AGE REASON
++ Ready 2m
++ BrokerReady 2m
++ DependencyReady 2m
++ Subscribed 2m
++ SubscriberResolved 2m
In the following trigger example, only events with attribute type: dev.knative.samples.helloworld
will reach the event consumer.
$ kn trigger create <trigger_name> --broker <broker_name> --filter type=dev.knative.samples.helloworld --sink ksvc:<service_name>
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 <trigger_name> --broker <broker_name> --sink ksvc:<service_name> \ --filter type=dev.knative.samples.helloworld \ --filter source=dev.knative.samples/helloworldsource \ --filter myextension=my-extension-value
You can use the kn trigger update
command with certain flags to quickly update attributes of a trigger.
Update a trigger:
$ kn trigger update <trigger_name> --filter <key=value> --sink <sink_name> [flags]
You can update a trigger to filter exact event attributes that match incoming events. For example, using the type
attribute:
$ kn trigger update mytrigger --filter type=knative.dev.event
You can remove a filter attribute from a trigger. For example, you can remove the filter attribute with key type
:
$ kn trigger update mytrigger --filter type-
You can use the --sink
parameter to change the event sink of a trigger:
$ kn trigger update <trigger_name> --sink ksvc:my-event-sink