This sample shows how to configure Topics
, which is our Kubernetes object to
represent Cloud Pub/Sub topics. This resource can be used to interact directly
with Cloud Pub/Sub. It has a HTTP-addressable endpoint, where users can POST
CloudEvents, which will be then published to Cloud Pub/Sub. This is a core
construct used by higher-level objects, such as Channel
.
-
Create a Cloud Pub/Sub Topic.
export TOPIC_NAME=testing gcloud pubsub topics create $TOPIC_NAME --project=$PROJECT_ID
-
Update
googleServiceAccount
/secret
in thetopic.yaml
-
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.
-
-
Update
project
in thetopic.yaml
By default, the Topic will be created 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 is created in. -
Update
TOPIC_NAME
in thetopic.yaml
and apply it.If you're in the topic directory, you can replace
TOPIC_NAME
and apply in one command:sed "s/\TOPIC_NAME/$TOPIC_NAME/g" topic.yaml | \ kubectl apply --filename -
If you are replacing
TOPIC_NAME
manually, then make sure you apply the resulting YAML:kubectl apply --filename topic.yaml
-
Create a Pod using
curl
, which will act as the event producer.kubectl apply --filename curl.yaml
-
Create a Cloud Pub/Sub subscription so that you can receive the message published:
gcloud pubsub subscriptions create test-topic-subscription \ --topic=$TOPIC_NAME \ --topic-project=$PROJECT_ID --project=$PROJECT_ID
-
You can publish an event by sending an HTTP request to the
Topic
. SSH into thecurl
Pod by running the following command:kubectl --namespace default attach curl -it
-
While in the Pod prompt, create an event with:
curl -v "http://cre-testing-sample-publish.default.svc.cluster.local" \ -X POST \ -H "Ce-Id: my-id" \ -H "Ce-Specversion: 1.0" \ -H "Ce-Type: alpha-type" \ -H "Ce-Source: my-source" \ -H "Content-Type: application/json" \ -d '{"msg":"send-cloudevents-to-topic"}'
You should receive an HTTP 202 Accepted response.
We will verify that the event was actually sent to Cloud Pub/Sub by pulling the message from the subscription:
gcloud pubsub subscriptions pull test-topic-subscription --format=json --project=$PROJECT_ID
You should see log lines similar to:
[
{
"ackId": "TgQhIT4wPkVTRFAGFixdRkhRNxkIaFEOT14jPzUgKEURBggUBXx9cltXdV8zdQdRDRlzemF0blhFAgZFB3RfURsfWVx-Sg5WDxpwfWhxaVgRAgdNUVa4koT9iuWxRB1tNfOWpKBASs3pifF0Zhs9XxJLLD5-NTlFQV5AEkw-DERJUytDCypYEQ",
"message": {
"attributes": {
"ce-datacontenttype": "application/json",
"ce-id": "my-id",
"ce-source": "my-source",
"ce-specversion": "1.0",
"ce-time": "2020-02-05T07:10:02.602778258Z",
"ce-traceparent": "00-3c20e18012cac372b2c0168b9d99268a-dedab6911074371a-01",
"ce-type": "alpha-type"
},
"data": "eyJtc2ciOiJzZW5kLWNsb3VkZXZlbnRzLXRvLXRvcGljIn0=",
"messageId": "972320943223582",
"publishTime": "2020-02-05T07:10:02.893Z"
}
}
]
You may have issues receiving desired CloudEvent. Please use Authentication Mechanism Troubleshooting to check if it is due to an auth problem.
- For a higher-level construct to interact with Cloud Pub/Sub that sends Push-compatible format events, see the PubSub example.
- 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
Topic
If you're in the topic directory, you can replace
TOPIC_NAME
and delete in one command:sed "s/\TOPIC_NAME/$TOPIC_NAME/g" topic.yaml | \ kubectl delete --filename -
If you replaced
TOPIC_NAME
manually, then make sure you delete the resulting YAML:kubectl delete --filename topic.yaml
-
Delete the
curl
Pod used as the source:kubectl delete --filename curl.yaml