@@ -793,7 +793,7 @@ func (az *Cloud) updateNodeCaches(prevNode, newNode *v1.Node) {
793793 }
794794 }
795795
796- //Remove from nodeZones cache if using depreciated LabelFailureDomainBetaZone
796+ // Remove from nodeZones cache if using deprecated LabelFailureDomainBetaZone
797797 prevZoneFailureDomain , ok := prevNode .ObjectMeta .Labels [v1 .LabelFailureDomainBetaZone ]
798798 if ok && az .isAvailabilityZone (prevZoneFailureDomain ) {
799799 az .nodeZones [prevZone ].Delete (prevNode .ObjectMeta .Name )
@@ -808,16 +808,17 @@ func (az *Cloud) updateNodeCaches(prevNode, newNode *v1.Node) {
808808 delete (az .nodeResourceGroups , prevNode .ObjectMeta .Name )
809809 }
810810
811- // Remove from unmanagedNodes cache.
812811 managed , ok := prevNode .ObjectMeta .Labels [managedByAzureLabel ]
813- if ok && managed == "false" {
812+ isNodeManagedByCloudProvider := ! ok || managed != "false"
813+
814+ // Remove unmanagedNodes cache
815+ if ! isNodeManagedByCloudProvider {
814816 az .unmanagedNodes .Delete (prevNode .ObjectMeta .Name )
815- az .excludeLoadBalancerNodes .Delete (prevNode .ObjectMeta .Name )
816817 }
817818
818- // Remove from excludeLoadBalancerNodes cache.
819- if _ , hasExcludeBalancerLabel := prevNode . ObjectMeta . Labels [ v1 . LabelNodeExcludeBalancers ]; hasExcludeBalancerLabel {
820- az .excludeLoadBalancerNodes .Delete (prevNode .ObjectMeta .Name )
819+ if newNode == nil {
820+ // the node is being deleted from the cluster, exclude it from load balancers
821+ az .excludeLoadBalancerNodes .Insert (prevNode .ObjectMeta .Name )
821822 }
822823 }
823824
@@ -840,16 +841,26 @@ func (az *Cloud) updateNodeCaches(prevNode, newNode *v1.Node) {
840841 az .nodeResourceGroups [newNode .ObjectMeta .Name ] = strings .ToLower (newRG )
841842 }
842843
843- // Add to unmanagedNodes cache.
844+ _ , hasExcludeBalancerLabel := newNode . ObjectMeta . Labels [ v1 . LabelNodeExcludeBalancers ]
844845 managed , ok := newNode .ObjectMeta .Labels [managedByAzureLabel ]
845- if ok && managed == "false" {
846+ isNodeManagedByCloudProvider := ! ok || managed != "false"
847+
848+ // update unmanagedNodes cache
849+ if ! isNodeManagedByCloudProvider {
846850 az .unmanagedNodes .Insert (newNode .ObjectMeta .Name )
847- az .excludeLoadBalancerNodes .Insert (newNode .ObjectMeta .Name )
848851 }
849-
850- // Add to excludeLoadBalancerNodes cache.
851- if _ , hasExcludeBalancerLabel := newNode .ObjectMeta .Labels [v1 .LabelNodeExcludeBalancers ]; hasExcludeBalancerLabel {
852+ // update excludeLoadBalancerNodes cache
853+ switch {
854+ case ! isNodeManagedByCloudProvider :
855+ az .excludeLoadBalancerNodes .Insert (newNode .ObjectMeta .Name )
856+ case hasExcludeBalancerLabel :
852857 az .excludeLoadBalancerNodes .Insert (newNode .ObjectMeta .Name )
858+ case ! isNodeReady (newNode ):
859+ az .excludeLoadBalancerNodes .Insert (newNode .ObjectMeta .Name )
860+ default :
861+ // Nodes not falling into the three cases above are valid backends and
862+ // are removed from excludeLoadBalancerNodes cache.
863+ az .excludeLoadBalancerNodes .Delete (newNode .ObjectMeta .Name )
853864 }
854865 }
855866}
@@ -975,3 +986,14 @@ func (az *Cloud) ShouldNodeExcludedFromLoadBalancer(nodeName string) (bool, erro
975986
976987 return az .excludeLoadBalancerNodes .Has (nodeName ), nil
977988}
989+
990+ // This, along with the few lines that call this function in updateNodeCaches, should be
991+ // replaced by https://github.com/kubernetes-sigs/cloud-provider-azure/pull/1195 once that merges.
992+ func isNodeReady (node * v1.Node ) bool {
993+ for _ , cond := range node .Status .Conditions {
994+ if cond .Type == v1 .NodeReady && cond .Status == v1 .ConditionTrue {
995+ return true
996+ }
997+ }
998+ return false
999+ }
0 commit comments