Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of Scope for routes in IPAM #1087

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/ipam/ipam_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ func ConfigureIface(ifName string, res *current.Result) error {
route.Table = *r.Table
}

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 (Table: %d)': %v", r.Dst, gw, ifName, route.Table, err)
return fmt.Errorf("failed to add route '%v via %v dev %v (Scope: %v, Table: %d)': %v", r.Dst, gw, ifName, route.Scope, route.Table, err)
}
}

Expand Down
17 changes: 14 additions & 3 deletions pkg/ipam/ipam_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
var routeTable int

Expand Down Expand Up @@ -78,6 +79,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())

Expand All @@ -95,6 +100,7 @@ var _ = Describe("ConfigureIface", func() {
Expect(ipgw6).NotTo(BeNil())

routeTable := 5000
routeScope = 200

result = &current.Result{
Interfaces: []*current.Interface{
Expand Down Expand Up @@ -125,6 +131,7 @@ var _ = Describe("ConfigureIface", func() {
{Dst: *routev4, GW: routegwv4},
{Dst: *routev6, GW: routegwv6},
{Dst: *routev4, GW: routegwv4, Table: &routeTable},
{Dst: *routev4Scope, Scope: &routeScope},
},
}
})
Expand Down Expand Up @@ -166,7 +173,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) {
Expand All @@ -175,13 +182,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
})
Expand Down