8000 networking: ensure the netns directory is mounted by JulienBalestra · Pull Request #3761 · rkt/rkt · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Feb 24, 2020. It is now read-only.

networking: ensure the netns directory is mounted #3761

Merged
merged 1 commit into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions networking/networking.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ import (
)

const (
IfNamePattern = "eth%d"
selfNetNS = "/proc/self/ns/net"
IfNamePattern = "eth%d"
selfNetNS = "/proc/self/ns/net"
mountNetnsDirectory = "/var/run/netns"
)

// Networking describes the networking details of a pod.
Expand Down Expand Up @@ -83,8 +84,13 @@ func Setup(podRoot string, podID types.UUID, fps []commonnet.ForwardedPort, netL
},
}

err := n.mountNetnsDirectory()
if err != nil {
return nil, err
}

// Create the network namespace (and save its name in a file)
err := n.podNSCreate()
err = n.podNSCreate()
if err != nil {
return nil, err
}
Expand Down
33 changes: 33 additions & 0 deletions networking/podenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,39 @@ func (e *podEnv) loadNets() ([]activeNet, error) {
return netSlice, nil
}

// Ensure the netns directory is mounted before adding new netns like `ip netns add <netns>` command does.
// See https://github.com/kubernetes/kubernetes/issues/48427
// Make it possible for network namespace mounts to propagate between mount namespaces.
// This makes it likely that an unmounting a network namespace file in one namespace will unmount the network namespace.
// file in all namespaces allowing the network namespace to be freed sooner.
func (e *podEnv) mountNetnsDirectory() error {
err := os.MkdirAll(mountNetnsDirectory, 0755)
if err != nil {
return err
}

err = syscall.Mount("", mountNetnsDirectory, "none", syscall.MS_SHARED|syscall.MS_REC, "")
if err != nil {
// Fail unless we need to make the mount point
if err != syscall.EINVAL {
return fmt.Errorf("mount --make-rshared %s failed: %q", mountNetnsDirectory, err)
}

// Upgrade mountTarget to a mount point
err = syscall.Mount(mountNetnsDirectory, mountNetnsDirectory, "none", syscall.MS_BIND|syscall.MS_REC, "")
if err != nil {
return fmt.Errorf("mount --rbind %s %s failed: %q", mountNetnsDirectory, mountNetnsDirectory, err)
}

// Remount after the Upgrade
err = syscall.Mount("", mountNetnsDirectory, "none", syscall.MS_SHARED|syscall.MS_REC, "")
if err != nil {
return fmt.Errorf("mount --make-rshared %s failed: %q", mountNetnsDirectory, err)
}
}
return nil
}

// podNSCreate creates the network namespace and saves a reference to its path.
// NewNS will bind-mount the namespace in /run/netns, so we write that filename
// to disk.
Expand Down
0