From 92f05da1ff99bcb318c58c809b05a05f3a8a5544 Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Thu, 2 Mar 2017 15:47:17 -0800 Subject: [PATCH] Modify getInstanceByName to avoid calling getInstancesByNames This PR modify getInstanceByname to loop through all management zones directly instead of calling getInstancesByNames. Currently getInstancesByNames use a node name prefix as a filter to list the instances. If the prefix does not match, it will return all instances which is very wasteful since getInstanceByName only query one instance with a specific name. --- pkg/cloudprovider/providers/gce/gce.go | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/pkg/cloudprovider/providers/gce/gce.go b/pkg/cloudprovider/providers/gce/gce.go index d8d31c2c0a5..63e86f965a3 100644 --- a/pkg/cloudprovider/providers/gce/gce.go +++ b/pkg/cloudprovider/providers/gce/gce.go @@ -2894,14 +2894,13 @@ func (gce *GCECloud) getInstancesByNames(names []string) ([]*gceInstance, error) // Gets the named instance, returning cloudprovider.InstanceNotFound if the instance is not found func (gce *GCECloud) getInstanceByName(name string) (*gceInstance, error) { // Avoid changing behaviour when not managing multiple zones - if len(gce.managedZones) == 1 { + for _, zone := range gce.managedZones { name = canonicalizeInstanceName(name) - zone := gce.managedZones[0] res, err := gce.service.Instances.Get(gce.projectID, zone, name).Do() if err != nil { - glog.Errorf("getInstanceByName/single-zone: failed to get instance %s; err: %v", name, err) + glog.Errorf("getInstanceByName: failed to get instance %s; err: %v", name, err) if isHTTPErrorCode(err, http.StatusNotFound) { - return nil, cloudprovider.InstanceNotFound + continue } return nil, err } @@ -2914,16 +2913,7 @@ func (gce *GCECloud) getInstanceByName(name string) (*gceInstance, error) { }, nil } - instances, err := gce.getInstancesByNames([]string{name}) - if err != nil { - glog.Errorf("getInstanceByName/multiple-zones: failed to get instance %s; err: %v", name, err) - return nil, err - } - if len(instances) != 1 || instances[0] == nil { - // getInstancesByNames not obeying its contract - return nil, fmt.Errorf("unexpected return value from getInstancesByNames: %v", instances) - } - return instances[0], nil + return nil, cloudprovider.InstanceNotFound } // Returns the last component of a URL, i.e. anything after the last slash