Skip to content

Latest commit

 

History

History
128 lines (95 loc) · 3.61 KB

File metadata and controls

128 lines (95 loc) · 3.61 KB

CloudPubSubSource Example

Overview

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.

Prerequisites

  1. Install Knative-GCP

  2. Create a Pub/Sub enabled Service Account

Deployment

  1. Create a GCP PubSub Topic. If you change its name (testing), you also need to update the topic in the CloudPubSubSource file.

    1. 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.

    2. 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
  2. Create a CloudPubSubSource

    kubectl apply --filename cloudpubsubsource.yaml
  3. Create a Service that the CloudPubSubSource will sink into:

    kubectl apply --filename event-display.yaml

Publish

Publish messages to your GCP PubSub topic:

gcloud pubsub topics publish testing --message='{"Hello": "world"}'

Verify

We will verify that the published event was sent by looking at the logs of the service that this CloudPubSubSource sinks to.

  1. 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.

  2. 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"
    }
  }

What's Next

  1. For more details on Cloud Pub/Sub formats refer to the Subscriber overview guide.
  2. For integrating with Cloud Storage see the Storage example.
  3. For integrating with Cloud Scheduler see the Scheduler example.
  4. For integrating with Cloud Audit Logs see the Cloud Audit Logs example.
  5. For more information about CloudEvents, see the HTTP transport bindings documentation.

Cleaning Up

  1. Delete the CloudPubSubSource

    kubectl delete -f ./cloudpubsubsource.yaml
  2. Delete the Service

    kubectl delete -f ./event-display.yaml