Skip to content
This repository has been archived by the owner on Jun 19, 2022. It is now read-only.

Latest commit

 

History

History
201 lines (151 loc) · 6.16 KB

README.md

File metadata and controls

201 lines (151 loc) · 6.16 KB

Cloud Pub/Sub PullSubscription

This sample shows how to configure the Cloud Pub/Sub Pull Subscription. This event source is most useful as a bridge from other Google Cloud services from a Pub/Sub topic, such as Cloud Storage, IoT Core and Cloud Scheduler.

Prerequisites

  1. Create a Google Cloud project and install the gcloud CLI and run gcloud auth login. This sample will use a mix of gcloud and kubectl commands. The rest of the sample assumes that you've set the $PROJECT_ID environment variable to your Google Cloud project id, and also set your project ID as default using gcloud config set project $PROJECT_ID.

  2. Install Knative with GCP.

  3. Enable the Cloud Pub/Sub API on your project:

    gcloud services enable pubsub.googleapis.com
  4. Create a Google Cloud Service Account. This sample creates one service account for both registration and receiving messages, but you can also create a separate service account for receiving messages if you want additional privilege separation.

    1. Create a new service account named cloudrunevents-pullsub with the following command:

      gcloud iam service-accounts create cloudrunevents-pullsub
    2. Give that Service Account the Pub/Sub Editor role on your Google Cloud project:

      gcloud projects add-iam-policy-binding $PROJECT_ID \
        --member=serviceAccount:cloudrunevents-pullsub@$PROJECT_ID.iam.gserviceaccount.com \
        --role roles/pubsub.editor
    3. Download a new JSON private key for that Service Account. Be sure not to check this key into source control!

      gcloud iam service-accounts keys create cloudrunevents-pullsub.json \
        --iam-account=cloudrunevents-pullsub@$PROJECT_ID.iam.gserviceaccount.com
    4. Create a secret on the kubernetes cluster with the downloaded key:

          # The secret should not already exist, so just try to create it.
          kubectl --namespace default create secret generic google-cloud-key --from-file=key.json=cloudrunevents-pullsub.json

      google-cloud-key and key.json are pre-configured values in pullsubscription.yaml.

Deployment

  1. Create a Cloud Pub/Sub Topic.

    export TOPIC_NAME=testing
    gcloud pubsub topics create $TOPIC_NAME
  2. Update TOPIC_NAME in the pullsubscription.yaml and apply it.

    If you're in the pullsubscription directory, you can replace TOPIC_NAME and apply in one command:

    sed "s/\TOPIC_NAME/$TOPIC_NAME/g" pullsubscription.yaml | \
        kubectl apply --filename -

    If you are replacing TOPIC_NAME manually, then make sure you apply the resulting YAML:

    kubectl apply --filename pullsubscription.yaml
  3. [Optional] If not using GKE, or want to use a Pub/Sub topic from another project, uncomment and replace the MY_PROJECT placeholder in pullsubscription.yaml and apply it.

    If you're in the pullsubscription directory, you can replace MY_PROJECT and TOPIC_NAME and then apply in one command:

    sed "s/\TOPIC_NAME/$TOPIC_NAME/g" pullsubscription.yaml | \
    sed "s/\#project: MY_PROJECT/project: $PROJECT_ID/g" | \
        kubectl apply --filename -

    If you are replacing MY_PROJECT manually, then make sure you apply the resulting YAML:

    kubectl apply --filename pullsubscription.yaml
  4. Create a service that the Pub/Sub Subscription will sink into:

    kubectl apply --filename event-display.yaml

Publish

Publish messages to your Cloud Pub/Sub Topic:

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

Verify

We will verify that the published message was sent by looking at the logs of the service that this PullSubscription 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

You should see log lines similar to:

☁️ cloudevents.Event
Validation: valid
 Context Attributes,
   specversion: 0.2
   type: google.pubsub.topic.publish
   source: //pubsub.googleapis.com/PROJECT_ID/topics/TOPIC_NAME
   id: 9f9b0968-a15f-4e74-ac58-e8a1c4fa587d
   time: 2019-06-10T17:52:36.73Z
   contenttype: application/json
 Data,
   {
     "Hello": "world"
   }

For more information about the format of the Data, second of the message, see the data field of PubsubMessage documentation.

For more information about CloudEvents, see the HTTP transport bindings documentation.

What's Next

The Pub/Sub PullSubscription implements what Knative Eventing considers to be a source. This component can work alone, but it also works well when Knative Serving and Eventing are installed in the cluster.

Cleaning Up

  1. Delete the Pub/Sub PullSubscription:

If you're in the pullsubscription directory, you can replace TOPIC_NAME and delete in one command:

 sed "s/\TOPIC_NAME/$TOPIC_NAME/g" pullsubscription.yaml | \
     kubectl delete --filename -

If you are replaced TOPIC_NAME manually, then make sure you delete the resulting YAML:

kubectl apply --filename pullsubscription.yaml
  1. Delete the service used as the sink:
kubectl delete --filename event-display.yaml