$ kn trigger create <trigger_name> --broker <broker_name> --filter <key=value> --sink <sink_name>
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.
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 in the procedure below.
An available event consumer, for example, a Knative service.
kn
Create a trigger:
$ kn trigger create <trigger_name> --broker <broker_name> --filter <key=value> --sink <sink_name>
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>
apiVersion: eventing.knative.dev/v1alpha1
kind: Trigger
metadata:
name: trigger-example (1)
spec:
broker: default (2)
subscriber:
ref:
apiVersion: serving.knative.dev/v1
kind: service
name: my-service (3)
1 | The name of the trigger. |
2 | The name of the broker where events will be filtered from. If the broker is not specified, the trigger will revert to using the default broker. |
3 | The name of the service that will consumer filtered events. |
kn
The kn trigger list
command prints a list of available triggers.
Print a list of available triggers:
$ 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 True
kn
in JSON formatPrint a list of triggers in JSON format:
$ kn trigger list -o json
kn
The kn trigger describe
command prints information about a trigger.
Enter the 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
kn
You can use the kn trigger update
command with certain flags to update attributes for a trigger.
Update a trigger to filter exact event attributes that match incoming events, such as type=knative.dev.event
:
$ kn trigger update <trigger_name> --filter type=knative.dev.event
Remove the filter attribute with key type
:
$ kn trigger update mytrigger --filter type-
Update the sink of a trigger to use a service named event-display
:
$ kn trigger update <trigger_name> --sink svc:event-display
In the following trigger example, only events with the attribute type: dev.knative.samples.helloworld
will reach the event sink.
$ kn trigger create <trigger_name> --broker <broker_name> --filter type=dev.knative.samples.helloworld --sink svc:<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 svc:<service_name> \
--filter type=dev.knative.samples.helloworld \
--filter source=dev.knative.samples/helloworldsource \
--filter myextension=my-extension-value