×

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
  • 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>
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
  1. Print a list of available triggers:

    $ kn trigger list
    Example output
    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

Listing triggers using kn in JSON format

Procedure
  1. Print a list of triggers in JSON format:

    $ kn trigger list -o json

Describing a trigger using kn

The kn trigger describe command prints information about a trigger.

Procedure
  • Enter the command:

    $ kn trigger describe <trigger_name>
    Example output
    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
  • Delete a trigger:

$ kn trigger delete <trigger_name>

Updating a trigger using kn

You can use the kn trigger update command with certain flags to update attributes for a trigger.

Example
  1. 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
  2. Remove the filter attribute with key type:

    $ kn trigger update mytrigger --filter type-
  3. Update the sink of a trigger to use a service named event-display:

    $ kn trigger update <trigger_name> --sink svc:event-display

Filtering events using triggers

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