Add type for namespaces for better UI

This adds `type Namespaces []Namespace` so that methods can be added to
this slice so that it is easier for consumers to work with the values.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2014-12-16 15:32:54 -08:00
parent ef1c1c4289
commit 4661c239dc
2 changed files with 37 additions and 18 deletions

View File

@ -17,6 +17,39 @@ type Namespace struct {
Path string `json:"path,omitempty"`
}
type Namespaces []Namespace
func (n Namespaces) Exists(name string) bool {
return n.index(name) != -1
}
func (n Namespaces) Remove(name string) bool {
i := n.index(name)
if i == -1 {
return false
}
n = append(n[:i], n[i+1:]...)
return true
}
func (n Namespaces) Add(name, path string) {
i := n.index(name)
if i == -1 {
n = append(n, Namespace{Name: name, Path: path})
return
}
n[i].Path = path
}
func (n Namespaces) index(name string) int {
for i, ns := range n {
if ns.Name == name {
return i
}
}
return -1
}
// Config defines configuration options for executing a process inside a contained environment.
type Config struct {
// Mount specific options.
@ -45,7 +78,7 @@ type Config struct {
// Namespaces specifies the container's namespaces that it should setup when cloning the init process
// If a namespace is not provided that namespace is shared from the container's parent process
Namespaces []Namespace `json:"namespaces,omitempty"`
Namespaces Namespaces `json:"namespaces,omitempty"`
// Capabilities specify the capabilities to keep when executing the process inside the container
// All capbilities not specified will be dropped from the processes capability mask

View File

@ -4,8 +4,6 @@ import (
"os"
"strings"
"testing"
"github.com/docker/libcontainer"
)
func TestExecPS(t *testing.T) {
@ -88,8 +86,7 @@ func TestIPCHost(t *testing.T) {
}
config := newTemplateConfig(rootfs)
i := getNamespaceIndex(config, "NEWIPC")
config.Namespaces = append(config.Namespaces[:i], config.Namespaces[i+1:]...)
config.Namespaces.Remove("NEWIPC")
buffers, exitCode, err := runContainer(config, "", "readlink", "/proc/self/ns/ipc")
if err != nil {
t.Fatal(err)
@ -121,8 +118,7 @@ func TestIPCJoinPath(t *testing.T) {
}
config := newTemplateConfig(rootfs)
i := getNamespaceIndex(config, "NEWIPC")
config.Namespaces[i].Path = "/proc/1/ns/ipc"
config.Namespaces.Add("NEWIPC", "/proc/1/ns/ipc")
buffers, exitCode, err := runContainer(config, "", "readlink", "/proc/self/ns/ipc")
if err != nil {
@ -150,8 +146,7 @@ func TestIPCBadPath(t *testing.T) {
defer remove(rootfs)
config := newTemplateConfig(rootfs)
i := getNamespaceIndex(config, "NEWIPC")
config.Namespaces[i].Path = "/proc/1/ns/ipcc"
config.Namespaces.Add("NEWIPC", "/proc/1/ns/ipcc")
_, _, err = runContainer(config, "", "true")
if err == nil {
@ -179,12 +174,3 @@ func TestRlimit(t *testing.T) {
t.Fatalf("expected rlimit to be 1024, got %s", limit)
}
}
func getNamespaceIndex(config *libcontainer.Config, name string) int {
for i, v := range config.Namespaces {
if v.Name == name {
return i
}
}
return -1
}