diff --git a/namespaces/init.go b/namespaces/init.go index d357d164..5c7e1a71 100644 --- a/namespaces/init.go +++ b/namespaces/init.go @@ -314,12 +314,11 @@ func LoadContainerEnvironment(container *libcontainer.Config) error { func joinExistingNamespaces(namespaces []libcontainer.Namespace) error { for _, ns := range namespaces { if ns.Path != "" { - nsf := GetNamespace(ns.Name) f, err := os.OpenFile(ns.Path, os.O_RDONLY, 0) if err != nil { return err } - err = system.Setns(f.Fd(), uintptr(nsf.Value)) + err = system.Setns(f.Fd(), uintptr(namespaceInfo[ns.Name])) f.Close() if err != nil { return err diff --git a/namespaces/types.go b/namespaces/types.go deleted file mode 100644 index 16ce981e..00000000 --- a/namespaces/types.go +++ /dev/null @@ -1,50 +0,0 @@ -package namespaces - -import "errors" - -type ( - Namespace struct { - Key string `json:"key,omitempty"` - Value int `json:"value,omitempty"` - File string `json:"file,omitempty"` - } - Namespaces []*Namespace -) - -// namespaceList is used to convert the libcontainer types -// into the names of the files located in /proc//ns/* for -// each namespace -var ( - namespaceList = Namespaces{} - ErrUnkownNamespace = errors.New("Unknown namespace") - ErrUnsupported = errors.New("Unsupported method") -) - -func (ns *Namespace) String() string { - return ns.Key -} - -func GetNamespace(key string) *Namespace { - for _, ns := range namespaceList { - if ns.Key == key { - cpy := *ns - return &cpy - } - } - return nil -} - -// Contains returns true if the specified Namespace is -// in the slice -func (n Namespaces) Contains(ns string) bool { - return n.Get(ns) != nil -} - -func (n Namespaces) Get(ns string) *Namespace { - for _, nsp := range n { - if nsp != nil && nsp.Key == ns { - return nsp - } - } - return nil -} diff --git a/namespaces/types_linux.go b/namespaces/types_linux.go deleted file mode 100644 index d3079944..00000000 --- a/namespaces/types_linux.go +++ /dev/null @@ -1,16 +0,0 @@ -package namespaces - -import ( - "syscall" -) - -func init() { - namespaceList = Namespaces{ - {Key: "NEWNS", Value: syscall.CLONE_NEWNS, File: "mnt"}, - {Key: "NEWUTS", Value: syscall.CLONE_NEWUTS, File: "uts"}, - {Key: "NEWIPC", Value: syscall.CLONE_NEWIPC, File: "ipc"}, - {Key: "NEWUSER", Value: syscall.CLONE_NEWUSER, File: "user"}, - {Key: "NEWPID", Value: syscall.CLONE_NEWPID, File: "pid"}, - {Key: "NEWNET", Value: syscall.CLONE_NEWNET, File: "net"}, - } -} diff --git a/namespaces/types_test.go b/namespaces/types_test.go deleted file mode 100644 index 4d0a72c9..00000000 --- a/namespaces/types_test.go +++ /dev/null @@ -1,30 +0,0 @@ -package namespaces - -import ( - "testing" -) - -func TestNamespacesContains(t *testing.T) { - ns := Namespaces{ - GetNamespace("NEWPID"), - GetNamespace("NEWNS"), - GetNamespace("NEWUTS"), - } - - if ns.Contains("NEWNET") { - t.Fatal("namespaces should not contain NEWNET") - } - - if !ns.Contains("NEWPID") { - t.Fatal("namespaces should contain NEWPID but does not") - } - - withNil := Namespaces{ - GetNamespace("UNDEFINED"), // this element will be nil - GetNamespace("NEWPID"), - } - - if !withNil.Contains("NEWPID") { - t.Fatal("namespaces should contain NEWPID but does not") - } -} diff --git a/namespaces/utils.go b/namespaces/utils.go index 88420fe5..556ea669 100644 --- a/namespaces/utils.go +++ b/namespaces/utils.go @@ -17,6 +17,15 @@ func (i initError) Error() string { return i.Message } +var namespaceInfo = map[string]int{ + "NEWNET": syscall.CLONE_NEWNET, + "NEWNS": syscall.CLONE_NEWNS, + "NEWUSER": syscall.CLONE_NEWUSER, + "NEWIPC": syscall.CLONE_NEWIPC, + "NEWUTS": syscall.CLONE_NEWUTS, + "NEWPID": syscall.CLONE_NEWPID, +} + // New returns a newly initialized Pipe for communication between processes func newInitPipe() (parent *os.File, child *os.File, err error) { fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0) @@ -30,9 +39,7 @@ func newInitPipe() (parent *os.File, child *os.File, err error) { // flags on clone, unshare, and setns func GetNamespaceFlags(namespaces []libcontainer.Namespace) (flag int) { for _, v := range namespaces { - if ns := GetNamespace(v.Name); ns != nil { - flag |= ns.Value - } + flag |= namespaceInfo[v.Name] } return flag }