Sequence wired to another Sequence

We are going to create the following logical configuration. We create a PingSource, feeding events to a Sequence, then taking the output of that Sequence and sending it to a second Sequence and finally displaying the resulting output.

Logical Configuration

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

Prerequisites

For this example, we’ll assume you have set up an InMemoryChannel as well as Knative Serving (for our functions). The examples use default namespace, again, if you want to deploy to another namespace, you will need to modify the examples to reflect this.

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

Setup

Create the Knative Services

Change default in the following command to create the steps in the namespace where you want resources created:

  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. - 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. spec:
  34. containers:
  35. - image: gcr.io/knative-releases/knative.dev/eventing/cmd/appender
  36. env:
  37. - name: MESSAGE
  38. value: " - Handled by 2"
  39. ---
  40. apiVersion: serving.knative.dev/v1
  41. kind: Service
  42. metadata:
  43. name: fourth
  44. spec:
  45. template:
  46. spec:
  47. containers:
  48. - image: gcr.io/knative-releases/knative.dev/eventing/cmd/appender
  49. env:
  50. - name: MESSAGE
  51. value: " - Handled by 3"
  52. ---
  53. apiVersion: serving.knative.dev/v1
  54. kind: Service
  55. metadata:
  56. name: fifth
  57. spec:
  58. template:
  59. spec:
  60. containers:
  61. - image: gcr.io/knative-releases/knative.dev/eventing/cmd/appender
  62. env:
  63. - name: MESSAGE
  64. value: " - Handled by 4"
  65. ---
  66. apiVersion: serving.knative.dev/v1
  67. kind: Service
  68. metadata:
  69. name: sixth
  70. spec:
  71. template:
  72. spec:
  73. containers:
  74. - image: gcr.io/knative-releases/knative.dev/eventing/cmd/appender
  75. env:
  76. - name: MESSAGE
  77. value: " - Handled by 5"
  78. ---
  1. kubectl -n default create -f ./steps.yaml

Create the first Sequence

The sequence1.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.

  1. apiVersion: flows.knative.dev/v1
  2. kind: Sequence
  3. metadata:
  4. name: first-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: Sequence
  25. apiVersion: flows.knative.dev/v1
  26. name: second-sequence

Change default in the following command to create the Sequence in the namespace where you want your resources created:

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

Create the second Sequence

The sequence2.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.

  1. apiVersion: flows.knative.dev/v1
  2. kind: Sequence
  3. metadata:
  4. name: second-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: fourth
  14. - ref:
  15. apiVersion: serving.knative.dev/v1
  16. kind: Service
  17. name: fifth
  18. - ref:
  19. apiVersion: serving.knative.dev/v1
  20. kind: Service
  21. name: sixth
  22. reply:
  23. ref:
  24. kind: Service
  25. apiVersion: serving.knative.dev/v1
  26. name: event-display
  1. kubectl -n default create -f ./sequence2.yaml

Create the Service displaying the events created by Sequence

  1. apiVersion: serving.knative.dev/v1
  2. kind: Service
  3. metadata:
  4. name: event-display
  5. spec:
  6. template:
  7. spec:
  8. containers:
  9. - image: gcr.io/knative-releases/knative.dev/eventing/cmd/event_display

Change default in the following command to create the Sequence in the namespace where you want your resources created:

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

Create the PingSource targeting the first Sequence

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/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: flows.knative.dev/v1
  12. kind: Sequence
  13. name: first-sequence
  1. kubectl -n default create -f ./ping-source.yaml

Inspecting the results

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:

  1. kubectl -n default logs -l serving.knative.dev/service=event-display -c user-container --tail=-1
  2. ☁️ cloudevents.Event
  3. Validation: valid
  4. Context Attributes,
  5. specversion: 1.0
  6. type: dev.knative.sources.ping
  7. source: /apis/v1/namespaces/default/pingsources/ping-source
  8. id: 29d531df-78d8-4d11-9ffd-ba24045241a9
  9. time: 2020-03-02T21:18:00.0011708Z
  10. datacontenttype: application/json
  11. Extensions,
  12. knativehistory: first-sequence-kn-sequence-0-kn-channel.default.svc.cluster.local; first-sequence-kn-sequence-1-kn-channel.default.svc.cluster.local; first-sequence-kn-sequence-2-kn-channel.default.svc.cluster.local; second-sequence-kn-sequence-0-kn-channel.default.svc.cluster.local; second-sequence-kn-sequence-1-kn-channel.default.svc.cluster.local; second-sequence-kn-sequence-2-kn-channel.default.svc.cluster.local
  13. traceparent: 00-e5abc9de525a89ead80560b8f328de5c-fc12b64a6296f541-00
  14. Data,
  15. {
  16. "id": 0,
  17. "message": "Hello world! - Handled by 0 - Handled by 1 - Handled by 2 - Handled by 3 - Handled by 4 - Handled by 5"
  18. }

And you can see that the initial PingSource message ("Hello World!") has been appended to it by each of the steps in the Sequence.