Skip to content

Commit d222bf0

Browse files
committed
daemon: reload runtimes w/o breaking containers
The existing runtimes reload logic went to great lengths to replace the directory containing runtime wrapper scripts as atomically as possible within the limitations of the Linux filesystem ABI. Trouble is, atomically swapping the wrapper scripts directory solves the wrong problem! The runtime configuration is "locked in" when a container is started, including the path to the runC binary. If a container is started with a runtime which requires a daemon-managed wrapper script and then the daemon is reloaded with a config which no longer requires the wrapper script (i.e. some args -> no args, or the runtime is dropped from the config), that container would become unmanageable. Any attempts to stop, exec or otherwise perform lifecycle management operations on the container are likely to fail due to the wrapper script no longer existing at its original path. Atomically swapping the wrapper scripts is also incompatible with the read-copy-update paradigm for reloading configuration. A handler in the daemon could retain a reference to the pre-reload configuration for an indeterminate amount of time after the daemon configuration has been reloaded and updated. It is possible for the daemon to attempt to start a container using a deleted wrapper script if a request to run a container races a reload. Solve the problem of deleting referenced wrapper scripts by ensuring that all wrapper scripts are *immutable* for the lifetime of the daemon process. Any given runtime wrapper script must always exist with the same contents, no matter how many times the daemon config is reloaded, or what changes are made to the config. This is accomplished by using everyone's favourite design pattern: content-addressable storage. Each wrapper script file name is suffixed with the SHA-256 digest of its contents to (probabilistically) guarantee immutability without needing any concurrency control. Stale runtime wrapper scripts are only cleaned up on the next daemon restart. Split the derived runtimes configuration from the user-supplied configuration to have a place to store derived state without mutating the user-supplied configuration or exposing daemon internals in API struct types. Hold the derived state and the user-supplied configuration in a single struct value so that they can be updated as an atomic unit. Signed-off-by: Cory Snider <[email protected]>
1 parent 0b59246 commit d222bf0

40 files changed

Lines changed: 505 additions & 382 deletions

api/types/types.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/docker/docker/api/types/swarm"
1717
"github.com/docker/docker/api/types/volume"
1818
"github.com/docker/go-connections/nat"
19-
"github.com/opencontainers/runtime-spec/specs-go/features"
2019
)
2120

2221
const (
@@ -657,16 +656,6 @@ type Runtime struct {
657656

658657
Type string `json:"runtimeType,omitempty"`
659658
Options map[string]interface{} `json:"options,omitempty"`
660-
661-
// This is exposed here only for internal use
662-
ShimConfig *ShimConfig `json:"-"`
663-
Features *features.Features `json:"-"`
664-
}
665-
666-
// ShimConfig is used by runtime to configure containerd shims
667-
type ShimConfig struct {
668-
Binary string
669-
Opts interface{}
670659
}
671660

672661
// DiskUsageObject represents an object type used for disk usage query filtering.

cmd/dockerd/daemon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
268268
// Restart all autostart containers which has a swarm endpoint
269269
// and is not yet running now that we have successfully
270270
// initialized the cluster.
271-
d.RestartSwarmContainers(cli.Config)
271+
d.RestartSwarmContainers()
272272

273273
logrus.Info("Daemon has completed initialization")
274274

daemon/config/config_linux.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,6 @@ type Config struct {
8181
Rootless bool `json:"rootless,omitempty"`
8282
}
8383

84-
// GetRuntime returns the runtime path and arguments for a given
85-
// runtime name
86-
func (conf *Config) GetRuntime(name string) *types.Runtime {
87-
if rt, ok := conf.Runtimes[name]; ok {
88-
return &rt
89-
}
90-
return nil
91-
}
92-
9384
// GetAllRuntimes returns a copy of the runtimes map
9485
func (conf *Config) GetAllRuntimes() map[string]types.Runtime {
9586
return conf.Runtimes

daemon/config/config_windows.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ type Config struct {
3030
// for the Windows daemon.)
3131
}
3232

33-
// GetRuntime returns the runtime path and arguments for a given
34-
// runtime name
35-
func (conf *Config) GetRuntime(name string) *types.Runtime {
36-
return nil
37-
}
38-
3933
// GetAllRuntimes returns a copy of the runtimes map
4034
func (conf *Config) GetAllRuntimes() map[string]types.Runtime {
4135
return map[string]types.Runtime{}

daemon/container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func (daemon *Daemon) setHostConfig(container *container.Container, hostConfig *
235235

236236
// verifyContainerSettings performs validation of the hostconfig and config
237237
// structures.
238-
func (daemon *Daemon) verifyContainerSettings(daemonCfg *config.Config, hostConfig *containertypes.HostConfig, config *containertypes.Config, update bool) (warnings []string, err error) {
238+
func (daemon *Daemon) verifyContainerSettings(daemonCfg *configStore, hostConfig *containertypes.HostConfig, config *containertypes.Config, update bool) (warnings []string, err error) {
239239
// First perform verification of settings common across all platforms.
240240
if err = validateContainerConfig(config); err != nil {
241241
return warnings, err

daemon/container_operations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName
10751075
}
10761076
}
10771077
} else {
1078-
if err := daemon.connectToNetwork(daemon.config(), container, idOrName, endpointConfig, true); err != nil {
1078+
if err := daemon.connectToNetwork(&daemon.config().Config, container, idOrName, endpointConfig, true); err != nil {
10791079
return err
10801080
}
10811081
}

daemon/container_unix_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ func TestContainerWarningHostAndPublishPorts(t *testing.T) {
3131
NetworkMode: "host",
3232
PortBindings: tc.ports,
3333
}
34-
cs := &config.Config{}
35-
configureRuntimes(cs)
3634
d := &Daemon{}
37-
d.configStore.Store(cs)
38-
wrns, err := d.verifyContainerSettings(cs, hostConfig, &containertypes.Config{}, false)
35+
cfg, err := config.New()
36+
assert.NilError(t, err)
37+
configureRuntimes(cfg)
38+
runtimes, err := setupRuntimes(cfg)
39+
assert.NilError(t, err)
40+
daemonCfg := &configStore{Config: *cfg, Runtimes: runtimes}
41+
wrns, err := d.verifyContainerSettings(daemonCfg, hostConfig, &containertypes.Config{}, false)
3942
assert.NilError(t, err)
4043
assert.DeepEqual(t, tc.warnings, wrns)
4144
}

daemon/create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (daemon *Daemon) ContainerCreateIgnoreImagesArgsEscaped(ctx context.Context
5757
})
5858
}
5959

60-
func (daemon *Daemon) containerCreate(ctx context.Context, daemonCfg *config.Config, opts createOpts) (containertypes.CreateResponse, error) {
60+
func (daemon *Daemon) containerCreate(ctx context.Context, daemonCfg *configStore, opts createOpts) (containertypes.CreateResponse, error) {
6161
start := time.Now()
6262
if opts.params.Config == nil {
6363
return containertypes.CreateResponse{}, errdefs.InvalidParameter(errors.New("Config cannot be empty in order to create a container"))
@@ -95,12 +95,12 @@ func (daemon *Daemon) containerCreate(ctx context.Context, daemonCfg *config.Con
9595
if opts.params.HostConfig == nil {
9696
opts.params.HostConfig = &containertypes.HostConfig{}
9797
}
98-
err = daemon.adaptContainerSettings(daemonCfg, opts.params.HostConfig, opts.params.AdjustCPUShares)
98+
err = daemon.adaptContainerSettings(&daemonCfg.Config, opts.params.HostConfig, opts.params.AdjustCPUShares)
9999
if err != nil {
100100
return containertypes.CreateResponse{Warnings: warnings}, errdefs.InvalidParameter(err)
101101
}
102102

103-
ctr, err := daemon.create(ctx, daemonCfg, opts)
103+
ctr, err := daemon.create(ctx, &daemonCfg.Config, opts)
104104
if err != nil {
105105
return containertypes.CreateResponse{Warnings: warnings}, err
106106
}

0 commit comments

Comments
 (0)