Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Hoehl <[email protected]>
  • Loading branch information
Lukas Hoehl committed Aug 13, 2024
1 parent bea48fa commit b7586b1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
8 changes: 6 additions & 2 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ func NewSidecarApp(params *ConfigParams) (*SidecarApp, error) {
// TeardownNetworking removes the network interface added by apiserver-proxy
func (c *SidecarApp) TeardownNetworking() error {
klog.Infof("Cleaning up")
return c.netManager.RemoveIPAddress()
err := c.netManager.RemoveIPAddress()
if err != nil {
return err
}
return c.netManager.CleanupDevice()
}

func (c *SidecarApp) runPeriodic(ctx context.Context) {
Expand Down Expand Up @@ -72,7 +76,7 @@ func (c *SidecarApp) runChecks() {

// RunApp invokes the background checks and runs coreDNS as a cache
func (c *SidecarApp) RunApp(ctx context.Context) {
c.netManager = netif.NewNetifManager(c.localIP, c.params.Interface, c.params.ManageInterface)
c.netManager = netif.NewNetifManager(c.localIP, c.params.Interface)

if c.params.Cleanup {
defer func() {
Expand Down
2 changes: 0 additions & 2 deletions internal/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ type ConfigParams struct {
Daemon bool
// IPAddress specifies the IP address on which the proxy is listening
IPAddress string
// ManageInterface specifies if the interface should be managed by the sidecar.
ManageInterface bool
}

// SidecarApp contains all the config required to run sidecar proxy.
Expand Down
59 changes: 31 additions & 28 deletions internal/netif/netif.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,27 @@ type Handle interface {
type Manager interface {
EnsureIPAddress() error
RemoveIPAddress() error
CleanupDevice() error
}

// netifManagerDefault is the default implementation handling creating
// and removing of the dummy interface.
type netifManagerDefault struct {
Handle
addr *netlink.Addr
devName string
manageInterface bool
addr *netlink.Addr
devName string
}

// NewNetifManager returns a new instance of NetifManager with the ip address set to the provided values
// These ip addresses will be bound to any devices created by this instance.
func NewNetifManager(addr *netlink.Addr, devName string, manageDevice bool) Manager {
func NewNetifManager(addr *netlink.Addr, devName string) Manager {
// Set scope to host only
addr.Scope = 0xfe

return &netifManagerDefault{
&netlink.Handle{},
addr,
devName,
manageDevice,
}
}

Expand All @@ -58,19 +57,24 @@ func (m *netifManagerDefault) EnsureIPAddress() error {
l, err := m.LinkByName(m.devName)
if err != nil {
var linkNotFoundErr netlink.LinkNotFoundError
if !errors.As(err, &linkNotFoundErr) && !m.manageInterface {
return xerrors.Errorf("could not get interface %s: %v", m.devName, err)
if !errors.As(err, &linkNotFoundErr) {
return xerrors.Errorf("could not get interface %s:\n%v", m.devName, err)
}

attrs := netlink.LinkAttrs{
Name: m.devName,
MTU: 65535,
}
dummyLink := &netlink.Dummy{LinkAttrs: attrs}
err = m.LinkAdd(dummyLink)
if err != nil {
return xerrors.Errorf("could add dummy interface %v", err)
return xerrors.Errorf("could add dummy interface %s:\n%v", err)
}

err = m.LinkSetUp(l)
if err != nil {
return fmt.Errorf("could set link up: %v", err)
}

l = dummyLink
}

Expand All @@ -87,13 +91,6 @@ func (m *netifManagerDefault) EnsureIPAddress() error {

klog.Infof("Successfully added %q to %q", m.addr.String(), m.devName)

if m.manageInterface {
err = m.LinkSetUp(l)
if err != nil {
return fmt.Errorf("could set link up: %v", err)
}
}

return nil
}

Expand All @@ -103,18 +100,7 @@ func (m *netifManagerDefault) RemoveIPAddress() error {

l, err := m.LinkByName(m.devName)
if err != nil {
var linkNotFoundErr netlink.LinkNotFoundError
if !errors.As(err, &linkNotFoundErr) && !m.manageInterface {
return xerrors.Errorf("could not get interface %s: %v", m.devName, err)
}
attrs := netlink.LinkAttrs{
Name: m.devName,
}
dummyLink := &netlink.Dummy{LinkAttrs: attrs}
err = m.LinkDel(dummyLink)
if err != nil {
return xerrors.Errorf("could add dummy interface %v", err)
}
return xerrors.Errorf("could not get interface %s:\n%v", m.devName, err)
}

klog.V(6).Infof("Got interface %+v", l)
Expand All @@ -132,3 +118,20 @@ func (m *netifManagerDefault) RemoveIPAddress() error {

return nil
}

func (m *netifManagerDefault) CleanupDevice() error {
link, err := netlink.LinkByName(m.devName)
if err != nil {
var linkNotFoundErr netlink.LinkNotFoundError
if !errors.As(err, &linkNotFoundErr) {
return xerrors.Errorf("could not get interface %s:\n%v", m.devName, err)
}
// link already gone
return nil
}
err = netlink.LinkDel(link)
if err != nil {
return xerrors.Errorf("could not delete interface %s:\n%v", m.devName, err)
}
return nil
}

0 comments on commit b7586b1

Please sign in to comment.