2014-08-01 13:34:06 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-04-02 18:21:56 -04:00
|
|
|
"context"
|
2015-05-07 12:49:07 -04:00
|
|
|
"crypto/tls"
|
2015-01-21 19:55:05 -05:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-12-08 18:13:43 -05:00
|
|
|
"runtime"
|
2015-05-05 00:18:28 -04:00
|
|
|
"strings"
|
2015-04-27 17:11:29 -04:00
|
|
|
"time"
|
2015-01-21 19:55:05 -05:00
|
|
|
|
2018-09-21 18:58:34 -04:00
|
|
|
containerddefaults "github.com/containerd/containerd/defaults"
|
2015-07-29 17:47:30 -04:00
|
|
|
"github.com/docker/distribution/uuid"
|
2016-04-08 19:22:39 -04:00
|
|
|
"github.com/docker/docker/api"
|
2015-04-16 15:48:04 -04:00
|
|
|
apiserver "github.com/docker/docker/api/server"
|
2017-04-13 14:37:32 -04:00
|
|
|
buildbackend "github.com/docker/docker/api/server/backend/build"
|
2016-04-08 19:22:39 -04:00
|
|
|
"github.com/docker/docker/api/server/middleware"
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
"github.com/docker/docker/api/server/router"
|
2016-02-10 15:16:59 -05:00
|
|
|
"github.com/docker/docker/api/server/router/build"
|
2016-11-16 20:58:06 -05:00
|
|
|
checkpointrouter "github.com/docker/docker/api/server/router/checkpoint"
|
2016-02-10 15:16:59 -05:00
|
|
|
"github.com/docker/docker/api/server/router/container"
|
2017-05-01 19:17:35 -04:00
|
|
|
distributionrouter "github.com/docker/docker/api/server/router/distribution"
|
2019-04-02 00:08:51 -04:00
|
|
|
grpcrouter "github.com/docker/docker/api/server/router/grpc"
|
2016-02-10 15:16:59 -05:00
|
|
|
"github.com/docker/docker/api/server/router/image"
|
|
|
|
"github.com/docker/docker/api/server/router/network"
|
2016-11-09 20:49:09 -05:00
|
|
|
pluginrouter "github.com/docker/docker/api/server/router/plugin"
|
2017-05-15 15:59:15 -04:00
|
|
|
sessionrouter "github.com/docker/docker/api/server/router/session"
|
2016-06-13 22:52:49 -04:00
|
|
|
swarmrouter "github.com/docker/docker/api/server/router/swarm"
|
2016-02-10 15:16:59 -05:00
|
|
|
systemrouter "github.com/docker/docker/api/server/router/system"
|
|
|
|
"github.com/docker/docker/api/server/router/volume"
|
2018-04-17 18:50:17 -04:00
|
|
|
buildkit "github.com/docker/docker/builder/builder-next"
|
2017-05-15 17:54:27 -04:00
|
|
|
"github.com/docker/docker/builder/dockerfile"
|
|
|
|
"github.com/docker/docker/builder/fscache"
|
2016-12-12 03:33:58 -05:00
|
|
|
"github.com/docker/docker/cli/debug"
|
2014-08-08 05:12:39 -04:00
|
|
|
"github.com/docker/docker/daemon"
|
2016-06-13 22:52:49 -04:00
|
|
|
"github.com/docker/docker/daemon/cluster"
|
2017-01-23 06:23:07 -05:00
|
|
|
"github.com/docker/docker/daemon/config"
|
2017-08-08 12:14:04 -04:00
|
|
|
"github.com/docker/docker/daemon/listeners"
|
2015-11-09 13:32:46 -05:00
|
|
|
"github.com/docker/docker/dockerversion"
|
2018-05-23 15:15:21 -04:00
|
|
|
"github.com/docker/docker/libcontainerd/supervisor"
|
2016-06-21 16:42:47 -04:00
|
|
|
dopts "github.com/docker/docker/opts"
|
2016-04-08 19:22:39 -04:00
|
|
|
"github.com/docker/docker/pkg/authorization"
|
2018-10-15 03:52:53 -04:00
|
|
|
"github.com/docker/docker/pkg/homedir"
|
2017-09-22 14:40:10 -04:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2015-04-27 17:11:29 -04:00
|
|
|
"github.com/docker/docker/pkg/pidfile"
|
2016-10-31 16:14:00 -04:00
|
|
|
"github.com/docker/docker/pkg/plugingetter"
|
2014-08-06 04:12:22 -04:00
|
|
|
"github.com/docker/docker/pkg/signal"
|
2017-04-24 13:28:21 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2017-03-17 17:57:23 -04:00
|
|
|
"github.com/docker/docker/plugin"
|
2018-10-15 03:52:53 -04:00
|
|
|
"github.com/docker/docker/rootless"
|
2016-03-28 14:22:23 -04:00
|
|
|
"github.com/docker/docker/runconfig"
|
2015-12-29 19:27:12 -05:00
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2017-04-02 18:21:56 -04:00
|
|
|
swarmapi "github.com/docker/swarmkit/api"
|
2017-07-28 19:04:34 -04:00
|
|
|
"github.com/moby/buildkit/session"
|
2017-05-15 15:59:15 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-06-21 16:42:47 -04:00
|
|
|
"github.com/spf13/pflag"
|
2014-08-01 13:34:06 -04:00
|
|
|
)
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
// DaemonCli represents the daemon CLI.
|
|
|
|
type DaemonCli struct {
|
2017-01-23 06:23:07 -05:00
|
|
|
*config.Config
|
2016-06-21 16:42:47 -04:00
|
|
|
configFile *string
|
|
|
|
flags *pflag.FlagSet
|
2016-04-22 20:16:14 -04:00
|
|
|
|
2016-05-16 14:12:48 -04:00
|
|
|
api *apiserver.Server
|
|
|
|
d *daemon.Daemon
|
|
|
|
authzMiddleware *authorization.Middleware // authzMiddleware enables to dynamically reload the authorization plugins
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
|
|
|
|
2016-06-21 16:42:47 -04:00
|
|
|
// NewDaemonCli returns a daemon CLI
|
2015-05-05 00:18:28 -04:00
|
|
|
func NewDaemonCli() *DaemonCli {
|
2016-06-21 16:42:47 -04:00
|
|
|
return &DaemonCli{}
|
2014-08-09 21:18:32 -04:00
|
|
|
}
|
|
|
|
|
2017-06-01 16:34:31 -04:00
|
|
|
func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
|
2016-04-22 20:16:14 -04:00
|
|
|
stopc := make(chan bool)
|
|
|
|
defer close(stopc)
|
|
|
|
|
2015-07-29 17:47:30 -04:00
|
|
|
// warn from uuid package when running the daemon
|
|
|
|
uuid.Loggerf = logrus.Warnf
|
|
|
|
|
2017-06-01 16:34:31 -04:00
|
|
|
opts.SetDefaultOptions(opts.flags)
|
2015-05-05 00:18:28 -04:00
|
|
|
|
2016-06-21 16:42:47 -04:00
|
|
|
if cli.Config, err = loadDaemonCliConfig(opts); err != nil {
|
2016-04-22 20:16:14 -04:00
|
|
|
return err
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
2018-11-24 08:39:08 -05:00
|
|
|
|
|
|
|
if err := configureDaemonLogs(cli.Config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-19 12:06:59 -04:00
|
|
|
logrus.Info("Starting up")
|
|
|
|
|
2016-06-21 16:42:47 -04:00
|
|
|
cli.configFile = &opts.configFile
|
|
|
|
cli.flags = opts.flags
|
2015-12-10 18:35:10 -05:00
|
|
|
|
|
|
|
if cli.Config.Debug {
|
2016-12-12 03:33:58 -05:00
|
|
|
debug.Enable()
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
2015-05-21 13:48:36 -04:00
|
|
|
|
2016-10-06 10:09:54 -04:00
|
|
|
if cli.Config.Experimental {
|
2015-05-05 00:18:28 -04:00
|
|
|
logrus.Warn("Running experimental build")
|
2018-10-15 03:52:53 -04:00
|
|
|
if cli.Config.IsRootless() {
|
|
|
|
logrus.Warn("Running in rootless mode. Cgroups, AppArmor, and CRIU are disabled.")
|
|
|
|
}
|
2019-04-19 03:53:58 -04:00
|
|
|
if rootless.RunningWithRootlessKit() {
|
|
|
|
logrus.Info("Running with RootlessKit integration")
|
|
|
|
if !cli.Config.IsRootless() {
|
|
|
|
return fmt.Errorf("rootless mode needs to be enabled for running with RootlessKit")
|
|
|
|
}
|
|
|
|
}
|
2018-10-15 03:52:53 -04:00
|
|
|
} else {
|
|
|
|
if cli.Config.IsRootless() {
|
|
|
|
return fmt.Errorf("rootless mode is supported only when running in experimental mode")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// return human-friendly error before creating files
|
|
|
|
if runtime.GOOS == "linux" && os.Geteuid() != 0 {
|
|
|
|
return fmt.Errorf("dockerd needs to be started with root. To see how to run dockerd in rootless mode with unprivileged user, see the documentation")
|
2014-08-01 13:34:06 -04:00
|
|
|
}
|
2015-03-27 21:38:00 -04:00
|
|
|
|
2017-09-14 15:29:42 -04:00
|
|
|
system.InitLCOW(cli.Config.Experimental)
|
|
|
|
|
2015-06-15 09:36:19 -04:00
|
|
|
if err := setDefaultUmask(); err != nil {
|
2018-08-22 10:00:22 -04:00
|
|
|
return err
|
2015-06-15 09:36:19 -04:00
|
|
|
}
|
|
|
|
|
2016-11-04 15:42:21 -04:00
|
|
|
// Create the daemon root before we create ANY other files (PID, or migrate keys)
|
|
|
|
// to ensure the appropriate ACL is set (particularly relevant on Windows)
|
|
|
|
if err := daemon.CreateDaemonRoot(cli.Config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-23 15:15:21 -04:00
|
|
|
if err := system.MkdirAll(cli.Config.ExecRoot, 0700, ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-15 03:52:53 -04:00
|
|
|
potentiallyUnderRuntimeDir := []string{cli.Config.ExecRoot}
|
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
if cli.Pidfile != "" {
|
|
|
|
pf, err := pidfile.New(cli.Pidfile)
|
2015-04-27 17:11:29 -04:00
|
|
|
if err != nil {
|
2018-08-22 10:00:22 -04:00
|
|
|
return errors.Wrap(err, "failed to start daemon")
|
2015-04-27 17:11:29 -04:00
|
|
|
}
|
2018-10-15 03:52:53 -04:00
|
|
|
potentiallyUnderRuntimeDir = append(potentiallyUnderRuntimeDir, cli.Pidfile)
|
2015-04-27 17:11:29 -04:00
|
|
|
defer func() {
|
2016-04-22 20:16:14 -04:00
|
|
|
if err := pf.Remove(); err != nil {
|
2015-04-27 17:11:29 -04:00
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2014-08-20 11:31:24 -04:00
|
|
|
|
2019-02-13 22:48:41 -05:00
|
|
|
if cli.Config.IsRootless() {
|
|
|
|
// Set sticky bit if XDG_RUNTIME_DIR is set && the file is actually under XDG_RUNTIME_DIR
|
|
|
|
if _, err := homedir.StickRuntimeDirContents(potentiallyUnderRuntimeDir); err != nil {
|
|
|
|
// StickRuntimeDirContents returns nil error if XDG_RUNTIME_DIR is just unset
|
|
|
|
logrus.WithError(err).Warn("cannot set sticky bit on files under XDG_RUNTIME_DIR")
|
|
|
|
}
|
2018-10-15 03:52:53 -04:00
|
|
|
}
|
|
|
|
|
2018-04-12 14:44:20 -04:00
|
|
|
serverConfig, err := newAPIServerConfig(cli)
|
|
|
|
if err != nil {
|
2018-08-22 10:00:22 -04:00
|
|
|
return errors.Wrap(err, "failed to create API server")
|
2015-10-12 04:49:25 -04:00
|
|
|
}
|
2017-06-12 17:50:29 -04:00
|
|
|
cli.api = apiserver.New(serverConfig)
|
2016-02-11 13:30:23 -05:00
|
|
|
|
2018-04-12 14:44:20 -04:00
|
|
|
hosts, err := loadListeners(cli, serverConfig)
|
|
|
|
if err != nil {
|
2018-08-22 10:00:22 -04:00
|
|
|
return errors.Wrap(err, "failed to load listeners")
|
2015-10-05 12:32:08 -04:00
|
|
|
}
|
2015-04-16 15:48:04 -04:00
|
|
|
|
2018-05-23 15:15:21 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2019-04-08 05:44:14 -04:00
|
|
|
waitForContainerDShutdown, err := cli.initContainerD(ctx)
|
|
|
|
if waitForContainerDShutdown != nil {
|
|
|
|
defer waitForContainerDShutdown(10 * time.Second)
|
|
|
|
}
|
|
|
|
if err != nil {
|
Windows: Experimental: Allow containerd for runtime
Signed-off-by: John Howard <jhoward@microsoft.com>
This is the first step in refactoring moby (dockerd) to use containerd on Windows.
Similar to the current model in Linux, this adds the option to enable it for runtime.
It does not switch the graphdriver to containerd snapshotters.
- Refactors libcontainerd to a series of subpackages so that either a
"local" containerd (1) or a "remote" (2) containerd can be loaded as opposed
to conditional compile as "local" for Windows and "remote" for Linux.
- Updates libcontainerd such that Windows has an option to allow the use of a
"remote" containerd. Here, it communicates over a named pipe using GRPC.
This is currently guarded behind the experimental flag, an environment variable,
and the providing of a pipename to connect to containerd.
- Infrastructure pieces such as under pkg/system to have helper functions for
determining whether containerd is being used.
(1) "local" containerd is what the daemon on Windows has used since inception.
It's not really containerd at all - it's simply local invocation of HCS APIs
directly in-process from the daemon through the Microsoft/hcsshim library.
(2) "remote" containerd is what docker on Linux uses for it's runtime. It means
that there is a separate containerd service running, and docker communicates over
GRPC to it.
To try this out, you will need to start with something like the following:
Window 1:
containerd --log-level debug
Window 2:
$env:DOCKER_WINDOWS_CONTAINERD=1
dockerd --experimental -D --containerd \\.\pipe\containerd-containerd
You will need the following binary from github.com/containerd/containerd in your path:
- containerd.exe
You will need the following binaries from github.com/Microsoft/hcsshim in your path:
- runhcs.exe
- containerd-shim-runhcs-v1.exe
For LCOW, it will require and initrd.img and kernel in `C:\Program Files\Linux Containers`.
This is no different to the current requirements. However, you may need updated binaries,
particularly initrd.img built from Microsoft/opengcs as (at the time of writing), Linuxkit
binaries are somewhat out of date.
Note that containerd and hcsshim for HCS v2 APIs do not yet support all the required
functionality needed for docker. This will come in time - this is a baby (although large)
step to migrating Docker on Windows to containerd.
Note that the HCS v2 APIs are only called on RS5+ builds. RS1..RS4 will still use
HCS v1 APIs as the v2 APIs were not fully developed enough on these builds to be usable.
This abstraction is done in HCSShim. (Referring specifically to runtime)
Note the LCOW graphdriver still uses HCS v1 APIs regardless.
Note also that this does not migrate docker to use containerd snapshotters
rather than graphdrivers. This needs to be done in conjunction with Linux also
doing the same switch.
2019-01-08 17:30:52 -05:00
|
|
|
cancel()
|
|
|
|
return err
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
2018-05-23 15:15:21 -04:00
|
|
|
defer cancel()
|
|
|
|
|
2016-06-13 02:10:37 -04:00
|
|
|
signal.Trap(func() {
|
|
|
|
cli.stop()
|
|
|
|
<-stopc // wait for daemonCli.start() to return
|
2017-07-31 23:32:57 -04:00
|
|
|
}, logrus.StandardLogger())
|
2016-03-18 14:50:19 -04:00
|
|
|
|
2017-02-01 13:52:16 -05:00
|
|
|
// Notify that the API is active, but before daemon is set up.
|
|
|
|
preNotifySystem()
|
|
|
|
|
2017-03-17 17:57:23 -04:00
|
|
|
pluginStore := plugin.NewStore()
|
|
|
|
|
2017-06-12 17:50:29 -04:00
|
|
|
if err := cli.initMiddlewares(cli.api, serverConfig, pluginStore); err != nil {
|
2017-03-17 17:57:23 -04:00
|
|
|
logrus.Fatalf("Error creating middlewares: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-05-23 15:15:21 -04:00
|
|
|
d, err := daemon.NewDaemon(ctx, cli.Config, pluginStore)
|
2015-05-07 18:35:12 -04:00
|
|
|
if err != nil {
|
2018-08-22 10:00:22 -04:00
|
|
|
return errors.Wrap(err, "failed to start daemon")
|
2015-05-07 18:35:12 -04:00
|
|
|
}
|
|
|
|
|
Don't create source directory while the daemon is being shutdown, fix #30348
If a container mount the socket the daemon is listening on into
container while the daemon is being shutdown, the socket will
not exist on the host, then daemon will assume it's a directory
and create it on the host, this will cause the daemon can't start
next time.
fix issue https://github.com/moby/moby/issues/30348
To reproduce this issue, you can add following code
```
--- a/daemon/oci_linux.go
+++ b/daemon/oci_linux.go
@@ -8,6 +8,7 @@ import (
"sort"
"strconv"
"strings"
+ "time"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/container"
@@ -666,7 +667,8 @@ func (daemon *Daemon) createSpec(c *container.Container) (*libcontainerd.Spec, e
if err := daemon.setupIpcDirs(c); err != nil {
return nil, err
}
-
+ fmt.Printf("===please stop the daemon===\n")
+ time.Sleep(time.Second * 2)
ms, err := daemon.setupMounts(c)
if err != nil {
return nil, err
```
step1 run a container which has `--restart always` and `-v /var/run/docker.sock:/sock`
```
$ docker run -ti --restart always -v /var/run/docker.sock:/sock busybox
/ #
```
step2 exit the the container
```
/ # exit
```
and kill the daemon when you see
```
===please stop the daemon===
```
in the daemon log
The daemon can't restart again and fail with `can't create unix socket /var/run/docker.sock: is a directory`.
Signed-off-by: Lei Jitang <leijitang@huawei.com>
2017-05-22 03:44:01 -04:00
|
|
|
d.StoreHosts(hosts)
|
|
|
|
|
2018-09-06 23:26:04 -04:00
|
|
|
// validate after NewDaemon has restored enabled plugins. Don't change order.
|
2017-03-17 17:57:23 -04:00
|
|
|
if err := validateAuthzPlugins(cli.Config.AuthorizationPlugins, pluginStore); err != nil {
|
2018-08-22 10:00:22 -04:00
|
|
|
return errors.Wrap(err, "failed to validate authorization plugin")
|
2017-03-17 17:57:23 -04:00
|
|
|
}
|
|
|
|
|
2019-09-11 07:40:11 -04:00
|
|
|
cli.d = d
|
|
|
|
|
|
|
|
if err := cli.startMetricsServer(cli.Config.MetricsAddress); err != nil {
|
|
|
|
return err
|
2016-07-20 19:11:28 -04:00
|
|
|
}
|
|
|
|
|
2018-04-12 14:44:20 -04:00
|
|
|
c, err := createAndStartCluster(cli, d)
|
2017-04-30 17:51:43 -04:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalf("Error starting cluster component: %v", err)
|
|
|
|
}
|
2016-06-13 22:52:49 -04:00
|
|
|
|
2016-09-09 12:55:57 -04:00
|
|
|
// Restart all autostart containers which has a swarm endpoint
|
|
|
|
// and is not yet running now that we have successfully
|
|
|
|
// initialized the cluster.
|
|
|
|
d.RestartSwarmContainers()
|
|
|
|
|
2015-05-07 18:35:12 -04:00
|
|
|
logrus.Info("Daemon has completed initialization")
|
|
|
|
|
2017-06-12 17:50:29 -04:00
|
|
|
routerOptions, err := newRouterOptions(cli.Config, d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
routerOptions.api = cli.api
|
|
|
|
routerOptions.cluster = c
|
|
|
|
|
|
|
|
initRouter(routerOptions)
|
2015-09-23 19:42:08 -04:00
|
|
|
|
2018-05-23 15:15:21 -04:00
|
|
|
go d.ProcessClusterNotifications(ctx, c.GetWatchStream())
|
2017-04-02 18:21:56 -04:00
|
|
|
|
2016-04-22 20:16:14 -04:00
|
|
|
cli.setupConfigReloadTrap()
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2015-11-24 21:17:37 -05:00
|
|
|
// The serve API routine never exits unless an error occurs
|
|
|
|
// We need to start it as a goroutine and wait on it so
|
|
|
|
// daemon doesn't exit
|
|
|
|
serveAPIWait := make(chan error)
|
2017-06-12 17:50:29 -04:00
|
|
|
go cli.api.Wait(serveAPIWait)
|
2015-11-24 21:17:37 -05:00
|
|
|
|
2015-11-25 14:05:31 -05:00
|
|
|
// after the daemon is done setting up we can notify systemd api
|
2015-09-23 19:42:08 -04:00
|
|
|
notifySystem()
|
2015-04-17 15:03:11 -04:00
|
|
|
|
2015-03-11 10:33:06 -04:00
|
|
|
// Daemon is fully initialized and handling API traffic
|
2015-04-27 17:11:29 -04:00
|
|
|
// Wait for serve API to complete
|
2015-03-11 10:33:06 -04:00
|
|
|
errAPI := <-serveAPIWait
|
2016-06-13 22:52:49 -04:00
|
|
|
c.Cleanup()
|
2018-05-23 15:15:21 -04:00
|
|
|
|
2016-06-06 23:29:05 -04:00
|
|
|
shutdownDaemon(d)
|
2018-05-23 15:15:21 -04:00
|
|
|
|
|
|
|
// Stop notification processing and any background processes
|
|
|
|
cancel()
|
|
|
|
|
2015-03-11 10:33:06 -04:00
|
|
|
if errAPI != nil {
|
2018-08-22 10:00:22 -04:00
|
|
|
return errors.Wrap(errAPI, "shutting down due to ServeAPI error")
|
2016-04-22 20:16:14 -04:00
|
|
|
}
|
|
|
|
|
2019-04-19 12:06:59 -04:00
|
|
|
logrus.Info("Daemon shutdown complete")
|
2016-04-22 20:16:14 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-12 17:50:29 -04:00
|
|
|
type routerOptions struct {
|
|
|
|
sessionManager *session.Manager
|
|
|
|
buildBackend *buildbackend.Backend
|
2018-05-15 13:02:16 -04:00
|
|
|
buildCache *fscache.FSCache // legacy
|
2018-08-22 02:05:26 -04:00
|
|
|
features *map[string]bool
|
2018-05-15 13:02:16 -04:00
|
|
|
buildkit *buildkit.Builder
|
2017-06-12 17:50:29 -04:00
|
|
|
daemon *daemon.Daemon
|
|
|
|
api *apiserver.Server
|
|
|
|
cluster *cluster.Cluster
|
|
|
|
}
|
|
|
|
|
2018-09-06 14:50:41 -04:00
|
|
|
func newRouterOptions(config *config.Config, d *daemon.Daemon) (routerOptions, error) {
|
2017-06-12 17:50:29 -04:00
|
|
|
opts := routerOptions{}
|
|
|
|
sm, err := session.NewManager()
|
|
|
|
if err != nil {
|
|
|
|
return opts, errors.Wrap(err, "failed to create sessionmanager")
|
|
|
|
}
|
|
|
|
|
|
|
|
builderStateDir := filepath.Join(config.Root, "builder")
|
|
|
|
|
|
|
|
buildCache, err := fscache.NewFSCache(fscache.Opt{
|
|
|
|
Backend: fscache.NewNaiveCacheBackend(builderStateDir),
|
|
|
|
Root: builderStateDir,
|
|
|
|
GCPolicy: fscache.GCPolicy{ // TODO: expose this in config
|
|
|
|
MaxSize: 1024 * 1024 * 512, // 512MB
|
|
|
|
MaxKeepDuration: 7 * 24 * time.Hour, // 1 week
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return opts, errors.Wrap(err, "failed to create fscache")
|
|
|
|
}
|
|
|
|
|
2018-09-06 14:50:41 -04:00
|
|
|
manager, err := dockerfile.NewBuildManager(d.BuilderBackend(), sm, buildCache, d.IdentityMapping())
|
2017-06-12 17:50:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return opts, err
|
|
|
|
}
|
2018-09-07 16:43:21 -04:00
|
|
|
cgroupParent := newCgroupParent(config)
|
2018-08-05 21:52:35 -04:00
|
|
|
bk, err := buildkit.New(buildkit.Opt{
|
2018-08-14 21:40:28 -04:00
|
|
|
SessionManager: sm,
|
|
|
|
Root: filepath.Join(config.Root, "buildkit"),
|
2018-09-06 14:50:41 -04:00
|
|
|
Dist: d.DistributionServices(),
|
|
|
|
NetworkController: d.NetworkController(),
|
2018-08-14 21:40:28 -04:00
|
|
|
DefaultCgroupParent: cgroupParent,
|
2018-09-14 19:07:16 -04:00
|
|
|
ResolverOpt: d.NewResolveOptionsFunc(),
|
2018-09-04 22:12:44 -04:00
|
|
|
BuilderConfig: config.Builder,
|
2019-02-28 03:12:55 -05:00
|
|
|
Rootless: d.Rootless(),
|
2019-06-10 19:28:01 -04:00
|
|
|
IdentityMapping: d.IdentityMapping(),
|
2019-06-05 21:36:33 -04:00
|
|
|
DNSConfig: config.DNSConfig,
|
2018-04-17 18:50:17 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return opts, err
|
|
|
|
}
|
|
|
|
|
2018-09-06 14:50:41 -04:00
|
|
|
bb, err := buildbackend.NewBackend(d.ImageService(), manager, buildCache, bk)
|
2017-06-12 17:50:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return opts, errors.Wrap(err, "failed to create buildmanager")
|
|
|
|
}
|
|
|
|
return routerOptions{
|
|
|
|
sessionManager: sm,
|
|
|
|
buildBackend: bb,
|
|
|
|
buildCache: buildCache,
|
2018-08-05 21:52:35 -04:00
|
|
|
buildkit: bk,
|
2018-09-06 14:50:41 -04:00
|
|
|
features: d.Features(),
|
|
|
|
daemon: d,
|
2017-06-12 17:50:29 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-04-22 20:16:14 -04:00
|
|
|
func (cli *DaemonCli) reloadConfig() {
|
2018-04-23 15:24:01 -04:00
|
|
|
reload := func(c *config.Config) {
|
2016-05-16 14:12:48 -04:00
|
|
|
|
2016-10-31 16:14:00 -04:00
|
|
|
// Revalidate and reload the authorization plugins
|
2018-04-23 15:24:01 -04:00
|
|
|
if err := validateAuthzPlugins(c.AuthorizationPlugins, cli.d.PluginStore); err != nil {
|
2016-10-31 16:14:00 -04:00
|
|
|
logrus.Fatalf("Error validating authorization plugin: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2018-04-23 15:24:01 -04:00
|
|
|
cli.authzMiddleware.SetPlugins(c.AuthorizationPlugins)
|
|
|
|
|
|
|
|
// The namespaces com.docker.*, io.docker.*, org.dockerproject.* have been documented
|
|
|
|
// to be reserved for Docker's internal use, but this was never enforced. Allowing
|
|
|
|
// configured labels to use these namespaces are deprecated for 18.05.
|
|
|
|
//
|
|
|
|
// The following will check the usage of such labels, and report a warning for deprecation.
|
|
|
|
//
|
|
|
|
// TODO: At the next stable release, the validation should be folded into the other
|
|
|
|
// configuration validation functions and an error will be returned instead, and this
|
|
|
|
// block should be deleted.
|
|
|
|
if err := config.ValidateReservedNamespaceLabels(c.Labels); err != nil {
|
|
|
|
logrus.Warnf("Configured labels using reserved namespaces is deprecated: %s", err)
|
|
|
|
}
|
2016-05-16 14:12:48 -04:00
|
|
|
|
2018-04-23 15:24:01 -04:00
|
|
|
if err := cli.d.Reload(c); err != nil {
|
2016-04-22 20:16:14 -04:00
|
|
|
logrus.Errorf("Error reconfiguring the daemon: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2016-05-16 14:12:48 -04:00
|
|
|
|
2018-04-23 15:24:01 -04:00
|
|
|
if c.IsValueSet("debug") {
|
2016-12-12 03:33:58 -05:00
|
|
|
debugEnabled := debug.IsEnabled()
|
2016-04-22 20:16:14 -04:00
|
|
|
switch {
|
2018-04-23 15:24:01 -04:00
|
|
|
case debugEnabled && !c.Debug: // disable debug
|
2016-12-12 03:33:58 -05:00
|
|
|
debug.Disable()
|
2018-04-23 15:24:01 -04:00
|
|
|
case c.Debug && !debugEnabled: // enable debug
|
2016-12-12 03:33:58 -05:00
|
|
|
debug.Enable()
|
2015-04-27 17:11:29 -04:00
|
|
|
}
|
|
|
|
}
|
2015-03-11 10:33:06 -04:00
|
|
|
}
|
2016-04-22 20:16:14 -04:00
|
|
|
|
2017-01-23 06:23:07 -05:00
|
|
|
if err := config.Reload(*cli.configFile, cli.flags, reload); err != nil {
|
2016-04-22 20:16:14 -04:00
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *DaemonCli) stop() {
|
|
|
|
cli.api.Close()
|
2014-08-01 13:34:06 -04:00
|
|
|
}
|
2015-03-29 15:48:52 -04:00
|
|
|
|
2015-04-27 17:11:29 -04:00
|
|
|
// shutdownDaemon just wraps daemon.Shutdown() to handle a timeout in case
|
|
|
|
// d.Shutdown() is waiting too long to kill container or worst it's
|
|
|
|
// blocked there
|
2016-06-06 23:29:05 -04:00
|
|
|
func shutdownDaemon(d *daemon.Daemon) {
|
|
|
|
shutdownTimeout := d.ShutdownTimeout()
|
2015-04-27 17:11:29 -04:00
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
2015-09-29 13:51:40 -04:00
|
|
|
d.Shutdown()
|
2015-04-27 17:11:29 -04:00
|
|
|
close(ch)
|
|
|
|
}()
|
2016-06-06 23:29:05 -04:00
|
|
|
if shutdownTimeout < 0 {
|
|
|
|
<-ch
|
|
|
|
logrus.Debug("Clean shutdown succeeded")
|
|
|
|
return
|
|
|
|
}
|
2015-04-27 17:11:29 -04:00
|
|
|
select {
|
|
|
|
case <-ch:
|
2015-08-07 18:24:18 -04:00
|
|
|
logrus.Debug("Clean shutdown succeeded")
|
2016-06-06 23:29:05 -04:00
|
|
|
case <-time.After(time.Duration(shutdownTimeout) * time.Second):
|
2015-04-27 17:11:29 -04:00
|
|
|
logrus.Error("Force shutdown daemon")
|
|
|
|
}
|
|
|
|
}
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2017-06-01 16:34:31 -04:00
|
|
|
func loadDaemonCliConfig(opts *daemonOptions) (*config.Config, error) {
|
2017-01-23 06:23:07 -05:00
|
|
|
conf := opts.daemonConfig
|
2016-06-21 16:42:47 -04:00
|
|
|
flags := opts.flags
|
2017-06-01 16:34:31 -04:00
|
|
|
conf.Debug = opts.Debug
|
|
|
|
conf.Hosts = opts.Hosts
|
|
|
|
conf.LogLevel = opts.LogLevel
|
|
|
|
conf.TLS = opts.TLS
|
|
|
|
conf.TLSVerify = opts.TLSVerify
|
2017-01-23 06:23:07 -05:00
|
|
|
conf.CommonTLSOptions = config.CommonTLSOptions{}
|
2015-12-10 18:35:10 -05:00
|
|
|
|
2017-06-01 16:34:31 -04:00
|
|
|
if opts.TLSOptions != nil {
|
|
|
|
conf.CommonTLSOptions.CAFile = opts.TLSOptions.CAFile
|
|
|
|
conf.CommonTLSOptions.CertFile = opts.TLSOptions.CertFile
|
|
|
|
conf.CommonTLSOptions.KeyFile = opts.TLSOptions.KeyFile
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
|
|
|
|
2019-06-17 20:23:44 -04:00
|
|
|
if conf.TrustKeyPath == "" {
|
|
|
|
daemonConfDir, err := getDaemonConfDir(conf.Root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conf.TrustKeyPath = filepath.Join(daemonConfDir, defaultTrustKeyFile)
|
|
|
|
}
|
|
|
|
|
2017-03-28 10:19:35 -04:00
|
|
|
if flags.Changed("graph") && flags.Changed("data-root") {
|
2018-08-22 10:00:22 -04:00
|
|
|
return nil, errors.New(`cannot specify both "--graph" and "--data-root" option`)
|
2017-03-28 10:19:35 -04:00
|
|
|
}
|
|
|
|
|
2016-06-21 16:42:47 -04:00
|
|
|
if opts.configFile != "" {
|
2017-01-23 06:23:07 -05:00
|
|
|
c, err := config.MergeDaemonConfigurations(conf, flags, opts.configFile)
|
2015-12-10 18:35:10 -05:00
|
|
|
if err != nil {
|
2017-03-30 06:01:00 -04:00
|
|
|
if flags.Changed("config-file") || !os.IsNotExist(err) {
|
2018-08-22 10:00:22 -04:00
|
|
|
return nil, errors.Wrapf(err, "unable to configure the Docker daemon with file %s", opts.configFile)
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// the merged configuration can be nil if the config file didn't exist.
|
|
|
|
// leave the current configuration as it is if when that happens.
|
|
|
|
if c != nil {
|
2017-01-23 06:23:07 -05:00
|
|
|
conf = c
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-23 06:23:07 -05:00
|
|
|
if err := config.Validate(conf); err != nil {
|
2016-05-23 17:49:50 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-28 10:19:35 -04:00
|
|
|
if flags.Changed("graph") {
|
2017-06-11 08:39:28 -04:00
|
|
|
logrus.Warnf(`The "-g / --graph" flag is deprecated. Please use "--data-root" instead`)
|
2017-03-28 10:19:35 -04:00
|
|
|
}
|
|
|
|
|
2017-11-11 21:09:28 -05:00
|
|
|
// Check if duplicate label-keys with different values are found
|
|
|
|
newLabels, err := config.GetConflictFreeLabels(conf.Labels)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-07-12 08:08:05 -04:00
|
|
|
}
|
2018-04-23 15:24:01 -04:00
|
|
|
// The namespaces com.docker.*, io.docker.*, org.dockerproject.* have been documented
|
|
|
|
// to be reserved for Docker's internal use, but this was never enforced. Allowing
|
|
|
|
// configured labels to use these namespaces are deprecated for 18.05.
|
|
|
|
//
|
|
|
|
// The following will check the usage of such labels, and report a warning for deprecation.
|
|
|
|
//
|
|
|
|
// TODO: At the next stable release, the validation should be folded into the other
|
|
|
|
// configuration validation functions and an error will be returned instead, and this
|
|
|
|
// block should be deleted.
|
|
|
|
if err := config.ValidateReservedNamespaceLabels(newLabels); err != nil {
|
|
|
|
logrus.Warnf("Configured labels using reserved namespaces is deprecated: %s", err)
|
|
|
|
}
|
2017-11-11 21:09:28 -05:00
|
|
|
conf.Labels = newLabels
|
2016-07-12 08:08:05 -04:00
|
|
|
|
2016-01-19 14:16:07 -05:00
|
|
|
// Regardless of whether the user sets it to true or false, if they
|
|
|
|
// specify TLSVerify at all then we need to turn on TLS
|
2017-06-01 16:34:31 -04:00
|
|
|
if conf.IsValueSet(FlagTLSVerify) {
|
2017-01-23 06:23:07 -05:00
|
|
|
conf.TLS = true
|
2016-01-19 14:16:07 -05:00
|
|
|
}
|
|
|
|
|
2017-01-23 06:23:07 -05:00
|
|
|
return conf, nil
|
2015-12-10 18:35:10 -05:00
|
|
|
}
|
2016-02-10 15:16:59 -05:00
|
|
|
|
2017-06-12 17:50:29 -04:00
|
|
|
func initRouter(opts routerOptions) {
|
2016-03-28 14:22:23 -04:00
|
|
|
decoder := runconfig.ContainerDecoder{}
|
|
|
|
|
2016-11-16 20:58:06 -05:00
|
|
|
routers := []router.Router{
|
|
|
|
// we need to add the checkpoint router before the container router or the DELETE gets masked
|
2017-06-12 17:50:29 -04:00
|
|
|
checkpointrouter.NewRouter(opts.daemon, decoder),
|
|
|
|
container.NewRouter(opts.daemon, decoder),
|
2018-02-02 17:18:46 -05:00
|
|
|
image.NewRouter(opts.daemon.ImageService()),
|
2018-08-22 02:05:26 -04:00
|
|
|
systemrouter.NewRouter(opts.daemon, opts.cluster, opts.buildCache, opts.buildkit, opts.features),
|
2018-03-22 17:11:03 -04:00
|
|
|
volume.NewRouter(opts.daemon.VolumesService()),
|
2018-08-22 02:05:26 -04:00
|
|
|
build.NewRouter(opts.buildBackend, opts.daemon, opts.features),
|
2017-06-12 17:50:29 -04:00
|
|
|
sessionrouter.NewRouter(opts.sessionManager),
|
|
|
|
swarmrouter.NewRouter(opts.cluster),
|
|
|
|
pluginrouter.NewRouter(opts.daemon.PluginManager()),
|
2018-02-02 17:18:46 -05:00
|
|
|
distributionrouter.NewRouter(opts.daemon.ImageService()),
|
2016-11-16 20:58:06 -05:00
|
|
|
}
|
2016-08-30 10:10:44 -04:00
|
|
|
|
2019-04-02 00:08:51 -04:00
|
|
|
grpcBackends := []grpcrouter.Backend{}
|
|
|
|
for _, b := range []interface{}{opts.daemon, opts.buildBackend} {
|
|
|
|
if b, ok := b.(grpcrouter.Backend); ok {
|
|
|
|
grpcBackends = append(grpcBackends, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(grpcBackends) > 0 {
|
|
|
|
routers = append(routers, grpcrouter.NewRouter(grpcBackends...))
|
|
|
|
}
|
|
|
|
|
2017-06-12 17:50:29 -04:00
|
|
|
if opts.daemon.NetworkControllerEnabled() {
|
|
|
|
routers = append(routers, network.NewRouter(opts.daemon, opts.cluster))
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
}
|
|
|
|
|
2017-06-12 17:50:29 -04:00
|
|
|
if opts.daemon.HasExperimental() {
|
2016-11-16 20:58:06 -05:00
|
|
|
for _, r := range routers {
|
|
|
|
for _, route := range r.Routes() {
|
|
|
|
if experimental, ok := route.(router.ExperimentalRoute); ok {
|
|
|
|
experimental.Enable()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-08 14:43:42 -04:00
|
|
|
opts.api.InitRouter(routers...)
|
2016-02-10 15:16:59 -05:00
|
|
|
}
|
2016-04-08 19:22:39 -04:00
|
|
|
|
2017-06-12 17:50:29 -04:00
|
|
|
// TODO: remove this from cli and return the authzMiddleware
|
2017-08-23 18:21:41 -04:00
|
|
|
func (cli *DaemonCli) initMiddlewares(s *apiserver.Server, cfg *apiserver.Config, pluginStore plugingetter.PluginGetter) error {
|
2016-04-19 10:56:54 -04:00
|
|
|
v := cfg.Version
|
2016-04-08 19:22:39 -04:00
|
|
|
|
2017-03-17 17:57:23 -04:00
|
|
|
exp := middleware.NewExperimentalMiddleware(cli.Config.Experimental)
|
2016-10-06 10:09:54 -04:00
|
|
|
s.UseMiddleware(exp)
|
|
|
|
|
2016-04-08 19:22:39 -04:00
|
|
|
vm := middleware.NewVersionMiddleware(v, api.DefaultVersion, api.MinVersion)
|
|
|
|
s.UseMiddleware(vm)
|
|
|
|
|
2017-09-12 06:43:34 -04:00
|
|
|
if cfg.CorsHeaders != "" {
|
2016-04-08 19:22:39 -04:00
|
|
|
c := middleware.NewCORSMiddleware(cfg.CorsHeaders)
|
|
|
|
s.UseMiddleware(c)
|
|
|
|
}
|
|
|
|
|
2017-03-17 17:57:23 -04:00
|
|
|
cli.authzMiddleware = authorization.NewMiddleware(cli.Config.AuthorizationPlugins, pluginStore)
|
|
|
|
cli.Config.AuthzMiddleware = cli.authzMiddleware
|
2016-05-16 14:12:48 -04:00
|
|
|
s.UseMiddleware(cli.authzMiddleware)
|
2016-10-31 16:14:00 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-23 15:15:21 -04:00
|
|
|
func (cli *DaemonCli) getContainerdDaemonOpts() ([]supervisor.DaemonOpt, error) {
|
|
|
|
opts, err := cli.getPlatformContainerdDaemonOpts()
|
2017-09-22 09:52:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-23 15:15:21 -04:00
|
|
|
|
|
|
|
if cli.Config.Debug {
|
|
|
|
opts = append(opts, supervisor.WithLogLevel("debug"))
|
|
|
|
} else if cli.Config.LogLevel != "" {
|
|
|
|
opts = append(opts, supervisor.WithLogLevel(cli.Config.LogLevel))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cli.Config.CriContainerd {
|
|
|
|
opts = append(opts, supervisor.WithPlugin("cri", nil))
|
|
|
|
}
|
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
return opts, nil
|
|
|
|
}
|
|
|
|
|
2018-04-12 14:44:20 -04:00
|
|
|
func newAPIServerConfig(cli *DaemonCli) (*apiserver.Config, error) {
|
|
|
|
serverConfig := &apiserver.Config{
|
|
|
|
Logging: true,
|
|
|
|
SocketGroup: cli.Config.SocketGroup,
|
|
|
|
Version: dockerversion.Version,
|
|
|
|
CorsHeaders: cli.Config.CorsHeaders,
|
|
|
|
}
|
|
|
|
|
|
|
|
if cli.Config.TLS {
|
|
|
|
tlsOptions := tlsconfig.Options{
|
|
|
|
CAFile: cli.Config.CommonTLSOptions.CAFile,
|
|
|
|
CertFile: cli.Config.CommonTLSOptions.CertFile,
|
|
|
|
KeyFile: cli.Config.CommonTLSOptions.KeyFile,
|
|
|
|
ExclusiveRootPools: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if cli.Config.TLSVerify {
|
|
|
|
// server requires and verifies client's certificate
|
|
|
|
tlsOptions.ClientAuth = tls.RequireAndVerifyClientCert
|
|
|
|
}
|
|
|
|
tlsConfig, err := tlsconfig.Server(tlsOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
serverConfig.TLSConfig = tlsConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cli.Config.Hosts) == 0 {
|
|
|
|
cli.Config.Hosts = make([]string, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return serverConfig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadListeners(cli *DaemonCli, serverConfig *apiserver.Config) ([]string, error) {
|
|
|
|
var hosts []string
|
2019-07-12 19:53:12 -04:00
|
|
|
seen := make(map[string]struct{}, len(cli.Config.Hosts))
|
|
|
|
|
2018-04-12 14:44:20 -04:00
|
|
|
for i := 0; i < len(cli.Config.Hosts); i++ {
|
|
|
|
var err error
|
2019-04-19 03:53:58 -04:00
|
|
|
if cli.Config.Hosts[i], err = dopts.ParseHost(cli.Config.TLS, honorXDG, cli.Config.Hosts[i]); err != nil {
|
2018-08-22 10:00:22 -04:00
|
|
|
return nil, errors.Wrapf(err, "error parsing -H %s", cli.Config.Hosts[i])
|
2018-04-12 14:44:20 -04:00
|
|
|
}
|
2019-07-12 19:53:12 -04:00
|
|
|
if _, ok := seen[cli.Config.Hosts[i]]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
seen[cli.Config.Hosts[i]] = struct{}{}
|
2018-04-12 14:44:20 -04:00
|
|
|
|
|
|
|
protoAddr := cli.Config.Hosts[i]
|
|
|
|
protoAddrParts := strings.SplitN(protoAddr, "://", 2)
|
|
|
|
if len(protoAddrParts) != 2 {
|
|
|
|
return nil, fmt.Errorf("bad format %s, expected PROTO://ADDR", protoAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
proto := protoAddrParts[0]
|
|
|
|
addr := protoAddrParts[1]
|
|
|
|
|
|
|
|
// It's a bad idea to bind to TCP without tlsverify.
|
|
|
|
if proto == "tcp" && (serverConfig.TLSConfig == nil || serverConfig.TLSConfig.ClientAuth != tls.RequireAndVerifyClientCert) {
|
|
|
|
logrus.Warn("[!] DON'T BIND ON ANY IP ADDRESS WITHOUT setting --tlsverify IF YOU DON'T KNOW WHAT YOU'RE DOING [!]")
|
|
|
|
}
|
|
|
|
ls, err := listeners.Init(proto, addr, serverConfig.SocketGroup, serverConfig.TLSConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ls = wrapListeners(proto, ls)
|
|
|
|
// If we're binding to a TCP port, make sure that a container doesn't try to use it.
|
|
|
|
if proto == "tcp" {
|
|
|
|
if err := allocateDaemonPort(addr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logrus.Debugf("Listener created for HTTP on %s (%s)", proto, addr)
|
|
|
|
hosts = append(hosts, protoAddrParts[1])
|
|
|
|
cli.api.Accept(addr, ls...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hosts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createAndStartCluster(cli *DaemonCli, d *daemon.Daemon) (*cluster.Cluster, error) {
|
|
|
|
name, _ := os.Hostname()
|
|
|
|
|
|
|
|
// Use a buffered channel to pass changes from store watch API to daemon
|
|
|
|
// A buffer allows store watch API and daemon processing to not wait for each other
|
|
|
|
watchStream := make(chan *swarmapi.WatchMessage, 32)
|
|
|
|
|
|
|
|
c, err := cluster.New(cluster.Config{
|
|
|
|
Root: cli.Config.Root,
|
|
|
|
Name: name,
|
|
|
|
Backend: d,
|
2018-03-22 17:11:03 -04:00
|
|
|
VolumeBackend: d.VolumesService(),
|
2018-04-12 14:44:20 -04:00
|
|
|
ImageBackend: d.ImageService(),
|
|
|
|
PluginBackend: d.PluginManager(),
|
|
|
|
NetworkSubnetsProvider: d,
|
|
|
|
DefaultAdvertiseAddr: cli.Config.SwarmDefaultAdvertiseAddr,
|
|
|
|
RaftHeartbeatTick: cli.Config.SwarmRaftHeartbeatTick,
|
|
|
|
RaftElectionTick: cli.Config.SwarmRaftElectionTick,
|
|
|
|
RuntimeRoot: cli.getSwarmRunRoot(),
|
|
|
|
WatchStream: watchStream,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
d.SetCluster(c)
|
|
|
|
err = c.Start()
|
|
|
|
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
2016-10-31 16:14:00 -04:00
|
|
|
// validates that the plugins requested with the --authorization-plugin flag are valid AuthzDriver
|
|
|
|
// plugins present on the host and available to the daemon
|
|
|
|
func validateAuthzPlugins(requestedPlugins []string, pg plugingetter.PluginGetter) error {
|
|
|
|
for _, reqPlugin := range requestedPlugins {
|
2016-12-19 21:18:43 -05:00
|
|
|
if _, err := pg.Get(reqPlugin, authorization.AuthZApiImplements, plugingetter.Lookup); err != nil {
|
2016-10-31 16:14:00 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2016-04-08 19:22:39 -04:00
|
|
|
}
|
2018-09-21 18:58:34 -04:00
|
|
|
|
2019-04-19 03:53:58 -04:00
|
|
|
func systemContainerdRunning(honorXDG bool) (string, bool, error) {
|
2018-10-15 03:52:53 -04:00
|
|
|
addr := containerddefaults.DefaultAddress
|
2019-04-19 03:53:58 -04:00
|
|
|
if honorXDG {
|
2018-10-15 03:52:53 -04:00
|
|
|
runtimeDir, err := homedir.GetRuntimeDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
addr = filepath.Join(runtimeDir, "containerd", "containerd.sock")
|
|
|
|
}
|
|
|
|
_, err := os.Lstat(addr)
|
|
|
|
return addr, err == nil, nil
|
2018-09-21 18:58:34 -04:00
|
|
|
}
|
2018-11-24 08:39:08 -05:00
|
|
|
|
|
|
|
// configureDaemonLogs sets the logrus logging level and formatting
|
|
|
|
func configureDaemonLogs(conf *config.Config) error {
|
|
|
|
if conf.LogLevel != "" {
|
|
|
|
lvl, err := logrus.ParseLevel(conf.LogLevel)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to parse logging level: %s", conf.LogLevel)
|
|
|
|
}
|
|
|
|
logrus.SetLevel(lvl)
|
|
|
|
} else {
|
|
|
|
logrus.SetLevel(logrus.InfoLevel)
|
|
|
|
}
|
|
|
|
logrus.SetFormatter(&logrus.TextFormatter{
|
|
|
|
TimestampFormat: jsonmessage.RFC3339NanoFixed,
|
|
|
|
DisableColors: conf.RawLogs,
|
|
|
|
FullTimestamp: true,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|