Bladeren bron

Change syntax to use dots
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby 11 jaren geleden
bovenliggende
commit
146a212f71

+ 2 - 1
runconfig/parse.go

@@ -256,10 +256,11 @@ func parseLxcOpt(opt string) (string, string, error) {
 	return strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]), nil
 }
 
+// options will come in the format of name.type=value
 func parseDriverOpts(opts opts.ListOpts) (map[string][]string, error) {
 	out := make(map[string][]string, len(opts.GetAll()))
 	for _, o := range opts.GetAll() {
-		parts := strings.SplitN(o, " ", 2)
+		parts := strings.SplitN(o, ".", 2)
 		if len(parts) < 2 {
 			return nil, fmt.Errorf("invalid opt format %s", o)
 		}

+ 0 - 27
runtime/execdriver/native/configuration/caps.go

@@ -1,27 +0,0 @@
-package configuration
-
-import (
-	"fmt"
-	"github.com/dotcloud/docker/pkg/libcontainer"
-	"strings"
-)
-
-// i.e: cap +MKNOD cap -NET_ADMIN
-func parseCapOpt(container *libcontainer.Container, opts []string) error {
-	var (
-		value = strings.TrimSpace(opts[0])
-		c     = container.CapabilitiesMask.Get(value[1:])
-	)
-	if c == nil {
-		return fmt.Errorf("%s is not a valid capability", value[1:])
-	}
-	switch value[0] {
-	case '-':
-		c.Enabled = false
-	case '+':
-		c.Enabled = true
-	default:
-		return fmt.Errorf("%c is not a valid modifier for capabilities", value[0])
-	}
-	return nil
-}

+ 0 - 19
runtime/execdriver/native/configuration/fs.go

@@ -1,19 +0,0 @@
-package configuration
-
-import (
-	"fmt"
-	"github.com/dotcloud/docker/pkg/libcontainer"
-	"strings"
-)
-
-func parseFsOpts(container *libcontainer.Container, opts []string) error {
-	opt := strings.TrimSpace(opts[0])
-
-	switch opt {
-	case "readonly":
-		container.ReadonlyFs = true
-	default:
-		return fmt.Errorf("%s is not a valid filesystem option", opt)
-	}
-	return nil
-}

+ 0 - 35
runtime/execdriver/native/configuration/net.go

@@ -1,35 +0,0 @@
-package configuration
-
-import (
-	"fmt"
-	"github.com/dotcloud/docker/pkg/libcontainer"
-	"os/exec"
-	"path/filepath"
-	"strings"
-)
-
-// i.e: net join <name>
-func parseNetOpt(container *libcontainer.Container, running map[string]*exec.Cmd, opts []string) error {
-	opt := strings.TrimSpace(opts[1])
-	switch opt {
-	case "join":
-		var (
-			id  = strings.TrimSpace(opts[2])
-			cmd = running[id]
-		)
-
-		if cmd == nil || cmd.Process == nil {
-			return fmt.Errorf("%s is not a valid running container to join", id)
-		}
-		nspath := filepath.Join("/proc", fmt.Sprint(cmd.Process.Pid), "ns", "net")
-		container.Networks = append(container.Networks, &libcontainer.Network{
-			Type: "netns",
-			Context: libcontainer.Context{
-				"nspath": nspath,
-			},
-		})
-	default:
-		return fmt.Errorf("%s is not a valid network option", opt)
-	}
-	return nil
-}

+ 0 - 26
runtime/execdriver/native/configuration/ns.go

@@ -1,26 +0,0 @@
-package configuration
-
-import (
-	"fmt"
-	"github.com/dotcloud/docker/pkg/libcontainer"
-	"strings"
-)
-
-func parseNsOpt(container *libcontainer.Container, opts []string) error {
-	var (
-		value = strings.TrimSpace(opts[0])
-		ns    = container.Namespaces.Get(value[1:])
-	)
-	if ns == nil {
-		return fmt.Errorf("%s is not a valid namespace", value[1:])
-	}
-	switch value[0] {
-	case '-':
-		ns.Enabled = false
-	case '+':
-		ns.Enabled = true
-	default:
-		return fmt.Errorf("%c is not a valid modifier for namespaces", value[0])
-	}
-	return nil
-}

+ 85 - 16
runtime/execdriver/native/configuration/parse.go

@@ -4,9 +4,86 @@ import (
 	"fmt"
 	"github.com/dotcloud/docker/pkg/libcontainer"
 	"os/exec"
+	"path/filepath"
 	"strings"
 )
 
+type Action func(*libcontainer.Container, interface{}, string) error
+
+var actions = map[string]Action{
+	"cap.add":     addCap,
+	"cap.drop":    dropCap,
+	"fs.readonly": readonlyFs,
+	"ns.add":      addNamespace,
+	"ns.drop":     dropNamespace,
+	"net.join":    joinNetNamespace,
+}
+
+func addCap(container *libcontainer.Container, context interface{}, value string) error {
+	c := container.CapabilitiesMask.Get(value)
+	if c == nil {
+		return fmt.Errorf("%s is not a valid capability", value)
+	}
+	c.Enabled = true
+	return nil
+}
+
+func dropCap(container *libcontainer.Container, context interface{}, value string) error {
+	c := container.CapabilitiesMask.Get(value)
+	if c == nil {
+		return fmt.Errorf("%s is not a valid capability", value)
+	}
+	c.Enabled = false
+	return nil
+}
+
+func addNamespace(container *libcontainer.Container, context interface{}, value string) error {
+	ns := container.Namespaces.Get(value)
+	if ns == nil {
+		return fmt.Errorf("%s is not a valid namespace", value[1:])
+	}
+	ns.Enabled = true
+	return nil
+}
+
+func dropNamespace(container *libcontainer.Container, context interface{}, value string) error {
+	ns := container.Namespaces.Get(value)
+	if ns == nil {
+		return fmt.Errorf("%s is not a valid namespace", value[1:])
+	}
+	ns.Enabled = false
+	return nil
+}
+
+func readonlyFs(container *libcontainer.Container, context interface{}, value string) error {
+	switch value {
+	case "1", "true":
+		container.ReadonlyFs = true
+	default:
+		container.ReadonlyFs = false
+	}
+	return nil
+}
+
+func joinNetNamespace(container *libcontainer.Container, context interface{}, value string) error {
+	var (
+		running = context.(map[string]*exec.Cmd)
+		cmd     = running[value]
+	)
+
+	if cmd == nil || cmd.Process == nil {
+		return fmt.Errorf("%s is not a valid running container to join", value)
+	}
+	nspath := filepath.Join("/proc", fmt.Sprint(cmd.Process.Pid), "ns", "net")
+	container.Networks = append(container.Networks, &libcontainer.Network{
+		Type: "netns",
+		Context: libcontainer.Context{
+			"nspath": nspath,
+		},
+	})
+	return nil
+}
+
 // configureCustomOptions takes string commands from the user and allows modification of the
 // container's default configuration.
 //
@@ -14,25 +91,17 @@ import (
 // i.e: cgroup devices.allow *:*
 func ParseConfiguration(container *libcontainer.Container, running map[string]*exec.Cmd, opts []string) error {
 	for _, opt := range opts {
-		var (
-			err   error
-			parts = strings.Split(strings.TrimSpace(opt), " ")
-		)
-		if len(parts) < 2 {
-			return fmt.Errorf("invalid native driver opt %s", opt)
+		kv := strings.SplitN(opt, "=", 2)
+		if len(kv) < 2 {
+			return fmt.Errorf("invalid format for %s", opt)
 		}
 
-		switch parts[0] {
-		case "cap":
-			err = parseCapOpt(container, parts[1:])
-		case "ns":
-			err = parseNsOpt(container, parts[1:])
-		case "net":
-			err = parseNetOpt(container, running, parts[1:])
-		default:
-			return fmt.Errorf("%s is not a valid configuration option for the native driver", parts[0])
+		action, exists := actions[kv[0]]
+		if !exists {
+			return fmt.Errorf("%s is not a valid option for the native driver", kv[0])
 		}
-		if err != nil {
+
+		if err := action(container, running, kv[1]); err != nil {
 			return err
 		}
 	}