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

Latest commit

 

History

History
168 lines (126 loc) · 5.02 KB

README.md

File metadata and controls

168 lines (126 loc) · 5.02 KB

Topic Example

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.

Prerequisites

  1. Install Knative-GCP

  2. Create a Service Account for the Data Plane

Deployment

  1. Create a Cloud Pub/Sub Topic.

    export TOPIC_NAME=testing
    gcloud pubsub topics create $TOPIC_NAME
  2. Update googleServiceAccount / secret in the topic.yaml

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

    2. If you are using standard Kubernetes secrets, but want to use a non-default one, update secret with your own secret.

  3. Update TOPIC_NAME in the topic.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
  4. Create a Pod using curl, which will act as the event producer.

    kubectl apply --filename curl.yaml
  5. 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

Publish

  1. You can publish an event by sending an HTTP request to the Topic. SSH into the curl Pod by running the following command:

    kubectl --namespace default attach curl -it
  2. 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.

Verify

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

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"
    }
  }
]

Troubleshooting

You may have issues receiving desired CloudEvent. Please use Authentication Mechanism Troubleshooting to check if it is due to an auth problem.

What's next

  1. For a higher-level construct to interact with Cloud Pub/Sub that sends Push-compatible format events, see the PubSub example.
  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 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
  2. Delete the curl Pod used as the source:

    kubectl delete --filename curl.yaml