Skip to content

Commit 322f2c2

Browse files
committed
pkg/netns: split out makeNetnsDir logic
Create a new function to create the netns dir. Signed-off-by: Paul Holzinger <[email protected]>
1 parent 52c82b1 commit 322f2c2

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

pkg/netns/netns_linux.go

+30-21
Original file line numberDiff line numberDiff line change
@@ -91,36 +91,45 @@ func NewNSWithName(name string) (ns.NetNS, error) {
9191
// Create the directory for mounting network namespaces
9292
// This needs to be a shared mountpoint in case it is mounted in to
9393
// other namespaces (containers)
94-
err = os.MkdirAll(nsRunDir, 0o755)
94+
err = makeNetnsDir(nsRunDir)
9595
if err != nil {
9696
return nil, err
9797
}
9898

99-
// Remount the namespace directory shared. This will fail if it is not
100-
// already a mountpoint, so bind-mount it on to itself to "upgrade" it
101-
// to a mountpoint.
102-
err = unix.Mount("", nsRunDir, "none", unix.MS_SHARED|unix.MS_REC, "")
99+
nsPath := path.Join(nsRunDir, name)
100+
return newNSPath(nsPath)
101+
}
102+
103+
func makeNetnsDir(nsRunDir string) error {
104+
err := os.MkdirAll(nsRunDir, 0o755)
103105
if err != nil {
104-
if err != unix.EINVAL {
105-
return nil, fmt.Errorf("mount --make-rshared %s failed: %q", nsRunDir, err)
106-
}
106+
return err
107+
}
108+
// Remount the namespace directory shared. This will fail with EINVAL
109+
// if it is not already a mountpoint, so bind-mount it on to itself
110+
// to "upgrade" it to a mountpoint.
111+
err = unix.Mount("", nsRunDir, "none", unix.MS_SHARED|unix.MS_REC, "")
112+
if err == nil {
113+
return nil
114+
}
115+
if err != unix.EINVAL {
116+
return fmt.Errorf("mount --make-rshared %s failed: %q", nsRunDir, err)
117+
}
107118

108-
// Recursively remount /run/netns on itself. The recursive flag is
109-
// so that any existing netns bindmounts are carried over.
110-
err = unix.Mount(nsRunDir, nsRunDir, "none", unix.MS_BIND|unix.MS_REC, "")
111-
if err != nil {
112-
return nil, fmt.Errorf("mount --rbind %s %s failed: %q", nsRunDir, nsRunDir, err)
113-
}
119+
// Recursively remount /run/netns on itself. The recursive flag is
120+
// so that any existing netns bindmounts are carried over.
121+
err = unix.Mount(nsRunDir, nsRunDir, "none", unix.MS_BIND|unix.MS_REC, "")
122+
if err != nil {
123+
return fmt.Errorf("mount --rbind %s %s failed: %q", nsRunDir, nsRunDir, err)
124+
}
114125

115-
// Now we can make it shared
116-
err = unix.Mount("", nsRunDir, "none", unix.MS_SHARED|unix.MS_REC, "")
117-
if err != nil {
118-
return nil, fmt.Errorf("mount --make-rshared %s failed: %q", nsRunDir, err)
119-
}
126+
// Now we can make it shared
127+
err = unix.Mount("", nsRunDir, "none", unix.MS_SHARED|unix.MS_REC, "")
128+
if err != nil {
129+
return fmt.Errorf("mount --make-rshared %s failed: %q", nsRunDir, err)
120130
}
121131

122-
nsPath := path.Join(nsRunDir, name)
123-
return newNSPath(nsPath)
132+
return nil
124133
}
125134

126135
func newNSPath(nsPath string) (ns.NetNS, error) {

0 commit comments

Comments
 (0)