Commit graph

138 commits

Author SHA1 Message Date
Sebastiaan van Stijn
1f187e640c
daemon/config: use more assertions in tests
Removes some custom handling, some of which were giving the wrong
error on failure ("expected no error" when we were checking for an
error).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-29 19:59:23 +02:00
Sebastiaan van Stijn
10e42f599a
daemon/config: TestUnixValidateConfigurationErrors: use subtests
Use sub-tests and make sure we get the expected error

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-29 19:59:21 +02:00
Sebastiaan van Stijn
751222d907
daemon/config: verify that flags were set correctly in tests
To prevent (e.g.) introducing a typo in the flag-name and invalidating
the tests because of that.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-29 19:59:20 +02:00
Sebastiaan van Stijn
f73aadb230
daemon/config: New(): set more defaults
Set the defaults when constructing the config, instead of setting them
indirectly through the command-line flags.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-29 19:59:18 +02:00
Sebastiaan van Stijn
a0d0db126c
daemon/config: set default MTU when initializing config
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-29 19:59:16 +02:00
Sebastiaan van Stijn
62f71c4505
daemon/config: fix TestDaemonConfigurationMerge
This test was validating that the config file would not overwrite the
log-opt, but the test did not set up the flags correctly; as the flags
were not marked as "changed", it would not detect a conflict between
the config-file and daemon-flags.

This patch:

- removes the incorrect fields from the JSON file
- initializes the Config using config.New(), so that any defaults are also set
- sets flag values by actually setting them through the flags

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-29 19:59:14 +02:00
Sebastiaan van Stijn
9b39cab510
daemon/config: improve some tests
- TestReloadWithDuplicateLabels() also check value
- TestReloadDefaultConfigNotExist, TestReloadBadDefaultConfig,
  TestReloadWithConflictingLabels: verify that config is not
  reloaded.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-29 19:59:08 +02:00
Sebastiaan van Stijn
f8231c62f4
daemon/config: Validate() also validate default MTU
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-29 19:55:08 +02:00
Akihiro Suda
2c7a6d7bb1
daemon: remove support for deprecated io.containerd.runtime.v1.linux
This has been deprecated in Docker 20.10.0 (f63f73a4a8)

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
2022-06-05 18:41:30 +09:00
Sebastiaan van Stijn
e62382d014
daemon/config: remove uses of pointers for ints
Use the default (0) value to indicate "not set", which simplifies
working with these configuration options, preventing the need to
use intermediate variables etc.

While changing this code, also making some small cleanups, such
as replacing "fmt.Sprintf()" for "strconv" variants.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-29 09:39:34 +02:00
Sebastiaan van Stijn
4d22584432
Merge pull request #43536 from thaJeztah/daemon_fix_hosts_validation_step1g
daemon: improvements to config (re)loading
2022-04-29 09:39:11 +02:00
Sebastiaan van Stijn
647aede6ad
Merge pull request #43515 from corhere/swarmkit-v2
Bump swarmkit to v2
2022-04-28 20:08:42 +02:00
Sebastiaan van Stijn
4cf904494e
daemon: reloadMaxDownloadAttempts() remove validation
reloadMaxDownloadAttempts() is used to reload the configuration,
but validation happened before merging the config with the defaults.

This removes the validation from this function, instead centralizing
validation in config.Validate().

NOTE:
Currently this validation is "ok", as it checks for "nil" values;
I am working on changes to reduce the use of pointers in the config,
and instead provide a mechanism to fill in defaults. This change is
in preparation of that.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-28 09:30:06 +02:00
Sebastiaan van Stijn
c46e2e85ee
daemon/config: Reload(): add TODO for config reload logic
The Reload logic is problematic and needs a rewrite.

Currently, config.Reload() is validating newConfig before the reload callback
is executed. At that point, newConfig may be a partial configuration, yet to be
merged with the existing configuration (in the "reload()" callback). Validating
this config before it's merged can result in incorrect validation errors.

However, the current "reload()" callback we use is DaemonCli.reloadConfig(),
which includes a call to Daemon.Reload(), which both performs "merging" and
validation, as well as actually updating the daemon configuration. Calling
DaemonCli.reloadConfig() *before* validation, could thus lead to a failure in
that function (making the reload non-atomic).

While *some* errors could always occur when applying/updating the config, we
should make it more atomic, and;

1. get (a copy of) the active configuration
2. get the new configuration
3. apply the (reloadable) options from the new configuration
4. validate the merged results
5. apply the new configuration.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-28 09:30:04 +02:00
Sebastiaan van Stijn
9a54dadc44
daemon/config: MergeDaemonConfigurations() don't validate intermediates
MergeDaemonConfigurations was validating the configs before and after
merging. However, the "fileConfig" configuration may contain only a
"partial" configuration (options to apply to / override the existing
config). This means that some options may not be set and contain default
or empty values.

Validating such partial configurations can produce validation failures,
so to prevent those, we should validate the configuration _after_
merging, to validate the "final" state.

There's more cleaning up / improvements to be made in this area; for
example, we currently use our "self crafted" `getConflictFreeConfiguration()`
function, which is used to detect options that are not allowed to
be overridden, and which could potentially be handled by mergo.Merge(),
but leaving those changes for a future exercise.

This patch removes the first validation step, changing the function
to only validate the resulting configuration after merging.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-28 09:30:02 +02:00
Sebastiaan van Stijn
b819480899
daemon/config: Reload(): normalize labels before validation
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-28 09:30:00 +02:00
Sebastiaan van Stijn
390c7d6871
daemon/config: Validate(): validate log-level
Log-level validation was previously performed when configuring the daemon-logs;
this moves the validation to config.Validate() so that we can catch invalid
settings when running dockerd --validate.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-27 00:34:14 +02:00
Cory Snider
1c129103b4 Bump swarmkit to v2
Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-04-21 17:33:07 -04:00
Sebastiaan van Stijn
83969fa3dd
daemon: move DefaultShutdownTimeout to daemon/config
Unifying defaults to the daemon/config package

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-17 13:11:03 +02:00
Sebastiaan van Stijn
690a6fddf9
daemon: move default namespaces to daemon/config
Keeping the defaults in a single location, which also reduces
the list of imports needed.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-17 13:10:57 +02:00
Sebastiaan van Stijn
881e326f7a
daemon/config: remove unneeded alias
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-17 13:08:34 +02:00
Samuel Karp
5179299b98
Merge pull request #43457 from thaJeztah/daemon_fix_hosts_validation_step1 2022-04-09 00:55:40 -07:00
Sebastiaan van Stijn
7ea283fd91
Merge pull request #43458 from thaJeztah/fix_daemon_config_test
daemon/config: fix TestReloadDefaultConfigNotExist if file exists
2022-04-08 12:34:30 +02:00
Sebastiaan van Stijn
101dafd049
daemon/config: move proxy settings to "proxies" struct within daemon.json
This is a follow-up to 427c7cc5f8, which added
proxy-configuration options ("http-proxy", "https-proxy", "no-proxy") to the
dockerd cli and in `daemon.json`.

While working on documentation changes for this feature, I realised that those
options won't be "next" to each-other when formatting the daemon.json JSON, for
example using `jq` (which sorts the fields alphabetically). As it's possible that
additional proxy configuration options are added in future, I considered that
grouping these options in a struct within the JSON may help setting these options,
as well as discovering related options.

This patch introduces a "proxies" field in the JSON, which includes the
"http-proxy", "https-proxy", "no-proxy" options.

Conflict detection continues to work as before; with this patch applied:

    mkdir -p /etc/docker/
    echo '{"proxies":{"http-proxy":"http-config", "https-proxy":"https-config", "no-proxy": "no-proxy-config"}}' > /etc/docker/daemon.json

    dockerd --http-proxy=http-flag --https-proxy=https-flag --no-proxy=no-proxy-flag --validate

    unable to configure the Docker daemon with file /etc/docker/daemon.json:
    the following directives are specified both as a flag and in the configuration file:
    http-proxy: (from flag: http-flag, from file: http-config),
    https-proxy: (from flag: https-flag, from file: https-config),
    no-proxy: (from flag: no-proxy-flag, from file: no-proxy-config)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-07 19:43:02 +02:00
Sebastiaan van Stijn
a35b4ac54a
daemon/config: Validate(): validate hosts
The config.Validate() function did not validate hosts that were configured in
the daemon.json configuration file, resulting in `--validate` to pass, but the
daemon failing to start.

before this patch:

    echo '{"hosts":["127.0.0.1:2375/path"]}' > /etc/docker/daemon.json

    dockerd --validate
    configuration OK

    dockerd
    INFO[2022-04-03T11:42:22.162366200Z] Starting up
    failed to load listeners: error parsing -H 127.0.0.1:2375/path: invalid bind address (127.0.0.1:2375/path): should not contain a path element

with this patch:

    echo '{"hosts":["127.0.0.1:2375/path"]}' > /etc/docker/daemon.json

    dockerd --validate
    unable to configure the Docker daemon with file /etc/docker/daemon.json: configuration validation from file failed: invalid bind address (127.0.0.1:2375/path): should not contain a path element

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-04 15:18:01 +02:00
Sebastiaan van Stijn
5cfcd88d57
daemon/config: fix TestReloadDefaultConfigNotExist if file exists
The TestReloadDefaultConfigNotExist() test assumed it was running in a clean
environment, in which the `/etc/docker/daemon.json` file doesn't exist, and
would fail if that was not the case.

This patch updates the test to override the default location to a a non-existing
path, to allow running the test in an environment where `/etc/docker/daemon.json`
is present.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-04 15:07:05 +02:00
Sebastiaan van Stijn
2c97295ad8
daemon: remove v1 shim configuration for containerd
This removes the plugin section from the containerd configuration file
(`/var/run/docker/containerd/containerd.toml`) that is generated when
starting containerd as child process;

```toml
[plugins]
  [plugins.linux]
    shim = "containerd-shim"
    runtime = "runc"
    runtime_root = "/var/lib/docker/runc"
    no_shim = false
    shim_debug = true
```

This configuration doesn't appear to be used since commit:
0b14c2b67a, which switched the default runtime
to to io.containerd.runc.v2.

Note that containerd itself uses `containerd-shim` and `runc` as default
for `shim` and `runtime` v1, so omitting that configuration doesn't seem
to make a difference.

I'm slightly confused if any of the other options in this configuration were
actually used: for example, even though `runtime_root` was configured to be
`/var/lib/docker/runc`, when starting a container with that coniguration set
on docker 19.03, `/var/lib/docker/runc` doesn't appear to exist:

```console
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
098baa4cb0e7        nginx:alpine        "/docker-entrypoint.…"   59 minutes ago      Up 59 minutes       80/tcp              foo

$ ls /var/lib/docker/runc
ls: /var/lib/docker/runc: No such file or directory

$ ps auxf
PID   USER     TIME  COMMAND
    1 root      0:00 sh
   16 root      0:11 dockerd --debug
   26 root      0:09 containerd --config /var/run/docker/containerd/containerd.toml --log-level debug
  234 root      0:00 containerd-shim -namespace moby -workdir /var/lib/docker/containerd/daemon/io.containerd.runtime.v1.linux/moby/09
  251 root      0:00 nginx: master process nginx -g daemon off;
  304 101       0:00 nginx: worker process
...

```

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-07 19:31:24 +01:00
Sebastiaan van Stijn
821b4d4108
daemon/config: DefaultShmSize: minor tweak and improve docs
I had to check what the actual size was, so added it to the const's documentation.

While at it, also made use of it in a test, so that we're testing against the expected
value, and changed one alias to be consistent with other places where we alias this
import.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-02-18 23:29:36 +01:00
Sebastiaan van Stijn
ff2a5301b8
daemon: remove discovery-related config handling
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-01-06 18:28:17 +01:00
Sebastiaan van Stijn
702cb7fe14
daemon: remove discovery related tests
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-01-06 18:28:10 +01:00
Sebastiaan van Stijn
76016b846d
daemon: make sure proxy settings are sanitized when printing
The daemon can print the proxy configuration as part of error-messages,
and when reloading the daemon configuration (SIGHUP). Make sure that
the configuration is sanitized before printing.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-10-27 12:39:02 +02:00
Anca Iordache
427c7cc5f8
Add http(s) proxy properties to daemon configuration
This allows configuring the daemon's proxy server through the daemon.json con-
figuration file or command-line flags configuration file, in addition to the
existing option (through environment variables).

Configuring environment variables on Windows to configure a service is more
complicated than on Linux, and adding alternatives for this to the daemon con-
figuration makes the configuration more transparent and easier to use.

The configuration as set through command-line flags or through the daemon.json
configuration file takes precedence over env-vars in the daemon's environment,
which allows the daemon to use a different proxy. If both command-line flags
and a daemon.json configuration option is set, an error is produced when starting
the daemon.

Note that this configuration is not "live reloadable" due to Golang's use of
`sync.Once()` for proxy configuration, which means that changing the proxy
configuration requires a restart of the daemon (reload / SIGHUP will not update
the configuration.

With this patch:

    cat /etc/docker/daemon.json
    {
        "http-proxy": "http://proxytest.example.com:80",
        "https-proxy": "https://proxytest.example.com:443"
    }

    docker pull busybox
    Using default tag: latest
    Error response from daemon: Get "https://registry-1.docker.io/v2/": proxyconnect tcp: dial tcp: lookup proxytest.example.com on 127.0.0.11:53: no such host

    docker build .
    Sending build context to Docker daemon  89.28MB
    Step 1/3 : FROM golang:1.16-alpine AS base
    Get "https://registry-1.docker.io/v2/": proxyconnect tcp: dial tcp: lookup proxytest.example.com on 127.0.0.11:53: no such host

Integration tests were added to test the behavior:

- verify that the configuration through all means are used (env-var,
  command-line flags, damon.json), and used in the expected order of
  preference.
- verify that conflicting options produce an error.

Signed-off-by: Anca Iordache <anca.iordache@docker.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-10-27 12:38:59 +02:00
Sebastiaan van Stijn
a6ce7eff65
daemon: move maskCredentials to config package
This allows the utility to be used in other places.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-10-27 12:38:56 +02:00
Brian Goff
7ccf750daa Allow switching Windows runtimes.
This adds support for 2 runtimes on Windows, one that uses the built-in
HCSv1 integration and another which uses containerd with the runhcs
shim.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-09-23 17:44:04 +00:00
Akihiro Suda
9e7bbdb9ba
Merge pull request #40084 from thaJeztah/hostconfig_const_cleanup
api/types: hostconfig: add some constants/enums and minor code cleanup
2021-08-28 00:21:31 +09:00
Eng Zer Jun
c55a4ac779
refactor: move from io/ioutil to io and os package
The io/ioutil package has been deprecated in Go 1.16. This commit
replaces the existing io/ioutil functions with their new definitions in
io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2021-08-27 14:56:57 +08:00
Sebastiaan van Stijn
ac449d6b5a
daemon/config: rename the default seccomp profile to "builtin"
Using "default" as a name is a bit ambiguous, because the _daemon_ default
can be changed using the '--seccomp-profile' daemon flag.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-08-07 15:37:03 +02:00
Sebastiaan van Stijn
ee02257553
Add const for "unconfined" and default seccomp profiles
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-08-07 15:36:06 +02:00
Sebastiaan van Stijn
09cf117b31
api/types: hostconfig: create enum for CgroupnsMode
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-08-06 19:05:54 +02:00
Sebastiaan van Stijn
98f0f0dd87
api/types: hostconfig: define consts for IpcMode
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-08-06 19:05:51 +02:00
Sebastiaan van Stijn
0ff80c844d
daemon/config.New(): rewrite to be slightly more idiomatic
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-07-11 11:06:56 +02:00
Sebastiaan van Stijn
5588a78ab3
daemon/config: restrict "unix" code is linux
This code is not generically useful on "unix", and contains linux-
specific code, so make it only compile on linux.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-07-11 11:06:55 +02:00
Sebastiaan van Stijn
96f843ef30
daemon/config: move "common" tests
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-07-11 11:06:53 +02:00
Sebastiaan van Stijn
9d9679975f
daemon/config: remove CommonUnixConfig type
This type was added to support Solaris (which didn't support these
options). Solaris support was removed, so we can integrate this type
back into the "unix" type.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-07-11 11:06:51 +02:00
Sebastiaan van Stijn
defeab7387
daemon/config: remove commonUnixBridgeConfig
This type was added to support Solaris (which didn't support these
options). Solaris support was removed, so we can integrate this type
back into the "unix" type.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-07-11 11:06:49 +02:00
Sebastiaan van Stijn
a65f83317c
daemon/config: reorganize code between unix and windows files
Put variables and functions in the same owrder between both,
to allow for easier comparing between platforms.

Also synchronised some comments/godoc between both.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-07-11 11:06:42 +02:00
Sebastiaan van Stijn
314759dc2f
Merge pull request #42393 from aiordache/daemon_config
Daemon config validation
2021-06-23 19:32:07 +02:00
Rich Horwood
8f80e55111 Add configuration validation option and tests.
Fixes #36911

If config file is invalid we'll exit anyhow, so this just prevents
the daemon from starting if the configuration is fine.

Mainly useful for making config changes and restarting the daemon
iff the config is valid.

Signed-off-by: Rich Horwood <rjhorwood@apple.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Anca Iordache <anca.iordache@docker.com>
2021-06-23 09:54:55 +00:00
Sebastiaan van Stijn
16ced7622b
daemon/config: error strings should not be capitalized
daemon/config/config_unix.go:92:21: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
            return fmt.Errorf("Default cgroup namespace mode (%v) is invalid. Use \"host\" or \"private\".", cm) // nolint: golint
                              ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-06-10 13:03:43 +02:00
Sebastiaan van Stijn
bb17074119
reformat "nolint" comments
Unlike regular comments, nolint comments should not have a leading space.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-06-10 13:03:42 +02:00