@@ -62,12 +62,26 @@ func NewNSAtPath(nsPath string) (ns.NetNS, error) {
62
62
// NewNS creates a new persistent (bind-mounted) network namespace and returns
63
63
// an object representing that namespace, without switching to it.
64
64
func NewNS () (ns.NetNS , error ) {
65
+ nsRunDir , err := GetNSRunDir ()
66
+ if err != nil {
67
+ return nil , err
68
+ }
69
+
70
+ // Create the directory for mounting network namespaces
71
+ // This needs to be a shared mountpoint in case it is mounted in to
72
+ // other namespaces (containers)
73
+ err = makeNetnsDir (nsRunDir )
74
+ if err != nil {
75
+ return nil , err
76
+ }
77
+
65
78
for i := 0 ; i < 10000 ; i ++ {
66
79
nsName , err := getRandomNetnsName ()
67
80
if err != nil {
68
81
return nil , err
69
82
}
70
- ns , err := NewNSWithName (nsName )
83
+ nsPath := path .Join (nsRunDir , nsName )
84
+ ns , err := newNSPath (nsPath )
71
85
if err == nil {
72
86
return ns , nil
73
87
}
@@ -80,26 +94,6 @@ func NewNS() (ns.NetNS, error) {
80
94
return nil , errNoFreeName
81
95
}
82
96
83
- // NewNSWithName creates a new persistent (bind-mounted) network namespace and returns
84
- // an object representing that namespace, without switching to it.
85
- func NewNSWithName (name string ) (ns.NetNS , error ) {
86
- nsRunDir , err := GetNSRunDir ()
87
- if err != nil {
88
- return nil , err
89
- }
90
-
91
- // Create the directory for mounting network namespaces
92
- // This needs to be a shared mountpoint in case it is mounted in to
93
- // other namespaces (containers)
94
- err = makeNetnsDir (nsRunDir )
95
- if err != nil {
96
- return nil , err
97
- }
98
-
99
- nsPath := path .Join (nsRunDir , name )
100
- return newNSPath (nsPath )
101
- }
102
-
103
97
// NewNSFrom creates a persistent (bind-mounted) network namespace from the
104
98
// given netns path, i.e. /proc/<pid>/ns/net, and returns the new full path to
105
99
// the bind mounted file in the netns run dir.
0 commit comments