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/blob/main/cmd/appender/main.go.

    Setup

    1. To create the cluster default Broker type, copy the following YAML into a file:

      1. kubectl apply -f <filename>.yaml

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

    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/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/cmd/appender
    23. env:
    24. value: " - Handled by 1"
    25. ---
    26. apiVersion: serving.knative.dev/v1
    27. kind: Service
    28. metadata:
    29. name: third
    30. spec:
    31. template:
    32. containers:
    33. - image: gcr.io/knative-releases/knative.dev/eventing/cmd/appender
    34. env:
    35. - name: MESSAGE
    36. value: " - Handled by 2"
    37. - name: TYPE
    38. value: "samples.http.mod3"
    39. ---

    Change default in the following command to create the services in the namespace where you have configured your broker:

    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

    Change default in the following command to create the sequence in the namespace where you have configured your broker:

    1. kubectl -n default create -f ./sequence.yaml

    Create the PingSource targeting the Broker

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

    Change default in the following command to create the PingSource in the namespace where you have configured your broker and sequence:

    1. kubectl -n default create -f ./ping-source.yaml

    Change default in the following command to create the trigger in the namespace where you have configured your broker and sequence:

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

    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. template:
    6. spec:
    7. containers:
    8. ---
    9. apiVersion: eventing.knative.dev/v1
    10. kind: Trigger
    11. metadata:
    12. name: display-trigger
    13. spec:
    14. broker: default
    15. filter:
    16. attributes:
    17. type: samples.http.mod3
    18. subscriber:
    19. ref:
    20. apiVersion: serving.knative.dev/v1
    21. kind: Service
    22. name: sequence-display
    23. ---

    Change default in the following command 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 sequence-display pods.

    View the logs for the sequence-display pod:

    1. kubectl -n default logs -l serving.knative.dev/service=sequence-display -c user-container --tail=-1
    2. ☁️ cloudevents.Event
    3. Validation: valid
    4. Context Attributes,
    5. specversion: 1.0
    6. type: samples.http.mod3
    7. source: /apis/v1/namespaces/default/pingsources/ping-source
    8. id: 159bba01-054a-4ae7-b7be-d4e7c5f773d2
    9. time: 2020-03-03T14:56:00.000652027Z
    10. datacontenttype: application/json
    11. Extensions,
    12. knativearrivaltime: 2020-03-03T14:56:00.018390608Z
    13. knativehistory: default-kne-trigger-kn-channel.default.svc.cluster.local; sequence-kn-sequence-0-kn-channel.default.svc.cluster.local; sequence-kn-sequence-1-kn-channel.default.svc.cluster.local; sequence-kn-sequence-2-kn-channel.default.svc.cluster.local; default-kne-trigger-kn-channel.default.svc.cluster.local
    14. traceparent: 00-e893412106ff417a90a5695e53ffd9cc-5829ae45a14ed462-00
    15. Data,
    16. {
    17. "id": 0,
    18. }