diff --git a/pkg/netns/netns_linux.go b/pkg/netns/netns_linux.go index 1aa797d10..3a3372889 100644 --- a/pkg/netns/netns_linux.go +++ b/pkg/netns/netns_linux.go @@ -62,12 +62,26 @@ func NewNSAtPath(nsPath string) (ns.NetNS, error) { // NewNS creates a new persistent (bind-mounted) network namespace and returns // an object representing that namespace, without switching to it. func NewNS() (ns.NetNS, error) { + nsRunDir, err := GetNSRunDir() + if err != nil { + return nil, err + } + + // Create the directory for mounting network namespaces + // This needs to be a shared mountpoint in case it is mounted in to + // other namespaces (containers) + err = makeNetnsDir(nsRunDir) + if err != nil { + return nil, err + } + for i := 0; i < 10000; i++ { nsName, err := getRandomNetnsName() if err != nil { return nil, err } - ns, err := NewNSWithName(nsName) + nsPath := path.Join(nsRunDir, nsName) + ns, err := newNSPath(nsPath) if err == nil { return ns, nil } @@ -80,26 +94,6 @@ func NewNS() (ns.NetNS, error) { return nil, errNoFreeName } -// NewNSWithName creates a new persistent (bind-mounted) network namespace and returns -// an object representing that namespace, without switching to it. -func NewNSWithName(name string) (ns.NetNS, error) { - nsRunDir, err := GetNSRunDir() - if err != nil { - return nil, err - } - - // Create the directory for mounting network namespaces - // This needs to be a shared mountpoint in case it is mounted in to - // other namespaces (containers) - err = makeNetnsDir(nsRunDir) - if err != nil { - return nil, err - } - - nsPath := path.Join(nsRunDir, name) - return newNSPath(nsPath) -} - // NewNSFrom creates a persistent (bind-mounted) network namespace from the // given netns path, i.e. /proc//ns/net, and returns the new full path to // the bind mounted file in the netns run dir.