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
serviceAccountName
with the Kubernetes service account you created in Create a Service Account for the Data Plane, which is bound to the Pub/Sub enabled Google service account. -
If you are using standard Kubernetes secrets, but want to use a non-default one, update
secret
with your own secret. -
By default, the Pub/Sub Topic will exist in the same project as your GKE cluster. However, if you are managing multiple projects, then you can specify
spec.project
, which is the Google Cloud Project that the Topic exists in.
gcloud pubsub topics create testing --project=your-project-id
-
-
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"}' --topic-project=$PROJECT_ID
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: google.cloud.pubsub.topic.v1.messagePublished
source: //pubsub.googleapis.com/projects/test-project/topics/testing
id: 1314133748793931
time: 2020-06-30T16:32:57.012Z
dataschema: https://raw.githubusercontent.com/googleapis/google-cloudevents/master/proto/google/events/cloud/pubsub/v1/data.proto
datacontenttype: application/json
Extensions,
knativearrivaltime: 2020-06-30T16:32:58.016175839Z
knsourcetrigger: link0.13999342310482066
traceparent: 00-e9ce0f38d85d8333bd1a3334ead78b4d-acd063b2d3e93980-00
Data,
{
"subscription": "cre-src_rc3_source-for-knativegcp-test-pubsub-tr_fcdf7716-c4bd-43b9-8ccc-e6e8ff848cd4",
"message": {
"messageId": "1314133748793931",
"data": "eyJIZWxsbyI6ICJ3b3JsZCJ9", // base64 encoded '{"Hello": "world"}'
"publishTime": "2020-06-30T16:32:57.012Z"
}
}
You may have issues receiving desired CloudEvent. Please use Authentication Mechanism Troubleshooting to check if it is due to an auth problem.
- 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 integrating with Cloud Build see the Build 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