Using Triggers

A trigger represents a desire to subscribe to events from a specific broker.

The subscriber value must be a Destination.

Example Triggers

The following trigger receives all the events from the default broker and delivers them to the Knative Serving service my-service:

  1. Create a YAML file using the following example:

    1. apiVersion: eventing.knative.dev/v1
    2. kind: Trigger
    3. metadata:
    4. name: my-service-trigger
    5. spec:
    6. broker: default
    7. subscriber:
    8. ref:
    9. apiVersion: serving.knative.dev/v1
    10. kind: Service
    11. name: my-service
  2. Apply the YAML file by running the command:

    1. kubectl apply -f <filename>.yaml

    Where <filename> is the name of the file you created in the previous step.

The following trigger receives all the events from the default broker and delivers them to the custom path /my-custom-path for the Kubernetes service my-service:

  1. Create a YAML file using the following example:

    1. apiVersion: eventing.knative.dev/v1
    2. kind: Trigger
    3. metadata:
    4. name: my-service-trigger
    5. spec:
    6. broker: default
    7. subscriber:
    8. ref:
    9. apiVersion: v1
    10. kind: Service
    11. name: my-service
    12. uri: /my-custom-path
  2. Apply the YAML file by running the command:

    1. kubectl apply -f <filename>.yaml

    Where <filename> is the name of the file you created in the previous step.

Trigger filtering

Various forms of filtering based on any number of CloudEvents attributes and extensions are supported. If multiple filters are provided, all of them must evaluate to true in order for the event to be passed to the subscriber of the trigger. Note that we do not support filtering on the data field of CloudEvents.

Important

The filters described in this section are currently only supported in the Apache Kafka Broker and the MTChannelBasedBroker. For other brokers, please refer to the Legacy attribute filter.

Example

This example filters events from the default broker that are of type dev.knative.foo.bar and have the extension myextension which ends with `-extensions.

  1. Create a YAML file using the following example:

    1. apiVersion: eventing.knative.dev/v1
    2. kind: Trigger
    3. metadata:
    4. name: my-service-trigger
    5. spec:
    6. broker: default
    7. filters:
    8. exact:
    9. type: dev.knative.foo.bar
    10. suffix:
    11. myextension: -value
    12. subscriber:
    13. ref:
    14. apiVersion: serving.knative.dev/v1
    15. kind: Service
    16. name: my-service
  2. Apply the YAML file by running the command:

    1. kubectl apply -f <filename>.yaml

    Where <filename> is the name of the file you created in the previous step.

Supported filter dialects

exact

CloudEvent attribute String value must exactly match the specified String value. Matching is case-sensitive. If the attribute is not a String, the filter will compare a String representation of the attribute to the specified String value.

  1. apiVersion: eventing.knative.dev/v1
  2. kind: Trigger
  3. metadata:
  4. ...
  5. spec:
  6. ...
  7. filters:
  8. - exact:
  9. type: com.github.push

prefix

CloudEvent attribute String value must start with the specified String value. Matching is case-sensitive. If the attribute is not a String, the filter will compare a String representation of the attribute to the specified String value.

  1. apiVersion: eventing.knative.dev/v1
  2. kind: Trigger
  3. metadata:
  4. ...
  5. spec:
  6. ...
  7. filters:
  8. - prefix:
  9. type: com.github.

suffix

CloudEvent attribute String value must end with the specified String value. Matching is case-sensitive. If the attribute is not a String, the filter will compare a String representation of the attribute to the specified String value.

  1. apiVersion: eventing.knative.dev/v1
  2. kind: Trigger
  3. metadata:
  4. ...
  5. spec:
  6. ...
  7. filters:
  8. - suffix:
  9. type: .created

all

All nested filter expressions must evaluate to true.

  1. apiVersion: eventing.knative.dev/v1
  2. kind: Trigger
  3. metadata:
  4. ...
  5. spec:
  6. ...
  7. filters:
  8. - all:
  9. - exact:
  10. type: com.github.push
  11. - exact:
  12. subject: https://github.com/cloudevents/spec

any

At least one nested filter expression must evaluate to true.

  1. apiVersion: eventing.knative.dev/v1
  2. kind: Trigger
  3. metadata:
  4. ...
  5. spec:
  6. ...
  7. filters:
  8. - any:
  9. - exact:
  10. type: com.github.push
  11. - exact:
  12. subject: https://github.com/cloudevents/spec

not

The nested expression evaluated must evaluate to false.

  1. apiVersion: eventing.knative.dev/v1
  2. kind: Trigger
  3. metadata:
  4. ...
  5. spec:
  6. ...
  7. filters:
  8. - not:
  9. exact:
  10. type: com.github.push

cesql

The provided CloudEvents SQL Expression must evaluate to true.

Important

Knative 1.15+ only supports the CloudEvents SQL v1.0 specification. Any CESQL expressions written prior to Knative v1.15 should be verified, as there were some changes in the CESQL specification.

  1. apiVersion: eventing.knative.dev/v1
  2. kind: Trigger
  3. metadata:
  4. ...
  5. spec:
  6. ...
  7. filters:
  8. - cesql: "source LIKE '%commerce%' AND type IN ('order.created', 'order.updated', 'order.canceled')"

Legacy attributes filter

The legacy attributes filter provides exact match filtering on any number of CloudEvents attributes as well as extensions. It’s semantics and behaviour are the same as the exact filter dialect and wherever possible users should move to the exact filter. However, for backwards compatability the attributes filter will continue to work for all users. The following example filters events from the default broker that are of type dev.knative.foo.bar and have the extension myextension with the value my-extension-value.

  1. apiVersion: eventing.knative.dev/v1
  2. kind: Trigger
  3. metadata:
  4. name: my-service-trigger
  5. spec:
  6. broker: default
  7. filter:
  8. attributes:
  9. type: dev.knative.foo.bar
  10. myextension: my-extension-value
  11. subscriber:
  12. ref:
  13. apiVersion: serving.knative.dev/v1
  14. kind: Service
  15. name: my-service

Whenver both the filters field and the filter field are both provided, the filters field will override the filter field. For example, in the following trigger events with type dev.knative.a will be delivered, while events with type dev.knative.b will not.

  1. apiVersion: eventing.knative.dev/v1
  2. kind: Trigger
  3. metadata:
  4. name: my-service-trigger
  5. spec:
  6. broker: default
  7. filters:
  8. exact:
  9. type: dev.knative.a
  10. filter:
  11. attributes:
  12. type: dev.knative.b
  13. subscriber:
  14. ref:
  15. apiVersion: serving.knative.dev/v1
  16. kind: Service
  17. name: my-service

Trigger annotations

You can modify a Trigger’s behavior by setting the following two annotations:

  • eventing.knative.dev/injection: if set to enabled, Eventing automatically creates a Broker for a Trigger if it doesn’t exist. The Broker is created in the namespace where the Trigger is created. This annotation only works if you have the Sugar Controller enabled, which is optional and not enabled by default.
  • knative.dev/dependency: this annotation is used to mark the sources that the Trigger depends on. If one of the dependencies is not ready, the Trigger will not be ready.

The following YAML is an example of a Trigger with a dependency:

  1. apiVersion: eventing.knative.dev/v1
  2. kind: Trigger
  3. metadata:
  4. name: my-service-trigger
  5. annotations:
  6. knative.dev/dependency: '{"kind":"PingSource","name":"test-ping-source","apiVersion":"sources.knative.dev/v1"}'
  7. spec:
  8. broker: default
  9. filter:
  10. attributes:
  11. type: dev.knative.foo.bar
  12. myextension: my-extension-value
  13. subscriber:
  14. ref:
  15. apiVersion: serving.knative.dev/v1
  16. kind: Service
  17. name: my-service