Skip to content

Commit 24ad2f4

Browse files
committed
Add (hidden) flags to set containerd namespaces
This allows our tests, which all share a containerd instance, to be a bit more isolated by setting the containerd namespaces to the generated daemon ID's rather than the default namespaces. This came about because I found in some cases we had test daemons failing to start (really very slow to start) because it was (seemingly) processing events from other tests. Signed-off-by: Brian Goff <[email protected]>
1 parent 2fc3480 commit 24ad2f4

7 files changed

Lines changed: 32 additions & 11 deletions

File tree

cmd/dockerd/config.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package main
33
import (
44
"runtime"
55

6+
"github.com/docker/docker/daemon"
67
"github.com/docker/docker/daemon/config"
78
"github.com/docker/docker/opts"
9+
"github.com/docker/docker/plugin/executor/containerd"
810
"github.com/docker/docker/registry"
911
"github.com/spf13/pflag"
1012
)
@@ -85,7 +87,13 @@ func installCommonConfigFlags(conf *config.Config, flags *pflag.FlagSet) error {
8587

8688
conf.MaxConcurrentDownloads = &maxConcurrentDownloads
8789
conf.MaxConcurrentUploads = &maxConcurrentUploads
88-
return nil
90+
91+
flags.StringVar(&conf.ContainerdNamespace, "containerd-namespace", daemon.ContainersNamespace, "Containerd namespace to use")
92+
if err := flags.MarkHidden("containerd-namespace"); err != nil {
93+
return err
94+
}
95+
flags.StringVar(&conf.ContainerdPluginNamespace, "containerd-plugins-namespace", containerd.PluginNamespace, "Containerd namespace to use for plugins")
96+
return flags.MarkHidden("containerd-plugins-namespace")
8997
}
9098

9199
func installRegistryServiceFlags(options *registry.ServiceOptions, flags *pflag.FlagSet) {

daemon/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ type CommonConfig struct {
230230
Features map[string]bool `json:"features,omitempty"`
231231

232232
Builder BuilderConfig `json:"builder,omitempty"`
233+
234+
ContainerdNamespace string `json:"containerd-namespace,omitempty"`
235+
ContainerdPluginNamespace string `json:"containerd-plugin-namespace,omitempty"`
233236
}
234237

235238
// IsValueSet returns true if a configuration value

daemon/daemon.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ func NewDaemon(ctx context.Context, config *config.Config, pluginStore *plugin.S
888888
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(defaults.DefaultMaxSendMsgSize)),
889889
}
890890
if config.ContainerdAddr != "" {
891-
d.containerdCli, err = containerd.New(config.ContainerdAddr, containerd.WithDefaultNamespace(ContainersNamespace), containerd.WithDialOpts(gopts), containerd.WithTimeout(60*time.Second))
891+
d.containerdCli, err = containerd.New(config.ContainerdAddr, containerd.WithDefaultNamespace(config.ContainerdNamespace), containerd.WithDialOpts(gopts), containerd.WithTimeout(60*time.Second))
892892
if err != nil {
893893
return nil, errors.Wrapf(err, "failed to dial %q", config.ContainerdAddr)
894894
}
@@ -900,13 +900,13 @@ func NewDaemon(ctx context.Context, config *config.Config, pluginStore *plugin.S
900900
// Windows is not currently using containerd, keep the
901901
// client as nil
902902
if config.ContainerdAddr != "" {
903-
pluginCli, err = containerd.New(config.ContainerdAddr, containerd.WithDefaultNamespace(pluginexec.PluginNamespace), containerd.WithDialOpts(gopts), containerd.WithTimeout(60*time.Second))
903+
pluginCli, err = containerd.New(config.ContainerdAddr, containerd.WithDefaultNamespace(config.ContainerdPluginNamespace), containerd.WithDialOpts(gopts), containerd.WithTimeout(60*time.Second))
904904
if err != nil {
905905
return nil, errors.Wrapf(err, "failed to dial %q", config.ContainerdAddr)
906906
}
907907
}
908908

909-
return pluginexec.New(ctx, getPluginExecRoot(config.Root), pluginCli, m)
909+
return pluginexec.New(ctx, getPluginExecRoot(config.Root), pluginCli, config.ContainerdPluginNamespace, m)
910910
}
911911

912912
// Plugin system initialization should happen before restore. Do not change order.
@@ -1054,7 +1054,7 @@ func NewDaemon(ctx context.Context, config *config.Config, pluginStore *plugin.S
10541054

10551055
go d.execCommandGC()
10561056

1057-
d.containerd, err = libcontainerd.NewClient(ctx, d.containerdCli, filepath.Join(config.ExecRoot, "containerd"), ContainersNamespace, d)
1057+
d.containerd, err = libcontainerd.NewClient(ctx, d.containerdCli, filepath.Join(config.ExecRoot, "containerd"), config.ContainerdNamespace, d)
10581058
if err != nil {
10591059
return nil, err
10601060
}

daemon/daemon_unix.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,9 @@ func (daemon *Daemon) initRuntimes(runtimes map[string]types.Runtime) (err error
757757

758758
// verifyDaemonSettings performs validation of daemon config struct
759759
func verifyDaemonSettings(conf *config.Config) error {
760+
if conf.ContainerdNamespace == conf.ContainerdPluginNamespace {
761+
return errors.New("containers namespace and plugins namespace cannot be the same")
762+
}
760763
// Check for mutually incompatible config options
761764
if conf.BridgeConfig.Iface != "" && conf.BridgeConfig.IP != "" {
762765
return fmt.Errorf("You specified -b & --bip, mutually exclusive options. Please specify only one")

integration-cli/docker_cli_daemon_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525

2626
"github.com/cloudflare/cfssl/helpers"
2727
"github.com/docker/docker/api/types"
28-
moby_daemon "github.com/docker/docker/daemon"
2928
"github.com/docker/docker/integration-cli/checker"
3029
"github.com/docker/docker/integration-cli/cli"
3130
"github.com/docker/docker/integration-cli/cli/build"
@@ -1457,7 +1456,7 @@ func (s *DockerDaemonSuite) TestCleanupMountsAfterDaemonAndContainerKill(c *chec
14571456

14581457
// kill the container
14591458
icmd.RunCommand(ctrBinary, "--address", containerdSocket,
1460-
"--namespace", moby_daemon.ContainersNamespace, "tasks", "kill", id).Assert(c, icmd.Success)
1459+
"--namespace", d.ContainersNamespace(), "tasks", "kill", id).Assert(c, icmd.Success)
14611460

14621461
// restart daemon.
14631462
d.Restart(c)
@@ -1977,7 +1976,7 @@ func (s *DockerDaemonSuite) TestDaemonRestartWithKilledRunningContainer(t *check
19771976

19781977
// kill the container
19791978
icmd.RunCommand(ctrBinary, "--address", containerdSocket,
1980-
"--namespace", moby_daemon.ContainersNamespace, "tasks", "kill", cid).Assert(t, icmd.Success)
1979+
"--namespace", s.d.ContainersNamespace(), "tasks", "kill", cid).Assert(t, icmd.Success)
19811980

19821981
// Give time to containerd to process the command if we don't
19831982
// the exit event might be received after we do the inspect
@@ -2080,7 +2079,7 @@ func (s *DockerDaemonSuite) TestDaemonRestartWithUnpausedRunningContainer(t *che
20802079
result := icmd.RunCommand(
20812080
ctrBinary,
20822081
"--address", containerdSocket,
2083-
"--namespace", moby_daemon.ContainersNamespace,
2082+
"--namespace", s.d.ContainersNamespace(),
20842083
"tasks", "resume", cid)
20852084
result.Assert(t, icmd.Success)
20862085

internal/test/daemon/daemon.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ func New(t testingT, ops ...func(*Daemon)) *Daemon {
137137
return d
138138
}
139139

140+
// ContainersNamespace returns the containerd namespace used for containers.
141+
func (d *Daemon) ContainersNamespace() string {
142+
return d.id
143+
}
144+
140145
// RootDir returns the root directory of the daemon.
141146
func (d *Daemon) RootDir() string {
142147
return d.Root
@@ -226,12 +231,15 @@ func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
226231
if err != nil {
227232
return errors.Wrapf(err, "[%s] could not find docker binary in $PATH", d.id)
228233
}
234+
229235
args := append(d.GlobalFlags,
230236
"--containerd", containerdSocket,
231237
"--data-root", d.Root,
232238
"--exec-root", d.execRoot,
233239
"--pidfile", fmt.Sprintf("%s/docker.pid", d.Folder),
234240
fmt.Sprintf("--userland-proxy=%t", d.userlandProxy),
241+
"--containerd-namespace", d.id,
242+
"--containerd-plugins-namespace", d.id+"p",
235243
)
236244
if d.defaultCgroupNamespaceMode != "" {
237245
args = append(args, []string{"--default-cgroupns-mode", d.defaultCgroupNamespaceMode}...)

plugin/executor/containerd/containerd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ type ExitHandler interface {
2626
}
2727

2828
// New creates a new containerd plugin executor
29-
func New(ctx context.Context, rootDir string, cli *containerd.Client, exitHandler ExitHandler) (*Executor, error) {
29+
func New(ctx context.Context, rootDir string, cli *containerd.Client, ns string, exitHandler ExitHandler) (*Executor, error) {
3030
e := &Executor{
3131
rootDir: rootDir,
3232
exitHandler: exitHandler,
3333
}
3434

35-
client, err := libcontainerd.NewClient(ctx, cli, rootDir, PluginNamespace, e)
35+
client, err := libcontainerd.NewClient(ctx, cli, rootDir, ns, e)
3636
if err != nil {
3737
return nil, errors.Wrap(err, "error creating containerd exec client")
3838
}

0 commit comments

Comments
 (0)