From 5f0e3c3682feeabed23c1d9714dea6a1222fad13 Mon Sep 17 00:00:00 2001 From: Christian Schlotter Date: Mon, 26 Sep 2022 13:40:42 +0200 Subject: [PATCH] kindnetd remove wrong routes --- images/kindnetd/cmd/kindnetd/routes.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/images/kindnetd/cmd/kindnetd/routes.go b/images/kindnetd/cmd/kindnetd/routes.go index 39f628d161..38bca2b2d5 100644 --- a/images/kindnetd/cmd/kindnetd/routes.go +++ b/images/kindnetd/cmd/kindnetd/routes.go @@ -34,15 +34,31 @@ func syncRoute(nodeIP string, podCIDRs []string) error { return err } - // Check if the route exists to the other node's PodCIDR + // Declare the wanted route. routeToDst := netlink.Route{Dst: dst, Gw: ip} - route, err := netlink.RouteListFiltered(nl.GetIPFamily(ip), &routeToDst, netlink.RT_FILTER_DST) + // List all routes which have the same dst set. + // RouteListFiltered ignores the gw for filtering because of the passed filterMask. + routes, err := netlink.RouteListFiltered(nl.GetIPFamily(ip), &routeToDst, netlink.RT_FILTER_DST) if err != nil { return err } + // Check if the wanted route exists and delete wrong routes + found := false + for _, route := range routes { + if routeToDst.Gw.Equal(ip) { + found = true + continue + } + // Delete wrong route because of invalid gateway. + klog.Infof("Removing invalid route %v\n", route) + if err := netlink.RouteDel(&route); err != nil { + return err + } + } + // Add route if not present - if len(route) == 0 { + if !found { klog.Infof("Adding route %v \n", routeToDst) if err := netlink.RouteAdd(&routeToDst); err != nil { return err