×

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.

Prerequisites

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.

Creating a trigger using kn

Procedure

To create a trigger, enter the following command:

$ 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>
Example trigger YAML:
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.

Listing triggers using kn

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

    Example output:

    $ 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
  • To print a list of triggers in JSON format, enter the following command:

    $ kn trigger list -o json

Describing a trigger using kn

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>

Example output:

$ kn trigger describe ping
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

Deleting a trigger using kn

Procedure

To delete a trigger, enter the following command:

$ kn trigger delete <TRIGGER-NAME>

Updating a trigger using kn

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]

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

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-

The following example shows how to update the sink of a trigger to svc:new-service:

$ kn trigger update mytrigger --sink svc:new-service

Filtering events using triggers

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

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