pkg/ip: do not leak types from vendored netlink package
The exported function SetupVeth now returns a package-defined type. Signed-off-by: Gabe Rosenhouse <grosenhouse@pivotal.io>
This commit is contained in:
parent
4f36e5994e
commit
58c834c4f3
@ -98,30 +98,49 @@ func RenameLink(curName, newName string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type LinkAttrs struct {
|
||||||
|
Name string
|
||||||
|
HardwareAddr net.HardwareAddr
|
||||||
|
Index int
|
||||||
|
}
|
||||||
|
|
||||||
|
type link struct {
|
||||||
|
netlink.Link
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *link) Attrs() LinkAttrs {
|
||||||
|
a := l.Link.Attrs()
|
||||||
|
return LinkAttrs{
|
||||||
|
Name: a.Name,
|
||||||
|
HardwareAddr: a.HardwareAddr,
|
||||||
|
Index: a.Index,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Link interface {
|
||||||
|
Attrs() LinkAttrs
|
||||||
|
}
|
||||||
|
|
||||||
// SetupVeth sets up a virtual ethernet link.
|
// SetupVeth sets up a virtual ethernet link.
|
||||||
// Should be in container netns, and will switch back to hostNS to set the host
|
// Should be in container netns, and will switch back to hostNS to set the host
|
||||||
// veth end up.
|
// veth end up.
|
||||||
func SetupVeth(contVethName string, mtu int, hostNS ns.NetNS) (hostVeth, contVeth netlink.Link, err error) {
|
func SetupVeth(contVethName string, mtu int, hostNS ns.NetNS) (Link, Link, error) {
|
||||||
var hostVethName string
|
hostVethName, contVeth, err := makeVeth(contVethName, mtu)
|
||||||
hostVethName, contVeth, err = makeVeth(contVethName, mtu)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = netlink.LinkSetUp(contVeth); err != nil {
|
if err = netlink.LinkSetUp(contVeth); err != nil {
|
||||||
err = fmt.Errorf("failed to set %q up: %v", contVethName, err)
|
return nil, nil, fmt.Errorf("failed to set %q up: %v", contVethName, err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hostVeth, err = netlink.LinkByName(hostVethName)
|
hostVeth, err := netlink.LinkByName(hostVethName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("failed to lookup %q: %v", hostVethName, err)
|
return nil, nil, fmt.Errorf("failed to lookup %q: %v", hostVethName, err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = netlink.LinkSetNsFd(hostVeth, int(hostNS.Fd())); err != nil {
|
if err = netlink.LinkSetNsFd(hostVeth, int(hostNS.Fd())); err != nil {
|
||||||
err = fmt.Errorf("failed to move veth to host netns: %v", err)
|
return nil, nil, fmt.Errorf("failed to move veth to host netns: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = hostNS.Do(func(_ ns.NetNS) error {
|
err = hostNS.Do(func(_ ns.NetNS) error {
|
||||||
@ -135,7 +154,10 @@ func SetupVeth(contVethName string, mtu int, hostNS ns.NetNS) (hostVeth, contVet
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
return &link{hostVeth}, &link{contVeth}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DelLinkByName removes an interface link.
|
// DelLinkByName removes an interface link.
|
||||||
|
@ -46,8 +46,8 @@ var _ = Describe("Link", func() {
|
|||||||
hostNetNS ns.NetNS
|
hostNetNS ns.NetNS
|
||||||
containerNetNS ns.NetNS
|
containerNetNS ns.NetNS
|
||||||
ifaceCounter int = 0
|
ifaceCounter int = 0
|
||||||
hostVeth netlink.Link
|
hostVeth ip.Link
|
||||||
containerVeth netlink.Link
|
containerVeth ip.Link
|
||||||
hostVethName string
|
hostVethName string
|
||||||
containerVethName string
|
containerVethName string
|
||||||
|
|
||||||
|
@ -63,14 +63,14 @@ func setupContainerVeth(netns ns.NetNS, ifName string, mtu int, pr *current.Resu
|
|||||||
containerInterface := ¤t.Interface{}
|
containerInterface := ¤t.Interface{}
|
||||||
|
|
||||||
err := netns.Do(func(hostNS ns.NetNS) error {
|
err := netns.Do(func(hostNS ns.NetNS) error {
|
||||||
hostVeth, contVeth, err := ip.SetupVeth(ifName, mtu, hostNS)
|
hostVeth, contVeth0, err := ip.SetupVeth(ifName, mtu, hostNS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
hostInterface.Name = hostVeth.Attrs().Name
|
hostInterface.Name = hostVeth.Attrs().Name
|
||||||
hostInterface.Mac = hostVeth.Attrs().HardwareAddr.String()
|
hostInterface.Mac = hostVeth.Attrs().HardwareAddr.String()
|
||||||
containerInterface.Name = contVeth.Attrs().Name
|
containerInterface.Name = contVeth0.Attrs().Name
|
||||||
containerInterface.Mac = contVeth.Attrs().HardwareAddr.String()
|
containerInterface.Mac = contVeth0.Attrs().HardwareAddr.String()
|
||||||
containerInterface.Sandbox = netns.Path()
|
containerInterface.Sandbox = netns.Path()
|
||||||
|
|
||||||
var firstV4Addr net.IP
|
var firstV4Addr net.IP
|
||||||
@ -103,12 +103,12 @@ func setupContainerVeth(netns ns.NetNS, ifName string, mtu int, pr *current.Resu
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := ip.SetHWAddrByIP(contVeth.Attrs().Name, firstV4Addr, nil /* TODO IPv6 */); err != nil {
|
if err := ip.SetHWAddrByIP(contVeth0.Attrs().Name, firstV4Addr, nil /* TODO IPv6 */); err != nil {
|
||||||
return fmt.Errorf("failed to set hardware addr by IP: %v", err)
|
return fmt.Errorf("failed to set hardware addr by IP: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Re-fetch container veth to update attributes
|
// Re-fetch container veth to update attributes
|
||||||
contVeth, err = netlink.LinkByName(ifName)
|
contVeth, err := netlink.LinkByName(ifName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to look up %q: %v", ifName, err)
|
return fmt.Errorf("failed to look up %q: %v", ifName, err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user