Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for panic when instance not found #19967

Merged
merged 1 commit into from
Jan 22, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions pkg/cloudprovider/providers/gce/gce.go
Expand Up @@ -1815,6 +1815,7 @@ type gceDisk struct {
Kind string
}

// Gets the named instances, returning cloudprovider.InstanceNotFound if any instance is not found
func (gce *GCECloud) getInstancesByNames(names []string) ([]*gceInstance, error) {
instances := make(map[string]*gceInstance)

Expand Down Expand Up @@ -1863,24 +1864,28 @@ func (gce *GCECloud) getInstancesByNames(names []string) ([]*gceInstance, error)
for i, name := range names {
instance := instances[name]
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]
}

return instanceArray, nil
}

// 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 {
name = canonicalizeInstanceName(name)
zone := gce.managedZones[0]
res, err := gce.service.Instances.Get(gce.projectID, zone, name).Do()
if err != nil {
if !isHTTPErrorCode(err, http.StatusNotFound) {
return nil, err
glog.Errorf("Failed to retrieve TargetInstance resource for instance: %s", name)
if isHTTPErrorCode(err, http.StatusNotFound) {
return nil, cloudprovider.InstanceNotFound
}
return nil, err
}
return &gceInstance{
Zone: lastComponent(res.Zone),
Expand All @@ -1895,6 +1900,7 @@ func (gce *GCECloud) getInstanceByName(name string) (*gceInstance, error) {
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
Expand Down