Skip to content

Commit

Permalink
fix: Remove all routes that have the node name as prefix (#8497)
Browse files Browse the repository at this point in the history
Co-authored-by: Qi Ni <pomelonicky@gmail.com>
  • Loading branch information
k8s-infra-cherrypick-robot and nilo19 authored Mar 3, 2025
1 parent 6e34d82 commit d62bda5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
26 changes: 21 additions & 5 deletions pkg/provider/azure_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type delayedRouteOperation struct {
routeTableTags map[string]*string
operation routeOperation
result chan batchOperationResult
nodeName string
}

// wait waits for the operation completion and returns the result.
Expand Down Expand Up @@ -179,6 +180,19 @@ func (d *delayedRouteUpdater) updateRoutes(ctx context.Context) {
break
}
}
// After removing the matched routes (if any), loop again to remove the outdated routes,
// whose name and IP addresses may not match but target the same node.
for i := len(routes) - 1; i >= 0; i-- {
existingRoute := routes[i]
// remove all routes that target the node when the operation is delete
if strings.HasPrefix(ptr.Deref(existingRoute.Name, ""), ptr.Deref(rt.route.Name, "")) {
if rt.operation == routeOperationDelete {
routes = append(routes[:i], routes[i+1:]...)
dirty = true
klog.V(2).Infof("updateRoutes: found outdated route %s targeting node %s, removing it", ptr.Deref(rt.route.Name, ""), rt.nodeName)
}
}
}
if rt.operation == routeOperationDelete && !dirty {
klog.Warningf("updateRoutes: route to be deleted %s does not match any of the existing route", ptr.Deref(rt.route.Name, ""))
}
Expand Down Expand Up @@ -239,17 +253,19 @@ func (d *delayedRouteUpdater) cleanupOutdatedRoutes(existingRoutes []*armnetwork
return existingRoutes, changed
}

func getAddRouteOperation(route *armnetwork.Route) batchOperation {
func getAddRouteOperation(route *armnetwork.Route, nodeName string) batchOperation {
return &delayedRouteOperation{
route: route,
nodeName: nodeName,
operation: routeOperationAdd,
result: make(chan batchOperationResult),
}
}

func getDeleteRouteOperation(route *armnetwork.Route) batchOperation {
func getDeleteRouteOperation(route *armnetwork.Route, nodeName string) batchOperation {
return &delayedRouteOperation{
route: route,
nodeName: nodeName,
operation: routeOperationDelete,
result: make(chan batchOperationResult),
}
Expand Down Expand Up @@ -421,7 +437,7 @@ func (az *Cloud) CreateRoute(ctx context.Context, clusterName string, _ string,
}

klog.V(2).Infof("CreateRoute: creating route for clusterName=%q instance=%q cidr=%q", clusterName, kubeRoute.TargetNode, kubeRoute.DestinationCIDR)
op := az.routeUpdater.addOperation(getAddRouteOperation(route))
op := az.routeUpdater.addOperation(getAddRouteOperation(route, string(kubeRoute.TargetNode)))

// Wait for operation complete.
err = op.wait().err
Expand Down Expand Up @@ -466,7 +482,7 @@ func (az *Cloud) DeleteRoute(_ context.Context, clusterName string, kubeRoute *c
Name: ptr.To(routeName),
Properties: &armnetwork.RoutePropertiesFormat{},
}
op := az.routeUpdater.addOperation(getDeleteRouteOperation(route))
op := az.routeUpdater.addOperation(getDeleteRouteOperation(route, string(kubeRoute.TargetNode)))

// Wait for operation complete.
err = op.wait().err
Expand All @@ -483,7 +499,7 @@ func (az *Cloud) DeleteRoute(_ context.Context, clusterName string, kubeRoute *c
Name: ptr.To(routeNameWithoutIPV6Suffix),
Properties: &armnetwork.RoutePropertiesFormat{},
}
op := az.routeUpdater.addOperation(getDeleteRouteOperation(route))
op := az.routeUpdater.addOperation(getDeleteRouteOperation(route, string(kubeRoute.TargetNode)))

// Wait for operation complete.
err = op.wait().err
Expand Down
4 changes: 4 additions & 0 deletions pkg/provider/azure_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestDeleteRoute(t *testing.T) {
DestinationCIDR: "1.2.3.4/24",
}
routeName := mapNodeNameToRouteName(false, route.TargetNode, route.DestinationCIDR)
routeNameWithNodeNamePrefix := "node____bar"
routeTables := &armnetwork.RouteTable{
Name: &cloud.RouteTableName,
Location: &cloud.Location,
Expand All @@ -68,6 +69,9 @@ func TestDeleteRoute(t *testing.T) {
{
Name: &routeName,
},
{
Name: &routeNameWithNodeNamePrefix,
},
},
},
}
Expand Down

0 comments on commit d62bda5

Please sign in to comment.