Added support for regional calls to GetClusters

Fixed issues from lavalamp and verification.
Added todo.
Fixed import sections.
This commit is contained in:
Walter Fender 2018-09-19 17:13:49 -07:00
parent 54dd6e4f14
commit 47bfaa3aba
2 changed files with 31 additions and 12 deletions

View File

@ -18,6 +18,10 @@ package gce
import ( import (
"context" "context"
"errors"
"fmt"
"github.com/golang/glog"
container "google.golang.org/api/container/v1" container "google.golang.org/api/container/v1"
) )
@ -41,16 +45,22 @@ func (gce *GCECloud) ListClusters(ctx context.Context) ([]string, error) {
} }
func (gce *GCECloud) GetManagedClusters(ctx context.Context) ([]*container.Cluster, error) { func (gce *GCECloud) GetManagedClusters(ctx context.Context) ([]*container.Cluster, error) {
managedClusters := []*container.Cluster{} var location string
for _, zone := range gce.managedZones { if len(gce.managedZones) > 1 {
clusters, err := gce.getClustersInZone(zone) // Multiple managed zones means this is a regional cluster
if err != nil { // so use the regional location and not the zone.
return nil, err location = gce.region
} } else if len(gce.managedZones) == 1 {
managedClusters = append(managedClusters, clusters...) location = gce.managedZones[0]
} else {
return nil, errors.New(fmt.Sprintf("no zones associated with this cluster(%s)", gce.ProjectID()))
}
clusters, err := gce.getClustersInLocation(location)
if err != nil {
return nil, err
} }
return managedClusters, nil return clusters, nil
} }
func (gce *GCECloud) Master(ctx context.Context, clusterName string) (string, error) { func (gce *GCECloud) Master(ctx context.Context, clusterName string) (string, error) {
@ -58,7 +68,7 @@ func (gce *GCECloud) Master(ctx context.Context, clusterName string) (string, er
} }
func (gce *GCECloud) listClustersInZone(zone string) ([]string, error) { func (gce *GCECloud) listClustersInZone(zone string) ([]string, error) {
clusters, err := gce.getClustersInZone(zone) clusters, err := gce.getClustersInLocation(zone)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -70,13 +80,18 @@ func (gce *GCECloud) listClustersInZone(zone string) ([]string, error) {
return result, nil return result, nil
} }
func (gce *GCECloud) getClustersInZone(zone string) ([]*container.Cluster, error) { func (gce *GCECloud) getClustersInLocation(zoneOrRegion string) ([]*container.Cluster, error) {
mc := newClustersMetricContext("list_zone", zone) // TODO: Issue/68913 migrate metric to list_location instead of list_zone.
mc := newClustersMetricContext("list_zone", zoneOrRegion)
// TODO: use PageToken to list all not just the first 500 // TODO: use PageToken to list all not just the first 500
list, err := gce.containerService.Projects.Zones.Clusters.List(gce.projectID, zone).Do() location := getLocationName(gce.projectID, zoneOrRegion)
list, err := gce.containerService.Projects.Locations.Clusters.List(location).Do()
if err != nil { if err != nil {
return nil, mc.Observe(err) return nil, mc.Observe(err)
} }
if list.Header.Get("nextPageToken") != "" {
glog.Errorf("Failed to get all clusters for request, received next page token %s", list.Header.Get("nextPageToken"))
}
return list.Clusters, mc.Observe(nil) return list.Clusters, mc.Observe(nil)
} }

View File

@ -281,3 +281,7 @@ func typeOfNetwork(network *compute.Network) netType {
return netTypeCustom return netTypeCustom
} }
func getLocationName(project, zoneOrRegion string) string {
return fmt.Sprintf("projects/%s/locations/%s", project, zoneOrRegion)
}