Merge pull request #396 from cf-container-networking/vendored-types-leakage

pkg/ip: do not leak types from vendored netlink package
This commit is contained in:
Gabe Rosenhouse 2017-03-16 19:20:40 -07:00 committed by GitHub
commit b87126377a
4 changed files with 44 additions and 33 deletions

View File

@ -98,30 +98,38 @@ func RenameLink(curName, newName string) error {
return err return err
} }
// SetupVeth sets up a virtual ethernet link. func ifaceFromNetlinkLink(l netlink.Link) net.Interface {
// Should be in container netns, and will switch back to hostNS to set the host a := l.Attrs()
// veth end up. return net.Interface{
func SetupVeth(contVethName string, mtu int, hostNS ns.NetNS) (hostVeth, contVeth netlink.Link, err error) { Index: a.Index,
var hostVethName string MTU: a.MTU,
hostVethName, contVeth, err = makeVeth(contVethName, mtu) Name: a.Name,
HardwareAddr: a.HardwareAddr,
Flags: a.Flags,
}
}
// SetupVeth sets up a pair of virtual ethernet devices.
// Call SetupVeth from inside the container netns. It will create both veth
// devices and move the host-side veth into the provided hostNS namespace.
// On success, SetupVeth returns (hostVeth, containerVeth, nil)
func SetupVeth(contVethName string, mtu int, hostNS ns.NetNS) (net.Interface, net.Interface, error) {
hostVethName, contVeth, err := makeVeth(contVethName, mtu)
if err != nil { if err != nil {
return return net.Interface{}, net.Interface{}, 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 net.Interface{}, net.Interface{}, 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 net.Interface{}, net.Interface{}, 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 net.Interface{}, net.Interface{}, 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 +143,10 @@ func SetupVeth(contVethName string, mtu int, hostNS ns.NetNS) (hostVeth, contVet
} }
return nil return nil
}) })
return if err != nil {
return net.Interface{}, net.Interface{}, err
}
return ifaceFromNetlinkLink(hostVeth), ifaceFromNetlinkLink(contVeth), nil
} }
// DelLinkByName removes an interface link. // DelLinkByName removes an interface link.

View File

@ -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 net.Interface
containerVeth netlink.Link containerVeth net.Interface
hostVethName string hostVethName string
containerVethName string containerVethName string
@ -78,8 +78,8 @@ var _ = Describe("Link", func() {
} }
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
hostVethName = hostVeth.Attrs().Name hostVethName = hostVeth.Name
containerVethName = containerVeth.Attrs().Name containerVethName = containerVeth.Name
return nil return nil
}) })
@ -98,7 +98,7 @@ var _ = Describe("Link", func() {
containerVethFromName, err := netlink.LinkByName(containerVethName) containerVethFromName, err := netlink.LinkByName(containerVethName)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(containerVethFromName.Attrs().Index).To(Equal(containerVeth.Attrs().Index)) Expect(containerVethFromName.Attrs().Index).To(Equal(containerVeth.Index))
return nil return nil
}) })
@ -108,7 +108,7 @@ var _ = Describe("Link", func() {
hostVethFromName, err := netlink.LinkByName(hostVethName) hostVethFromName, err := netlink.LinkByName(hostVethName)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(hostVethFromName.Attrs().Index).To(Equal(hostVeth.Attrs().Index)) Expect(hostVethFromName.Attrs().Index).To(Equal(hostVeth.Index))
return nil return nil
}) })
@ -156,7 +156,7 @@ var _ = Describe("Link", func() {
hostVeth, _, err := ip.SetupVeth(containerVethName, mtu, hostNetNS) hostVeth, _, err := ip.SetupVeth(containerVethName, mtu, hostNetNS)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
hostVethName = hostVeth.Attrs().Name hostVethName = hostVeth.Name
return nil return nil
}) })

View File

@ -168,10 +168,10 @@ func setupVeth(netns ns.NetNS, br *netlink.Bridge, ifName string, mtu int, hairp
if err != nil { if err != nil {
return err return err
} }
contIface.Name = containerVeth.Attrs().Name contIface.Name = containerVeth.Name
contIface.Mac = containerVeth.Attrs().HardwareAddr.String() contIface.Mac = containerVeth.HardwareAddr.String()
contIface.Sandbox = netns.Path() contIface.Sandbox = netns.Path()
hostIface.Name = hostVeth.Attrs().Name hostIface.Name = hostVeth.Name
return nil return nil
}) })
if err != nil { if err != nil {

View File

@ -63,14 +63,14 @@ func setupContainerVeth(netns ns.NetNS, ifName string, mtu int, pr *current.Resu
containerInterface := &current.Interface{} containerInterface := &current.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.Name
hostInterface.Mac = hostVeth.Attrs().HardwareAddr.String() hostInterface.Mac = hostVeth.HardwareAddr.String()
containerInterface.Name = contVeth.Attrs().Name containerInterface.Name = contVeth0.Name
containerInterface.Mac = contVeth.Attrs().HardwareAddr.String() containerInterface.Mac = contVeth0.HardwareAddr.String()
containerInterface.Sandbox = netns.Path() containerInterface.Sandbox = netns.Path()
var firstV4Addr net.IP var firstV4Addr net.IP
@ -87,7 +87,7 @@ func setupContainerVeth(netns ns.NetNS, ifName string, mtu int, pr *current.Resu
if firstV4Addr != nil { if firstV4Addr != nil {
err = hostNS.Do(func(_ ns.NetNS) error { err = hostNS.Do(func(_ ns.NetNS) error {
hostVethName := hostVeth.Attrs().Name hostVethName := hostVeth.Name
if err := ip.SetHWAddrByIP(hostVethName, firstV4Addr, nil /* TODO IPv6 */); err != nil { if err := ip.SetHWAddrByIP(hostVethName, 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)
} }
@ -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.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)
} }