Skip to content

Commit ddae20c

Browse files
committed
Update libcontainerd to use containerd 1.0
Signed-off-by: Kenfe-Mickael Laventure <[email protected]>
1 parent 7acea2a commit ddae20c

File tree

113 files changed

+4556
-3962
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+4556
-3962
lines changed

api/server/router/container/exec.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (s *containerRouter) postContainerExecStart(ctx context.Context, w http.Res
126126
return err
127127
}
128128
stdout.Write([]byte(err.Error() + "\r\n"))
129-
logrus.Errorf("Error running exec in container: %v", err)
129+
logrus.Errorf("Error running exec %s in container: %v", execName, err)
130130
}
131131
return nil
132132
}

builder/dockerfile/containerbackend.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (c *containerManager) Run(ctx context.Context, cID string, stdout, stderr i
102102

103103
func logCancellationError(cancelErrCh chan error, msg string) {
104104
if cancelErr := <-cancelErrCh; cancelErr != nil {
105-
logrus.Debugf("Build cancelled (%v): ", cancelErr, msg)
105+
logrus.Debugf("Build cancelled (%v): %s", cancelErr, msg)
106106
}
107107
}
108108

cmd/dockerd/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ func installCommonConfigFlags(conf *config.Config, flags *pflag.FlagSet) {
2727
flags.Var(opts.NewNamedListOptsRef("exec-opts", &conf.ExecOptions, nil), "exec-opt", "Runtime execution options")
2828
flags.StringVarP(&conf.Pidfile, "pidfile", "p", defaultPidFile, "Path to use for daemon PID file")
2929
flags.StringVarP(&conf.Root, "graph", "g", defaultDataRoot, "Root of the Docker runtime")
30+
flags.StringVar(&conf.ExecRoot, "exec-root", defaultExecRoot, "Root directory for execution state files")
31+
flags.StringVar(&conf.ContainerdAddr, "containerd", "", "containerd grpc address")
3032

3133
// "--graph" is "soft-deprecated" in favor of "data-root". This flag was added
3234
// before Docker 1.0, so won't be removed, only hidden, to discourage its usage.

cmd/dockerd/config_unix.go

-2
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@ func installConfigFlags(conf *config.Config, flags *pflag.FlagSet) {
2929
flags.BoolVar(&conf.BridgeConfig.EnableIPForward, "ip-forward", true, "Enable net.ipv4.ip_forward")
3030
flags.BoolVar(&conf.BridgeConfig.EnableIPMasq, "ip-masq", true, "Enable IP masquerading")
3131
flags.BoolVar(&conf.BridgeConfig.EnableIPv6, "ipv6", false, "Enable IPv6 networking")
32-
flags.StringVar(&conf.ExecRoot, "exec-root", defaultExecRoot, "Root directory for execution state files")
3332
flags.StringVar(&conf.BridgeConfig.FixedCIDRv6, "fixed-cidr-v6", "", "IPv6 subnet for fixed IPs")
3433
flags.BoolVar(&conf.BridgeConfig.EnableUserlandProxy, "userland-proxy", true, "Use userland proxy for loopback traffic")
3534
flags.StringVar(&conf.BridgeConfig.UserlandProxyPath, "userland-proxy-path", "", "Path to the userland proxy binary")
3635
flags.StringVar(&conf.CgroupParent, "cgroup-parent", "", "Set parent cgroup for all containers")
3736
flags.StringVar(&conf.RemappedRoot, "userns-remap", "", "User/Group setting for user namespaces")
38-
flags.StringVar(&conf.ContainerdAddr, "containerd", "", "Path to containerd socket")
3937
flags.BoolVar(&conf.LiveRestoreEnabled, "live-restore", false, "Enable live restore of docker when containers are still running")
4038
flags.IntVar(&conf.OOMScoreAdjust, "oom-score-adjust", -500, "Set the oom_score_adj for the daemon")
4139
flags.BoolVar(&conf.Init, "init", false, "Run an init in the container to forward signals and reap processes")

cmd/dockerd/config_windows.go

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
var (
1212
defaultPidFile string
1313
defaultDataRoot = filepath.Join(os.Getenv("programdata"), "docker")
14+
defaultExecRoot = filepath.Join(os.Getenv("programdata"), "docker", "exec-root")
1415
)
1516

1617
// installConfigFlags adds flags to the pflag.FlagSet to configure the daemon

cmd/dockerd/daemon.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,11 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
204204
return err
205205
}
206206

207-
containerdRemote, err := libcontainerd.New(cli.getLibcontainerdRoot(), cli.getPlatformRemoteOptions()...)
207+
rOpts, err := cli.getRemoteOptions()
208+
if err != nil {
209+
return fmt.Errorf("Failed to generate containerd options: %s", err)
210+
}
211+
containerdRemote, err := libcontainerd.New(filepath.Join(cli.Config.Root, "containerd"), filepath.Join(cli.Config.ExecRoot, "containerd"), rOpts...)
208212
if err != nil {
209213
return err
210214
}
@@ -560,6 +564,17 @@ func (cli *DaemonCli) initMiddlewares(s *apiserver.Server, cfg *apiserver.Config
560564
return nil
561565
}
562566

567+
func (cli *DaemonCli) getRemoteOptions() ([]libcontainerd.RemoteOption, error) {
568+
opts := []libcontainerd.RemoteOption{}
569+
570+
pOpts, err := cli.getPlatformRemoteOptions()
571+
if err != nil {
572+
return nil, err
573+
}
574+
opts = append(opts, pOpts...)
575+
return opts, nil
576+
}
577+
563578
// validates that the plugins requested with the --authorization-plugin flag are valid AuthzDriver
564579
// plugins present on the host and available to the daemon
565580
func validateAuthzPlugins(requestedPlugins []string, pg plugingetter.PluginGetter) error {

cmd/dockerd/daemon_linux.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ func preNotifySystem() {
1111
// notifySystem sends a message to the host when the server is ready to be used
1212
func notifySystem() {
1313
// Tell the init daemon we are accepting requests
14-
go systemdDaemon.SdNotify("READY=1")
14+
go systemdDaemon.SdNotify(false, "READY=1")
1515
}

cmd/dockerd/daemon_solaris.go

+2-14
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,8 @@ func preNotifySystem() {
4141
func notifySystem() {
4242
}
4343

44-
func (cli *DaemonCli) getPlatformRemoteOptions() []libcontainerd.RemoteOption {
45-
opts := []libcontainerd.RemoteOption{}
46-
if cli.Config.ContainerdAddr != "" {
47-
opts = append(opts, libcontainerd.WithRemoteAddr(cli.Config.ContainerdAddr))
48-
} else {
49-
opts = append(opts, libcontainerd.WithStartDaemon(true))
50-
}
51-
return opts
52-
}
53-
54-
// getLibcontainerdRoot gets the root directory for libcontainerd/containerd to
55-
// store their state.
56-
func (cli *DaemonCli) getLibcontainerdRoot() string {
57-
return filepath.Join(cli.Config.ExecRoot, "libcontainerd")
44+
func (cli *DaemonCli) getPlatformRemoteOptions() ([]libcontainerd.RemoteOption, error) {
45+
return nil, nil
5846
}
5947

6048
// getSwarmRunRoot gets the root directory for swarm to store runtime state

cmd/dockerd/daemon_unix.go

+33-25
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import (
1010
"path/filepath"
1111
"strconv"
1212

13+
"github.com/containerd/containerd/linux"
1314
"github.com/docker/docker/cmd/dockerd/hack"
1415
"github.com/docker/docker/daemon"
1516
"github.com/docker/docker/libcontainerd"
17+
"github.com/docker/docker/pkg/parsers/kernel"
1618
"github.com/docker/libnetwork/portallocator"
1719
"golang.org/x/sys/unix"
1820
)
@@ -35,42 +37,48 @@ func getDaemonConfDir(_ string) string {
3537
return "/etc/docker"
3638
}
3739

38-
// setupConfigReloadTrap configures the USR2 signal to reload the configuration.
39-
func (cli *DaemonCli) setupConfigReloadTrap() {
40-
c := make(chan os.Signal, 1)
41-
signal.Notify(c, unix.SIGHUP)
42-
go func() {
43-
for range c {
44-
cli.reloadConfig()
45-
}
46-
}()
47-
}
40+
func (cli *DaemonCli) getPlatformRemoteOptions() ([]libcontainerd.RemoteOption, error) {
41+
// On older kernel, letting putting the containerd-shim in its own
42+
// namespace will effectively prevent operations such as unlink, rename
43+
// and remove on mountpoints that were present at the time the shim
44+
// namespace was created. This would led to a famous EBUSY will trying to
45+
// remove shm mounts.
46+
var noNewNS bool
47+
if !kernel.CheckKernelVersion(3, 18, 0) {
48+
noNewNS = true
49+
}
4850

49-
func (cli *DaemonCli) getPlatformRemoteOptions() []libcontainerd.RemoteOption {
5051
opts := []libcontainerd.RemoteOption{
51-
libcontainerd.WithDebugLog(cli.Config.Debug),
5252
libcontainerd.WithOOMScore(cli.Config.OOMScoreAdjust),
53+
libcontainerd.WithPlugin("linux", &linux.Config{
54+
Shim: daemon.DefaultShimBinary,
55+
Runtime: daemon.DefaultRuntimeBinary,
56+
RuntimeRoot: filepath.Join(cli.Config.Root, "runc"),
57+
ShimDebug: cli.Config.Debug,
58+
ShimNoMountNS: noNewNS,
59+
}),
60+
}
61+
if cli.Config.Debug {
62+
opts = append(opts, libcontainerd.WithLogLevel("debug"))
5363
}
5464
if cli.Config.ContainerdAddr != "" {
5565
opts = append(opts, libcontainerd.WithRemoteAddr(cli.Config.ContainerdAddr))
5666
} else {
5767
opts = append(opts, libcontainerd.WithStartDaemon(true))
5868
}
59-
if daemon.UsingSystemd(cli.Config) {
60-
args := []string{"--systemd-cgroup=true"}
61-
opts = append(opts, libcontainerd.WithRuntimeArgs(args))
62-
}
63-
if cli.Config.LiveRestoreEnabled {
64-
opts = append(opts, libcontainerd.WithLiveRestore(true))
65-
}
66-
opts = append(opts, libcontainerd.WithRuntimePath(daemon.DefaultRuntimeBinary))
67-
return opts
69+
70+
return opts, nil
6871
}
6972

70-
// getLibcontainerdRoot gets the root directory for libcontainerd/containerd to
71-
// store their state.
72-
func (cli *DaemonCli) getLibcontainerdRoot() string {
73-
return filepath.Join(cli.Config.ExecRoot, "libcontainerd")
73+
// setupConfigReloadTrap configures the USR2 signal to reload the configuration.
74+
func (cli *DaemonCli) setupConfigReloadTrap() {
75+
c := make(chan os.Signal, 1)
76+
signal.Notify(c, unix.SIGHUP)
77+
go func() {
78+
for range c {
79+
cli.reloadConfig()
80+
}
81+
}()
7482
}
7583

7684
// getSwarmRunRoot gets the root directory for swarm to store runtime state

cmd/dockerd/daemon_windows.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func notifyShutdown(err error) {
4848
}
4949
}
5050

51+
func (cli *DaemonCli) getPlatformRemoteOptions() ([]libcontainerd.RemoteOption, error) {
52+
return nil, nil
53+
}
54+
5155
// setupConfigReloadTrap configures a Win32 event to reload the configuration.
5256
func (cli *DaemonCli) setupConfigReloadTrap() {
5357
go func() {
@@ -65,17 +69,6 @@ func (cli *DaemonCli) setupConfigReloadTrap() {
6569
}()
6670
}
6771

68-
func (cli *DaemonCli) getPlatformRemoteOptions() []libcontainerd.RemoteOption {
69-
return nil
70-
}
71-
72-
// getLibcontainerdRoot gets the root directory for libcontainerd to store its
73-
// state. The Windows libcontainerd implementation does not need to write a spec
74-
// or state to disk, so this is a no-op.
75-
func (cli *DaemonCli) getLibcontainerdRoot() string {
76-
return ""
77-
}
78-
7972
// getSwarmRunRoot gets the root directory for swarm to store runtime state
8073
// For example, the control socket
8174
func (cli *DaemonCli) getSwarmRunRoot() string {

container/container.go

+34-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"syscall"
1616
"time"
1717

18+
"github.com/containerd/containerd"
1819
containertypes "github.com/docker/docker/api/types/container"
1920
mounttypes "github.com/docker/docker/api/types/mount"
2021
networktypes "github.com/docker/docker/api/types/network"
@@ -61,6 +62,18 @@ var (
6162
errInvalidNetwork = errors.New("invalid network settings while building port map info")
6263
)
6364

65+
// ExitStatus provides exit reasons for a container.
66+
type ExitStatus struct {
67+
// The exit code with which the container exited.
68+
ExitCode int
69+
70+
// Whether the container encountered an OOM.
71+
OOMKilled bool
72+
73+
// Time at which the container died
74+
ExitedAt time.Time
75+
}
76+
6477
// Container holds the structure defining a container object.
6578
type Container struct {
6679
StreamConfig *stream.Config
@@ -996,10 +1009,10 @@ func (container *Container) CloseStreams() error {
9961009
}
9971010

9981011
// InitializeStdio is called by libcontainerd to connect the stdio.
999-
func (container *Container) InitializeStdio(iop libcontainerd.IOPipe) error {
1012+
func (container *Container) InitializeStdio(iop *libcontainerd.IOPipe) (containerd.IO, error) {
10001013
if err := container.startLogging(); err != nil {
10011014
container.Reset(false)
1002-
return err
1015+
return nil, err
10031016
}
10041017

10051018
container.StreamConfig.CopyToPipe(iop)
@@ -1012,7 +1025,7 @@ func (container *Container) InitializeStdio(iop libcontainerd.IOPipe) error {
10121025
}
10131026
}
10141027

1015-
return nil
1028+
return &cio{IO: iop, sc: container.StreamConfig}, nil
10161029
}
10171030

10181031
// SecretMountPath returns the path of the secret mount for the container
@@ -1069,3 +1082,21 @@ func (container *Container) CreateDaemonEnvironment(tty bool, linkedEnv []string
10691082
env = ReplaceOrAppendEnvValues(env, container.Config.Env)
10701083
return env
10711084
}
1085+
1086+
type cio struct {
1087+
containerd.IO
1088+
1089+
sc *stream.Config
1090+
}
1091+
1092+
func (i *cio) Close() error {
1093+
i.IO.Close()
1094+
1095+
return i.sc.CloseStreams()
1096+
}
1097+
1098+
func (i *cio) Wait() {
1099+
i.sc.Wait()
1100+
1101+
i.IO.Wait()
1102+
}

container/container_unix.go

-9
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,6 @@ const (
2424
containerSecretMountPath = "/run/secrets"
2525
)
2626

27-
// ExitStatus provides exit reasons for a container.
28-
type ExitStatus struct {
29-
// The exit code with which the container exited.
30-
ExitCode int
31-
32-
// Whether the container encountered an OOM.
33-
OOMKilled bool
34-
}
35-
3627
// TrySetNetworkMount attempts to set the network mounts given a provided destination and
3728
// the path to use for it; return true if the given destination was a network mount file
3829
func (container *Container) TrySetNetworkMount(destination string, path string) bool {

container/container_windows.go

-6
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ const (
1818
containerInternalConfigsDirPath = `C:\ProgramData\Docker\internal\configs`
1919
)
2020

21-
// ExitStatus provides exit reasons for a container.
22-
type ExitStatus struct {
23-
// The exit code with which the container exited.
24-
ExitCode int
25-
}
26-
2721
// UnmountIpcMount unmounts Ipc related mounts.
2822
// This is a NOOP on windows.
2923
func (container *Container) UnmountIpcMount(unmount func(pth string) error) error {

container/state.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ func (s *State) SetExitCode(ec int) {
276276
// SetRunning sets the state of the container to "running".
277277
func (s *State) SetRunning(pid int, initial bool) {
278278
s.ErrorMsg = ""
279+
s.Paused = false
279280
s.Running = true
280281
s.Restarting = false
281282
if initial {
@@ -294,9 +295,14 @@ func (s *State) SetStopped(exitStatus *ExitStatus) {
294295
s.Paused = false
295296
s.Restarting = false
296297
s.Pid = 0
297-
s.FinishedAt = time.Now().UTC()
298-
s.setFromExitStatus(exitStatus)
299-
close(s.waitStop) // Fire waiters for stop
298+
if exitStatus.ExitedAt.IsZero() {
299+
s.FinishedAt = time.Now().UTC()
300+
} else {
301+
s.FinishedAt = exitStatus.ExitedAt
302+
}
303+
s.ExitCodeValue = exitStatus.ExitCode
304+
s.OOMKilled = exitStatus.OOMKilled
305+
close(s.waitStop) // fire waiters for stop
300306
s.waitStop = make(chan struct{})
301307
}
302308

@@ -310,8 +316,9 @@ func (s *State) SetRestarting(exitStatus *ExitStatus) {
310316
s.Paused = false
311317
s.Pid = 0
312318
s.FinishedAt = time.Now().UTC()
313-
s.setFromExitStatus(exitStatus)
314-
close(s.waitStop) // Fire waiters for stop
319+
s.ExitCodeValue = exitStatus.ExitCode
320+
s.OOMKilled = exitStatus.OOMKilled
321+
close(s.waitStop) // fire waiters for stop
315322
s.waitStop = make(chan struct{})
316323
}
317324

container/state_unix.go

-10
This file was deleted.

container/state_windows.go

-7
This file was deleted.

0 commit comments

Comments
 (0)