Browse code

Remove runconfig.Merge

Merge was used by builder and daemon. With this commit, the builder
call has been inlined and the function moved to the daemon package,
which is the only other caller.

Signed-off-by: Anusha Ragunathan <anusha@docker.com>

Anusha Ragunathan authored on 2016/01/06 01:48:09
Showing 6 changed files
... ...
@@ -24,7 +24,6 @@ import (
24 24
 	derr "github.com/docker/docker/errors"
25 25
 	"github.com/docker/docker/pkg/signal"
26 26
 	"github.com/docker/docker/pkg/system"
27
-	"github.com/docker/docker/runconfig"
28 27
 	runconfigopts "github.com/docker/docker/runconfig/opts"
29 28
 	"github.com/docker/go-connections/nat"
30 29
 )
... ...
@@ -317,7 +316,10 @@ func run(b *Builder, args []string, attributes map[string]bool, original string)
317 317
 
318 318
 	// stash the cmd
319 319
 	cmd := b.runConfig.Cmd
320
-	runconfig.Merge(b.runConfig, config)
320
+	if b.runConfig.Entrypoint.Len() == 0 && b.runConfig.Cmd.Len() == 0 {
321
+		b.runConfig.Cmd = config.Cmd
322
+	}
323
+
321 324
 	// stash the config environment
322 325
 	env := b.runConfig.Env
323 326
 
... ...
@@ -8,6 +8,7 @@ import (
8 8
 	"time"
9 9
 
10 10
 	"github.com/docker/docker/api/types"
11
+	containertypes "github.com/docker/docker/api/types/container"
11 12
 	"github.com/docker/docker/container"
12 13
 	"github.com/docker/docker/dockerversion"
13 14
 	"github.com/docker/docker/image"
... ...
@@ -15,9 +16,82 @@ import (
15 15
 	"github.com/docker/docker/pkg/archive"
16 16
 	"github.com/docker/docker/pkg/ioutils"
17 17
 	"github.com/docker/docker/reference"
18
-	"github.com/docker/docker/runconfig"
18
+	"github.com/docker/go-connections/nat"
19 19
 )
20 20
 
21
+// merge merges two Config, the image container configuration (defaults values),
22
+// and the user container configuration, either passed by the API or generated
23
+// by the cli.
24
+// It will mutate the specified user configuration (userConf) with the image
25
+// configuration where the user configuration is incomplete.
26
+func merge(userConf, imageConf *containertypes.Config) error {
27
+	if userConf.User == "" {
28
+		userConf.User = imageConf.User
29
+	}
30
+	if len(userConf.ExposedPorts) == 0 {
31
+		userConf.ExposedPorts = imageConf.ExposedPorts
32
+	} else if imageConf.ExposedPorts != nil {
33
+		if userConf.ExposedPorts == nil {
34
+			userConf.ExposedPorts = make(nat.PortSet)
35
+		}
36
+		for port := range imageConf.ExposedPorts {
37
+			if _, exists := userConf.ExposedPorts[port]; !exists {
38
+				userConf.ExposedPorts[port] = struct{}{}
39
+			}
40
+		}
41
+	}
42
+
43
+	if len(userConf.Env) == 0 {
44
+		userConf.Env = imageConf.Env
45
+	} else {
46
+		for _, imageEnv := range imageConf.Env {
47
+			found := false
48
+			imageEnvKey := strings.Split(imageEnv, "=")[0]
49
+			for _, userEnv := range userConf.Env {
50
+				userEnvKey := strings.Split(userEnv, "=")[0]
51
+				if imageEnvKey == userEnvKey {
52
+					found = true
53
+					break
54
+				}
55
+			}
56
+			if !found {
57
+				userConf.Env = append(userConf.Env, imageEnv)
58
+			}
59
+		}
60
+	}
61
+
62
+	if userConf.Labels == nil {
63
+		userConf.Labels = map[string]string{}
64
+	}
65
+	if imageConf.Labels != nil {
66
+		for l := range userConf.Labels {
67
+			imageConf.Labels[l] = userConf.Labels[l]
68
+		}
69
+		userConf.Labels = imageConf.Labels
70
+	}
71
+
72
+	if userConf.Entrypoint.Len() == 0 {
73
+		if userConf.Cmd.Len() == 0 {
74
+			userConf.Cmd = imageConf.Cmd
75
+		}
76
+
77
+		if userConf.Entrypoint == nil {
78
+			userConf.Entrypoint = imageConf.Entrypoint
79
+		}
80
+	}
81
+	if userConf.WorkingDir == "" {
82
+		userConf.WorkingDir = imageConf.WorkingDir
83
+	}
84
+	if len(userConf.Volumes) == 0 {
85
+		userConf.Volumes = imageConf.Volumes
86
+	} else {
87
+		for k, v := range imageConf.Volumes {
88
+			userConf.Volumes[k] = v
89
+		}
90
+	}
91
+	return nil
92
+}
93
+
21 94
 // Commit creates a new filesystem image from the current state of a container.
22 95
 // The image can optionally be tagged into a repository.
23 96
 func (daemon *Daemon) Commit(name string, c *types.ContainerCommitConfig) (string, error) {
... ...
@@ -37,7 +111,7 @@ func (daemon *Daemon) Commit(name string, c *types.ContainerCommitConfig) (strin
37 37
 	}
38 38
 
39 39
 	if c.MergeConfigs {
40
-		if err := runconfig.Merge(c.Config, container.Config); err != nil {
40
+		if err := merge(c.Config, container.Config); err != nil {
41 41
 			return "", err
42 42
 		}
43 43
 	}
... ...
@@ -416,7 +416,7 @@ func (daemon *Daemon) restore() error {
416 416
 
417 417
 func (daemon *Daemon) mergeAndVerifyConfig(config *containertypes.Config, img *image.Image) error {
418 418
 	if img != nil && img.Config != nil {
419
-		if err := runconfig.Merge(config, img.Config); err != nil {
419
+		if err := merge(config, img.Config); err != nil {
420 420
 			return err
421 421
 		}
422 422
 	}
... ...
@@ -15,6 +15,7 @@ import (
15 15
 	volumedrivers "github.com/docker/docker/volume/drivers"
16 16
 	"github.com/docker/docker/volume/local"
17 17
 	"github.com/docker/docker/volume/store"
18
+	"github.com/docker/go-connections/nat"
18 19
 )
19 20
 
20 21
 //
... ...
@@ -310,3 +311,84 @@ func TestContainerInitDNS(t *testing.T) {
310 310
 		t.Fatal("Expected container DNSOptions to not be nil")
311 311
 	}
312 312
 }
313
+
314
+func newPortNoError(proto, port string) nat.Port {
315
+	p, _ := nat.NewPort(proto, port)
316
+	return p
317
+}
318
+
319
+func TestMerge(t *testing.T) {
320
+	volumesImage := make(map[string]struct{})
321
+	volumesImage["/test1"] = struct{}{}
322
+	volumesImage["/test2"] = struct{}{}
323
+	portsImage := make(nat.PortSet)
324
+	portsImage[newPortNoError("tcp", "1111")] = struct{}{}
325
+	portsImage[newPortNoError("tcp", "2222")] = struct{}{}
326
+	configImage := &containertypes.Config{
327
+		ExposedPorts: portsImage,
328
+		Env:          []string{"VAR1=1", "VAR2=2"},
329
+		Volumes:      volumesImage,
330
+	}
331
+
332
+	portsUser := make(nat.PortSet)
333
+	portsUser[newPortNoError("tcp", "2222")] = struct{}{}
334
+	portsUser[newPortNoError("tcp", "3333")] = struct{}{}
335
+	volumesUser := make(map[string]struct{})
336
+	volumesUser["/test3"] = struct{}{}
337
+	configUser := &containertypes.Config{
338
+		ExposedPorts: portsUser,
339
+		Env:          []string{"VAR2=3", "VAR3=3"},
340
+		Volumes:      volumesUser,
341
+	}
342
+
343
+	if err := merge(configUser, configImage); err != nil {
344
+		t.Error(err)
345
+	}
346
+
347
+	if len(configUser.ExposedPorts) != 3 {
348
+		t.Fatalf("Expected 3 ExposedPorts, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
349
+	}
350
+	for portSpecs := range configUser.ExposedPorts {
351
+		if portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
352
+			t.Fatalf("Expected 1111 or 2222 or 3333, found %s", portSpecs)
353
+		}
354
+	}
355
+	if len(configUser.Env) != 3 {
356
+		t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env))
357
+	}
358
+	for _, env := range configUser.Env {
359
+		if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" {
360
+			t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env)
361
+		}
362
+	}
363
+
364
+	if len(configUser.Volumes) != 3 {
365
+		t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes))
366
+	}
367
+	for v := range configUser.Volumes {
368
+		if v != "/test1" && v != "/test2" && v != "/test3" {
369
+			t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v)
370
+		}
371
+	}
372
+
373
+	ports, _, err := nat.ParsePortSpecs([]string{"0000"})
374
+	if err != nil {
375
+		t.Error(err)
376
+	}
377
+	configImage2 := &containertypes.Config{
378
+		ExposedPorts: ports,
379
+	}
380
+
381
+	if err := merge(configUser, configImage2); err != nil {
382
+		t.Error(err)
383
+	}
384
+
385
+	if len(configUser.ExposedPorts) != 4 {
386
+		t.Fatalf("Expected 4 ExposedPorts, 0000, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
387
+	}
388
+	for portSpecs := range configUser.ExposedPorts {
389
+		if portSpecs.Port() != "0" && portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
390
+			t.Fatalf("Expected %q or %q or %q or %q, found %s", 0, 1111, 2222, 3333, portSpecs)
391
+		}
392
+	}
393
+}
313 394
deleted file mode 100644
... ...
@@ -1,81 +0,0 @@
1
-package runconfig
2
-
3
-import (
4
-	"strings"
5
-
6
-	"github.com/docker/docker/api/types/container"
7
-	"github.com/docker/go-connections/nat"
8
-)
9
-
10
-// Merge merges two Config, the image container configuration (defaults values),
11
-// and the user container configuration, either passed by the API or generated
12
-// by the cli.
13
-// It will mutate the specified user configuration (userConf) with the image
14
-// configuration where the user configuration is incomplete.
15
-func Merge(userConf, imageConf *container.Config) error {
16
-	if userConf.User == "" {
17
-		userConf.User = imageConf.User
18
-	}
19
-	if len(userConf.ExposedPorts) == 0 {
20
-		userConf.ExposedPorts = imageConf.ExposedPorts
21
-	} else if imageConf.ExposedPorts != nil {
22
-		if userConf.ExposedPorts == nil {
23
-			userConf.ExposedPorts = make(nat.PortSet)
24
-		}
25
-		for port := range imageConf.ExposedPorts {
26
-			if _, exists := userConf.ExposedPorts[port]; !exists {
27
-				userConf.ExposedPorts[port] = struct{}{}
28
-			}
29
-		}
30
-	}
31
-
32
-	if len(userConf.Env) == 0 {
33
-		userConf.Env = imageConf.Env
34
-	} else {
35
-		for _, imageEnv := range imageConf.Env {
36
-			found := false
37
-			imageEnvKey := strings.Split(imageEnv, "=")[0]
38
-			for _, userEnv := range userConf.Env {
39
-				userEnvKey := strings.Split(userEnv, "=")[0]
40
-				if imageEnvKey == userEnvKey {
41
-					found = true
42
-					break
43
-				}
44
-			}
45
-			if !found {
46
-				userConf.Env = append(userConf.Env, imageEnv)
47
-			}
48
-		}
49
-	}
50
-
51
-	if userConf.Labels == nil {
52
-		userConf.Labels = map[string]string{}
53
-	}
54
-	if imageConf.Labels != nil {
55
-		for l := range userConf.Labels {
56
-			imageConf.Labels[l] = userConf.Labels[l]
57
-		}
58
-		userConf.Labels = imageConf.Labels
59
-	}
60
-
61
-	if userConf.Entrypoint.Len() == 0 {
62
-		if userConf.Cmd.Len() == 0 {
63
-			userConf.Cmd = imageConf.Cmd
64
-		}
65
-
66
-		if userConf.Entrypoint == nil {
67
-			userConf.Entrypoint = imageConf.Entrypoint
68
-		}
69
-	}
70
-	if userConf.WorkingDir == "" {
71
-		userConf.WorkingDir = imageConf.WorkingDir
72
-	}
73
-	if len(userConf.Volumes) == 0 {
74
-		userConf.Volumes = imageConf.Volumes
75
-	} else {
76
-		for k, v := range imageConf.Volumes {
77
-			userConf.Volumes[k] = v
78
-		}
79
-	}
80
-	return nil
81
-}
82 1
deleted file mode 100644
... ...
@@ -1,84 +0,0 @@
1
-package runconfig
2
-
3
-import (
4
-	"testing"
5
-
6
-	"github.com/docker/docker/api/types/container"
7
-	"github.com/docker/go-connections/nat"
8
-)
9
-
10
-func TestMerge(t *testing.T) {
11
-	volumesImage := make(map[string]struct{})
12
-	volumesImage["/test1"] = struct{}{}
13
-	volumesImage["/test2"] = struct{}{}
14
-	portsImage := make(nat.PortSet)
15
-	portsImage[newPortNoError("tcp", "1111")] = struct{}{}
16
-	portsImage[newPortNoError("tcp", "2222")] = struct{}{}
17
-	configImage := &container.Config{
18
-		ExposedPorts: portsImage,
19
-		Env:          []string{"VAR1=1", "VAR2=2"},
20
-		Volumes:      volumesImage,
21
-	}
22
-
23
-	portsUser := make(nat.PortSet)
24
-	portsUser[newPortNoError("tcp", "2222")] = struct{}{}
25
-	portsUser[newPortNoError("tcp", "3333")] = struct{}{}
26
-	volumesUser := make(map[string]struct{})
27
-	volumesUser["/test3"] = struct{}{}
28
-	configUser := &container.Config{
29
-		ExposedPorts: portsUser,
30
-		Env:          []string{"VAR2=3", "VAR3=3"},
31
-		Volumes:      volumesUser,
32
-	}
33
-
34
-	if err := Merge(configUser, configImage); err != nil {
35
-		t.Error(err)
36
-	}
37
-
38
-	if len(configUser.ExposedPorts) != 3 {
39
-		t.Fatalf("Expected 3 ExposedPorts, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
40
-	}
41
-	for portSpecs := range configUser.ExposedPorts {
42
-		if portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
43
-			t.Fatalf("Expected 1111 or 2222 or 3333, found %s", portSpecs)
44
-		}
45
-	}
46
-	if len(configUser.Env) != 3 {
47
-		t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env))
48
-	}
49
-	for _, env := range configUser.Env {
50
-		if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" {
51
-			t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env)
52
-		}
53
-	}
54
-
55
-	if len(configUser.Volumes) != 3 {
56
-		t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes))
57
-	}
58
-	for v := range configUser.Volumes {
59
-		if v != "/test1" && v != "/test2" && v != "/test3" {
60
-			t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v)
61
-		}
62
-	}
63
-
64
-	ports, _, err := nat.ParsePortSpecs([]string{"0000"})
65
-	if err != nil {
66
-		t.Error(err)
67
-	}
68
-	configImage2 := &container.Config{
69
-		ExposedPorts: ports,
70
-	}
71
-
72
-	if err := Merge(configUser, configImage2); err != nil {
73
-		t.Error(err)
74
-	}
75
-
76
-	if len(configUser.ExposedPorts) != 4 {
77
-		t.Fatalf("Expected 4 ExposedPorts, 0000, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
78
-	}
79
-	for portSpecs := range configUser.ExposedPorts {
80
-		if portSpecs.Port() != "0" && portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
81
-			t.Fatalf("Expected %q or %q or %q or %q, found %s", 0, 1111, 2222, 3333, portSpecs)
82
-		}
83
-	}
84
-}