Skip to content

Commit d3ef899

Browse files
authored
Added Events for Cloud Run for Anthos samples. Fixes GoogleCloudPlatform#3330 (GoogleCloudPlatform#3331)
1 parent 2b836bb commit d3ef899

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

run/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ This directory contains samples for [Google Cloud Run](https://cloud.run). [Clou
1919
|[Local Troubleshooting](hello-broken/) | Broken services for local troubleshooting tutorial | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_broken] |
2020
|[Cloud SQL (MySQL)][mysql] | Use MySQL with Cloud Run | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_sql] |
2121
|[Events - Pub/Sub](events-pubsub/) | Events for Cloud Run with Pub/Sub | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_events_pubsub] |
22+
|[Anthos Events - Pub/Sub](events-pubsub/anthos.md) | Events for Cloud Run on Anthos with Pub/Sub | - |
2223
|[Events - GCS](events-gcs/) | Events for Cloud Run with GCS | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_events_gcs] |
24+
|[Anthos Events - GCS](events-gcs/anthos.md) | Events for Cloud Run on Anthos with GCS | - |
2325
|[Authentication](authentication/) | Make an authenticated request by retrieving a JSON Web Tokens (JWT) | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_auth] |
2426

2527
For more Cloud Run samples beyond Java, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples).

run/events-pubsub/anthos.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Events for Cloud Run on Anthos - Pub/Sub tutorial
2+
3+
This sample shows how to create a service that processes Pub/Sub messages on
4+
Anthos. We assume that you have a GKE cluster created with Events for Cloud Run enabled.
5+
6+
For more details on how to work with this sample read the [Google Cloud Run Java Samples README](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/run).
7+
8+
## Dependencies
9+
10+
* **Spring Boot**: Web server framework.
11+
* **Jib**: Container build tool.
12+
* **Junit + SpringBootTest**: [development] Test running framework.
13+
* **MockMVC**: [development] Integration testing support framework.
14+
15+
## Quickstart
16+
17+
Set cluster name, location and platform:
18+
19+
```sh
20+
gcloud config set run/cluster your-cluster-name
21+
gcloud config set run/cluster_location us-central1-c
22+
gcloud config set run/platform gke
23+
```
24+
25+
Create a Cloud Pub/Sub topic:
26+
27+
```sh
28+
gcloud pubsub topics create my-topic
29+
```
30+
31+
Use the [Jib Maven Plugin](https://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin) to build and push your container image:
32+
33+
```sh
34+
mvn jib:build -Dimage gcr.io/$(gcloud config get-value project)/cloudrun-events-pubsub
35+
```
36+
37+
Deploy your Cloud Run service:
38+
39+
```sh
40+
gcloud run deploy cloudrun-events-pubsub \
41+
--image gcr.io/$(gcloud config get-value project)/cloudrun-events-pubsub
42+
```
43+
44+
Create a Cloud Pub/Sub trigger:
45+
46+
```sh
47+
gcloud alpha events triggers create pubsub-trigger \
48+
--source CloudPubSubSource \
49+
--target-service cloudrun-events-pubsub \
50+
--type com.google.cloud.pubsub.topic.publish \
51+
--parameters topic=my-topic
52+
```
53+
54+
## Test
55+
56+
Test your Cloud Run service by publishing a message to the topic:
57+
58+
```sh
59+
gcloud pubsub topics publish my-topic --message="Hello there"
60+
```
61+
62+
You may observe the Run service receiving an event in Cloud Logging.

run/events-storage/anthos.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Events for Cloud Run on Anthos - Cloud Storage tutorial
2+
3+
This sample shows how to create a service that processes GCS events on
4+
Anthos. We assume that you have a GKE cluster created with Events for Cloud Run enabled.
5+
6+
For more details on how to work with this sample read the [Google Cloud Run Java Samples README](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/run).
7+
8+
## Dependencies
9+
10+
* **Spring Boot**: Web server framework.
11+
* **Jib**: Container build tool.
12+
* **Junit + SpringBootTest**: [development] Test running framework.
13+
* **MockMVC**: [development] Integration testing support framework.
14+
15+
## Setup
16+
17+
Configure environment variables:
18+
19+
```sh
20+
export MY_RUN_SERVICE=gcs-service
21+
export MY_RUN_CONTAINER=gcs-container
22+
export MY_GCS_TRIGGER=gcs-trigger
23+
export MY_GCS_BUCKET="$(gcloud config get-value project)-gcs-bucket"
24+
```
25+
26+
## Quickstart
27+
28+
Set cluster name, location and platform:
29+
30+
```sh
31+
gcloud config set run/cluster your-cluster-name
32+
gcloud config set run/cluster_location us-central1-c
33+
gcloud config set run/platform gke
34+
```
35+
36+
Use the [Jib Maven Plugin](https://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin) to build and push your container image:
37+
38+
```sh
39+
mvn jib:build -Dimage gcr.io/$(gcloud config get-value project)/$MY_RUN_CONTAINER
40+
```
41+
42+
Deploy your Cloud Run service:
43+
44+
```sh
45+
gcloud run deploy $MY_RUN_SERVICE \
46+
--image gcr.io/$(gcloud config get-value project)/$MY_RUN_CONTAINER \
47+
```
48+
49+
Create a _single region_ Cloud Storage bucket:
50+
51+
```sh
52+
gsutil mb -p $(gcloud config get-value project) -l us-central1 gs://"$MY_GCS_BUCKET"
53+
```
54+
55+
Before creating a trigger, you need to give the default service account for
56+
Cloud Storage permission to publish to Pub/Sub.
57+
58+
Find the Service Account that Cloud Storage uses to publish
59+
to Pub/Sub. You can use the steps outlined in [Cloud Console or the JSON
60+
API](https://cloud.google.com/storage/docs/getting-service-account). Assume the
61+
service account you found from above was
62+
`service-XYZ@gs-project-accounts.iam.gserviceaccount.com`, set this to an
63+
environment variable:
64+
65+
```sh
66+
export GCS_SERVICE_ACCOUNT=service-XYZ@gs-project-accounts.iam.gserviceaccount.com
67+
gcloud projects add-iam-policy-binding $(gcloud config get-value project) \
68+
--member=serviceAccount:${GCS_SERVICE_ACCOUNT} \
69+
--role roles/pubsub.publisher
70+
```
71+
72+
Create Cloud Storage trigger:
73+
74+
```sh
75+
gcloud alpha events triggers create $MY_GCS_TRIGGER \
76+
--target-service $MY_RUN_SERVICE \
77+
--type=com.google.cloud.storage.object.finalize \
78+
--parameters bucket=$MY_GCS_BUCKET
79+
```
80+
81+
## Test
82+
83+
Test your Cloud Run service by uploading a file to the bucket:
84+
85+
```sh
86+
echo "Hello World" > random.txt
87+
gsutil cp random.txt gs://$MY_GCS_BUCKET/random.txt
88+
```
89+
90+
Observe the Cloud Run service printing upon receiving an event in Cloud Logging:
91+
92+
```sh
93+
gcloud logging read "resource.type=cloud_run_revision AND \
94+
resource.labels.service_name=$MY_RUN_SERVICE" --project \
95+
$(gcloud config get-value project) --limit 30 --format 'value(textPayload)'
96+
```

0 commit comments

Comments
 (0)