Browse code

Move ReplaceOrAppendEnvValues to container package

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2016/12/22 06:42:39
Showing 7 changed files
... ...
@@ -16,7 +16,6 @@ import (
16 16
 	"github.com/docker/docker/pkg/stringid"
17 17
 	"github.com/docker/docker/pkg/symlink"
18 18
 	"github.com/docker/docker/pkg/system"
19
-	"github.com/docker/docker/utils"
20 19
 	"github.com/docker/docker/volume"
21 20
 	"github.com/opencontainers/runc/libcontainer/label"
22 21
 	"golang.org/x/sys/unix"
... ...
@@ -69,7 +68,7 @@ func (container *Container) CreateDaemonEnvironment(tty bool, linkedEnv []string
69 69
 	// because the env on the container can override certain default values
70 70
 	// we need to replace the 'env' keys where they match and append anything
71 71
 	// else.
72
-	env = utils.ReplaceOrAppendEnvValues(env, container.Config.Env)
72
+	env = ReplaceOrAppendEnvValues(env, container.Config.Env)
73 73
 	return env
74 74
 }
75 75
 
... ...
@@ -8,7 +8,6 @@ import (
8 8
 	"path/filepath"
9 9
 
10 10
 	containertypes "github.com/docker/docker/api/types/container"
11
-	"github.com/docker/docker/utils"
12 11
 )
13 12
 
14 13
 // Container holds fields specific to the Windows implementation. See
... ...
@@ -30,7 +29,7 @@ func (container *Container) CreateDaemonEnvironment(_ bool, linkedEnv []string)
30 30
 	// because the env on the container can override certain default values
31 31
 	// we need to replace the 'env' keys where they match and append anything
32 32
 	// else.
33
-	return utils.ReplaceOrAppendEnvValues(linkedEnv, container.Config.Env)
33
+	return ReplaceOrAppendEnvValues(linkedEnv, container.Config.Env)
34 34
 }
35 35
 
36 36
 // UnmountIpcMounts unmounts Ipc related mounts.
37 37
new file mode 100644
... ...
@@ -0,0 +1,43 @@
0
+package container
1
+
2
+import (
3
+	"strings"
4
+)
5
+
6
+// ReplaceOrAppendEnvValues returns the defaults with the overrides either
7
+// replaced by env key or appended to the list
8
+func ReplaceOrAppendEnvValues(defaults, overrides []string) []string {
9
+	cache := make(map[string]int, len(defaults))
10
+	for i, e := range defaults {
11
+		parts := strings.SplitN(e, "=", 2)
12
+		cache[parts[0]] = i
13
+	}
14
+
15
+	for _, value := range overrides {
16
+		// Values w/o = means they want this env to be removed/unset.
17
+		if !strings.Contains(value, "=") {
18
+			if i, exists := cache[value]; exists {
19
+				defaults[i] = "" // Used to indicate it should be removed
20
+			}
21
+			continue
22
+		}
23
+
24
+		// Just do a normal set/update
25
+		parts := strings.SplitN(value, "=", 2)
26
+		if i, exists := cache[parts[0]]; exists {
27
+			defaults[i] = value
28
+		} else {
29
+			defaults = append(defaults, value)
30
+		}
31
+	}
32
+
33
+	// Now remove all entries that we want to "unset"
34
+	for i := 0; i < len(defaults); i++ {
35
+		if defaults[i] == "" {
36
+			defaults = append(defaults[:i], defaults[i+1:]...)
37
+			i--
38
+		}
39
+	}
40
+
41
+	return defaults
42
+}
0 43
new file mode 100644
... ...
@@ -0,0 +1,21 @@
0
+package container
1
+
2
+import "testing"
3
+
4
+func TestReplaceAndAppendEnvVars(t *testing.T) {
5
+	var (
6
+		d = []string{"HOME=/"}
7
+		o = []string{"HOME=/root", "TERM=xterm"}
8
+	)
9
+
10
+	env := ReplaceOrAppendEnvValues(d, o)
11
+	if len(env) != 2 {
12
+		t.Fatalf("expected len of 2 got %d", len(env))
13
+	}
14
+	if env[0] != "HOME=/root" {
15
+		t.Fatalf("expected HOME=/root got '%s'", env[0])
16
+	}
17
+	if env[1] != "TERM=xterm" {
18
+		t.Fatalf("expected TERM=xterm got '%s'", env[1])
19
+	}
20
+}
... ...
@@ -18,7 +18,6 @@ import (
18 18
 	"github.com/docker/docker/pkg/pools"
19 19
 	"github.com/docker/docker/pkg/signal"
20 20
 	"github.com/docker/docker/pkg/term"
21
-	"github.com/docker/docker/utils"
22 21
 )
23 22
 
24 23
 // Seconds to wait after sending TERM before trying KILL
... ...
@@ -94,7 +93,7 @@ func (d *Daemon) getActiveContainer(name string) (*container.Container, error) {
94 94
 
95 95
 // ContainerExecCreate sets up an exec in a running container.
96 96
 func (d *Daemon) ContainerExecCreate(name string, config *types.ExecConfig) (string, error) {
97
-	container, err := d.getActiveContainer(name)
97
+	cntr, err := d.getActiveContainer(name)
98 98
 	if err != nil {
99 99
 		return "", err
100 100
 	}
... ...
@@ -115,7 +114,7 @@ func (d *Daemon) ContainerExecCreate(name string, config *types.ExecConfig) (str
115 115
 	execConfig.OpenStdin = config.AttachStdin
116 116
 	execConfig.OpenStdout = config.AttachStdout
117 117
 	execConfig.OpenStderr = config.AttachStderr
118
-	execConfig.ContainerID = container.ID
118
+	execConfig.ContainerID = cntr.ID
119 119
 	execConfig.DetachKeys = keys
120 120
 	execConfig.Entrypoint = entrypoint
121 121
 	execConfig.Args = args
... ...
@@ -123,18 +122,18 @@ func (d *Daemon) ContainerExecCreate(name string, config *types.ExecConfig) (str
123 123
 	execConfig.Privileged = config.Privileged
124 124
 	execConfig.User = config.User
125 125
 
126
-	linkedEnv, err := d.setupLinkedContainers(container)
126
+	linkedEnv, err := d.setupLinkedContainers(cntr)
127 127
 	if err != nil {
128 128
 		return "", err
129 129
 	}
130
-	execConfig.Env = utils.ReplaceOrAppendEnvValues(container.CreateDaemonEnvironment(config.Tty, linkedEnv), config.Env)
130
+	execConfig.Env = container.ReplaceOrAppendEnvValues(cntr.CreateDaemonEnvironment(config.Tty, linkedEnv), config.Env)
131 131
 	if len(execConfig.User) == 0 {
132
-		execConfig.User = container.Config.User
132
+		execConfig.User = cntr.Config.User
133 133
 	}
134 134
 
135
-	d.registerExecCommand(container, execConfig)
135
+	d.registerExecCommand(cntr, execConfig)
136 136
 
137
-	d.LogContainerEvent(container, "exec_create: "+execConfig.Entrypoint+" "+strings.Join(execConfig.Args, " "))
137
+	d.LogContainerEvent(cntr, "exec_create: "+execConfig.Entrypoint+" "+strings.Join(execConfig.Args, " "))
138 138
 
139 139
 	return execConfig.ID, nil
140 140
 }
141 141
deleted file mode 100644
... ...
@@ -1,43 +0,0 @@
1
-package utils
2
-
3
-import (
4
-	"strings"
5
-)
6
-
7
-// ReplaceOrAppendEnvValues returns the defaults with the overrides either
8
-// replaced by env key or appended to the list
9
-func ReplaceOrAppendEnvValues(defaults, overrides []string) []string {
10
-	cache := make(map[string]int, len(defaults))
11
-	for i, e := range defaults {
12
-		parts := strings.SplitN(e, "=", 2)
13
-		cache[parts[0]] = i
14
-	}
15
-
16
-	for _, value := range overrides {
17
-		// Values w/o = means they want this env to be removed/unset.
18
-		if !strings.Contains(value, "=") {
19
-			if i, exists := cache[value]; exists {
20
-				defaults[i] = "" // Used to indicate it should be removed
21
-			}
22
-			continue
23
-		}
24
-
25
-		// Just do a normal set/update
26
-		parts := strings.SplitN(value, "=", 2)
27
-		if i, exists := cache[parts[0]]; exists {
28
-			defaults[i] = value
29
-		} else {
30
-			defaults = append(defaults, value)
31
-		}
32
-	}
33
-
34
-	// Now remove all entries that we want to "unset"
35
-	for i := 0; i < len(defaults); i++ {
36
-		if defaults[i] == "" {
37
-			defaults = append(defaults[:i], defaults[i+1:]...)
38
-			i--
39
-		}
40
-	}
41
-
42
-	return defaults
43
-}
44 1
deleted file mode 100644
... ...
@@ -1,21 +0,0 @@
1
-package utils
2
-
3
-import "testing"
4
-
5
-func TestReplaceAndAppendEnvVars(t *testing.T) {
6
-	var (
7
-		d = []string{"HOME=/"}
8
-		o = []string{"HOME=/root", "TERM=xterm"}
9
-	)
10
-
11
-	env := ReplaceOrAppendEnvValues(d, o)
12
-	if len(env) != 2 {
13
-		t.Fatalf("expected len of 2 got %d", len(env))
14
-	}
15
-	if env[0] != "HOME=/root" {
16
-		t.Fatalf("expected HOME=/root got '%s'", env[0])
17
-	}
18
-	if env[1] != "TERM=xterm" {
19
-		t.Fatalf("expected TERM=xterm got '%s'", env[1])
20
-	}
21
-}