-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Added Events for Cloud Run for Anthos – GCS and PubSub tutorials. Fix… #4232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5093b9b
46adb80
1c4f996
da358b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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)' | ||
| ``` |
| 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 \ | ||
| --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 \ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ran into this error
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI, when I run the I did run
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My guess is that you don't have
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Looks like this person had the same problem: #4228
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)' | ||
| ``` | ||
There was a problem hiding this comment.
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 refusedSuccessfully connected with
gcloud container clusters get-credentials events-cluster --zone us-central1-c --project {PROJECT_ID}and ran the deploy command without issueThere was a problem hiding this comment.
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-credentialssets up kubectl to talk to the cluster but it should not affectgcloud run deploy. The only thing that can affect deploy is therun/platform gkesetting. Make sure it's set togketo deploy to GKE/Anthos.