I am trying to run a very simple operation just to list the clusters in a project, using the google cloud sdk for go
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"google.golang.org/api/container/v1"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // register GCP auth provider
)
var fProjectId = flag.String("projectId", "", "specify a project id to examine")
func main() {
flag.Parse()
if *fProjectId == "" {
log.Fatal("must specific -projectId")
}
ctx := context.TODO()
svc, err := container.NewService(ctx)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Ask Google for a list of all kube clusters in the given project.
_, err = svc.Projects.Zones.Clusters.List(*fProjectId, "-").Context(ctx).Do()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
the code fails as follows
▶ go run main.go --projectId my-project-id
Get "https://container.googleapis.com/v1/projects/my-project-id/zones/-/clusters?alt=json&prettyPrint=false": oauth2: cannot fetch token: 400 Bad Request
Response: {
"error": "invalid_grant",
"error_description": "reauth related error (rapt_required)",
"error_uri": "https://support.google.com/a/answer/9368756",
"error_subtype": "rapt_required"
}
exit status 1
However the command
gcloud container clusters list
succeeds?
What might be the issue here?
The answer in the link is not very informative.
EDIT: The problem was solved once I run
gcloud auth application-default login
Why is this needed?