Skip to content

Commit 26340cd

Browse files
committed
Fix default route replacement
Multus was failing to detect existing Default routes in container and remove them before replacement, which causes later adding of default route to fail with EEXIST, which is ignored and not detected as a error. This leads to container keeping existing default route in case of chained CNIs instead of getting new default route from Multus. Fixes regression introduced by f18d96b There was a breaking change in that bump: vishvananda/netlink@acdc658 route.Dst is a zero IPNet instead of nil Check for default route using isIPNetZero adopted from github.com/containernetworking/plugins/pkg/ip.IsIPNetZero. Avoid importing ip.IsIPNetZero to avoid brining more dependencies. Signed-off-by: Viktor Oreshkin <imselfish@stek29.rocks>
1 parent f688790 commit 26340cd

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎pkg/netutils/netutils.go‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ import (
3030
"gopkg.in/k8snetworkplumbingwg/multus-cni.v4/pkg/logging"
3131
)
3232

33+
func isIPNetZero(ipnet *net.IPNet) bool {
34+
if ipnet == nil {
35+
return true
36+
}
37+
if ones, _ := ipnet.Mask.Size(); ones != 0 {
38+
return false
39+
}
40+
return ipnet.IP.Equal(net.IPv4zero) || ipnet.IP.Equal(net.IPv6zero)
41+
}
42+
3343
// DeleteDefaultGW removes the default gateway from marked interfaces.
3444
func DeleteDefaultGW(netnsPath string, ifName string) error {
3545
netns, err := ns.GetNS(netnsPath)
@@ -43,7 +53,7 @@ func DeleteDefaultGW(netnsPath string, ifName string) error {
4353
link, _ := netlink.LinkByName(ifName)
4454
routes, _ := netlink.RouteList(link, netlink.FAMILY_ALL)
4555
for _, nlroute := range routes {
46-
if nlroute.Dst == nil {
56+
if isIPNetZero(nlroute.Dst) {
4757
err = netlink.RouteDel(&nlroute)
4858
}
4959
}

‎pkg/netutils/netutils_test.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ func TestNetutils(t *testing.T) {
4040
RunSpecs(t, "netutils")
4141
}
4242

43+
var _ = Describe("default route detection", func() {
44+
DescribeTable("detects default routes",
45+
func(dst *net.IPNet) {
46+
Expect(isIPNetZero(dst)).To(BeTrue())
47+
},
48+
Entry("nil destination", nil),
49+
Entry("IPv4 zero prefix", &net.IPNet{IP: net.IPv4zero, Mask: net.CIDRMask(0, 32)}),
50+
Entry("IPv6 zero prefix", &net.IPNet{IP: net.IPv6zero, Mask: net.CIDRMask(0, 128)}),
51+
)
52+
})
53+
4354
// helper function
4455
func testAddRoute(link netlink.Link, ip net.IP, mask net.IPMask, gw net.IP) error {
4556
dst := &net.IPNet{
@@ -234,6 +245,18 @@ var _ = Describe("netutil netlink function testing", func() {
234245
Expect(DeleteDefaultGW(args.Netns, IFNAME)).Should(Succeed())
235246
return nil
236247
})).Should(Succeed())
248+
249+
Expect(targetNS.Do(func(ns.NetNS) error {
250+
defer GinkgoRecover()
251+
link, err := netlink.LinkByName(IFNAME)
252+
Expect(err).NotTo(HaveOccurred())
253+
routes, err := netlink.RouteList(link, netlink.FAMILY_ALL)
254+
Expect(err).NotTo(HaveOccurred())
255+
for _, route := range routes {
256+
Expect(isIPNetZero(route.Dst)).To(BeFalse())
257+
}
258+
return nil
259+
})).Should(Succeed())
237260
})
238261
})
239262

0 commit comments

Comments
 (0)