Merge pull request #42452 from jingxu97/Mar/nodeNamePrefix

Automatic merge from submit-queue (batch tested with PRs 42452, 43399)

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.

Partially fix issue #42445
This commit is contained in:
Kubernetes Submit Queue 2017-03-20 15:23:33 -07:00 committed by GitHub
commit a2d74cda38

View File

@ -2900,14 +2900,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
}
@ -2920,16 +2919,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