Merge pull request #344 from cadmuxe/veth_name

Make host-side veth name configurable.
This commit is contained in:
Casey Callendrello
2019-07-03 17:46:02 +02:00
committed by GitHub

View File

@ -60,11 +60,15 @@ func peerExists(name string) bool {
return true return true
} }
func makeVeth(name string, mtu int) (peerName string, veth netlink.Link, err error) { func makeVeth(name, vethPeerName string, mtu int) (peerName string, veth netlink.Link, err error) {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
peerName, err = RandomVethName() if vethPeerName != "" {
if err != nil { peerName = vethPeerName
return } else {
peerName, err = RandomVethName()
if err != nil {
return
}
} }
veth, err = makeVethPair(name, peerName, mtu) veth, err = makeVethPair(name, peerName, mtu)
@ -73,7 +77,7 @@ func makeVeth(name string, mtu int) (peerName string, veth netlink.Link, err err
return return
case os.IsExist(err): case os.IsExist(err):
if peerExists(peerName) { if peerExists(peerName) && vethPeerName == "" {
continue continue
} }
err = fmt.Errorf("container veth name provided (%v) already exists", name) err = fmt.Errorf("container veth name provided (%v) already exists", name)
@ -121,12 +125,13 @@ func ifaceFromNetlinkLink(l netlink.Link) net.Interface {
} }
} }
// SetupVeth sets up a pair of virtual ethernet devices. // SetupVethWithName sets up a pair of virtual ethernet devices.
// Call SetupVeth from inside the container netns. It will create both veth // Call SetupVethWithName from inside the container netns. It will create both veth
// devices and move the host-side veth into the provided hostNS namespace. // devices and move the host-side veth into the provided hostNS namespace.
// On success, SetupVeth returns (hostVeth, containerVeth, nil) // hostVethName: If hostVethName is not specified, the host-side veth name will use a random string.
func SetupVeth(contVethName string, mtu int, hostNS ns.NetNS) (net.Interface, net.Interface, error) { // On success, SetupVethWithName returns (hostVeth, containerVeth, nil)
hostVethName, contVeth, err := makeVeth(contVethName, mtu) func SetupVethWithName(contVethName, hostVethName string, mtu int, hostNS ns.NetNS) (net.Interface, net.Interface, error) {
hostVethName, contVeth, err := makeVeth(contVethName, hostVethName, mtu)
if err != nil { if err != nil {
return net.Interface{}, net.Interface{}, err return net.Interface{}, net.Interface{}, err
} }
@ -161,6 +166,14 @@ func SetupVeth(contVethName string, mtu int, hostNS ns.NetNS) (net.Interface, ne
return ifaceFromNetlinkLink(hostVeth), ifaceFromNetlinkLink(contVeth), nil return ifaceFromNetlinkLink(hostVeth), ifaceFromNetlinkLink(contVeth), nil
} }
// 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) {
return SetupVethWithName(contVethName, "", mtu, hostNS)
}
// DelLinkByName removes an interface link. // DelLinkByName removes an interface link.
func DelLinkByName(ifName string) error { func DelLinkByName(ifName string) error {
iface, err := netlink.LinkByName(ifName) iface, err := netlink.LinkByName(ifName)