@@ -72,11 +72,17 @@ func getZone(node *v1.Node) string {
7272 return zone
7373 }
7474 zone , ok = node .Labels [v1 .LabelFailureDomainBetaZone ]
75- if ! ok {
76- klog .Warningf ("Node without zone label, returning %q as zone. Node name: %v, node labels: %v" , emptyZone , node .Name , node .Labels )
77- return emptyZone
75+ if ok {
76+ return zone
7877 }
79- return zone
78+ if node .Spec .ProviderID != "" {
79+ _ , zone , _ , err := splitProviderID (node .Spec .ProviderID )
80+ if err == nil && zone != "" {
81+ return zone
82+ }
83+ }
84+ klog .Warningf ("Node without zone label or providerID, returning %q as zone. Node name: %v, node labels: %v" , emptyZone , node .Name , node .Labels )
85+ return emptyZone
8086}
8187
8288func makeHostURL (projectsAPIEndpoint , projectID , zone , host string ) string {
@@ -715,7 +721,8 @@ func (g *Cloud) getFoundInstanceByNames(names []string) ([]*gceInstance, error)
715721 found [name ] = nil
716722 }
717723
718- for _ , zone := range g .getManagedZones () {
724+ initialZones := g .getManagedZones ()
725+ for _ , zone := range initialZones {
719726 if remaining == 0 {
720727 break
721728 }
@@ -745,6 +752,47 @@ func (g *Cloud) getFoundInstanceByNames(names []string) ([]*gceInstance, error)
745752 }
746753 }
747754
755+ if remaining > 0 && g .dynamicZones {
756+ if err := g .refreshManagedZones (); err != nil {
757+ klog .Errorf ("Failed to refresh GCE managed zones for remaining instances: %v" , err )
758+ } else {
759+ refreshedZones := g .getManagedZones ()
760+ checkedZones := sets .NewString (initialZones ... )
761+ for _ , zone := range refreshedZones {
762+ if remaining == 0 {
763+ break
764+ }
765+ if checkedZones .Has (zone ) {
766+ continue
767+ }
768+ instances , err := g .c .Instances ().List (ctx , zone , filter .Regexp ("name" , nodeInstancePrefix + ".*" ))
769+ if err != nil {
770+ return nil , err
771+ }
772+ for _ , inst := range instances {
773+ if remaining == 0 {
774+ break
775+ }
776+ if _ , ok := found [inst .Name ]; ! ok {
777+ continue
778+ }
779+ if found [inst .Name ] != nil {
780+ klog .Errorf ("Instance name %q was duplicated (in zone %q and %q)" , inst .Name , zone , found [inst .Name ].Zone )
781+ continue
782+ }
783+ found [inst .Name ] = & gceInstance {
784+ Zone : zone ,
785+ Name : inst .Name ,
786+ ID : inst .Id ,
787+ Disks : inst .Disks ,
788+ Type : lastComponent (inst .MachineType ),
789+ }
790+ remaining --
791+ }
792+ }
793+ }
794+ }
795+
748796 var ret []* gceInstance
749797 var failed []string
750798 for name , instance := range found {
@@ -761,12 +809,8 @@ func (g *Cloud) getFoundInstanceByNames(names []string) ([]*gceInstance, error)
761809 return ret , nil
762810}
763811
764- // Gets the named instance, returning cloudprovider.InstanceNotFound if the instance is not found
765- func (g * Cloud ) getInstanceByName (name string ) (* gceInstance , error ) {
766- klog .Infof ("Searching node %s in managed zones %v" , name , g .getManagedZones ())
767-
768- // Avoid changing behaviour when not managing multiple zones
769- for _ , zone := range g .getManagedZones () {
812+ func (g * Cloud ) findInstanceInZones (name string , zones []string ) (* gceInstance , error ) {
813+ for _ , zone := range zones {
770814 instance , err := g .getInstanceFromProjectInZoneByName (g .projectID , zone , name )
771815 if err != nil {
772816 if isHTTPErrorCode (err , http .StatusNotFound ) {
@@ -777,6 +821,33 @@ func (g *Cloud) getInstanceByName(name string) (*gceInstance, error) {
777821 }
778822 return instance , nil
779823 }
824+ return nil , nil
825+ }
826+
827+ // Gets the named instance, returning cloudprovider.InstanceNotFound if the instance is not found
828+ func (g * Cloud ) getInstanceByName (name string ) (* gceInstance , error ) {
829+ initialZones := g .getManagedZones ()
830+ klog .Infof ("Searching node %s in managed zones %v" , name , initialZones )
831+
832+ if instance , err := g .findInstanceInZones (name , initialZones ); err != nil || instance != nil {
833+ return instance , err
834+ }
835+
836+ if g .dynamicZones {
837+ klog .Infof ("Node %s not found in managed zones %v; attempting dynamic zone refresh" , name , initialZones )
838+ if err := g .refreshManagedZones (); err != nil {
839+ klog .Errorf ("Failed to refresh GCE managed zones: %v" , err )
840+ } else {
841+ refreshedZones := g .getManagedZones ()
842+ newZones := sets .NewString (refreshedZones ... ).Difference (sets .NewString (initialZones ... )).List ()
843+ if len (newZones ) > 0 {
844+ klog .Infof ("Searching node %s in newly discovered zones %v" , name , newZones )
845+ if instance , err := g .findInstanceInZones (name , newZones ); err != nil || instance != nil {
846+ return instance , err
847+ }
848+ }
849+ }
850+ }
780851
781852 return nil , cloudprovider .InstanceNotFound
782853}
0 commit comments