In this sample, we'll take a look at how to connect GCP Pub/Sub messages to a service with Knative Eventing. We'll roughly be following CloudPubSubSource Example docs page.
We're assuming that you already went through Install Knative with GCP section of the setup.
Create a Pub/Sub topic where messages will be sent:
gcloud pubsub topics create testing
Create a CloudPubSubSource to connect PubSub messages to Knative Eventing. The default cloudpubsubsource.yaml connects Pub/Sub messages to a service directly.
Instead, use the following cloudpubsubsource.yaml to connect Pub/Sub messages to a Broker, so, we can have multiple triggers to invoke multiple services on the same message.
Note that there's an alternative cloudpubsubsource-workload.yaml. Use this version instead if you setup workload identity on GKE.
Create the CloudPubSubSource:
kubectl apply -f cloudpubsubsource.yaml
Make sure there's a Broker in the default namespace by following instructions in Broker Creation page.
For the event consumer, we can use the Event Display service defined in kservice.yaml.
Create the service:
kubectl apply -f kservice.yaml
Connect the Event Display service to the Broker with a Trigger defined in trigger.yaml:
kubectl apply -f trigger.yaml
Check that the trigger is ready:
kubectl get trigger
NAME READY REASON BROKER SUBSCRIBER_URI AGE
trigger-event-display-pubsub True default http://event-display.default.svc.cluster.local 95s
We can now test our service by sending a message to Pub/Sub topic:
gcloud pubsub topics publish testing --message="Hello World"
messageIds:
- '198012587785403'
Wait a little and check that a pod is created:
kubectl get pods
Inspect the logs of the pod (replace <podid>
with actual pod id):
kubectl logs <podid> -c user-container --follow
You should see something similar to this:
info: event_display.Startup[0]
Received CloudEvent
ID: 1203138894276445
Source: //pubsub.googleapis.com/projects/knative-atamel/topics/testing
Type: com.google.cloud.pubsub.topic.publish
Subject:
DataSchema:
DataContentType: application/json
Time: 2020-05-13T14:57:20.106Z
SpecVersion: V1_0
Data: {"subscription":"cre-pull-b1d2ed86-d4ed-4cea-919e-39895b1f818d","message":{"messageId":"1203138894276445","data":"SGVsbG8gV29ybGQ=","publishTime":"2020-05-13T14:57:20.106Z"}}