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

add route to local table for non loopback devices #138

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions internal/netif/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 45 additions & 5 deletions internal/netif/netif.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ import (
"os"

"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
"golang.org/x/xerrors"
"k8s.io/klog/v2"
)

const (
// Also visible in /etc/iproute2/rt_tables file on linux hosts
localRoutingTableId int = unix.RT_TABLE_LOCAL
// Also visible in /etc/iproute2/rt_scopes file on linux hosts
hostScopeId int = unix.RT_SCOPE_HOST
)

type Handle interface {
RouteAdd(route *netlink.Route) error
RouteDel(route *netlink.Route) error
AddrAdd(link netlink.Link, addr *netlink.Addr) error
AddrDel(link netlink.Link, addr *netlink.Addr) error
LinkByName(name string) (netlink.Link, error)
Expand Down Expand Up @@ -81,14 +91,23 @@ func (m *netifManagerDefault) EnsureIPAddress() error {
klog.V(6).Infof("Got interface %+v", l)

if err := m.AddrAdd(l, m.addr); err != nil {
if os.IsExist(err) {
klog.V(4).Infof("Address %q already exists. Skipping", m.addr.String())
return nil
if !os.IsExist(err) {
return xerrors.Errorf("could not add IPV4 addresses %v", err)
}

return xerrors.Errorf("could not add IPV4 addresses %v", err)
klog.V(4).Infof("Address %q already exists.", m.addr.String())
}

// loopback device adds new ip addresses to the local routing table by default.
// If we are using a different interface, we need to do the updates ourself
if l.Attrs().Name != "lo" {
route := localRoute(l.Attrs().Index, m.addr)
if err = m.RouteAdd(route); err != nil {
if !os.IsExist(err) {
return xerrors.Errorf("could not add route for %s to interface %s:\n%v", m.addr, m.devName, err)
}
klog.V(4).Infof("Route for %q already exists", m.addr.String())
}
}
klog.Infof("Successfully added %q to %q", m.addr.String(), m.devName)

return nil
Expand All @@ -105,6 +124,16 @@ func (m *netifManagerDefault) RemoveIPAddress() error {

klog.V(6).Infof("Got interface %+v", l)

if m.devName != "lo" {
route := localRoute(l.Attrs().Index, m.addr)
if err := m.RouteDel(route); err != nil {
if !os.IsNotExist(err) {
return xerrors.Errorf("could not remove route for %s from interface %s:\n%v", m.addr, m.devName, err)
}
klog.V(4).Infof("Route for %q already removed. Skipping", m.addr.String())
}
}

if err := m.AddrDel(l, m.addr); err != nil {
if os.IsNotExist(err) {
klog.V(4).Infof("Address %q already removed. Skipping", m.addr.String())
Expand All @@ -119,6 +148,17 @@ func (m *netifManagerDefault) RemoveIPAddress() error {
return nil
}

func localRoute(linkIndex int, addr *netlink.Addr) *netlink.Route {
return &netlink.Route{
LinkIndex: linkIndex,
Table: localRoutingTableId,
Dst: addr.IPNet,
Src: addr.IP,
Type: unix.RTN_LOCAL,
Scope: netlink.Scope(hostScopeId),
}
}

func (m *netifManagerDefault) CleanupDevice() error {
link, err := netlink.LinkByName(m.devName)
if err != nil {
Expand Down
29 changes: 27 additions & 2 deletions internal/netif/netif_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ var _ = Describe("Manager", func() {
})

It("should return error when deleting ip address", func() {
mh.EXPECT().
RouteDel(gomock.Any()).
Return(nil).
Times(1)

mh.EXPECT().
AddrDel(gomock.Eq(dummy), gomock.Eq(addr)).
Return(fmt.Errorf("err")).
Expand All @@ -116,6 +121,10 @@ var _ = Describe("Manager", func() {
})

It("should return already removed error", func() {
mh.EXPECT().
RouteDel(gomock.Any()).
Return(nil).
Times(1)
mh.EXPECT().
AddrDel(gomock.Eq(dummy), gomock.Eq(addr)).
// Return(syscall.EEXIST).
Expand All @@ -132,6 +141,11 @@ var _ = Describe("Manager", func() {
Return(nil).
Times(1)

mh.EXPECT().
RouteDel(gomock.Any()).
Return(nil).
Times(1)

err := manager.RemoveIPAddress()
Expect(err).ToNot(HaveOccurred())
})
Expand Down Expand Up @@ -195,6 +209,11 @@ var _ = Describe("Manager", func() {
Return(syscall.EEXIST).
Times(1)

mh.EXPECT().
RouteAdd(gomock.Any()).
Return(nil).
Times(1)

err := manager.EnsureIPAddress()
Expect(err).ToNot(HaveOccurred())
})
Expand Down Expand Up @@ -239,7 +258,10 @@ var _ = Describe("Manager", func() {
AddrAdd(gomock.Eq(dummy), gomock.Eq(addr)).
Return(syscall.EEXIST).
Times(1)

mh.EXPECT().
RouteAdd(gomock.Any()).
Return(nil).
Times(1)
err := manager.EnsureIPAddress()
Expect(err).ToNot(HaveOccurred())
})
Expand All @@ -249,7 +271,10 @@ var _ = Describe("Manager", func() {
AddrAdd(gomock.Eq(dummy), gomock.Eq(addr)).
Return(nil).
Times(1)

mh.EXPECT().
RouteAdd(gomock.Any()).
Return(nil).
Times(1)
err := manager.EnsureIPAddress()
Expect(err).ToNot(HaveOccurred())
})
Expand Down