Skip to content

Commit

Permalink
Fixing #228
Browse files Browse the repository at this point in the history
DANM now sets the link Id into the netlink Route object, making sure the kernel attaches the IP route to the correct device.
  • Loading branch information
Levovar committed Aug 28, 2020
1 parent 6454a2c commit 330797b
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions pkg/danmep/ep.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,26 +142,30 @@ func addIpToLink(ip string, iface netlink.Link) error {

func addIpRoutes(ep *danmtypes.DanmEp, dnet *danmtypes.DanmNet) error {
defaultRoutingTable := 0
err := addRoutes(dnet.Spec.Options.Routes, ep.Spec.Iface.Address, defaultRoutingTable)
link, err := netlink.LinkByName(ep.Spec.Iface.Name)
if err != nil {
return errors.New("cannot find Pod interface: " + ep.Spec.Iface.Name + " for adding IP route because:" + err.Error())
}
err = addRouteForLink(dnet.Spec.Options.Routes, ep.Spec.Iface.Address, defaultRoutingTable, link)
if err != nil {
return err
}
err = addRoutes(dnet.Spec.Options.Routes6, ep.Spec.Iface.AddressIPv6, defaultRoutingTable)
err = addRouteForLink(dnet.Spec.Options.Routes6, ep.Spec.Iface.AddressIPv6, defaultRoutingTable, link)
if err != nil {
return err
}
err = addPolicyRoute(dnet.Spec.Options.RTables, ep.Spec.Iface.Address, ep.Spec.Iface.Proutes)
err = addPolicyRouteForLink(dnet.Spec.Options.RTables, ep.Spec.Iface.Address, ep.Spec.Iface.Proutes, link)
if err != nil {
return err
}
err = addPolicyRoute(dnet.Spec.Options.RTables, ep.Spec.Iface.AddressIPv6, ep.Spec.Iface.Proutes6)
err = addPolicyRouteForLink(dnet.Spec.Options.RTables, ep.Spec.Iface.AddressIPv6, ep.Spec.Iface.Proutes6, link)
if err != nil {
return err
}
return nil
}

func addRoutes(routes map[string]string, allocatedIp string, rtable int) error {
func addRouteForLink(routes map[string]string, allocatedIp string, rtable int, link netlink.Link) error {
if routes == nil || allocatedIp == "" || allocatedIp == ipam.NoneAllocType {
return nil
}
Expand All @@ -176,7 +180,8 @@ func addRoutes(routes map[string]string, allocatedIp string, rtable int) error {
//Bad gateway in IP route, ignoring the route
continue
}
route := netlink.Route{
route := netlink.Route {
LinkIndex: link.Attrs().Index,
Dst: ipnet,
Gw: ip,
}
Expand All @@ -193,7 +198,7 @@ func addRoutes(routes map[string]string, allocatedIp string, rtable int) error {
return nil
}

func addPolicyRoute(rtable int, cidr string, proutes map[string]string) error {
func addPolicyRouteForLink(rtable int, cidr string, proutes map[string]string, link netlink.Link) error {
if rtable == 0 || cidr == "" || cidr == ipam.NoneAllocType || proutes == nil {
return nil
}
Expand All @@ -206,7 +211,7 @@ func addPolicyRoute(rtable int, cidr string, proutes map[string]string) error {
if err != nil {
return errors.New("cannot add rule for policy-based IP routes because:" + err.Error())
}
err = addRoutes(proutes, cidr, rtable)
err = addRouteForLink(proutes, cidr, rtable, link)
if err != nil {
return err
}
Expand Down

0 comments on commit 330797b

Please sign in to comment.