Skip to content

Commit 94fce42

Browse files
committed
Fixing #228
DANM now sets the link Id into the netlink Route object, making sure the kernel attaches the IP route to the correct device.
1 parent b892e74 commit 94fce42

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

pkg/danmep/ep.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,26 +142,30 @@ func addIpToLink(ip string, iface netlink.Link) error {
142142

143143
func addIpRoutes(ep *danmtypes.DanmEp, dnet *danmtypes.DanmNet) error {
144144
defaultRoutingTable := 0
145-
err := addRoutes(dnet.Spec.Options.Routes, ep.Spec.Iface.Address, defaultRoutingTable)
145+
link, err := netlink.LinkByName(ep.Spec.Iface.Name)
146+
if err != nil {
147+
return errors.New("cannot find Pod interface: " + ep.Spec.Iface.Name + " for adding IP route because:" + err.Error())
148+
}
149+
err = addRouteForLink(dnet.Spec.Options.Routes, ep.Spec.Iface.Address, defaultRoutingTable, link)
146150
if err != nil {
147151
return err
148152
}
149-
err = addRoutes(dnet.Spec.Options.Routes6, ep.Spec.Iface.AddressIPv6, defaultRoutingTable)
153+
err = addRouteForLink(dnet.Spec.Options.Routes6, ep.Spec.Iface.AddressIPv6, defaultRoutingTable, link)
150154
if err != nil {
151155
return err
152156
}
153-
err = addPolicyRoute(dnet.Spec.Options.RTables, ep.Spec.Iface.Address, ep.Spec.Iface.Proutes)
157+
err = addPolicyRouteForLink(dnet.Spec.Options.RTables, ep.Spec.Iface.Address, ep.Spec.Iface.Proutes, link)
154158
if err != nil {
155159
return err
156160
}
157-
err = addPolicyRoute(dnet.Spec.Options.RTables, ep.Spec.Iface.AddressIPv6, ep.Spec.Iface.Proutes6)
161+
err = addPolicyRouteForLink(dnet.Spec.Options.RTables, ep.Spec.Iface.AddressIPv6, ep.Spec.Iface.Proutes6, link)
158162
if err != nil {
159163
return err
160164
}
161165
return nil
162166
}
163167

164-
func addRoutes(routes map[string]string, allocatedIp string, rtable int) error {
168+
func addRouteForLink(routes map[string]string, allocatedIp string, rtable int, link netlink.Link) error {
165169
if routes == nil || allocatedIp == "" || allocatedIp == ipam.NoneAllocType {
166170
return nil
167171
}
@@ -176,7 +180,8 @@ func addRoutes(routes map[string]string, allocatedIp string, rtable int) error {
176180
//Bad gateway in IP route, ignoring the route
177181
continue
178182
}
179-
route := netlink.Route{
183+
route := netlink.Route {
184+
LinkIndex: link.Attrs().Index,
180185
Dst: ipnet,
181186
Gw: ip,
182187
}
@@ -193,7 +198,7 @@ func addRoutes(routes map[string]string, allocatedIp string, rtable int) error {
193198
return nil
194199
}
195200

196-
func addPolicyRoute(rtable int, cidr string, proutes map[string]string) error {
201+
func addPolicyRouteForLink(rtable int, cidr string, proutes map[string]string, link netlink.Link) error {
197202
if rtable == 0 || cidr == "" || cidr == ipam.NoneAllocType || proutes == nil {
198203
return nil
199204
}
@@ -206,7 +211,7 @@ func addPolicyRoute(rtable int, cidr string, proutes map[string]string) error {
206211
if err != nil {
207212
return errors.New("cannot add rule for policy-based IP routes because:" + err.Error())
208213
}
209-
err = addRoutes(proutes, cidr, rtable)
214+
err = addRouteForLink(proutes, cidr, rtable, link)
210215
if err != nil {
211216
return err
212217
}

0 commit comments

Comments
 (0)