Skip to content

Commit

Permalink
Remove unexpected route from route list
Browse files Browse the repository at this point in the history
  • Loading branch information
chenchun committed Oct 24, 2017
1 parent 5d22389 commit 1dc26f8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/hostgw/hostgw_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (n *network) handleSubnetEvents(batch []subnet.Event) {
log.Errorf("Error deleting route to %v: %v", evt.Lease.Subnet, err)
continue
}
n.removeFromRouteList(routeList[0])
}
if len(routeList) > 0 && routeList[0].Gw.Equal(route.Gw) {
// Same Dst and same Gw, keep it and do not attempt to add it.
Expand Down
56 changes: 56 additions & 0 deletions backend/hostgw/hostgw_network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package hostgw

import (
"net"
"runtime"
"testing"

"github.com/coreos/flannel/backend"
"github.com/coreos/flannel/pkg/ip"
"github.com/coreos/flannel/subnet"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
)

func TestRouteCache(t *testing.T) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
origns, _ := netns.Get()
defer origns.Close()
newns, _ := netns.New()
netns.Set(newns)
defer newns.Close()

lo, err := netlink.LinkByName("lo")
if err != nil {
t.Fatal(err)
}
if err := netlink.AddrAdd(lo, &netlink.Addr{IPNet: &net.IPNet{IP: net.ParseIP("127.0.0.1"), Mask: net.CIDRMask(32, 32)}}); err != nil {
t.Fatal(err)
}
if err := netlink.LinkSetUp(lo); err != nil {
t.Fatal(err)
}
nw := network{extIface: &backend.ExternalInterface{Iface: &net.Interface{Index: lo.Attrs().Index}}}
gw1, gw2 := ip.FromIP(net.ParseIP("127.0.0.1")), ip.FromIP(net.ParseIP("127.0.0.2"))
subnet1 := ip.IP4Net{IP: ip.FromIP(net.ParseIP("192.168.0.0")), PrefixLen: 24}
nw.handleSubnetEvents([]subnet.Event{
{Type: subnet.EventAdded, Lease: subnet.Lease{Subnet: subnet1, Attrs: subnet.LeaseAttrs{PublicIP: gw1, BackendType: "host-gw"}}},
})
if len(nw.rl) != 1 {
t.Fatal(nw.rl)
}
if !routeEqual(nw.rl[0], netlink.Route{Dst: subnet1.ToIPNet(), Gw: gw1.ToIP()}) {
t.Fatal(nw.rl[0])
}
// change gateway of previous route
nw.handleSubnetEvents([]subnet.Event{
{Type: subnet.EventAdded, Lease: subnet.Lease{
Subnet: subnet1, Attrs: subnet.LeaseAttrs{PublicIP: gw2, BackendType: "host-gw"}}}})
if len(nw.rl) != 1 {
t.Fatal(nw.rl)
}
if !routeEqual(nw.rl[0], netlink.Route{Dst: subnet1.ToIPNet(), Gw: gw2.ToIP()}) {
t.Fatal(nw.rl[0])
}
}

0 comments on commit 1dc26f8

Please sign in to comment.