Skip to content

Instantly share code, notes, and snippets.

@ahmetb

ahmetb/main.go Secret

Last active June 19, 2017 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmetb/d358aaacd970d3e9baf0cb9a52e0b041 to your computer and use it in GitHub Desktop.
Save ahmetb/d358aaacd970d3e9baf0cb9a52e0b041 to your computer and use it in GitHub Desktop.
Authenticate to Google Container Engine API with Go client library
// Copyright 2017 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Command listclusters lists all clusters and their node pools for a given project and zone.
package main
import (
"flag"
"fmt"
"log"
"os"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
container "google.golang.org/api/container/v1"
)
var (
projectID = flag.String("project", "", "Project ID")
zone = flag.String("zone", "", "Compute zone")
)
func main() {
flag.Parse()
if *projectID == "" {
fmt.Fprintln(os.Stderr, "missing -project flag")
flag.Usage()
os.Exit(2)
}
if *zone == "" {
fmt.Fprintln(os.Stderr, "missing -zone flag")
flag.Usage()
os.Exit(2)
}
ctx := context.Background()
// See https://cloud.google.com/docs/authentication/.
// Use GOOGLE_APPLICATION_CREDENTIALS environment variable to specify
// a service account key file to authenticate to the API.
hc, err := google.DefaultClient(ctx, container.CloudPlatformScope)
if err != nil {
log.Fatalf("Could not get authenticated client: %v", err)
}
svc, err := container.New(hc)
if err != nil {
log.Fatalf("Could not initialize gke client: %v", err)
}
if err := listClusters(svc, *projectID, *zone); err != nil {
log.Fatal(err)
}
}
func listClusters(svc *container.Service, projectID, zone string) error {
list, err := svc.Projects.Zones.Clusters.List(projectID, zone).Do()
if err != nil {
return fmt.Errorf("failed to list clusters: %v", err)
}
for _, v := range list.Clusters {
fmt.Printf("Cluster %q (%s) master_version: v%s", v.Name, v.Status, v.CurrentMasterVersion)
poolList, err := svc.Projects.Zones.Clusters.NodePools.List(projectID, zone, v.Name).Do()
if err != nil {
return fmt.Errorf("failed to list node pools for cluster %q: %v", v.Name, err)
}
for _, np := range poolList.NodePools {
fmt.Printf(" -> Pool %q (%s) machineType=%s node_version=v%s autoscaling=%v", np.Name, np.Status,
np.Config.MachineType, np.Version, np.Autoscaling != nil && np.Autoscaling.Enabled)
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment