This sample shows how to configure CloudPubSubSources
. The CloudPubSubSource
fires a new event each time a message is published on a
Cloud Pub/Sub topic. This source sends
events using a Push-compatible format.
-
Create a GCP PubSub Topic. If you change its name (
testing
), you also need to update thetopic
in theCloudPubSubSource
file.-
If you are in GKE and using Workload Identity, update
serviceAccount
with the Pub/Sub enabled service account you created in Create a Pub/Sub enabled Service Account. -
If you are using standard Kubernetes secrets, but want to use a non-default one, update
secret
with your own secret.
gcloud pubsub topics create testing
-
-
Create a
CloudPubSubSource
kubectl apply --filename cloudpubsubsource.yaml
-
Create a
Service
that the CloudPubSubSource will sink into:kubectl apply --filename event-display.yaml
Publish messages to your GCP PubSub topic:
gcloud pubsub topics publish testing --message='{"Hello": "world"}'
We will verify that the published event was sent by looking at the logs of the service that this CloudPubSubSource sinks to.
-
We need to wait for the downstream pods to get started and receive our event, wait 60 seconds. You can check the status of the downstream pods with:
kubectl get pods --selector app=event-display
You should see at least one.
-
Inspect the logs of the service:
kubectl logs --selector app=event-display -c user-container --tail=200
You should see log lines similar to:
☁️ cloudevents.Event
Validation: valid
Context Attributes,
specversion: 1.0
type: com.google.cloud.pubsub.topic.publish
source: //pubsub.googleapis.com/projects/PROJECT_ID/topics/TOPIC_NAME
id: 951049449503068
time: 2020-01-24T18:29:36.874Z
datacontenttype: application/json
Extensions,
knativearrivaltime: 2020-01-24T18:29:37.212883996Z
knativecemode: push
traceparent: 00-7e7fb503ae694cc0f1cbf84ea63354be-f8c4848c9c11e073-00
Data,
{
"subscription": "cre-pull-7b35a745-877f-4f1f-9434-74062631a958",
"message": {
"messageId": "951049449503068",
"data": "eyJIZWxsbyI6ICJ3b3JsZCJ9Cg==",
"publishTime": "2020-01-24T18:29:36.874Z"
}
}
- For more details on Cloud Pub/Sub formats refer to the Subscriber overview guide.
- For integrating with Cloud Storage see the Storage example.
- For integrating with Cloud Scheduler see the Scheduler example.
- For integrating with Cloud Audit Logs see the Cloud Audit Logs example.
- For more information about CloudEvents, see the HTTP transport bindings documentation.
-
Delete the
CloudPubSubSource
kubectl delete -f ./cloudpubsubsource.yaml
-
Delete the
Service
kubectl delete -f ./event-display.yaml