Merge pull request #96278 from cheftako/master

Switch GCP list calls to paginated calls.
This commit is contained in:
Kubernetes Prow Robot 2020-11-05 17:20:50 -08:00 committed by GitHub
commit 4b30a88ab8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 12 deletions

View File

@ -873,17 +873,20 @@ func getZonesForRegion(svc *compute.Service, projectID, region string) ([]string
// (tested in https://cloud.google.com/compute/docs/reference/latest/zones/list)
// listCall = listCall.Filter("region eq " + region)
res, err := listCall.Do()
var zones []string
var accumulator = func(response *compute.ZoneList) error {
for _, zone := range response.Items {
regionName := lastComponent(zone.Region)
if regionName == region {
zones = append(zones, zone.Name)
}
}
return nil
}
err := listCall.Pages(context.TODO(), accumulator)
if err != nil {
return nil, fmt.Errorf("unexpected response listing zones: %v", err)
}
zones := []string{}
for _, zone := range res.Items {
regionName := lastComponent(zone.Region)
if regionName == region {
zones = append(zones, zone.Name)
}
}
return zones, nil
}

View File

@ -126,22 +126,32 @@ func (g *Cloud) ListTPUs(ctx context.Context, zone string) ([]*tpuapi.Node, erro
mc := newTPUMetricContext("list", zone)
parent := getTPUParentName(g.projectID, zone)
response, err := g.tpuService.projects.Locations.Nodes.List(parent).Do()
var nodes []*tpuapi.Node
var accumulator = func(response *tpuapi.ListNodesResponse) error {
nodes = append(nodes, response.Nodes...)
return nil
}
err := g.tpuService.projects.Locations.Nodes.List(parent).Pages(ctx, accumulator)
if err != nil {
return nil, mc.Observe(err)
}
return response.Nodes, mc.Observe(nil)
return nodes, mc.Observe(nil)
}
// ListLocations returns the zones where Cloud TPUs are available.
func (g *Cloud) ListLocations(ctx context.Context) ([]*tpuapi.Location, error) {
mc := newTPUMetricContext("list_locations", "")
parent := getTPUProjectURL(g.projectID)
response, err := g.tpuService.projects.Locations.List(parent).Do()
var locations []*tpuapi.Location
var accumulator = func(response *tpuapi.ListLocationsResponse) error {
locations = append(locations, response.Locations...)
return nil
}
err := g.tpuService.projects.Locations.List(parent).Pages(ctx, accumulator)
if err != nil {
return nil, mc.Observe(err)
}
return response.Locations, mc.Observe(nil)
return locations, mc.Observe(nil)
}
// waitForTPUOp checks whether the op is done every 30 seconds before the ctx