From 2b4efdea7d2264990366b65b7241fba946f2eaf9 Mon Sep 17 00:00:00 2001 From: "Hantao (Will) Wang" Date: Mon, 15 Jul 2019 16:14:51 -0700 Subject: [PATCH 1/2] move getInstancesByName logic to helper function --- .../gce/gce_instances.go | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/staging/src/k8s.io/legacy-cloud-providers/gce/gce_instances.go b/staging/src/k8s.io/legacy-cloud-providers/gce/gce_instances.go index ef705fa808b..f1855ba8e4f 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/gce/gce_instances.go +++ b/staging/src/k8s.io/legacy-cloud-providers/gce/gce_instances.go @@ -426,6 +426,28 @@ func (g *Cloud) AddAliasToInstance(nodeName types.NodeName, alias *net.IPNet) er // Gets the named instances, returning cloudprovider.InstanceNotFound if any // instance is not found func (g *Cloud) getInstancesByNames(names []string) ([]*gceInstance, error) { + instanceOrErrors, err := g.getInstanceOrErrorsByNames(names) + if err != nil { + return nil, err + } + var allInstances []*gceInstance + for _, entry := range instanceOrErrors { + if entry.err != nil { + return nil, entry.err + } + allInstances = append(allInstances, entry.instance) + } + return allInstances, nil +} + +type instanceOrError struct { + instance *gceInstance + err error +} + +// Gets the named instances, returning a map of each name to either the found instances or +// cloudprovider.InstanceNotFound if the instance is not found +func (g *Cloud) getInstanceOrErrorsByNames(allNames []string) (map[string]*instanceOrError, error) { ctx, cancel := cloud.ContextWithCallTimeout() defer cancel() From d44abb4ebe7fbcc46fa12a2f77374bd248f47e9f Mon Sep 17 00:00:00 2001 From: "Hantao (Will) Wang" Date: Tue, 16 Jul 2019 10:03:36 -0700 Subject: [PATCH 2/2] implement functionality to return all found instances --- .../gce/gce_instances.go | 46 +++++++------------ 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/staging/src/k8s.io/legacy-cloud-providers/gce/gce_instances.go b/staging/src/k8s.io/legacy-cloud-providers/gce/gce_instances.go index f1855ba8e4f..5cd2679c66b 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/gce/gce_instances.go +++ b/staging/src/k8s.io/legacy-cloud-providers/gce/gce_instances.go @@ -426,28 +426,19 @@ func (g *Cloud) AddAliasToInstance(nodeName types.NodeName, alias *net.IPNet) er // Gets the named instances, returning cloudprovider.InstanceNotFound if any // instance is not found func (g *Cloud) getInstancesByNames(names []string) ([]*gceInstance, error) { - instanceOrErrors, err := g.getInstanceOrErrorsByNames(names) + foundInstances, err := g.getFoundInstanceByNames(names) if err != nil { return nil, err } - var allInstances []*gceInstance - for _, entry := range instanceOrErrors { - if entry.err != nil { - return nil, entry.err - } - allInstances = append(allInstances, entry.instance) + if len(foundInstances) != len(names) { + return nil, cloudprovider.InstanceNotFound } - return allInstances, nil + return foundInstances, nil } -type instanceOrError struct { - instance *gceInstance - err error -} - -// Gets the named instances, returning a map of each name to either the found instances or -// cloudprovider.InstanceNotFound if the instance is not found -func (g *Cloud) getInstanceOrErrorsByNames(allNames []string) (map[string]*instanceOrError, error) { +// Gets the named instances, returning a list of gceInstances it was able to find from the provided +// list of names. +func (g *Cloud) getFoundInstanceByNames(names []string) ([]*gceInstance, error) { ctx, cancel := cloud.ContextWithCallTimeout() defer cancel() @@ -494,20 +485,17 @@ func (g *Cloud) getInstanceOrErrorsByNames(allNames []string) (map[string]*insta } } - if remaining > 0 { - var failed []string - for k := range found { - if found[k] == nil { - failed = append(failed, k) - } - } - klog.Errorf("Failed to retrieve instances: %v", failed) - return nil, cloudprovider.InstanceNotFound - } - var ret []*gceInstance - for _, instance := range found { - ret = append(ret, instance) + var failed []string + for name, instance := range found { + if instance != nil { + ret = append(ret, instance) + } else { + failed = append(failed, name) + } + } + if len(failed) > 0 { + klog.Errorf("Failed to retrieve instances: %v", failed) } return ret, nil