0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Cloud Run API メモ

Last updated at Posted at 2024-05-28

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

list

api v2 list

list

Go Sample

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.

Ref: https://cloud.google.com/run/docs/reference/about-apis

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?