Use Trigger Conditions

This document describes how to use Trigger conditions.

Prerequisites

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

Use Trigger Conditions

Create two event sources

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

    1. apiVersion: events.openfunction.io/v1alpha1
    2. kind: EventSource
    3. metadata:
    4. name: eventsource-a
    5. spec:
    6. logLevel: "2"
    7. eventBus: "default"
    8. kafka:
    9. sample-five:
    10. brokers: "kafka-server-kafka-brokers.default.svc.cluster.local:9092"
    11. topic: "events-sample"
    12. authRequired: false
  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"
  3. Run the following commands to apply these two configuration files.

    1. kubectl apply -f eventsource-a.yaml
    2. kubectl apply -f eventsource-b.yaml

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. metadata:
    4. name: condition-trigger
    5. spec:
    6. logLevel: "2"
    7. eventBus: "default"
    8. inputs:
    9. eventA:
    10. eventSource: "eventsource-a"
    11. event: "sample-five"
    12. eventB:
    13. eventSource: "eventsource-b"
    14. event: "sample-five"
    15. subscribers:
    16. - condition: eventB
    17. sink:
    18. uri: "http://openfunction.io.svc.cluster.local/default/sink"
    19. - condition: eventA && eventB
    20. 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. kubectl apply -f condition-trigger.yaml
  3. Run the following commands to check the results.

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

    1. $ kubectl get functions.core.openfunction.io
    2. NAME BUILDSTATE SERVINGSTATE BUILDER SERVING URL AGE
    3. sink Skipped Running serving-4x5wh http://openfunction.io/default/sink 3h25m
    4. $ kubectl get po
    5. NAME READY STATUS RESTARTS AGE
    6. serving-4x5wh-ksvc-wxbf2-v100-deployment-5c495c84f6-k2jdg 2/2 Running 0 46s
  5. Create an event producer by referring to Create an Event Producer.

  6. 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

Last modified June 18, 2022: Add function inputs and outputs docs (#105) (dd9d200)