Rename nativeID to nsPath. Simplify.

Signed-off-by: Vlad A. Ionescu <vladaionescu@users.noreply.github.com>
v0.8
Vlad A. Ionescu 2020-11-02 11:39:17 -08:00
parent 39fd96d2dd
commit 03987d2a75
1 changed files with 21 additions and 22 deletions

View File

@ -15,29 +15,28 @@ import (
) )
func createNetNS(c *cniProvider, id string) (string, error) { func createNetNS(c *cniProvider, id string) (string, error) {
p := filepath.Join(c.root, "net/cni", id) nsPath := filepath.Join(c.root, "net/cni", id)
if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil { if err := os.MkdirAll(filepath.Dir(nsPath), 0700); err != nil {
deleteNetNS(p)
return "", err return "", err
} }
f, err := os.Create(p) f, err := os.Create(nsPath)
if err != nil { if err != nil {
deleteNetNS(p) deleteNetNS(nsPath)
return "", err return "", err
} }
if err := f.Close(); err != nil { if err := f.Close(); err != nil {
deleteNetNS(p) deleteNetNS(nsPath)
return "", err return "", err
} }
procNetNSBytes, err := syscall.BytePtrFromString("/proc/self/ns/net") procNetNSBytes, err := syscall.BytePtrFromString("/proc/self/ns/net")
if err != nil { if err != nil {
deleteNetNS(p) deleteNetNS(nsPath)
return "", err return "", err
} }
pBytes, err := syscall.BytePtrFromString(p) nsPathBytes, err := syscall.BytePtrFromString(nsPath)
if err != nil { if err != nil {
deleteNetNS(p) deleteNetNS(nsPath)
return "", err return "", err
} }
beforeFork() beforeFork()
@ -45,7 +44,7 @@ func createNetNS(c *cniProvider, id string) (string, error) {
pid, _, errno := syscall.RawSyscall6(syscall.SYS_CLONE, uintptr(syscall.SIGCHLD)|unix.CLONE_NEWNET, 0, 0, 0, 0, 0) pid, _, errno := syscall.RawSyscall6(syscall.SYS_CLONE, uintptr(syscall.SIGCHLD)|unix.CLONE_NEWNET, 0, 0, 0, 0, 0)
if errno != 0 { if errno != 0 {
afterFork() afterFork()
deleteNetNS(p) deleteNetNS(nsPath)
return "", errno return "", errno
} }
@ -58,31 +57,31 @@ func createNetNS(c *cniProvider, id string) (string, error) {
} }
if err != nil { if err != nil {
deleteNetNS(p) deleteNetNS(nsPath)
return "", errors.Wrapf(err, "failed to find pid=%d process", pid) return "", errors.Wrapf(err, "failed to find pid=%d process", pid)
} }
errno = syscall.Errno(ws.ExitStatus()) errno = syscall.Errno(ws.ExitStatus())
if errno != 0 { if errno != 0 {
deleteNetNS(p) deleteNetNS(nsPath)
return "", errors.Wrapf(errno, "failed to mount %s (pid=%d)", p, pid) return "", errors.Wrapf(errno, "failed to mount %s (pid=%d)", nsPath, pid)
} }
return p, nil return nsPath, nil
} }
afterForkInChild() afterForkInChild()
_, _, errno = syscall.RawSyscall6(syscall.SYS_MOUNT, uintptr(unsafe.Pointer(procNetNSBytes)), uintptr(unsafe.Pointer(pBytes)), 0, uintptr(unix.MS_BIND), 0, 0) _, _, errno = syscall.RawSyscall6(syscall.SYS_MOUNT, uintptr(unsafe.Pointer(procNetNSBytes)), uintptr(unsafe.Pointer(nsPathBytes)), 0, uintptr(unix.MS_BIND), 0, 0)
syscall.RawSyscall(syscall.SYS_EXIT, uintptr(errno), 0, 0) syscall.RawSyscall(syscall.SYS_EXIT, uintptr(errno), 0, 0)
panic("unreachable") panic("unreachable")
} }
func setNetNS(s *specs.Spec, nativeID string) error { func setNetNS(s *specs.Spec, nsPath string) error {
return oci.WithLinuxNamespace(specs.LinuxNamespace{ return oci.WithLinuxNamespace(specs.LinuxNamespace{
Type: specs.NetworkNamespace, Type: specs.NetworkNamespace,
Path: nativeID, Path: nsPath,
})(nil, nil, nil, s) })(nil, nil, nil, s)
} }
func unmountNetNS(nativeID string) error { func unmountNetNS(nsPath string) error {
if err := unix.Unmount(nativeID, unix.MNT_DETACH); err != nil { if err := unix.Unmount(nsPath, unix.MNT_DETACH); err != nil {
if err != syscall.EINVAL && err != syscall.ENOENT { if err != syscall.EINVAL && err != syscall.ENOENT {
return errors.Wrap(err, "error unmounting network namespace") return errors.Wrap(err, "error unmounting network namespace")
} }
@ -90,9 +89,9 @@ func unmountNetNS(nativeID string) error {
return nil return nil
} }
func deleteNetNS(nativeID string) error { func deleteNetNS(nsPath string) error {
if err := os.RemoveAll(nativeID); err != nil && !errors.Is(err, os.ErrNotExist) { if err := os.Remove(nsPath); err != nil && !errors.Is(err, os.ErrNotExist) {
return errors.Wrapf(err, "error removing network namespace %s", nativeID) return errors.Wrapf(err, "error removing network namespace %s", nsPath)
} }
return nil return nil
} }