Skip to content

Conversation

@mysterytony
Copy link
Contributor

@mysterytony mysterytony commented Aug 19, 2025

Description

Create a sample service for Cloud Run to demonstrate the upcoming readiness probe & service health feature

Checklist

  • I have followed Contributing Guidelines from CONTRIBUTING.MD
  • Tests pass: go test -v ./.. (see Testing)
    • ran this command from run/testing directory GOLANG_SAMPLES_E2E_TEST=true GOLANG_SAMPLES_PROJECT_ID=lihenan-s8s-networking-test go test -run ^TestServiceHealth$ -v .
  • Code formatted: gofmt (see Formatting)
  • Vetting pass: go vet (see Formatting)
  • These samples need a new API enabled in testing projects to pass (let us know which ones)
  • These samples need a new/updated env vars in testing projects set to pass (let us know which ones)
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners for this sample
  • This sample adds a new Product API, and I updated the Blunderbuss issue/PR auto-assigner with the codeowners for this sample
  • Please merge this PR for me once it is approved

@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Aug 19, 2025
@mysterytony mysterytony force-pushed the main branch 4 times, most recently from 6dc5066 to 61ce8ed Compare August 19, 2025 20:20
@mysterytony mysterytony marked this pull request as ready for review August 19, 2025 20:22
@mysterytony mysterytony requested review from a team as code owners August 19, 2025 20:22
@mysterytony
Copy link
Contributor Author

mysterytony commented Aug 19, 2025

To fix this check, run "goimports -w . && find . -name go.mod -execdir go mod tidy \;"
HEAD detached at pull/5368/merge
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   functions/functionsv2/imagemagick/go.mod

no changes added to commit (use "git add" and/or "git commit -a")

um I couldn't reproduce it locally

@mysterytony mysterytony changed the title create sample service for cloud run service health feature feat(cloudrun): create sample service for cloud run readiness probe and service health feature Aug 19, 2025
@mysterytony mysterytony force-pushed the main branch 2 times, most recently from 9681278 to 41ef965 Compare August 21, 2025 06:18
@mysterytony
Copy link
Contributor Author

@mysterytony
Copy link
Contributor Author

/gemini summary

@gemini-code-assist
Copy link
Contributor

Summary of Changes

This pull request introduces a new sample service for Cloud Run, demonstrating the upcoming readiness probe and service health features. It includes updates to the internal CI tooling to support readiness probe configurations, a new Go service with a web UI, and associated GCS bucket management for health data.

Highlights

  • New Cloud Run Sample Service: A new Go service named service-health is added under run/ to illustrate how Cloud Run's readiness probes and service health features function.
  • Enhanced Readiness Probe Configuration: The internal/cloudrunci/cloudrunci.go file is modified to allow detailed configuration of readiness probes (timeout, period, success/failure thresholds, HTTP/gRPC paths/ports) within the Service struct, enabling comprehensive testing of these features.
  • GCS-based Health State Management: New Go files (internal/cloudrunci/gcs.go, internal/cloudrunci/gcs_test.go) are introduced to facilitate the storage and retrieval of service health states in a GCS bucket, including a utility function for cleaning up bucket contents.
  • Interactive Web UI for Health Demo: Static SVG assets (celebration.svg, cloud_bg.svg, sad-unicorn.svg) and an HTML template (layout.html) are added to create a visual interface for the service-health sample, allowing users to interact with and observe the service's health status.
  • New Go Module and Dependencies: A dedicated Go module (service_health) is created with its go.mod and go.sum files, incorporating necessary dependencies for GCS interaction and API calls.
  • End-to-End Testing: A new end-to-end test (run/testing/service_health.e2e_test.go) is implemented to validate the deployment and functionality of the new service-health sample, ensuring the readiness probe and GCS integration work as expected.
Changelog
  • go.work
    • Added run/service-health to the Go workspace for module management.
  • internal/cloudrunci/cloudrunci.go
    • Introduced a Readiness struct within the Service type to define readiness probe parameters.
    • Refactored the version() method to Version() and updated all its call sites.
    • Extended the deployCmd() function to dynamically include the --readiness-probe flag based on the configured Readiness settings.
  • internal/cloudrunci/gcs.go
    • New file containing DeleteBucketAndContents for GCS object management.
  • internal/cloudrunci/gcs_test.go
    • New file with unit tests for DeleteBucketAndContents.
  • run/service-health/assets/celebration.svg
    • New SVG asset for the sample service UI.
  • run/service-health/assets/cloud_bg.svg
    • New SVG asset for the sample service UI background.
  • run/service-health/assets/sad-unicorn.svg
    • New SVG asset for the sample service UI.
  • run/service-health/go.mod
    • New Go module definition file for service_health.
  • run/service-health/go.sum
    • New Go module checksums file for service_health.
  • run/service-health/layout.html
    • New HTML template for the service-health sample's web interface.
  • run/service-health/main.go
    • New Go source file implementing the core logic of the service-health sample.
  • run/testing/service_health.e2e_test.go
    • New end-to-end test for the service-health sample.
Activity
  • mysterytony encountered an issue with goimports and go mod tidy locally, but could not reproduce it.
  • mysterytony confirmed that the run/testing tests are passing on the BTX platform.
  • mysterytony requested a summary of the pull request.

@mysterytony
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new sample service for Cloud Run to demonstrate the readiness probe and service health features. The changes include updates to the CI tooling to support readiness probe configurations, the new service's Go source code, HTML templates, and end-to-end tests.

My review has identified a few areas for improvement. There are some typos in variable names and test data. The CI tooling has a bug in how it constructs the gcloud command for readiness probes. The new service's HTML has some minor issues with invalid attributes. Most importantly, the service implementation has a potential performance bottleneck due to excessive locking around network operations, which could lead to request timeouts under load. I've provided detailed comments and suggestions for each of these points.

@mysterytony mysterytony force-pushed the main branch 5 times, most recently from 0042fb0 to 71c40e5 Compare August 26, 2025 20:31
Copy link
Collaborator

@muncus muncus left a comment

Choose a reason for hiding this comment

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

When structuring samples, please consider exactly what you're intending to demonstrate. You may want to read the samples style guide for additional advice.

This sample can be simplified a lot by removing the caching, and using appropriate API clients instead of rolling your own.

var err error

var longRegion string
if longRegion, err = queryMetadataServer("/computeMetadata/v1/instance/region"); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

please use the metadata client from https://pkg.go.dev/cloud.google.com/go/compute/metadata - it has some builtin retry logic that makes it more robust than raw http, and avoids hardcoding many of these strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like Cloud Run metadata server doesn't exactly have the same interface as GCE metadata server. For example "computeMetadata/v1/instance/region" not present in GCE metadata server.

log.Fatal(err)
}

apiURL := fmt.Sprintf(
Copy link
Collaborator

Choose a reason for hiding this comment

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

again, you should be using a client library. https://pkg.go.dev/cloud.google.com/go/run

this will greatly simplify this code by removing the hardcoded urls, the structs for returned objects, and the authentication wrangling.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the go client doesn't have this "readiness probe" field in the container type yet, but it is in v1 API

}
}()

go func() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

spawning goroutines in samples is generally frowned upon unless there's some concurrent processing necessary.

these seem to be tidying up the GCS cache? what is the significance of the GCS cache to this specific sample? it seems to be adding a lot of complexity here and distracting from readiness probes entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image

The UI looks like this. The main reason to leverage GCS is to manipulate the "readiness" state for other Cloud Run instances. If enough readiness status are reporting unhealthy, and if the Cloud Run service is attached to a load balancer, that load balancer will failover the traffic to a different region. This is why each running instance has to periodically sync from GCS.

@@ -0,0 +1,57 @@
module service_health
Copy link
Collaborator

Choose a reason for hiding this comment

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

(minor) Go does not usually have separators in package or directory names. directory name and package name should match.
see the style guide https://google.github.io/styleguide/go/decisions#package-names

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh the style guide said it should be "short and contain only lowercase letters."

but I see other examples like module github.com/GoogleCloudPlatform/golang-samples/run/hello-broken

should I use just module servicehealth or module github.com/GoogleCloudPlatform/golang-samples/run/service-health?

@mysterytony mysterytony requested a review from muncus September 12, 2025 21:35
@mysterytony mysterytony marked this pull request as ready for review September 12, 2025 21:36
briandorsey added a commit that referenced this pull request Sep 27, 2025
`cloudrunci` updates to support readiness probe additions in #5368

* feat(cloudrun): allow adding new readiness probe to cloud run
* fix(cloudrun): add comment to Readiness field

---------

Co-authored-by: Brian Dorsey <[email protected]>
@mysterytony mysterytony marked this pull request as draft September 29, 2025 20:57
@mysterytony mysterytony marked this pull request as ready for review October 1, 2025 23:58
@mysterytony
Copy link
Contributor Author

@briandorsey This PR should be ready for review, thank you in advance!

@briandorsey briandorsey added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Oct 2, 2025
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Oct 2, 2025
@briandorsey briandorsey self-assigned this Oct 2, 2025
Copy link
Contributor

@briandorsey briandorsey left a comment

Choose a reason for hiding this comment

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

Thank you for the submission and all the edits.

I confirmed that we're not seeing new test-suite breakage due to these changes. (The overall tests failed due to errors in other packages.)

@mysterytony I recognize that it's not possible to use client libraries in cases where they don't yet support new/unreleased features. Could I ask you to make a follow up PR to update this sample code to use the client libraries once they do have the support you need (ReadinessProbe)?

@mysterytony
Copy link
Contributor Author

Could I ask you to make a follow up PR to update this sample code to use the client libraries once they do have the support you need?

Yes I will do that.

@briandorsey
Copy link
Contributor

I've taken over this review from @muncus, so I'm going to go head and merge.

@briandorsey briandorsey merged commit f883d42 into GoogleCloudPlatform:main Oct 2, 2025
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants