mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Make GenerateIfaceName generic
Currently GenerateIfaceName is defined in bridge.go and it specifically tries to only generate an interface name only with `veth` prefix. Make it generic so that it can accept a prefix and length of random bytes. Also move it to netutils since it is useful to generate various kinds of interface names using it. Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
This commit is contained in:
parent
5f53eaf5a7
commit
6d6aeff780
3 changed files with 26 additions and 24 deletions
|
@ -5,7 +5,6 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
|
@ -701,13 +700,13 @@ func (d *driver) CreateEndpoint(nid, eid types.UUID, epInfo driverapi.EndpointIn
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Generate a name for what will be the host side pipe interface
|
// Generate a name for what will be the host side pipe interface
|
||||||
name1, err := generateIfaceName()
|
name1, err := netutils.GenerateIfaceName(vethPrefix, vethLen)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a name for what will be the sandbox side pipe interface
|
// Generate a name for what will be the sandbox side pipe interface
|
||||||
name2, err := generateIfaceName()
|
name2, err := netutils.GenerateIfaceName(vethPrefix, vethLen)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1184,22 +1183,3 @@ func electMacAddress(epConfig *endpointConfiguration) net.HardwareAddr {
|
||||||
}
|
}
|
||||||
return netutils.GenerateRandomMAC()
|
return netutils.GenerateRandomMAC()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generates a name to be used for a virtual ethernet
|
|
||||||
// interface. The name is constructed by 'veth' appended
|
|
||||||
// by a randomly generated hex value. (example: veth0f60e2c)
|
|
||||||
func generateIfaceName() (string, error) {
|
|
||||||
for i := 0; i < 3; i++ {
|
|
||||||
name, err := netutils.GenerateRandomName(vethPrefix, vethLen)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if _, err := net.InterfaceByName(name); err != nil {
|
|
||||||
if strings.Contains(err.Error(), "no such") {
|
|
||||||
return name, nil
|
|
||||||
}
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "", &ErrIfaceName{}
|
|
||||||
}
|
|
||||||
|
|
|
@ -9,7 +9,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/docker/libnetwork/types"
|
||||||
"github.com/vishvananda/netlink"
|
"github.com/vishvananda/netlink"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -147,3 +149,22 @@ func GenerateRandomName(prefix string, size int) (string, error) {
|
||||||
}
|
}
|
||||||
return prefix + hex.EncodeToString(id)[:size], nil
|
return prefix + hex.EncodeToString(id)[:size], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenerateIfaceName returns an interface name using the passed in
|
||||||
|
// prefix and the length of random bytes. The api ensures that the
|
||||||
|
// there are is no interface which exists with that name.
|
||||||
|
func GenerateIfaceName(prefix string, len int) (string, error) {
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
name, err := GenerateRandomName(prefix, len)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, err := net.InterfaceByName(name); err != nil {
|
||||||
|
if strings.Contains(err.Error(), "no such") {
|
||||||
|
return name, nil
|
||||||
|
}
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", types.InternalErrorf("could not generate interface name")
|
||||||
|
}
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
_ "github.com/docker/libnetwork/netutils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var runningInContainer = flag.Bool("incontainer", false, "Indicates if the test is running in a container")
|
||||||
|
|
||||||
func TestErrorConstructors(t *testing.T) {
|
func TestErrorConstructors(t *testing.T) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue