|
| 1 | +--- |
| 2 | +title: "Sequence Wired to event-display" |
| 3 | +weight: 20 |
| 4 | +type: "docs" |
| 5 | +--- |
| 6 | + |
| 7 | +# Using Sequences in series |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +We are going to create the following logical configuration. We create a CronJobSource, |
| 12 | +feeding events to a (`Sequence`)[../../../sequence.md], then taking the output of that `Sequence` and |
| 13 | +displaying the resulting output. |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +For this example, we'll assume you have set up an `InMemoryChannel` |
| 20 | +as well as Knative Serving (for our functions). The examples use `default` |
| 21 | +namespace, again, if you want to deploy to another Namespace, you will need |
| 22 | +to modify the examples to reflect this. |
| 23 | + |
| 24 | +If you want to use different type of `Channel`, you will have to modify the |
| 25 | +`Sequence.Spec.ChannelTemplate` to create the appropriate Channel resources. |
| 26 | + |
| 27 | + |
| 28 | +## Setup |
| 29 | + |
| 30 | +### Create the Knative Services |
| 31 | + |
| 32 | +Change `default` below to create the steps in the Namespace where you want resources |
| 33 | +created. |
| 34 | + |
| 35 | +```yaml |
| 36 | +apiVersion: serving.knative.dev/v1beta1 |
| 37 | +kind: Service |
| 38 | +metadata: |
| 39 | + name: first |
| 40 | +spec: |
| 41 | + template: |
| 42 | + spec: |
| 43 | + containers: |
| 44 | + - image: us.gcr.io/probable-summer-223122/cmd-03315b715ae8f3e08e3a9378df706fbb@sha256:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 |
| 45 | + env: |
| 46 | + - name: STEP |
| 47 | + value: "0" |
| 48 | + |
| 49 | +--- |
| 50 | +apiVersion: serving.knative.dev/v1beta1 |
| 51 | +kind: Service |
| 52 | +metadata: |
| 53 | + name: second |
| 54 | +spec: |
| 55 | + template: |
| 56 | + spec: |
| 57 | + containers: |
| 58 | + - image: us.gcr.io/probable-summer-223122/cmd-03315b715ae8f3e08e3a9378df706fbb@sha256:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 |
| 59 | + env: |
| 60 | + - name: STEP |
| 61 | + value: "1" |
| 62 | +--- |
| 63 | +apiVersion: serving.knative.dev/v1beta1 |
| 64 | +kind: Service |
| 65 | +metadata: |
| 66 | + name: third |
| 67 | +spec: |
| 68 | + template: |
| 69 | + spec: |
| 70 | + containers: |
| 71 | + - image: us.gcr.io/probable-summer-223122/cmd-03315b715ae8f3e08e3a9378df706fbb@sha256:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 |
| 72 | + env: |
| 73 | + - name: STEP |
| 74 | + value: "2" |
| 75 | +--- |
| 76 | +``` |
| 77 | + |
| 78 | + |
| 79 | +```shell |
| 80 | +kubectl -n default create -f ./steps.yaml |
| 81 | +``` |
| 82 | + |
| 83 | +### Create the Sequence |
| 84 | + |
| 85 | +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 |
| 86 | +spec.channelTemplate to point to your desired Channel. |
| 87 | + |
| 88 | +```yaml |
| 89 | +apiVersion: messaging.knative.dev/v1alpha1 |
| 90 | +kind: Sequence |
| 91 | +metadata: |
| 92 | + name: sequence |
| 93 | +spec: |
| 94 | + channelTemplate: |
| 95 | + apiVersion: messaging.knative.dev/v1alpha1 |
| 96 | + kind: InMemoryChannel |
| 97 | + steps: |
| 98 | + - ref: |
| 99 | + apiVersion: serving.knative.dev/v1beta1 |
| 100 | + kind: Service |
| 101 | + name: first |
| 102 | + - ref: |
| 103 | + apiVersion: serving.knative.dev/v1beta1 |
| 104 | + kind: Service |
| 105 | + name: second |
| 106 | + - ref: |
| 107 | + apiVersion: serving.knative.dev/v1beta1 |
| 108 | + kind: Service |
| 109 | + name: third |
| 110 | + reply: |
| 111 | + kind: Service |
| 112 | + apiVersion: serving.knative.dev/v1beta1 |
| 113 | + name: event-display |
| 114 | +``` |
| 115 | +
|
| 116 | +Change `default` below to create the `Sequence` in the Namespace where you want the |
| 117 | +resources to be created. |
| 118 | +```shell |
| 119 | +kubectl -n default create -f ./sequence.yaml |
| 120 | +``` |
| 121 | + |
| 122 | + |
| 123 | +### Create the Service displaying the events created by Sequence |
| 124 | + |
| 125 | +```yaml |
| 126 | +apiVersion: serving.knative.dev/v1beta1 |
| 127 | +kind: Service |
| 128 | +metadata: |
| 129 | + name: event-display |
| 130 | +spec: |
| 131 | + template: |
| 132 | + spec: |
| 133 | + containers: |
| 134 | + - image: gcr.io/knative-releases/github.com/knative/eventing-sources/cmd/event_display |
| 135 | +``` |
| 136 | + |
| 137 | +Change `default` below to create the `Sequence` in the Namespace where you want your resources |
| 138 | +to be created. |
| 139 | + |
| 140 | +```shell |
| 141 | +kubectl -n default create -f ./event-display.yaml |
| 142 | +``` |
| 143 | + |
| 144 | +### Create the CronJobSource targeting the Sequence |
| 145 | + |
| 146 | +This will create a CronJobSource which will send a CloudEvent with {"message": "Hello world!"} as |
| 147 | +the data payload every 2 minutes. |
| 148 | + |
| 149 | +```yaml |
| 150 | +apiVersion: sources.eventing.knative.dev/v1alpha1 |
| 151 | +kind: CronJobSource |
| 152 | +metadata: |
| 153 | + name: cronjob-source |
| 154 | +spec: |
| 155 | + schedule: "*/2 * * * *" |
| 156 | + data: '{"message": "Hello world!"}' |
| 157 | + sink: |
| 158 | + apiVersion: messaging.knative.dev/v1alpha1 |
| 159 | + kind: Sequence |
| 160 | + name: sequence |
| 161 | +``` |
| 162 | + |
| 163 | +```shell |
| 164 | +kubectl -n default create -f ./cron-source.yaml |
| 165 | +``` |
| 166 | + |
| 167 | +### Inspecting the results |
| 168 | + |
| 169 | +You can now see the final output by inspecting the logs of the event-display pods. |
| 170 | + |
| 171 | +```shell |
| 172 | +kubectl -n default get pods |
| 173 | +``` |
| 174 | + |
| 175 | +Then look at the logs for the event-display pod: |
| 176 | + |
| 177 | + |
| 178 | +```shell |
| 179 | +kubectl -n default logs -l serving.knative.dev/service=event-display -c user-container |
| 180 | +☁️ cloudevents.Event |
| 181 | +Validation: valid |
| 182 | +Context Attributes, |
| 183 | + cloudEventsVersion: 0.1 |
| 184 | + eventType: samples.http.mod3 |
| 185 | + source: /transformer/2 |
| 186 | + eventID: df52b47e-02fd-45b2-8180-dabb572573f5 |
| 187 | + eventTime: 2019-06-18T14:18:42.478140635Z |
| 188 | + contentType: application/json |
| 189 | +Data, |
| 190 | + { |
| 191 | + "id": 0, |
| 192 | + "message": "Hello world! - Handled by 0 - Handled by 1 - Handled by 2" |
| 193 | + } |
| 194 | +``` |
| 195 | + |
| 196 | +And you can see that the initial Cron Source message ("Hello World!") has been appended to it by each |
| 197 | +of the steps in the Sequence. |
0 commit comments