This labs shows how to configure the GCP PubSub event source for Knative. This event source is most useful as a bridge from other GCP services, such as Cloud Storage, IoT Core and Cloud Scheduler.
-
Enable the
Cloud Pub/Sub APIon your project:gcloud services enable pubsub.googleapis.com -
Create a GCP 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.
-
Create a new service account named
knative-pubsubwith the following command:gcloud iam service-accounts create knative-event
-
Give that Service Account the
Pub/Sub Editorrole on your GCP project:gcloud projects add-iam-policy-binding $PROJECT \ --member=serviceAccount:knative-event@$PROJECT.iam.gserviceaccount.com \ --role roles/pubsub.editor
-
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 knative-pubsub.json \ --iam-account=knative-event@$PROJECT.iam.gserviceaccount.com -
Create two secrets on the kubernetes cluster with the downloaded key:
# Note that the first secret may already have been created when installing # Knative Eventing. The following command will overwrite it. If you don't # want to overwrite it, then skip this command. kubectl --namespace knative-sources create secret generic gcppubsub-source-key --from-file=key.json=knative-pubsub.json --dry-run --output yaml | kubectl apply --filename - # The second secret should not already exist, so just try to create it. kubectl --namespace default create secret generic gcp-pubsub-key --from-file=key.json=knative-pubsub.json
gcppubsub-source-keyandkey.jsonare pre-configured values in thecontroller-managerStatefulSet which manages your Eventing sources.gcp-pubsub-keyandkey.jsonare pre-configured values ingcp-pubsub-source.yaml.
-
-
Create a Channel. This example creates a Channel called
pubsub-testwhich uses the in-memory provisioner, with the following definition:apiVersion: eventing.knative.dev/v1alpha1 kind: Channel metadata: name: pubsub-test spec: provisioner: apiVersion: eventing.knative.dev/v1alpha1 kind: ClusterChannelProvisioner name: in-memory-channel
If you're in the samples directory, you can apply the
channel.yamlfile:kubectl apply --filename channel.yaml
-
Create a GCP PubSub Topic. If you change its name (
testing), you also need to update thetopicin thegcp-pubsub-source.yamlfile:gcloud pubsub topics create testing
-
Replace the
MY_GCP_PROJECTplaceholder ingcp-pubsub-source.yamland apply it.If you're in the samples directory, you can replace
MY_GCP_PROJECTand apply in one command:sed "s/MY_GCP_PROJECT/$(gcloud config get-value project)/g" gcp-pubsub-source.yaml | \ kubectl apply --filename -
If you are replacing
MY_GCP_PROJECTmanually, then make sure you apply the resulting YAML:kubectl apply --filename gcp-pubsub-source.yaml
-
Create a function and subscribe it to the
pubsub-testchannel:kubectl apply --filename subscriber.yaml
Publish messages to your GCP PubSub Topic:
gcloud pubsub topics publish testing --message="Hello world"We will verify that the published message was sent into the Knative eventing
system by looking at what is downstream of the GcpPubSubSource. If you
deployed the Subscriber, then continue using this section. If
not, then you will need to look downstream yourself.
-
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 serving.knative.dev/service=message-dumper
You should see at least one.
-
-
Inspect the logs of the subscriber:
kubectl logs --selector serving.knative.dev/service=message-dumper -c user-container
You should see log lines similar to:
{
"ID": "284375451531353",
"Data": "SGVsbG8sIHdvcmxk",
"Attributes": null,
"PublishTime": "2018-10-31T00:00:00.00Z"
}The log message is a dump of the message sent by GCP PubSub. In particular, if
you base-64 decode the Data field, you should
see the sent message:
echo "SGVsbG8sIHdvcmxk" | base64 --decodeResults in: "Hello world"
For more information about the format of the message, see the PubsubMessage documentation.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License.