From c5782406154850aceda876453688349299bff2b2 Mon Sep 17 00:00:00 2001 From: Lionel Jouin Date: Fri, 30 Aug 2024 15:51:32 +0200 Subject: [PATCH] Use of Scope for routes in IPAM Add Scope for routes for cni spec v1.1 Signed-off-by: Lionel Jouin --- pkg/ipam/ipam_linux.go | 6 +++++- pkg/ipam/ipam_linux_test.go | 18 +++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/pkg/ipam/ipam_linux.go b/pkg/ipam/ipam_linux.go index 6c2bfe72f..d9b81ebf5 100644 --- a/pkg/ipam/ipam_linux.go +++ b/pkg/ipam/ipam_linux.go @@ -119,8 +119,12 @@ func ConfigureIface(ifName string, res *current.Result) error { Gw: gw, } + if r.Scope != nil { + route.Scope = netlink.Scope(*r.Scope) + } + if err = netlink.RouteAddEcmp(&route); err != nil { - return fmt.Errorf("failed to add route '%v via %v dev %v': %v", r.Dst, gw, ifName, err) + return fmt.Errorf("failed to add route '%v via %v dev %v (Scope: %v)': %v", r.Dst, gw, ifName, route.Scope, err) } } diff --git a/pkg/ipam/ipam_linux_test.go b/pkg/ipam/ipam_linux_test.go index 6d4ec4a47..a2feb4f74 100644 --- a/pkg/ipam/ipam_linux_test.go +++ b/pkg/ipam/ipam_linux_test.go @@ -41,8 +41,9 @@ func ipNetEqual(a, b *net.IPNet) bool { var _ = Describe("ConfigureIface", func() { var originalNS ns.NetNS - var ipv4, ipv6, routev4, routev6 *net.IPNet + var ipv4, ipv6, routev4, routev6, routev4Scope *net.IPNet var ipgw4, ipgw6, routegwv4, routegwv6 net.IP + var routeScope int var result *current.Result BeforeEach(func() { @@ -77,6 +78,10 @@ var _ = Describe("ConfigureIface", func() { routegwv4 = net.ParseIP("1.2.3.5") Expect(routegwv4).NotTo(BeNil()) + _, routev4Scope, err = net.ParseCIDR("1.2.3.4/32") + Expect(err).NotTo(HaveOccurred()) + Expect(routev4Scope).NotTo(BeNil()) + ipgw4 = net.ParseIP("1.2.3.1") Expect(ipgw4).NotTo(BeNil()) @@ -93,6 +98,8 @@ var _ = Describe("ConfigureIface", func() { ipgw6 = net.ParseIP("abcd:1234:ffff::1") Expect(ipgw6).NotTo(BeNil()) + routeScope = 200 + result = ¤t.Result{ Interfaces: []*current.Interface{ { @@ -121,6 +128,7 @@ var _ = Describe("ConfigureIface", func() { Routes: []*types.Route{ {Dst: *routev4, GW: routegwv4}, {Dst: *routev6, GW: routegwv6}, + {Dst: *routev4Scope, Scope: &routeScope}, }, } }) @@ -162,7 +170,7 @@ var _ = Describe("ConfigureIface", func() { routes, err := netlink.RouteList(link, 0) Expect(err).NotTo(HaveOccurred()) - var v4found, v6found bool + var v4found, v6found, v4Scopefound bool for _, route := range routes { isv4 := route.Dst.IP.To4() != nil if isv4 && ipNetEqual(route.Dst, routev4) && route.Gw.Equal(routegwv4) { @@ -171,13 +179,17 @@ var _ = Describe("ConfigureIface", func() { if !isv4 && ipNetEqual(route.Dst, routev6) && route.Gw.Equal(routegwv6) { v6found = true } + if isv4 && ipNetEqual(route.Dst, routev4Scope) && int(route.Scope) == routeScope { + v4Scopefound = true + } - if v4found && v6found { + if v4found && v6found && v4Scopefound { break } } Expect(v4found).To(BeTrue()) Expect(v6found).To(BeTrue()) + Expect(v4Scopefound).To(BeTrue()) return nil })