Mutual Exclusive Cases

In this example, we are going to see how we can create a Parallel with mutually exclusive branches.

This example is the same as the multiple branches example except that we are now going to rely on the Knative switch function to provide a soft mutual exclusivity guarantee.

NOTE: this example must be deployed in the default namespace.

Prerequisites

Please refer to the sample overview for the prerequisites.

Create the Knative Services

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

  1. apiVersion: serving.knative.dev/v1
  2. kind: Service
  3. metadata:
  4. name: me-even-odd-switcher
  5. spec:
  6. template:
  7. spec:
  8. containers:
  9. - image: villardl/switcher-nodejs:0.1
  10. env:
  11. - name: EXPRESSION
  12. value: Math.round(Date.parse(event.time) / 60000) % 2
  13. - name: CASES
  14. value: '[0, 1]'
  15. ---
  16. apiVersion: serving.knative.dev/v1
  17. kind: Service
  18. metadata:
  19. name: even-transformer
  20. spec:
  21. template:
  22. spec:
  23. containers:
  24. - image: villardl/transformer-nodejs:0.1
  25. env:
  26. - name: TRANSFORMER
  27. value: |
  28. ({"message": "we are even!"})
  29. ---
  30. apiVersion: serving.knative.dev/v1
  31. kind: Service
  32. metadata:
  33. name: odd-transformer
  34. spec:
  35. template:
  36. spec:
  37. containers:
  38. - image: villardl/transformer-nodejs:0.1
  39. env:
  40. - name: TRANSFORMER
  41. value: |
  42. ({"message": "this is odd!"})
  43. .
  1. kubectl create -f ./switcher.yaml -f ./transformers.yaml

Create the Service displaying the events created by Parallel

  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
  1. kubectl -n default create -f ./event-display.yaml

Create the Parallel object

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

  1. apiVersion: flows.knative.dev/v1
  2. kind: Parallel
  3. metadata:
  4. name: me-odd-even-parallel
  5. spec:
  6. channelTemplate:
  7. apiVersion: messaging.knative.dev/v1
  8. kind: InMemoryChannel
  9. branches:
  10. - filter:
  11. uri: "http://me-even-odd-switcher.default.svc.cluster.local/0"
  12. subscriber:
  13. ref:
  14. apiVersion: serving.knative.dev/v1
  15. kind: Service
  16. name: me-even-transformer
  17. - filter:
  18. uri: "http://me-even-odd-switcher.default.svc.cluster.local/1"
  19. subscriber:
  20. ref:
  21. apiVersion: serving.knative.dev/v1
  22. kind: Service
  23. name: me-odd-transformer
  24. reply:
  25. ref:
  26. apiVersion: serving.knative.dev/v1
  27. kind: Service
  28. name: me-event-display
  1. kubectl create -f ./parallel.yaml

Create the PingSource targeting the Parallel object

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/v1beta2
  2. kind: PingSource
  3. metadata:
  4. name: me-ping-source
  5. spec:
  6. schedule: "*/1 * * * *"
  7. contentType: "application/json"
  8. data: '{"message": "Even or odd?"}'
  9. sink:
  10. ref:
  11. apiVersion: flows.knative.dev/v1
  12. kind: Parallel
  13. name: me-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 me-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 me-event-display log:

  1. kubectl logs -l serving.knative.dev/service=me-event-display --tail=50 -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/me-ping-source
  8. id: 0b0db15c-9b36-4388-8eaa-8c23a9dc2707
  9. time: 2020-03-03T21:30:00.0007292Z
  10. datacontenttype: application/json; charset=utf-8
  11. Extensions,
  12. knativehistory: me-odd-even-parallel-kn-parallel-kn-channel.default.svc.cluster.local; me-odd-even-parallel-kn-parallel-0-kn-channel.default.svc.cluster.local
  13. traceparent: 00-e8b17109cd21d4fa66a86633b5a614c7-ba96d220fe13211c-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/me-ping-source
  24. id: 321170d1-dea7-4b18-9290-28adb1de089b
  25. time: 2020-03-03T21:31:00.0007847Z
  26. datacontenttype: application/json; charset=utf-8
  27. Extensions,
  28. knativehistory: me-odd-even-parallel-kn-parallel-kn-channel.default.svc.cluster.local; me-odd-even-parallel-kn-parallel-1-kn-channel.default.svc.cluster.local
  29. traceparent: 00-78d8b044d23c85789f0a13fd3679ac24-1d69ddde56d620c7-00
  30. Data,
  31. {
  32. "message": "this is odd!"
  33. }