Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions run/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][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).

Expand Down Expand Up @@ -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
81 changes: 81 additions & 0 deletions run/events-pubsub/anthos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Events for Cloud Run on Anthos – Pub/Sub tutorial

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

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
MY_CLUSTER_NAME=events-cluster
MY_CLUSTER_LOCATION=us-central1-c
```

## Quickstart

Set cluster name, location and platform:

```sh
gcloud config set run/cluster ${MY_CLUSTER_NAME}
gcloud config set run/cluster_location ${MY_CLUSTER_LOCATION}
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)'
```
99 changes: 99 additions & 0 deletions run/events-storage/anthos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Events for Cloud Run on Anthos – GCS tutorial

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

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)
MY_CLUSTER_NAME=events-cluster
MY_CLUSTER_LOCATION=us-central1-c
```

## Quickstart

Set cluster name, location and platform:

```sh
gcloud config set run/cluster ${MY_CLUSTER_NAME}
gcloud config set run/cluster_location ${MY_CLUSTER_LOCATION}
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 \
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially got ERROR: (gcloud.run.deploy) [Errno 61] Connection refused

Successfully connected with gcloud container clusters get-credentials events-cluster --zone us-central1-c --project {PROJECT_ID} and ran the deploy command without issue

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the two are related. get-credentials sets up kubectl to talk to the cluster but it should not affect gcloud run deploy. The only thing that can affect deploy is the run/platform gke setting. Make sure it's set to gke to deploy to GKE/Anthos.

--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 \
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran into this error

ERROR: (gcloud.alpha.events.triggers.create) Unknown event type: com.google.cloud.storage.object.finalize. If you're trying to use a custom event type, add the "--custom-type" flag.

Copy link
Copy Markdown
Contributor

@dinagraves dinagraves Jul 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, when I run the gcloud alpha events types list, it responds with
Listed 0 items.

I did run gcloud components update

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is that you don't have run/platform variable set to gke. So, it's trying to deploy to managed and managed does not support com.google.cloud.storage.object.finalize yet.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gcloud config list shows platform = gke

Looks like this person had the same problem: #4228

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's similar but not the same problem. #4228 is with Cloud Run Managed, whereas this PR is with Cloud Run for Anthos. Both features are in alpha and projects need to be whitelisted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Should that be noted somewhere? Or are you going wait until it's publicly available to merge this PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grant already submitted samples for Managed in preparation for upcoming Beta. I'm merely mirroring it for Anthos. I think we can merge this as is and if we need to note anything before Beta, this can be done holistically for Managed and Anthos.

--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)'
```