From 5093b9bd784768b3e682e5ab1b305ecd82d3909d Mon Sep 17 00:00:00 2001 From: Mete Atamel Date: Thu, 2 Jul 2020 11:16:22 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Added=20Events=20for=20Cloud=20Run=20for=20?= =?UTF-8?q?Anthos=20=E2=80=93=20GCS=20and=20PubSub=20tutorials.=20Fixes=20?= =?UTF-8?q?issue=20#4231?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- run/README.md | 8 ++- run/events-pubsub/anthos.md | 80 ++++++++++++++++++++++++++++++ run/events-storage/anthos.md | 96 ++++++++++++++++++++++++++++++++++++ 3 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 run/events-pubsub/anthos.md create mode 100644 run/events-storage/anthos.md diff --git a/run/README.md b/run/README.md index 51945e4a176..10614e5f5e7 100644 --- a/run/README.md +++ b/run/README.md @@ -15,8 +15,10 @@ This directory contains samples for [Google Cloud Run](https://cloud.run). [Clou |[Cloud Pub/Sub][pubsub] | Handling Pub/Sub push messages | [Run on Google Cloud][run_button_pubsub] | |[Cloud SQL (MySQL)][mysql] | Use MySQL with Cloud Run | - | |[Cloud SQL (Postgres)][postgres] | Use Postgres with Cloud Run | - | -|[Events – Pub/Sub][events_pubsub] | Event-driven service with Events for Cloud Run for Pub/Sub | - | -|[Events – GCS][events_storage] | Event-driven service with Events for Cloud Run for GCS | - | +|[Events – Pub/Sub][events_pubsub] | Event-driven service with Events for Cloud Run for Pub/Sub | - | +|[Anthos Events – Pub/Sub][anthos_events_pubsub] | Event-driven service with Events for Cloud Run on Anthos for Pub/Sub | - | +|[Events – GCS][events_storage] | Event-driven service with Events for Cloud Run for GCS | - | +|[Anthos Events – GCS][anthos_events_storage] | Event-driven service with Events for Cloud Run on Anthos for GCS | - | For more Cloud Run samples beyond Python, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples). @@ -112,7 +114,9 @@ for more information. [mysql]: ../cloud-sql/mysql/sqlalchemy [postgres]: ../cloud-sql/postgres/sqlalchemy [events_pubsub]: events-pubsub/ +[anthos_events_pubsub]: events-pubsub/anthos.md [events_storage]: events-storage/ +[anthos_events_storage]: events-storage/anthos.md [run_button_helloworld]: https://deploy.cloud.run/?git_repo=https://github.com/knative/docs&dir=docs/serving/samples/hello-world/helloworld-python [run_button_pubsub]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&dir=run/pubsub [testing]: https://cloud.google.com/run/docs/testing/local#running_locally_using_docker_with_access_to_services diff --git a/run/events-pubsub/anthos.md b/run/events-pubsub/anthos.md new file mode 100644 index 00000000000..992fb85360c --- /dev/null +++ b/run/events-pubsub/anthos.md @@ -0,0 +1,80 @@ +# Events for Cloud Run on Anthos – Pub/Sub tutorial + +This sample shows how to create a service that processes Pub/Sub events. It is +assumed that you already created a GKE cluster with Events for Cloud Run already +installed. + +## Setup + +Login to gcloud: + +```sh +gcloud auth login +``` + +Configure project id: + +```sh +gcloud config set project [PROJECT-ID] +``` + +Configure environment variables: + +```sh +MY_RUN_SERVICE=pubsub-service +MY_RUN_CONTAINER=pubsub-container +MY_TOPIC=pubsub-topic +MY_PUBSUB_TRIGGER=pubsub-trigger +``` + +## Quickstart + +Set cluster name, location and platform: + +```sh +gcloud config set run/cluster events-cluster +gcloud config set run/cluster_location europe-west1-b +gcloud config set run/platform gke +``` + +Deploy your Cloud Run service: + +```sh +gcloud builds submit \ + --tag gcr.io/$(gcloud config get-value project)/$MY_RUN_CONTAINER +gcloud run deploy $MY_RUN_SERVICE \ + --image gcr.io/$(gcloud config get-value project)/$MY_RUN_CONTAINER +``` + +Create a Cloud Pub/Sub topic: + +```sh +gcloud pubsub topics create $MY_TOPIC +``` + +Create a Cloud Pub/Sub trigger: + +```sh +gcloud alpha events triggers create $MY_PUBSUB_TRIGGER \ +--source CloudPubSubSource \ +--target-service $MY_RUN_SERVICE \ +--type com.google.cloud.pubsub.topic.publish \ +--parameters topic=$MY_TOPIC +``` + +## Test + +Test your Cloud Run service by publishing a message to the topic: + +```sh +gcloud pubsub topics publish $MY_TOPIC --message="John Doe" +``` + +You may observe the Cloud Run service printing upon receiving an event in +Cloud Logging. + +```sh +gcloud logging read "resource.type=cloud_run_revision AND \ +resource.labels.service_name=$MY_RUN_SERVICE" --project \ +$(gcloud config get-value project) --limit 30 --format 'value(textPayload)' +``` diff --git a/run/events-storage/anthos.md b/run/events-storage/anthos.md new file mode 100644 index 00000000000..5778a442bdc --- /dev/null +++ b/run/events-storage/anthos.md @@ -0,0 +1,96 @@ +# Events for Cloud Run on Anthos – GCS tutorial + +This sample shows how to create a service that processes GCS events. + +## Setup + +Login to gcloud: + +```sh +gcloud auth login +``` + +Configure project id: + +```sh +gcloud config set project [PROJECT-ID] +``` + +Configure environment variables: + +```sh +MY_RUN_SERVICE=gcs-service +MY_RUN_CONTAINER=gcs-container +MY_GCS_TRIGGER=gcs-trigger +MY_GCS_BUCKET=gcs-bucket-$(gcloud config get-value project) +``` + +## Quickstart + +Set cluster name, location and platform: + +```sh +gcloud config set run/cluster events-cluster +gcloud config set run/cluster_location europe-west1-b +gcloud config set run/platform gke +``` + +Deploy your Cloud Run service: + +```sh +gcloud builds submit \ + --tag gcr.io/$(gcloud config get-value project)/$MY_RUN_CONTAINER +gcloud run deploy $MY_RUN_SERVICE \ + --image gcr.io/$(gcloud config get-value project)/$MY_RUN_CONTAINER +``` + +Create a bucket: + +```sh +gsutil mb -p $(gcloud config get-value project) -l \ +us-central1 gs://"$MY_GCS_BUCKET" +``` + +Before creating a trigger, you need to give the default service account for +Cloud Storage permission to publish to Pub/Sub. + +Find the Service Account that Cloud Storage uses to publish +to Pub/Sub. You can use the steps outlined in [Cloud Console or the JSON +API](https://cloud.google.com/storage/docs/getting-service-account). Assume the +service account you found from above was +`service-XYZ@gs-project-accounts.iam.gserviceaccount.com`, set this to an +environment variable: + +```sh +export GCS_SERVICE_ACCOUNT=service-XYZ@gs-project-accounts.iam.gserviceaccount.com +gcloud projects add-iam-policy-binding $(gcloud config get-value project) \ +--member=serviceAccount:${GCS_SERVICE_ACCOUNT} \ +--role roles/pubsub.publisher +``` + +Create Cloud Storage trigger: + +```sh +gcloud alpha events triggers create $MY_GCS_TRIGGER \ +--target-service $MY_RUN_SERVICE \ +--type=com.google.cloud.storage.object.finalize \ +--parameters bucket=$MY_GCS_BUCKET +``` + +## Test + +Test your Cloud Run service by uploading a file to the bucket: + +```sh +echo "Hello World" > random.txt +gsutil cp random.txt gs://$MY_GCS_BUCKET/random.txt +``` + +Observe the Cloud Run service printing upon receiving an event in +Cloud Logging: + +```sh +gcloud logging read "resource.type=cloud_run_revision AND \ +resource.labels.service_name=$MY_RUN_SERVICE" --project \ +$(gcloud config get-value project) --limit 30 --format 'value(textPayload)' +``` From 46adb80c1544c599aa0a190815d4b034e61077de Mon Sep 17 00:00:00 2001 From: Mete Atamel Date: Tue, 7 Jul 2020 15:40:11 +0100 Subject: [PATCH 2/3] Externalized cluster name and location into env vars --- run/events-pubsub/anthos.md | 6 ++++-- run/events-storage/anthos.md | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/run/events-pubsub/anthos.md b/run/events-pubsub/anthos.md index 992fb85360c..f5a44555c59 100644 --- a/run/events-pubsub/anthos.md +++ b/run/events-pubsub/anthos.md @@ -25,6 +25,8 @@ MY_RUN_SERVICE=pubsub-service MY_RUN_CONTAINER=pubsub-container MY_TOPIC=pubsub-topic MY_PUBSUB_TRIGGER=pubsub-trigger +MY_CLUSTER_NAME=events-cluster +MY_CLUSTER_LOCATION=us-central1-c ``` ## Quickstart @@ -32,8 +34,8 @@ MY_PUBSUB_TRIGGER=pubsub-trigger Set cluster name, location and platform: ```sh -gcloud config set run/cluster events-cluster -gcloud config set run/cluster_location europe-west1-b +gcloud config set run/cluster ${MY_CLUSTER_NAME} +gcloud config set run/cluster_location ${MY_CLUSTER_LOCATION} gcloud config set run/platform gke ``` diff --git a/run/events-storage/anthos.md b/run/events-storage/anthos.md index 5778a442bdc..6b073f8d94c 100644 --- a/run/events-storage/anthos.md +++ b/run/events-storage/anthos.md @@ -23,6 +23,8 @@ MY_RUN_SERVICE=gcs-service MY_RUN_CONTAINER=gcs-container MY_GCS_TRIGGER=gcs-trigger MY_GCS_BUCKET=gcs-bucket-$(gcloud config get-value project) +MY_CLUSTER_NAME=events-cluster +MY_CLUSTER_LOCATION=us-central1-c ``` ## Quickstart @@ -30,8 +32,8 @@ MY_GCS_BUCKET=gcs-bucket-$(gcloud config get-value project) Set cluster name, location and platform: ```sh -gcloud config set run/cluster events-cluster -gcloud config set run/cluster_location europe-west1-b +gcloud config set run/cluster ${MY_CLUSTER_NAME} +gcloud config set run/cluster_location ${MY_CLUSTER_LOCATION} gcloud config set run/platform gke ``` From 1c4f99616dc218ecd8576ab0d23264f21cbbefcb Mon Sep 17 00:00:00 2001 From: Mete Atamel Date: Tue, 7 Jul 2020 15:49:51 +0100 Subject: [PATCH 3/3] Added a note to let people know that a cluster in needed --- run/events-pubsub/anthos.md | 5 ++--- run/events-storage/anthos.md | 3 ++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/run/events-pubsub/anthos.md b/run/events-pubsub/anthos.md index f5a44555c59..679427dad73 100644 --- a/run/events-pubsub/anthos.md +++ b/run/events-pubsub/anthos.md @@ -1,8 +1,7 @@ # Events for Cloud Run on Anthos – Pub/Sub tutorial -This sample shows how to create a service that processes Pub/Sub events. It is -assumed that you already created a GKE cluster with Events for Cloud Run already -installed. +This sample shows how to create a service that processes Pub/Sub events. We assume +that you have a GKE cluster created with Events for Cloud Run enabled. ## Setup diff --git a/run/events-storage/anthos.md b/run/events-storage/anthos.md index 6b073f8d94c..8fab43ad456 100644 --- a/run/events-storage/anthos.md +++ b/run/events-storage/anthos.md @@ -1,6 +1,7 @@ # Events for Cloud Run on Anthos – GCS tutorial -This sample shows how to create a service that processes GCS events. +This sample shows how to create a service that processes GCS events. We assume +that you have a GKE cluster created with Events for Cloud Run enabled. ## Setup