Skip to content

Commit 767df67

Browse files
committed
Decode container configurations into typed structures.
Signed-off-by: David Calavera <david.calavera@gmail.com>
1 parent 002afbb commit 767df67

23 files changed

+486
-260
lines changed

api/server/server.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/docker/docker/pkg/streamformatter"
3434
"github.com/docker/docker/pkg/version"
3535
"github.com/docker/docker/registry"
36+
"github.com/docker/docker/runconfig"
3637
"github.com/docker/docker/utils"
3738
)
3839

@@ -811,14 +812,14 @@ func postContainersCreate(eng *engine.Engine, version version.Version, w http.Re
811812
var (
812813
warnings []string
813814
name = r.Form.Get("name")
814-
env = new(engine.Env)
815815
)
816816

817-
if err := env.Decode(r.Body); err != nil {
817+
config, hostConfig, err := runconfig.DecodeContainerConfig(r.Body)
818+
if err != nil {
818819
return err
819820
}
820821

821-
containerId, warnings, err := getDaemon(eng).ContainerCreate(name, env)
822+
containerId, warnings, err := getDaemon(eng).ContainerCreate(name, config, hostConfig)
822823
if err != nil {
823824
return err
824825
}
@@ -917,28 +918,28 @@ func postContainersStart(eng *engine.Engine, version version.Version, w http.Res
917918
if vars == nil {
918919
return fmt.Errorf("Missing parameter")
919920
}
920-
var (
921-
name = vars["name"]
922-
env = new(engine.Env)
923-
)
924921

925922
// If contentLength is -1, we can assumed chunked encoding
926923
// or more technically that the length is unknown
927924
// https://golang.org/src/pkg/net/http/request.go#L139
928925
// net/http otherwise seems to swallow any headers related to chunked encoding
929926
// including r.TransferEncoding
930927
// allow a nil body for backwards compatibility
928+
var hostConfig *runconfig.HostConfig
931929
if r.Body != nil && (r.ContentLength > 0 || r.ContentLength == -1) {
932930
if err := checkForJson(r); err != nil {
933931
return err
934932
}
935933

936-
if err := env.Decode(r.Body); err != nil {
934+
c, err := runconfig.DecodeHostConfig(r.Body)
935+
if err != nil {
937936
return err
938937
}
938+
939+
hostConfig = c
939940
}
940941

941-
if err := getDaemon(eng).ContainerStart(name, env); err != nil {
942+
if err := getDaemon(eng).ContainerStart(vars["name"], hostConfig); err != nil {
942943
if err.Error() == "Container already started" {
943944
w.WriteHeader(http.StatusNotModified)
944945
return nil

builder/dispatchers.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func run(b *Builder, args []string, attributes map[string]bool, original string)
262262
b.Config.Cmd = config.Cmd
263263
runconfig.Merge(b.Config, config)
264264

265-
defer func(cmd []string) { b.Config.Cmd = cmd }(cmd)
265+
defer func(cmd *runconfig.Command) { b.Config.Cmd = cmd }(cmd)
266266

267267
logrus.Debugf("[BUILDER] Command to be executed: %v", b.Config.Cmd)
268268

@@ -301,13 +301,15 @@ func run(b *Builder, args []string, attributes map[string]bool, original string)
301301
// Argument handling is the same as RUN.
302302
//
303303
func cmd(b *Builder, args []string, attributes map[string]bool, original string) error {
304-
b.Config.Cmd = handleJsonArgs(args, attributes)
304+
cmdSlice := handleJsonArgs(args, attributes)
305305

306306
if !attributes["json"] {
307-
b.Config.Cmd = append([]string{"/bin/sh", "-c"}, b.Config.Cmd...)
307+
cmdSlice = append([]string{"/bin/sh", "-c"}, cmdSlice...)
308308
}
309309

310-
if err := b.commit("", b.Config.Cmd, fmt.Sprintf("CMD %q", b.Config.Cmd)); err != nil {
310+
b.Config.Cmd = runconfig.NewCommand(cmdSlice...)
311+
312+
if err := b.commit("", b.Config.Cmd, fmt.Sprintf("CMD %q", cmdSlice)); err != nil {
311313
return err
312314
}
313315

@@ -332,13 +334,13 @@ func entrypoint(b *Builder, args []string, attributes map[string]bool, original
332334
switch {
333335
case attributes["json"]:
334336
// ENTRYPOINT ["echo", "hi"]
335-
b.Config.Entrypoint = parsed
337+
b.Config.Entrypoint = runconfig.NewEntrypoint(parsed...)
336338
case len(parsed) == 0:
337339
// ENTRYPOINT []
338340
b.Config.Entrypoint = nil
339341
default:
340342
// ENTRYPOINT echo hi
341-
b.Config.Entrypoint = []string{"/bin/sh", "-c", parsed[0]}
343+
b.Config.Entrypoint = runconfig.NewEntrypoint("/bin/sh", "-c", parsed[0])
342344
}
343345

344346
// when setting the entrypoint if a CMD was not explicitly set then

builder/internals.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (b *Builder) readContext(context io.Reader) error {
6161
return nil
6262
}
6363

64-
func (b *Builder) commit(id string, autoCmd []string, comment string) error {
64+
func (b *Builder) commit(id string, autoCmd *runconfig.Command, comment string) error {
6565
if b.disableCommit {
6666
return nil
6767
}
@@ -71,8 +71,8 @@ func (b *Builder) commit(id string, autoCmd []string, comment string) error {
7171
b.Config.Image = b.image
7272
if id == "" {
7373
cmd := b.Config.Cmd
74-
b.Config.Cmd = []string{"/bin/sh", "-c", "#(nop) " + comment}
75-
defer func(cmd []string) { b.Config.Cmd = cmd }(cmd)
74+
b.Config.Cmd = runconfig.NewCommand("/bin/sh", "-c", "#(nop) "+comment)
75+
defer func(cmd *runconfig.Command) { b.Config.Cmd = cmd }(cmd)
7676

7777
hit, err := b.probeCache()
7878
if err != nil {
@@ -182,8 +182,8 @@ func (b *Builder) runContextCommand(args []string, allowRemote bool, allowDecomp
182182
}
183183

184184
cmd := b.Config.Cmd
185-
b.Config.Cmd = []string{"/bin/sh", "-c", fmt.Sprintf("#(nop) %s %s in %s", cmdName, srcHash, dest)}
186-
defer func(cmd []string) { b.Config.Cmd = cmd }(cmd)
185+
b.Config.Cmd = runconfig.NewCommand("/bin/sh", "-c", fmt.Sprintf("#(nop) %s %s in %s", cmdName, srcHash, dest))
186+
defer func(cmd *runconfig.Command) { b.Config.Cmd = cmd }(cmd)
187187

188188
hit, err := b.probeCache()
189189
if err != nil {
@@ -559,12 +559,13 @@ func (b *Builder) create() (*daemon.Container, error) {
559559
b.TmpContainers[c.ID] = struct{}{}
560560
fmt.Fprintf(b.OutStream, " ---> Running in %s\n", stringid.TruncateID(c.ID))
561561

562-
if len(config.Cmd) > 0 {
562+
if config.Cmd.Len() > 0 {
563563
// override the entry point that may have been picked up from the base image
564-
c.Path = config.Cmd[0]
565-
c.Args = config.Cmd[1:]
564+
s := config.Cmd.Slice()
565+
c.Path = s[0]
566+
c.Args = s[1:]
566567
} else {
567-
config.Cmd = []string{}
568+
config.Cmd = runconfig.NewCommand()
568569
}
569570

570571
return c, nil

daemon/create.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@ import (
44
"fmt"
55
"strings"
66

7-
"github.com/docker/docker/engine"
87
"github.com/docker/docker/graph"
98
"github.com/docker/docker/image"
109
"github.com/docker/docker/pkg/parsers"
1110
"github.com/docker/docker/runconfig"
1211
"github.com/docker/libcontainer/label"
1312
)
1413

15-
func (daemon *Daemon) ContainerCreate(name string, env *engine.Env) (string, []string, error) {
14+
func (daemon *Daemon) ContainerCreate(name string, config *runconfig.Config, hostConfig *runconfig.HostConfig) (string, []string, error) {
1615
var warnings []string
1716

18-
config := runconfig.ContainerConfigFromJob(env)
19-
hostConfig := runconfig.ContainerHostConfigFromJob(env)
20-
21-
if len(hostConfig.LxcConf) > 0 && !strings.Contains(daemon.ExecutionDriver().Name(), "lxc") {
17+
if hostConfig.LxcConf.Len() > 0 && !strings.Contains(daemon.ExecutionDriver().Name(), "lxc") {
2218
return "", warnings, fmt.Errorf("Cannot use --lxc-conf with execdriver: %s", daemon.ExecutionDriver().Name())
2319
}
2420
if hostConfig.Memory != 0 && hostConfig.Memory < 4194304 {

daemon/daemon.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,8 @@ type Daemon struct {
119119
func (daemon *Daemon) Install(eng *engine.Engine) error {
120120
for name, method := range map[string]engine.Handler{
121121
"container_inspect": daemon.ContainerInspect,
122-
"container_stats": daemon.ContainerStats,
123-
"export": daemon.ContainerExport,
124122
"info": daemon.CmdInfo,
125123
"restart": daemon.ContainerRestart,
126-
"stop": daemon.ContainerStop,
127-
"wait": daemon.ContainerWait,
128124
"execCreate": daemon.ContainerExecCreate,
129125
"execStart": daemon.ContainerExecStart,
130126
} {
@@ -485,7 +481,7 @@ func (daemon *Daemon) mergeAndVerifyConfig(config *runconfig.Config, img *image.
485481
return nil, err
486482
}
487483
}
488-
if len(config.Entrypoint) == 0 && len(config.Cmd) == 0 {
484+
if config.Entrypoint.Len() == 0 && config.Cmd.Len() == 0 {
489485
return nil, fmt.Errorf("No command specified")
490486
}
491487
return warnings, nil
@@ -577,17 +573,20 @@ func (daemon *Daemon) generateHostname(id string, config *runconfig.Config) {
577573
}
578574
}
579575

580-
func (daemon *Daemon) getEntrypointAndArgs(configEntrypoint, configCmd []string) (string, []string) {
576+
func (daemon *Daemon) getEntrypointAndArgs(configEntrypoint *runconfig.Entrypoint, configCmd *runconfig.Command) (string, []string) {
581577
var (
582578
entrypoint string
583579
args []string
584580
)
585-
if len(configEntrypoint) != 0 {
586-
entrypoint = configEntrypoint[0]
587-
args = append(configEntrypoint[1:], configCmd...)
581+
582+
cmdSlice := configCmd.Slice()
583+
if configEntrypoint.Len() != 0 {
584+
eSlice := configEntrypoint.Slice()
585+
entrypoint = eSlice[0]
586+
args = append(eSlice[1:], cmdSlice...)
588587
} else {
589-
entrypoint = configCmd[0]
590-
args = configCmd[1:]
588+
entrypoint = cmdSlice[0]
589+
args = cmdSlice[1:]
591590
}
592591
return entrypoint, args
593592
}

daemon/exec.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ func (d *Daemon) ContainerExecCreate(job *engine.Job) error {
132132
return err
133133
}
134134

135-
entrypoint, args := d.getEntrypointAndArgs(nil, config.Cmd)
135+
cmd := runconfig.NewCommand(config.Cmd...)
136+
entrypoint, args := d.getEntrypointAndArgs(runconfig.NewEntrypoint(), cmd)
136137

137138
processConfig := execdriver.ProcessConfig{
138139
Tty: config.Tty,

daemon/start.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ package daemon
33
import (
44
"fmt"
55

6-
"github.com/docker/docker/engine"
76
"github.com/docker/docker/runconfig"
87
)
98

10-
func (daemon *Daemon) ContainerStart(name string, env *engine.Env) error {
9+
func (daemon *Daemon) ContainerStart(name string, hostConfig *runconfig.HostConfig) error {
1110
container, err := daemon.Get(name)
1211
if err != nil {
1312
return err
@@ -21,15 +20,14 @@ func (daemon *Daemon) ContainerStart(name string, env *engine.Env) error {
2120
return fmt.Errorf("Container already started")
2221
}
2322

24-
// If no environment was set, then no hostconfig was passed.
2523
// This is kept for backward compatibility - hostconfig should be passed when
2624
// creating a container, not during start.
27-
if len(env.Map()) > 0 {
28-
hostConfig := runconfig.ContainerHostConfigFromJob(env)
25+
if hostConfig != nil {
2926
if err := daemon.setHostConfig(container, hostConfig); err != nil {
3027
return err
3128
}
3229
}
30+
3331
if err := container.Start(); err != nil {
3432
container.LogEvent("die")
3533
return fmt.Errorf("Cannot start container %s: %s", name, err)

daemon/utils.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ func mergeLxcConfIntoOptions(hostConfig *runconfig.HostConfig) ([]string, error)
4242

4343
// merge in the lxc conf options into the generic config map
4444
if lxcConf := hostConfig.LxcConf; lxcConf != nil {
45-
for _, pair := range lxcConf {
45+
lxSlice := lxcConf.Slice()
46+
for _, pair := range lxSlice {
4647
// because lxc conf gets the driver name lxc.XXXX we need to trim it off
4748
// and let the lxc driver add it back later if needed
4849
if !strings.Contains(pair.Key, ".") {

daemon/utils_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
)
88

99
func TestMergeLxcConfig(t *testing.T) {
10+
kv := []runconfig.KeyValuePair{
11+
{"lxc.cgroups.cpuset", "1,2"},
12+
}
1013
hostConfig := &runconfig.HostConfig{
11-
LxcConf: []runconfig.KeyValuePair{
12-
{Key: "lxc.cgroups.cpuset", Value: "1,2"},
13-
},
14+
LxcConf: runconfig.NewLxcConfig(kv),
1415
}
1516

1617
out, err := mergeLxcConfIntoOptions(hostConfig)

graph/history.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (s *TagStore) History(name string) ([]*types.ImageHistory, error) {
3131
history = append(history, &types.ImageHistory{
3232
ID: img.ID,
3333
Created: img.Created.Unix(),
34-
CreatedBy: strings.Join(img.ContainerConfig.Cmd, " "),
34+
CreatedBy: strings.Join(img.ContainerConfig.Cmd.Slice(), " "),
3535
Tags: lookupMap[img.ID],
3636
Size: img.Size,
3737
Comment: img.Comment,

0 commit comments

Comments
 (0)