Multiple Cases

We are going to create a Parallel with two branches:

  • the first branch accepts events with a time that is is even
  • the second branch accepts events with a time that is is odd

The events produced by each branch are then sent to the event-display service.

Prerequisites

Please refer to the sample overview for the prerequisites.

Create the Knative Services

Let’s first create the filter and transformer services that we will use in our Parallel.

  1. apiVersion: serving.knative.dev/v1
  2. kind: Service
  3. metadata:
  4. name: even-filter
  5. spec:
  6. template:
  7. spec:
  8. containers:
  9. - image: villardl/filter-nodejs:0.1
  10. env:
  11. - name: FILTER
  12. value: |
  13. Math.round(Date.parse(event.time) / 60000) % 2 === 0
  14. ---
  15. apiVersion: serving.knative.dev/v1
  16. kind: Service
  17. metadata:
  18. name: odd-filter
  19. spec:
  20. template:
  21. spec:
  22. containers:
  23. - image: villardl/filter-nodejs:0.1
  24. env:
  25. - name: FILTER
  26. value: |
  27. Math.round(Date.parse(event.time) / 60000) % 2 !== 0
  28. ---
  29. apiVersion: serving.knative.dev/v1
  30. kind: Service
  31. metadata:
  32. name: even-transformer
  33. spec:
  34. template:
  35. spec:
  36. containers:
  37. - image: villardl/transformer-nodejs
  38. env:
  39. - name: TRANSFORMER
  40. value: |
  41. ({"message": "we are even!"})
  42. ---
  43. apiVersion: serving.knative.dev/v1
  44. kind: Service
  45. metadata:
  46. name: odd-transformer
  47. spec:
  48. template:
  49. spec:
  50. containers:
  51. - image: villardl/transformer-nodejs:0.1
  52. env:
  53. - name: TRANSFORMER
  54. value: |
  55. ({"message": "this is odd!"})
  56. .
  1. kubectl create -f ./filters.yaml -f ./transformers.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-contrib/cmd/event_display

Change default below to create the Sequence in the Namespace where you want your resources to be created.

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

Create the Parallel

The parallel.yaml file contains the specifications for creating the Parallel.

  1. apiVersion: flows.knative.dev/v1beta1
  2. kind: Parallel
  3. metadata:
  4. name: odd-even-parallel
  5. spec:
  6. channelTemplate:
  7. apiVersion: messaging.knative.dev/v1
  8. kind: InMemoryChannel
  9. branches:
  10. - filter:
  11. ref:
  12. apiVersion: serving.knative.dev/v1
  13. kind: Service
  14. name: even-filter
  15. subscriber:
  16. ref:
  17. apiVersion: serving.knative.dev/v1
  18. kind: Service
  19. name: even-transformer
  20. - filter:
  21. ref:
  22. apiVersion: serving.knative.dev/v1
  23. kind: Service
  24. name: odd-filter
  25. subscriber:
  26. ref:
  27. apiVersion: serving.knative.dev/v1
  28. kind: Service
  29. name: odd-transformer
  30. reply:
  31. ref:
  32. apiVersion: serving.knative.dev/v1
  33. kind: Service
  34. name: event-display
  1. kubectl create -f ./parallel.yaml

Create the PingSource targeting the Parallel

This will create a PingSource which will send a CloudEvent with {"message": "Even or odd?"} as the data payload every minute.

  1. apiVersion: sources.knative.dev/v1alpha2
  2. kind: PingSource
  3. metadata:
  4. name: ping-source
  5. spec:
  6. schedule: "*/1 * * * *"
  7. data: '{"message": "Even or odd?"}'
  8. sink:
  9. ref:
  10. apiVersion: flows.knative.dev/v1alpha2
  11. kind: Parallel
  12. name: odd-even-parallel
  1. kubectl create -f ./ping-source.yaml

Inspecting the results

You can now see the final output by inspecting the logs of the event-display pods. Note that since we set the PingSource to emit every minute, it might take some time for the events to show up in the logs.

Let’s look at the event-display log:

  1. kubectl logs -l serving.knative.dev/service=event-display --tail=30 -c user-container
  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: 015a4cf4-8a43-44a9-8702-3d4171d27ba5
  9. time: 2020-03-03T21:24:00.0007254Z
  10. datacontenttype: application/json; charset=utf-8
  11. Extensions,
  12. knativehistory: odd-even-parallel-kn-parallel-kn-channel.default.svc.cluster.local; odd-even-parallel-kn-parallel-0-kn-channel.default.svc.cluster.local
  13. traceparent: 00-41a139bf073f3cfcba7bb7ce7f1488fc-68a891ace985221a-00
  14. Data,
  15. {
  16. "message": "we are even!"
  17. }
  18. ☁️ cloudevents.Event
  19. Validation: valid
  20. Context Attributes,
  21. specversion: 1.0
  22. type: dev.knative.sources.ping
  23. source: /apis/v1/namespaces/default/pingsources/ping-source
  24. id: 52e6b097-f914-4b5a-8539-165650e85bcd
  25. time: 2020-03-03T21:23:00.0004662Z
  26. datacontenttype: application/json; charset=utf-8
  27. Extensions,
  28. knativehistory: odd-even-parallel-kn-parallel-kn-channel.default.svc.cluster.local; odd-even-parallel-kn-parallel-1-kn-channel.default.svc.cluster.local
  29. traceparent: 00-58d371410d7daf2033be226860b4ee5d-05d686ee90c3226f-00
  30. Data,
  31. {
  32. "message": "this is odd!"
  33. }