@@ -46,14 +46,12 @@ import (
4646 "golang.org/x/sys/unix"
4747)
4848
49- const nsRunDir = "/var/run/netns"
50-
5149// Some of the following functions are migrated from
5250// https://github.com/containernetworking/plugins/blob/master/pkg/testutils/netns_linux.go
5351
5452// newNS creates a new persistent (bind-mounted) network namespace and returns the
5553// path to the network namespace.
56- func newNS () (nsPath string , err error ) {
54+ func newNS (baseDir string ) (nsPath string , err error ) {
5755 b := make ([]byte , 16 )
5856 if _ , err := rand .Reader .Read (b ); err != nil {
5957 return "" , errors .Wrap (err , "failed to generate random netns name" )
@@ -62,13 +60,13 @@ func newNS() (nsPath string, err error) {
6260 // Create the directory for mounting network namespaces
6361 // This needs to be a shared mountpoint in case it is mounted in to
6462 // other namespaces (containers)
65- if err := os .MkdirAll (nsRunDir , 0755 ); err != nil {
63+ if err := os .MkdirAll (baseDir , 0755 ); err != nil {
6664 return "" , err
6765 }
6866
6967 // create an empty file at the mount point
7068 nsName := fmt .Sprintf ("cni-%x-%x-%x-%x-%x" , b [0 :4 ], b [4 :6 ], b [6 :8 ], b [8 :10 ], b [10 :])
71- nsPath = path .Join (nsRunDir , nsName )
69+ nsPath = path .Join (baseDir , nsName )
7270 mountPointFd , err := os .Create (nsPath )
7371 if err != nil {
7472 return "" , err
@@ -162,8 +160,8 @@ type NetNS struct {
162160}
163161
164162// NewNetNS creates a network namespace.
165- func NewNetNS () (* NetNS , error ) {
166- path , err := newNS ()
163+ func NewNetNS (baseDir string ) (* NetNS , error ) {
164+ path , err := newNS (baseDir )
167165 if err != nil {
168166 return nil , errors .Wrap (err , "failed to setup netns" )
169167 }
0 commit comments