Use Trigger Conditions

    You have finished the steps described in Use EventBus and Trigger.

    Use Trigger Conditions

    1. Use the following content to create an EventSource configuration file (for example, ).

    2. Use the following content to create another EventSource configuration file (for example, eventsource-b.yaml).

      1. apiVersion: events.openfunction.io/v1alpha1
      2. kind: EventSource
      3. metadata:
      4. name: eventsource-b
      5. spec:
      6. logLevel: "2"
      7. eventBus: "default"
      8. cron:
      9. sample-five:
      10. schedule: "@every 5s"

    Create a trigger with condition

    1. Use the following content to create a configuration file (for example, condition-trigger.yaml) for a Trigger with condition.

      1. apiVersion: events.openfunction.io/v1alpha1
      2. kind: Trigger
      3. name: condition-trigger
      4. spec:
      5. logLevel: "2"
      6. inputs:
      7. eventA:
      8. eventSource: "eventsource-a"
      9. event: "sample-five"
      10. eventB:
      11. eventSource: "eventsource-b"
      12. event: "sample-five"
      13. subscribers:
      14. - condition: eventB
      15. sink:
      16. uri: "http://openfunction.io.svc.cluster.local/default/sink"
      17. - condition: eventA && eventB
      18. 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 input eventA 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.
    2. Run the following commands to apply the configuration files.

      1. NAME EVENTBUS SINK STATUS
      2. eventsource-a default Ready
      3. $ kubectl get triggers.events.openfunction.io
      4. NAME EVENTBUS STATUS
      5. condition-trigger default Ready
      6. $ kubectl get eventbus.events.openfunction.io
      7. NAME AGE
      8. default 12s
    3. 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 source eventsource-b is a cron task.

    4. Create an event producer by referring to Create an Event Producer.

    5. 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 the metrics topic of the event bus at the same time. The OpenFuncAsync function is triggered.

      1. $ kubectl get functions.core.openfunction.io
      2. NAME BUILDSTATE SERVINGSTATE BUILDER SERVING URL AGE
      3. trigger-target Skipped Running serving-7hghp 103s
      4. $ kubectl get po
      5. NAME READY STATUS RESTARTS AGE
      6. serving-7hghp-deployment-v100-z8wrf-946b4854d-svf55 2/2 Running 0 18s