diff options
| author | gy95 <guoyao17@huawei.com> | 2021-08-13 17:13:54 +0800 |
|---|---|---|
| committer | gy95 <guoyao17@huawei.com> | 2021-08-17 14:54:57 +0800 |
| commit | 4642c398c64ca30bcbad6ad1de38522172626333 (patch) | |
| tree | 4916334f17be66ac31a023acb66ebd67a2a822ad /vendor/github.com/vishvananda | |
| parent | bump kubernetes vendor to 1.21 (diff) | |
| download | kubeedge-4642c398c64ca30bcbad6ad1de38522172626333.tar.gz | |
update k8s version to v1.21.4
Signed-off-by: gy95 <guoyao17@huawei.com>
Diffstat (limited to 'vendor/github.com/vishvananda')
| -rw-r--r-- | vendor/github.com/vishvananda/netns/netns_linux.go | 60 |
1 files changed, 53 insertions, 7 deletions
diff --git a/vendor/github.com/vishvananda/netns/netns_linux.go b/vendor/github.com/vishvananda/netns/netns_linux.go index 7489c9913..c76acd087 100644 --- a/vendor/github.com/vishvananda/netns/netns_linux.go +++ b/vendor/github.com/vishvananda/netns/netns_linux.go @@ -6,21 +6,24 @@ import ( "fmt" "io/ioutil" "os" + "path" "path/filepath" "strconv" "strings" + "syscall" "golang.org/x/sys/unix" ) // Deprecated: use syscall pkg instead (go >= 1.5 needed). const ( - CLONE_NEWUTS = 0x04000000 /* New utsname group? */ - CLONE_NEWIPC = 0x08000000 /* New ipcs */ - CLONE_NEWUSER = 0x10000000 /* New user namespace */ - CLONE_NEWPID = 0x20000000 /* New pid namespace */ - CLONE_NEWNET = 0x40000000 /* New network namespace */ - CLONE_IO = 0x80000000 /* Get io context */ + CLONE_NEWUTS = 0x04000000 /* New utsname group? */ + CLONE_NEWIPC = 0x08000000 /* New ipcs */ + CLONE_NEWUSER = 0x10000000 /* New user namespace */ + CLONE_NEWPID = 0x20000000 /* New pid namespace */ + CLONE_NEWNET = 0x40000000 /* New network namespace */ + CLONE_IO = 0x80000000 /* Get io context */ + bindMountPath = "/run/netns" /* Bind mount path for named netns */ ) // Setns sets namespace using syscall. Note that this should be a method @@ -44,6 +47,49 @@ func New() (ns NsHandle, err error) { return Get() } +// NewNamed creates a new named network namespace and returns a handle to it +func NewNamed(name string) (NsHandle, error) { + if _, err := os.Stat(bindMountPath); os.IsNotExist(err) { + err = os.MkdirAll(bindMountPath, 0755) + if err != nil { + return None(), err + } + } + + newNs, err := New() + if err != nil { + return None(), err + } + + namedPath := path.Join(bindMountPath, name) + + f, err := os.OpenFile(namedPath, os.O_CREATE|os.O_EXCL, 0444) + if err != nil { + return None(), err + } + f.Close() + + nsPath := fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), syscall.Gettid()) + err = syscall.Mount(nsPath, namedPath, "bind", syscall.MS_BIND, "") + if err != nil { + return None(), err + } + + return newNs, nil +} + +// DeleteNamed deletes a named network namespace +func DeleteNamed(name string) error { + namedPath := path.Join(bindMountPath, name) + + err := syscall.Unmount(namedPath, syscall.MNT_DETACH) + if err != nil { + return err + } + + return os.Remove(namedPath) +} + // Get gets a handle to the current threads network namespace. func Get() (NsHandle, error) { return GetFromThread(os.Getpid(), unix.Gettid()) @@ -52,7 +98,7 @@ func Get() (NsHandle, error) { // GetFromPath gets a handle to a network namespace // identified by the path func GetFromPath(path string) (NsHandle, error) { - fd, err := unix.Open(path, unix.O_RDONLY, 0) + fd, err := unix.Open(path, unix.O_RDONLY|unix.O_CLOEXEC, 0) if err != nil { return -1, err } |
