Use Trigger Conditions
You have finished the steps described in Use EventBus and Trigger.
Use Trigger Conditions
Use the following content to create an EventSource configuration file (for example, ).
Use the following content to create another EventSource configuration file (for example,
eventsource-b.yaml
).apiVersion: events.openfunction.io/v1alpha1
kind: EventSource
metadata:
name: eventsource-b
spec:
logLevel: "2"
eventBus: "default"
cron:
sample-five:
schedule: "@every 5s"
Create a trigger with condition
Use the following content to create a configuration file (for example,
condition-trigger.yaml
) for a Trigger withcondition
.apiVersion: events.openfunction.io/v1alpha1
kind: Trigger
name: condition-trigger
spec:
logLevel: "2"
inputs:
eventA:
eventSource: "eventsource-a"
event: "sample-five"
eventB:
eventSource: "eventsource-b"
event: "sample-five"
subscribers:
- condition: eventB
sink:
uri: "http://openfunction.io.svc.cluster.local/default/sink"
- condition: eventA && eventB
topic: "metrics"
Note
In this example, two input sources and two subscribers are defined, and their triggering relationship is described as follows:
- When input
eventB
is received, the input event is sent to the Knative service. - When input
eventB
and inputeventA
are received, the input event is sent to the metrics topic of the event bus and sent to the Knative service at the same time.
Run the following commands to apply the configuration files.
-
NAME EVENTBUS SINK STATUS
eventsource-a default Ready
$ kubectl get triggers.events.openfunction.io
NAME EVENTBUS STATUS
condition-trigger default Ready
$ kubectl get eventbus.events.openfunction.io
NAME AGE
default 12s
Run the following command and you can see from the output that the
eventB
condition in the Trigger is matched and the Knative service is triggered because the event sourceeventsource-b
is a cron task.Create an event producer by referring to Create an Event Producer.
Run the following command and you can see from the output that the
eventA && eventB
condition in the Trigger is matched and the event is sent to themetrics
topic of the event bus at the same time. The OpenFuncAsync function is triggered.$ kubectl get functions.core.openfunction.io
NAME BUILDSTATE SERVINGSTATE BUILDER SERVING URL AGE
trigger-target Skipped Running serving-7hghp 103s
$ kubectl get po
NAME READY STATUS RESTARTS AGE
serving-7hghp-deployment-v100-z8wrf-946b4854d-svf55 2/2 Running 0 18s