Skip to content

Commit

Permalink
kindnetd remove wrong routes
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi committed Sep 26, 2022
1 parent 333bc7f commit 2d52636
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
4 changes: 2 additions & 2 deletions images/kindnetd/cmd/kindnetd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,12 @@ func makeNodesReconciler(cniConfig *CNIConfigWriter, hostIP string, ipFamily IPF
}

if nodeIPv4 != "" && len(podCIDRsv4) > 0 {
if err := syncRoute(nodeIPv4, podCIDRsv4); err != nil {
if err := syncRoutes(nodeIPv4, podCIDRsv4); err != nil {
return err
}
}
if nodeIPv6 != "" && len(podCIDRsv6) > 0 {
if err := syncRoute(nodeIPv6, podCIDRsv6); err != nil {
if err := syncRoutes(nodeIPv6, podCIDRsv6); err != nil {
return err
}
}
Expand Down
51 changes: 35 additions & 16 deletions images/kindnetd/cmd/kindnetd/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,49 @@ import (
"k8s.io/klog/v2"
)

func syncRoute(nodeIP string, podCIDRs []string) error {
func syncRoutes(nodeIP string, podCIDRs []string) error {
ip := net.ParseIP(nodeIP)

for _, podCIDR := range podCIDRs {
// parse subnet
dst, err := netlink.ParseIPNet(podCIDR)
if err != nil {
if err := syncRoute(podCIDR, ip); err != nil {
return err
}
}
return nil
}

// Check if the route exists to the other node's PodCIDR
routeToDst := netlink.Route{Dst: dst, Gw: ip}
route, err := netlink.RouteListFiltered(nl.GetIPFamily(ip), &routeToDst, netlink.RT_FILTER_DST)
if err != nil {
return err
func syncRoute(podCIDR string, gw net.IP) error {
// parse subnet
dst, err := netlink.ParseIPNet(podCIDR)
if err != nil {
return err
}

// List all routes to the other node's PodCIDR
routeToDst := netlink.Route{Dst: dst, Gw: gw}
routes, err := netlink.RouteListFiltered(nl.GetIPFamily(gw), &routeToDst, netlink.RT_FILTER_DST)
if err != nil {
return err
}

// Check if the route exists to the other node's PodCIDR and delete invalid routes
for _, route := range routes {
// Skip creation of a route if there is gateway matches the expected.
if route.Gw.Equal(gw) {
return nil
}

// Add route if not present
if len(route) == 0 {
klog.Infof("Adding route %v \n", routeToDst)
if err := netlink.RouteAdd(&routeToDst); err != nil {
return err
}
// 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
klog.Infof("Adding route %v \n", routeToDst)
if err := netlink.RouteAdd(&routeToDst); err != nil {
return err
}

return nil
}

0 comments on commit 2d52636

Please sign in to comment.