Commit ca69c2e3 authored by Alex Mohr's avatar Alex Mohr

Merge pull request #19967 from justinsb/fix_gce_npe

Fix for panic when instance not found
parents cd2e0c49 2201c631
...@@ -1815,6 +1815,7 @@ type gceDisk struct { ...@@ -1815,6 +1815,7 @@ type gceDisk struct {
Kind string Kind string
} }
// Gets the named instances, returning cloudprovider.InstanceNotFound if any instance is not found
func (gce *GCECloud) getInstancesByNames(names []string) ([]*gceInstance, error) { func (gce *GCECloud) getInstancesByNames(names []string) ([]*gceInstance, error) {
instances := make(map[string]*gceInstance) instances := make(map[string]*gceInstance)
...@@ -1863,7 +1864,8 @@ func (gce *GCECloud) getInstancesByNames(names []string) ([]*gceInstance, error) ...@@ -1863,7 +1864,8 @@ func (gce *GCECloud) getInstancesByNames(names []string) ([]*gceInstance, error)
for i, name := range names { for i, name := range names {
instance := instances[name] instance := instances[name]
if instance == nil { if instance == nil {
return nil, fmt.Errorf("failed to retrieve instance: %q", name) glog.Errorf("Failed to retrieve instance: %q", name)
return nil, cloudprovider.InstanceNotFound
} }
instanceArray[i] = instances[name] instanceArray[i] = instances[name]
} }
...@@ -1871,6 +1873,7 @@ func (gce *GCECloud) getInstancesByNames(names []string) ([]*gceInstance, error) ...@@ -1871,6 +1873,7 @@ func (gce *GCECloud) getInstancesByNames(names []string) ([]*gceInstance, error)
return instanceArray, nil return instanceArray, nil
} }
// Gets the named instance, returning cloudprovider.InstanceNotFound if the instance is not found
func (gce *GCECloud) getInstanceByName(name string) (*gceInstance, error) { func (gce *GCECloud) getInstanceByName(name string) (*gceInstance, error) {
// Avoid changing behaviour when not managing multiple zones // Avoid changing behaviour when not managing multiple zones
if len(gce.managedZones) == 1 { if len(gce.managedZones) == 1 {
...@@ -1878,9 +1881,11 @@ func (gce *GCECloud) getInstanceByName(name string) (*gceInstance, error) { ...@@ -1878,9 +1881,11 @@ func (gce *GCECloud) getInstanceByName(name string) (*gceInstance, error) {
zone := gce.managedZones[0] zone := gce.managedZones[0]
res, err := gce.service.Instances.Get(gce.projectID, zone, name).Do() res, err := gce.service.Instances.Get(gce.projectID, zone, name).Do()
if err != nil { if err != nil {
if !isHTTPErrorCode(err, http.StatusNotFound) { glog.Errorf("Failed to retrieve TargetInstance resource for instance: %s", name)
return nil, err if isHTTPErrorCode(err, http.StatusNotFound) {
return nil, cloudprovider.InstanceNotFound
} }
return nil, err
} }
return &gceInstance{ return &gceInstance{
Zone: lastComponent(res.Zone), Zone: lastComponent(res.Zone),
...@@ -1895,6 +1900,7 @@ func (gce *GCECloud) getInstanceByName(name string) (*gceInstance, error) { ...@@ -1895,6 +1900,7 @@ func (gce *GCECloud) getInstanceByName(name string) (*gceInstance, error) {
return nil, err return nil, err
} }
if len(instances) != 1 || instances[0] == nil { if len(instances) != 1 || instances[0] == nil {
// getInstancesByNames not obeying its contract
return nil, fmt.Errorf("unexpected return value from getInstancesByNames: %v", instances) return nil, fmt.Errorf("unexpected return value from getInstancesByNames: %v", instances)
} }
return instances[0], nil return instances[0], nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment