These examples are adapted from https://knative.dev/docs/eventing/getting-started/.
Apply example yamls in this directory:
kubectl apply -f namespace.yaml
kubectl apply -f event-publisher.yaml
kubectl apply -f event-consumers.yaml
kubectl apply -f broker.yaml
kubectl apply -f triggers.yaml
The yamls create the following resources:
broker-example
namespace- A pod in which you can run
curl
as the event producer - 2 Kubernetes services
hello-display
andgoodbye-display
as event consumers - A GCP broker named
test-broker
- 2 Triggers
hello-display
andgoodbye-display
that points to the 2 consumers, respectively.hello-display
filters events withtype: greeting
attribute andgoodbye-display
filters events withsource: sendoff
attribute.
Verify the broker is ready:
kubectl -n broker-example get broker test-broker
NAME READY REASON URL AGE
test-broker True http://default-brokercell-ingress.cloud-run-events.svc.cluster.local/broker-example/test-broker 9s
Verify the triggers are ready:
kubectl -n broker-example get triggers
NAME READY REASON BROKER SUBSCRIBER_URI AGE
goodbye-display True test-broker http://goodbye-display.broker-example.svc.cluster.local/ 4s
hello-display True test-broker http://hello-display.broker-example.svc.cluster.local/ 4s
SSH into the event publisher pod by running the following command:
kubectl -n broker-example attach curl -it
A prompt similar to the one below will appear:
Defaulting container name to curl.
Use 'kubectl describe pod/ -n broker-example' to see all of the containers in this pod.
If you don't see a command prompt, try pressing enter.
[ root@curl:/ ]$
To show the various types of events you can send, you will make three requests:
-
To make the first request, which creates an event that has the
type:greeting
, run the following in the SSH terminal:curl -v "http://default-brokercell-ingress.cloud-run-events.svc.cluster.local/broker-example/test-broker" \ -X POST \ -H "Ce-Id: say-hello" \ -H "Ce-Specversion: 1.0" \ -H "Ce-Type: greeting" \ -H "Ce-Source: not-sendoff" \ -H "Content-Type: application/json" \ -d '{"msg":"Hello Cloud Run Events!"}'
When the
Broker
receives your event,hello-display
will activate and send it to the event consumer of the same name.If the event has been received, you will receive a
202 Accepted
response similar to the one below:< HTTP/1.1 202 Accepted < Date: Thu, 23 Apr 2020 22:14:21 GMT < Content-Length: 0
-
To make the second request, which creates an event that has the
source:sendoff
, run the following in the SSH terminal:curl -v "http://default-brokercell-ingress.cloud-run-events.svc.cluster.local/broker-example/test-broker" \ -X POST \ -H "Ce-Id: say-goodbye" \ -H "Ce-Specversion: 1.0" \ -H "Ce-Type: not-greeting" \ -H "Ce-Source: sendoff" \ -H "Content-Type: application/json" \ -d '{"msg":"Goodbye Cloud Run Events!"}'
When the
Broker
receives your event,goodbye-display
will activate and send the event to the event consumer of the same name.If the event has been received, you will receive a
202 Accepted
response similar to the one below:< HTTP/1.1 202 Accepted < Date: Thu, 23 Apr 2020 22:14:21 GMT < Content-Length: 0
-
To make the third request, which creates an event that has the
type:greeting
and thesource:sendoff
, run the following in the SSH terminal:curl -v "http://default-brokercell-ingress.cloud-run-events.svc.cluster.local/broker-example/test-broker" \ -X POST \ -H "Ce-Id: say-hello-goodbye" \ -H "Ce-Specversion: 1.0" \ -H "Ce-Type: greeting" \ -H "Ce-Source: sendoff" \ -H "Content-Type: application/json" \ -d '{"msg":"Hello Cloud Run Events! Goodbye Cloud Run Events!"}'
When the
Broker
receives your event,hello-display
andgoodbye-display
will activate and send the event to the event consumer of the same name.If the event has been received, you will receive a
202 Accepted
response similar to the one below:< HTTP/1.1 202 Accepted < Date: Thu, 23 Apr 2020 22:14:21 GMT < Content-Length: 0
-
Exit SSH by typing
exit
into the command prompt.
You have sent two events to the hello-display
event consumer and two events to
the goodbye-display
event consumer (note that say-hello-goodbye
activates
the trigger conditions for both hello-display
and goodbye-display
). You
will verify that these events were received correctly in the next section.
After sending events, verify that your events were received by the appropriate
Subscribers
.
-
Look at the logs for the
hello-display
event consumer by running the following command:kubectl -n broker-example logs -l app=hello-display --tail=100
This returns the
Attributes
andData
of the events you sent tohello-display
:☁️ cloudevents.Event Validation: valid Context Attributes, specversion: 1.0 type: greeting source: not-sendoff id: say-hello time: 2019-05-20T17:59:43.81718488Z contenttype: application/json Extensions, knativehistory: default-broker-srk54-channel-24gls.broker-example.svc.cluster.local Data, { "msg": "Hello Cloud Run Events!" } ☁️ cloudevents.Event Validation: valid Context Attributes, specversion: 1.0 type: greeting source: sendoff id: say-hello-goodbye time: 2019-05-20T17:59:54.211866425Z contenttype: application/json Extensions, knativehistory: default-broker-srk54-channel-24gls.broker-example.svc.cluster.local Data, { "msg": "Hello Cloud Run Events! Goodbye Cloud Run Events!" }
-
Look at the logs for the
goodbye-display
event consumer by running the following command:kubectl -n broker-example logs -l app=goodbye-display --tail=100
This returns the
Attributes
andData
of the events you sent togoodbye-display
:☁️ cloudevents.Event Validation: valid Context Attributes, specversion: 1.0 type: not-greeting source: sendoff id: say-goodbye time: 2019-05-20T17:59:49.044926148Z contenttype: application/json Extensions, knativehistory: default-broker-srk54-channel-24gls.broker-example.svc.cluster.local Data, { "msg": "Goodbye Cloud Run Events!" } ☁️ cloudevents.Event Validation: valid Context Attributes, specversion: 1.0 type: greeting source: sendoff id: say-hello-goodbye time: 2019-05-20T17:59:54.211866425Z contenttype: application/json Extensions, knativehistory: default-broker-srk54-channel-24gls.broker-example.svc.cluster.local Data, { "msg": "Hello Cloud Run Events! Goodbye Cloud Run Events!" }
TODO
To demonstrate that GCP broker will guarantee at least once delivery, we will temporarily delete the consumers and verify events will be delivered when the consumers are back.
-
Delete the event consumers:
kubectl delete -f event-consumers.yaml
-
SSH into the event publisher pod by running the following command:
kubectl -n broker-example attach curl -it
-
Send an event that has the
type:greeting
and thesource:sendoff
:curl -v "http://default-brokercell-ingress.cloud-run-events.svc.cluster.local/broker-example/test-broker" \ -X POST \ -H "Ce-Id: say-hello-goodbye" \ -H "Ce-Specversion: 1.0" \ -H "Ce-Type: greeting" \ -H "Ce-Source: sendoff" \ -H "Content-Type: application/json" \ -d '{"msg":"Test GCP Broker Retry"}'
-
Exit SSH by typing
exit
into the command prompt. -
Now let's bring the event consumers back:
kubectl apply -f event-consumers.yaml
Wait a couple of seconds, and you should see the event delivered to the event consumers.
kubectl delete -f event-publisher.yaml
kubectl delete -f event-consumers.yaml
kubectl delete -f triggers.yaml
kubectl delete -f broker.yaml
kubectl delete -f namespace.yaml