@@ -56,6 +56,7 @@ type delayedRouteOperation struct {
5656 routeTableTags map [string ]* string
5757 operation routeOperation
5858 result chan batchOperationResult
59+ nodeName string
5960}
6061
6162// wait waits for the operation completion and returns the result.
@@ -179,6 +180,19 @@ func (d *delayedRouteUpdater) updateRoutes(ctx context.Context) {
179180 break
180181 }
181182 }
183+ // After removing the matched routes (if any), loop again to remove the outdated routes,
184+ // whose name and IP addresses may not match but target the same node.
185+ for i := len (routes ) - 1 ; i >= 0 ; i -- {
186+ existingRoute := routes [i ]
187+ // remove all routes that target the node when the operation is delete
188+ if strings .HasPrefix (ptr .Deref (existingRoute .Name , "" ), ptr .Deref (rt .route .Name , "" )) {
189+ if rt .operation == routeOperationDelete {
190+ routes = append (routes [:i ], routes [i + 1 :]... )
191+ dirty = true
192+ klog .V (2 ).Infof ("updateRoutes: found outdated route %s targeting node %s, removing it" , ptr .Deref (rt .route .Name , "" ), rt .nodeName )
193+ }
194+ }
195+ }
182196 if rt .operation == routeOperationDelete && ! dirty {
183197 klog .Warningf ("updateRoutes: route to be deleted %s does not match any of the existing route" , ptr .Deref (rt .route .Name , "" ))
184198 }
@@ -239,17 +253,19 @@ func (d *delayedRouteUpdater) cleanupOutdatedRoutes(existingRoutes []*armnetwork
239253 return existingRoutes , changed
240254}
241255
242- func getAddRouteOperation (route * armnetwork.Route ) batchOperation {
256+ func getAddRouteOperation (route * armnetwork.Route , nodeName string ) batchOperation {
243257 return & delayedRouteOperation {
244258 route : route ,
259+ nodeName : nodeName ,
245260 operation : routeOperationAdd ,
246261 result : make (chan batchOperationResult ),
247262 }
248263}
249264
250- func getDeleteRouteOperation (route * armnetwork.Route ) batchOperation {
265+ func getDeleteRouteOperation (route * armnetwork.Route , nodeName string ) batchOperation {
251266 return & delayedRouteOperation {
252267 route : route ,
268+ nodeName : nodeName ,
253269 operation : routeOperationDelete ,
254270 result : make (chan batchOperationResult ),
255271 }
@@ -421,7 +437,7 @@ func (az *Cloud) CreateRoute(ctx context.Context, clusterName string, _ string,
421437 }
422438
423439 klog .V (2 ).Infof ("CreateRoute: creating route for clusterName=%q instance=%q cidr=%q" , clusterName , kubeRoute .TargetNode , kubeRoute .DestinationCIDR )
424- op := az .routeUpdater .addOperation (getAddRouteOperation (route ))
440+ op := az .routeUpdater .addOperation (getAddRouteOperation (route , string ( kubeRoute . TargetNode ) ))
425441
426442 // Wait for operation complete.
427443 err = op .wait ().err
@@ -466,7 +482,7 @@ func (az *Cloud) DeleteRoute(_ context.Context, clusterName string, kubeRoute *c
466482 Name : ptr .To (routeName ),
467483 Properties : & armnetwork.RoutePropertiesFormat {},
468484 }
469- op := az .routeUpdater .addOperation (getDeleteRouteOperation (route ))
485+ op := az .routeUpdater .addOperation (getDeleteRouteOperation (route , string ( kubeRoute . TargetNode ) ))
470486
471487 // Wait for operation complete.
472488 err = op .wait ().err
@@ -483,7 +499,7 @@ func (az *Cloud) DeleteRoute(_ context.Context, clusterName string, kubeRoute *c
483499 Name : ptr .To (routeNameWithoutIPV6Suffix ),
484500 Properties : & armnetwork.RoutePropertiesFormat {},
485501 }
486- op := az .routeUpdater .addOperation (getDeleteRouteOperation (route ))
502+ op := az .routeUpdater .addOperation (getDeleteRouteOperation (route , string ( kubeRoute . TargetNode ) ))
487503
488504 // Wait for operation complete.
489505 err = op .wait ().err
0 commit comments