Using Sequence with Broker and Trigger

    • Knative Serving
    • InMemoryChannel

    NOTE: The examples use the default namespace.

    If you want to use different type of Channel, you will have to modify the Sequence.Spec.ChannelTemplate to create the appropriate Channel resources.

    The functions used in these examples live in https://github.com/knative/eventing-contrib/blob/master/cmd/appender/main.go.

    Setup

    To create the cluster default Broker type:

    Create the Knative Services

    1. apiVersion: serving.knative.dev/v1
    2. kind: Service
    3. metadata:
    4. name: first
    5. spec:
    6. template:
    7. spec:
    8. containers:
    9. - image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/appender
    10. env:
    11. - name: MESSAGE
    12. value: " - Handled by 0"
    13. ---
    14. apiVersion: serving.knative.dev/v1
    15. kind: Service
    16. metadata:
    17. name: second
    18. spec:
    19. template:
    20. spec:
    21. containers:
    22. - image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/appender
    23. env:
    24. - name: MESSAGE
    25. value: " - Handled by 1"
    26. ---
    27. apiVersion: serving.knative.dev/v1
    28. kind: Service
    29. metadata:
    30. name: third
    31. spec:
    32. template:
    33. containers:
    34. - image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/appender
    35. env:
    36. - name: MESSAGE
    37. value: " - Handled by 2"
    38. - name: TYPE
    39. value: "samples.http.mod3"
    1. kubectl -n default create -f ./steps.yaml

    The sequence.yaml file contains the specifications for creating the Sequence. If you are using a different type of Channel, you need to change the spec.channelTemplate to point to your desired Channel.

    Also, change the spec.reply.name to point to your Broker

    1. apiVersion: flows.knative.dev/v1
    2. kind: Sequence
    3. metadata:
    4. name: sequence
    5. spec:
    6. channelTemplate:
    7. apiVersion: messaging.knative.dev/v1
    8. kind: InMemoryChannel
    9. steps:
    10. - ref:
    11. apiVersion: serving.knative.dev/v1
    12. kind: Service
    13. name: first
    14. - ref:
    15. apiVersion: serving.knative.dev/v1
    16. kind: Service
    17. name: second
    18. - ref:
    19. apiVersion: serving.knative.dev/v1
    20. kind: Service
    21. name: third
    22. reply:
    23. ref:
    24. kind: Broker
    25. apiVersion: eventing.knative.dev/v1
    26. name: default

    Change default below to create the Sequence in the Namespace where you have configured your Broker.

    Create the PingSource targeting the Broker

    This will create a PingSource which will send a CloudEvent with {“message”: “Hello world!”} as the data payload every 2 minutes.

    1. apiVersion: sources.knative.dev/v1beta1
    2. kind: PingSource
    3. metadata:
    4. name: ping-source
    5. spec:
    6. schedule: "*/2 * * * *"
    7. jsonData: '{"message": "Hello world!"}'
    8. sink:
    9. ref:
    10. kind: Broker
    11. name: default

    Here, if you are using different type of Channel, you need to change the spec.channelTemplate to point to your desired Channel. Also, change the spec.reply.name to point to your Broker

    1. apiVersion: eventing.knative.dev/v1
    2. kind: Trigger
    3. metadata:
    4. name: sequence-trigger
    5. spec:
    6. broker: default
    7. filter:
    8. attributes:
    9. type: dev.knative.sources.ping
    10. subscriber:
    11. ref:
    12. apiVersion: flows.knative.dev/v1
    13. kind: Sequence
    14. name: sequence

    Change default below to create the Sequence in the Namespace where you have configured your Broker.

    Create the Service and Trigger displaying the events created by Sequence

    1. apiVersion: serving.knative.dev/v1
    2. kind: Service
    3. metadata:
    4. name: sequence-display
    5. spec:
    6. template:
    7. spec:
    8. containers:
    9. - image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/appender
    10. ---
    11. apiVersion: eventing.knative.dev/v1
    12. kind: Trigger
    13. metadata:
    14. name: display-trigger
    15. spec:
    16. broker: default
    17. filter:
    18. attributes:
    19. type: samples.http.mod3
    20. subscriber:
    21. ref:
    22. apiVersion: serving.knative.dev/v1
    23. kind: Service
    24. name: sequence-display
    25. ---

    Change default below to create the Service and Trigger in the Namespace where you have configured your Broker.

    1. kubectl -n default create -f ./display-trigger.yaml

    You can now see the final output by inspecting the logs of the event-display pods.

    1. kubectl -n default get pods

    Then look at the logs for the event-display pod:

    And you can see that the initial PingSource message has been appended to it by each of the steps in the Sequence.