gloud
version
gcloud --version
Google Cloud SDK 477.0.0
alpha 2024.05.17
beta 2024.05.17
bq 2.1.4
core 2024.05.17
gcloud-crc32c 1.0.0
gke-gcloud-auth-plugin 0.5.8
gsutil 5.29
kubectl 1.27.14
1. List services
run services listで使われているAPI確認
gcloud run services list --project <project> --log-http
==== request start ====
uri: https://run.googleapis.com/v1/projects/<project-name>/locations/-/services?alt=json
v1が使われている
api v1 list
api v2 list
Go Sample
- v1: https://pkg.go.dev/google.golang.org/api@v0.181.0/run/v1#NamespacesServicesService.List
- v2: https://pkg.go.dev/google.golang.org/api@v0.181.0/run/v2#ProjectsLocationsServicesService.List
v2はregion指定しないといけないっぽい
package main
import (
"context"
"fmt"
"os"
"google.golang.org/api/run/v1"
runv2 "google.golang.org/api/run/v2"
)
func main() {
project := os.Getenv("GCP_PROJECT")
ctx := context.Background()
runService, err := run.NewService(ctx)
if err != nil {
panic(err)
}
// v1
// https://pkg.go.dev/google.golang.org/api/run/v1#NamespacesServicesService.List
res, err := runService.Namespaces.Services.List(fmt.Sprintf("namespaces/%s", project)).Do()
if err != nil {
panic(err)
}
for _, svc := range res.Items {
fmt.Printf("%s\t%s\t%s\t%s\n", svc.Status.Conditions[0].Status, svc.Metadata.Name, svc.Metadata.Labels["cloud.googleapis.com/location"], svc.Status.Url)
}
// v2
runv2Service, err := runv2.NewService(ctx)
if err != nil {
panic(err)
}
// https://pkg.go.dev/google.golang.org/api@v0.181.0/run/v2#ProjectsLocationsServicesService.List
// need to specify region!
resv2, err := runv2.NewProjectsService(runv2Service).Locations.Services.List(fmt.Sprintf("projects/%s/locations/asia-northeast1", project)).Do()
if err != nil {
panic(err)
}
for _, svc := range resv2.Services {
fmt.Printf("%s\t%s\n", svc.Conditions[0].State, svc.Name)
}
}
v1 vs. v2
Who should use v1?
You should only use v1 if you need Knative or Kubernetes compatibility. The Service resource of the Cloud Run Admin API v1 is compatible with the Knative Serving API. The v1 Admin API is used when deploying from a YAML file.
Who should use v2?
All other usages should use v2, in particular, anyone making API calls. The Cloud Client Libraries, which are the client libraries recommended by Google, use v2.