From 0ff0b8693e1923d1c8e77aced450b1b0074222e2 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 23 Apr 2022 11:38:31 +0200 Subject: [PATCH 1/8] cmd/dockerd: move storage-driver flag to unix-only file The installCommonConfigFlags() function is meant for flags that are supported by all platforms, so removing it from that function. Signed-off-by: Sebastiaan van Stijn --- cmd/dockerd/config.go | 7 ------- cmd/dockerd/config_unix.go | 1 + 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/cmd/dockerd/config.go b/cmd/dockerd/config.go index 20cd999e9e..ce53f694b8 100644 --- a/cmd/dockerd/config.go +++ b/cmd/dockerd/config.go @@ -1,8 +1,6 @@ package main import ( - "runtime" - "github.com/docker/docker/daemon/config" "github.com/docker/docker/opts" "github.com/docker/docker/registry" @@ -48,11 +46,6 @@ func installCommonConfigFlags(conf *config.Config, flags *pflag.FlagSet) error { flags.BoolVarP(&conf.AutoRestart, "restart", "r", true, "--restart on the daemon has been deprecated in favor of --restart policies on docker run") _ = flags.MarkDeprecated("restart", "Please use a restart policy on docker run") - // Windows doesn't support setting the storage driver - there is no choice as to which ones to use. - if runtime.GOOS != "windows" { - flags.StringVarP(&conf.GraphDriver, "storage-driver", "s", "", "Storage driver to use") - } - flags.IntVar(&conf.Mtu, "mtu", 0, "Set the containers network MTU") flags.BoolVar(&conf.RawLogs, "raw-logs", false, "Full timestamps without ANSI coloring") flags.Var(opts.NewListOptsRef(&conf.DNS, opts.ValidateIPAddress), "dns", "DNS server to use") diff --git a/cmd/dockerd/config_unix.go b/cmd/dockerd/config_unix.go index 6bd6f4acc2..21a88f5091 100644 --- a/cmd/dockerd/config_unix.go +++ b/cmd/dockerd/config_unix.go @@ -35,6 +35,7 @@ func installConfigFlags(conf *config.Config, flags *pflag.FlagSet) error { conf.ShmSize = opts.MemBytes(config.DefaultShmSize) // Then platform-specific install flags + flags.StringVarP(&conf.GraphDriver, "storage-driver", "s", "", "Storage driver to use") flags.BoolVar(&conf.EnableSelinuxSupport, "selinux-enabled", false, "Enable selinux support") flags.Var(opts.NewNamedUlimitOpt("default-ulimits", &conf.Ulimits), "default-ulimit", "Default ulimits for containers") flags.BoolVar(&conf.BridgeConfig.EnableIPTables, "iptables", true, "Enable addition of iptables rules") From 782de17bd03a394eeaea6dfe33dea85333cf3562 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 23 Apr 2022 11:43:31 +0200 Subject: [PATCH 2/8] cmd/dockerd: installCommonConfigFlags() re-group some flags move deprecated options and some network-related options together Signed-off-by: Sebastiaan van Stijn --- cmd/dockerd/config.go | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/cmd/dockerd/config.go b/cmd/dockerd/config.go index ce53f694b8..4ffe4deb06 100644 --- a/cmd/dockerd/config.go +++ b/cmd/dockerd/config.go @@ -32,21 +32,16 @@ func installCommonConfigFlags(conf *config.Config, flags *pflag.FlagSet) error { flags.Var(opts.NewNamedListOptsRef("authorization-plugins", &conf.AuthorizationPlugins, nil), "authorization-plugin", "Authorization plugins to load") flags.Var(opts.NewNamedListOptsRef("exec-opts", &conf.ExecOptions, nil), "exec-opt", "Runtime execution options") flags.StringVarP(&conf.Pidfile, "pidfile", "p", defaultPidFile, "Path to use for daemon PID file") - flags.StringVarP(&conf.Root, "graph", "g", defaultDataRoot, "Root of the Docker runtime") + flags.StringVar(&conf.Root, "data-root", defaultDataRoot, "Root directory of persistent Docker state") flags.StringVar(&conf.ExecRoot, "exec-root", defaultExecRoot, "Root directory for execution state files") flags.StringVar(&conf.ContainerdAddr, "containerd", "", "containerd grpc address") flags.BoolVar(&conf.CriContainerd, "cri-containerd", false, "start containerd with cri") - // "--graph" is "soft-deprecated" in favor of "data-root". This flag was added - // before Docker 1.0, so won't be removed, only hidden, to discourage its usage. - _ = flags.MarkHidden("graph") - - flags.StringVar(&conf.Root, "data-root", defaultDataRoot, "Root directory of persistent Docker state") - - flags.BoolVarP(&conf.AutoRestart, "restart", "r", true, "--restart on the daemon has been deprecated in favor of --restart policies on docker run") - _ = flags.MarkDeprecated("restart", "Please use a restart policy on docker run") - flags.IntVar(&conf.Mtu, "mtu", 0, "Set the containers network MTU") + flags.IntVar(&conf.NetworkControlPlaneMTU, "network-control-plane-mtu", config.DefaultNetworkMtu, "Network Control plane MTU") + flags.IntVar(&conf.NetworkDiagnosticPort, "network-diagnostic-port", 0, "TCP port number of the network diagnostic server") + _ = flags.MarkHidden("network-diagnostic-port") + flags.BoolVar(&conf.RawLogs, "raw-logs", false, "Full timestamps without ANSI coloring") flags.Var(opts.NewListOptsRef(&conf.DNS, opts.ValidateIPAddress), "dns", "DNS server to use") flags.Var(opts.NewNamedListOptsRef("dns-opts", &conf.DNSOptions, nil), "dns-opt", "DNS options to use") @@ -56,42 +51,44 @@ func installCommonConfigFlags(conf *config.Config, flags *pflag.FlagSet) error { flags.StringVar(&conf.LogConfig.Type, "log-driver", "json-file", "Default driver for container logs") flags.Var(opts.NewNamedMapOpts("log-opts", conf.LogConfig.Config, nil), "log-opt", "Default log driver options for containers") - flags.StringVar(&conf.ClusterAdvertise, "cluster-advertise", "", "Address or interface name to advertise") - _ = flags.MarkDeprecated("cluster-advertise", "Swarm classic is deprecated. Please use Swarm-mode (docker swarm init)") - flags.StringVar(&conf.ClusterStore, "cluster-store", "", "URL of the distributed storage backend") - _ = flags.MarkDeprecated("cluster-store", "Swarm classic is deprecated. Please use Swarm-mode (docker swarm init)") - flags.Var(opts.NewNamedMapOpts("cluster-store-opts", conf.ClusterOpts, nil), "cluster-store-opt", "Set cluster store options") - _ = flags.MarkDeprecated("cluster-store-opt", "Swarm classic is deprecated. Please use Swarm-mode (docker swarm init)") - flags.StringVar(&conf.CorsHeaders, "api-cors-header", "", "Set CORS headers in the Engine API") flags.IntVar(&maxConcurrentDownloads, "max-concurrent-downloads", config.DefaultMaxConcurrentDownloads, "Set the max concurrent downloads for each pull") flags.IntVar(&maxConcurrentUploads, "max-concurrent-uploads", config.DefaultMaxConcurrentUploads, "Set the max concurrent uploads for each push") flags.IntVar(&maxDownloadAttempts, "max-download-attempts", config.DefaultDownloadAttempts, "Set the max download attempts for each pull") flags.IntVar(&conf.ShutdownTimeout, "shutdown-timeout", config.DefaultShutdownTimeout, "Set the default shutdown timeout") - flags.IntVar(&conf.NetworkDiagnosticPort, "network-diagnostic-port", 0, "TCP port number of the network diagnostic server") - _ = flags.MarkHidden("network-diagnostic-port") flags.StringVar(&conf.SwarmDefaultAdvertiseAddr, "swarm-default-advertise-addr", "", "Set default address or interface for swarm advertised address") flags.BoolVar(&conf.Experimental, "experimental", false, "Enable experimental features") flags.StringVar(&conf.MetricsAddress, "metrics-addr", "", "Set default address and port to serve the metrics api on") - flags.Var(opts.NewNamedListOptsRef("node-generic-resources", &conf.NodeGenericResources, opts.ValidateSingleGenericResource), "node-generic-resource", "Advertise user-defined resource") - flags.IntVar(&conf.NetworkControlPlaneMTU, "network-control-plane-mtu", config.DefaultNetworkMtu, "Network Control plane MTU") - conf.MaxConcurrentDownloads = &maxConcurrentDownloads conf.MaxConcurrentUploads = &maxConcurrentUploads conf.MaxDownloadAttempts = &maxDownloadAttempts flags.StringVar(&conf.ContainerdNamespace, "containerd-namespace", config.DefaultContainersNamespace, "Containerd namespace to use") flags.StringVar(&conf.ContainerdPluginNamespace, "containerd-plugins-namespace", config.DefaultPluginNamespace, "Containerd namespace to use for plugins") - flags.StringVar(&conf.DefaultRuntime, "default-runtime", config.StockRuntimeName, "Default OCI runtime for containers") flags.StringVar(&conf.HTTPProxy, "http-proxy", "", "HTTP proxy URL to use for outgoing traffic") flags.StringVar(&conf.HTTPSProxy, "https-proxy", "", "HTTPS proxy URL to use for outgoing traffic") flags.StringVar(&conf.NoProxy, "no-proxy", "", "Comma-separated list of hosts or IP addresses for which the proxy is skipped") + // Deprecated flags / options + + // "--graph" is "soft-deprecated" in favor of "data-root". This flag was added + // before Docker 1.0, so won't be removed, only hidden, to discourage its usage. + flags.StringVarP(&conf.Root, "graph", "g", defaultDataRoot, "Root of the Docker runtime") + _ = flags.MarkHidden("graph") + flags.BoolVarP(&conf.AutoRestart, "restart", "r", true, "--restart on the daemon has been deprecated in favor of --restart policies on docker run") + _ = flags.MarkDeprecated("restart", "Please use a restart policy on docker run") + flags.StringVar(&conf.ClusterAdvertise, "cluster-advertise", "", "Address or interface name to advertise") + _ = flags.MarkDeprecated("cluster-advertise", "Swarm classic is deprecated. Please use Swarm-mode (docker swarm init)") + flags.StringVar(&conf.ClusterStore, "cluster-store", "", "URL of the distributed storage backend") + _ = flags.MarkDeprecated("cluster-store", "Swarm classic is deprecated. Please use Swarm-mode (docker swarm init)") + flags.Var(opts.NewNamedMapOpts("cluster-store-opts", conf.ClusterOpts, nil), "cluster-store-opt", "Set cluster store options") + _ = flags.MarkDeprecated("cluster-store-opt", "Swarm classic is deprecated. Please use Swarm-mode (docker swarm init)") + return nil } From 1483905024879a3da6ba4c5beb4fa45ef408e9f8 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 23 Apr 2022 14:05:16 +0200 Subject: [PATCH 3/8] cmd/dockerd: remove some intermediate variables use the Config itself to hold the "default" values. Signed-off-by: Sebastiaan van Stijn --- cmd/dockerd/config.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/cmd/dockerd/config.go b/cmd/dockerd/config.go index 4ffe4deb06..a9b9869142 100644 --- a/cmd/dockerd/config.go +++ b/cmd/dockerd/config.go @@ -13,15 +13,16 @@ const defaultTrustKeyFile = "key.json" // installCommonConfigFlags adds flags to the pflag.FlagSet to configure the daemon func installCommonConfigFlags(conf *config.Config, flags *pflag.FlagSet) error { var maxConcurrentDownloads, maxConcurrentUploads, maxDownloadAttempts int - defaultPidFile, err := getDefaultPidFile() + var err error + conf.Pidfile, err = getDefaultPidFile() if err != nil { return err } - defaultDataRoot, err := getDefaultDataRoot() + conf.Root, err = getDefaultDataRoot() if err != nil { return err } - defaultExecRoot, err := getDefaultExecRoot() + conf.ExecRoot, err = getDefaultExecRoot() if err != nil { return err } @@ -31,9 +32,9 @@ func installCommonConfigFlags(conf *config.Config, flags *pflag.FlagSet) error { flags.Var(opts.NewNamedListOptsRef("storage-opts", &conf.GraphOptions, nil), "storage-opt", "Storage driver options") flags.Var(opts.NewNamedListOptsRef("authorization-plugins", &conf.AuthorizationPlugins, nil), "authorization-plugin", "Authorization plugins to load") flags.Var(opts.NewNamedListOptsRef("exec-opts", &conf.ExecOptions, nil), "exec-opt", "Runtime execution options") - flags.StringVarP(&conf.Pidfile, "pidfile", "p", defaultPidFile, "Path to use for daemon PID file") - flags.StringVar(&conf.Root, "data-root", defaultDataRoot, "Root directory of persistent Docker state") - flags.StringVar(&conf.ExecRoot, "exec-root", defaultExecRoot, "Root directory for execution state files") + flags.StringVarP(&conf.Pidfile, "pidfile", "p", conf.Pidfile, "Path to use for daemon PID file") + flags.StringVar(&conf.Root, "data-root", conf.Root, "Root directory of persistent Docker state") + flags.StringVar(&conf.ExecRoot, "exec-root", conf.ExecRoot, "Root directory for execution state files") flags.StringVar(&conf.ContainerdAddr, "containerd", "", "containerd grpc address") flags.BoolVar(&conf.CriContainerd, "cri-containerd", false, "start containerd with cri") @@ -78,7 +79,7 @@ func installCommonConfigFlags(conf *config.Config, flags *pflag.FlagSet) error { // "--graph" is "soft-deprecated" in favor of "data-root". This flag was added // before Docker 1.0, so won't be removed, only hidden, to discourage its usage. - flags.StringVarP(&conf.Root, "graph", "g", defaultDataRoot, "Root of the Docker runtime") + flags.StringVarP(&conf.Root, "graph", "g", conf.Root, "Root of the Docker runtime") _ = flags.MarkHidden("graph") flags.BoolVarP(&conf.AutoRestart, "restart", "r", true, "--restart on the daemon has been deprecated in favor of --restart policies on docker run") _ = flags.MarkDeprecated("restart", "Please use a restart policy on docker run") From 83f67c63a64b4088f5c53c781fb5734c37fc3bf8 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 23 Apr 2022 14:17:51 +0200 Subject: [PATCH 4/8] cmd/dockerd: combine installRegistryServiceFlags with installCommonConfigFlags There's no compelling reason to keep it in a separate function Signed-off-by: Sebastiaan van Stijn --- cmd/dockerd/config.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/cmd/dockerd/config.go b/cmd/dockerd/config.go index a9b9869142..8c2578339f 100644 --- a/cmd/dockerd/config.go +++ b/cmd/dockerd/config.go @@ -27,7 +27,14 @@ func installCommonConfigFlags(conf *config.Config, flags *pflag.FlagSet) error { return err } - installRegistryServiceFlags(&conf.ServiceOptions, flags) + var ( + allowNonDistributable = opts.NewNamedListOptsRef("allow-nondistributable-artifacts", &conf.AllowNondistributableArtifacts, registry.ValidateIndexName) + registryMirrors = opts.NewNamedListOptsRef("registry-mirrors", &conf.Mirrors, registry.ValidateMirror) + insecureRegistries = opts.NewNamedListOptsRef("insecure-registries", &conf.InsecureRegistries, registry.ValidateIndexName) + ) + flags.Var(allowNonDistributable, "allow-nondistributable-artifacts", "Allow push of nondistributable artifacts to registry") + flags.Var(registryMirrors, "registry-mirror", "Preferred Docker registry mirror") + flags.Var(insecureRegistries, "insecure-registry", "Enable insecure registry communication") flags.Var(opts.NewNamedListOptsRef("storage-opts", &conf.GraphOptions, nil), "storage-opt", "Storage driver options") flags.Var(opts.NewNamedListOptsRef("authorization-plugins", &conf.AuthorizationPlugins, nil), "authorization-plugin", "Authorization plugins to load") @@ -92,13 +99,3 @@ func installCommonConfigFlags(conf *config.Config, flags *pflag.FlagSet) error { return nil } - -func installRegistryServiceFlags(options *registry.ServiceOptions, flags *pflag.FlagSet) { - ana := opts.NewNamedListOptsRef("allow-nondistributable-artifacts", &options.AllowNondistributableArtifacts, registry.ValidateIndexName) - mirrors := opts.NewNamedListOptsRef("registry-mirrors", &options.Mirrors, registry.ValidateMirror) - insecureRegistries := opts.NewNamedListOptsRef("insecure-registries", &options.InsecureRegistries, registry.ValidateIndexName) - - flags.Var(ana, "allow-nondistributable-artifacts", "Allow push of nondistributable artifacts to registry") - flags.Var(mirrors, "registry-mirror", "Preferred Docker registry mirror") - flags.Var(insecureRegistries, "insecure-registry", "Enable insecure registry communication") -} From 92d9e892b53e9193726c1a0135b3050865252e05 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 23 Apr 2022 17:23:24 +0200 Subject: [PATCH 5/8] cmd/dockerd: combine installUnixConfigFlags with installConfigFlags installConfigFlags already has separate implementations for Linux and Windows, so no need to further differentiate. Signed-off-by: Sebastiaan van Stijn --- cmd/dockerd/config_common_unix.go | 20 -------------------- cmd/dockerd/config_unix.go | 14 +++++++++++--- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/cmd/dockerd/config_common_unix.go b/cmd/dockerd/config_common_unix.go index dfbc3e2e3d..ab64539fe3 100644 --- a/cmd/dockerd/config_common_unix.go +++ b/cmd/dockerd/config_common_unix.go @@ -6,11 +6,7 @@ package main import ( "path/filepath" - "github.com/docker/docker/api/types" - "github.com/docker/docker/daemon/config" - "github.com/docker/docker/opts" "github.com/docker/docker/pkg/homedir" - "github.com/spf13/pflag" ) func getDefaultPidFile() (string, error) { @@ -45,19 +41,3 @@ func getDefaultExecRoot() (string, error) { } return filepath.Join(runtimeDir, "docker"), nil } - -// installUnixConfigFlags adds command-line options to the top-level flag parser for -// the current process that are common across Unix platforms. -func installUnixConfigFlags(conf *config.Config, flags *pflag.FlagSet) { - conf.Runtimes = make(map[string]types.Runtime) - - flags.StringVarP(&conf.SocketGroup, "group", "G", "docker", "Group for the unix socket") - flags.StringVar(&conf.BridgeConfig.IP, "bip", "", "Specify network bridge IP") - flags.StringVarP(&conf.BridgeConfig.Iface, "bridge", "b", "", "Attach containers to a network bridge") - flags.StringVar(&conf.BridgeConfig.FixedCIDR, "fixed-cidr", "", "IPv4 subnet for fixed IPs") - flags.Var(opts.NewIPOpt(&conf.BridgeConfig.DefaultGatewayIPv4, ""), "default-gateway", "Container default gateway IPv4 address") - flags.Var(opts.NewIPOpt(&conf.BridgeConfig.DefaultGatewayIPv6, ""), "default-gateway-v6", "Container default gateway IPv6 address") - flags.BoolVar(&conf.BridgeConfig.InterContainerCommunication, "icc", true, "Enable inter-container communication") - flags.Var(opts.NewIPOpt(&conf.BridgeConfig.DefaultIP, "0.0.0.0"), "ip", "Default IP when binding container ports") - flags.Var(opts.NewNamedRuntimeOpt("runtimes", &conf.Runtimes, config.StockRuntimeName), "add-runtime", "Register an additional OCI compatible runtime") -} diff --git a/cmd/dockerd/config_unix.go b/cmd/dockerd/config_unix.go index 21a88f5091..6e6aa81bed 100644 --- a/cmd/dockerd/config_unix.go +++ b/cmd/dockerd/config_unix.go @@ -8,6 +8,7 @@ import ( "path/filepath" "github.com/containerd/cgroups" + "github.com/docker/docker/api/types" "github.com/docker/docker/daemon/config" "github.com/docker/docker/opts" "github.com/docker/docker/pkg/homedir" @@ -25,16 +26,16 @@ func installConfigFlags(conf *config.Config, flags *pflag.FlagSet) error { return err } - // Then install flags common to unix platforms - installUnixConfigFlags(conf, flags) - conf.Ulimits = make(map[string]*units.Ulimit) conf.NetworkConfig.DefaultAddressPools = opts.PoolsOpt{} // Set default value for `--default-shm-size` conf.ShmSize = opts.MemBytes(config.DefaultShmSize) + conf.Runtimes = make(map[string]types.Runtime) // Then platform-specific install flags + flags.Var(opts.NewNamedRuntimeOpt("runtimes", &conf.Runtimes, config.StockRuntimeName), "add-runtime", "Register an additional OCI compatible runtime") + flags.StringVarP(&conf.SocketGroup, "group", "G", "docker", "Group for the unix socket") flags.StringVarP(&conf.GraphDriver, "storage-driver", "s", "", "Storage driver to use") flags.BoolVar(&conf.EnableSelinuxSupport, "selinux-enabled", false, "Enable selinux support") flags.Var(opts.NewNamedUlimitOpt("default-ulimits", &conf.Ulimits), "default-ulimit", "Default ulimits for containers") @@ -43,7 +44,14 @@ func installConfigFlags(conf *config.Config, flags *pflag.FlagSet) error { flags.BoolVar(&conf.BridgeConfig.EnableIPForward, "ip-forward", true, "Enable net.ipv4.ip_forward") flags.BoolVar(&conf.BridgeConfig.EnableIPMasq, "ip-masq", true, "Enable IP masquerading") flags.BoolVar(&conf.BridgeConfig.EnableIPv6, "ipv6", false, "Enable IPv6 networking") + flags.StringVar(&conf.BridgeConfig.IP, "bip", "", "Specify network bridge IP") + flags.StringVarP(&conf.BridgeConfig.Iface, "bridge", "b", "", "Attach containers to a network bridge") + flags.StringVar(&conf.BridgeConfig.FixedCIDR, "fixed-cidr", "", "IPv4 subnet for fixed IPs") flags.StringVar(&conf.BridgeConfig.FixedCIDRv6, "fixed-cidr-v6", "", "IPv6 subnet for fixed IPs") + flags.Var(opts.NewIPOpt(&conf.BridgeConfig.DefaultGatewayIPv4, ""), "default-gateway", "Container default gateway IPv4 address") + flags.Var(opts.NewIPOpt(&conf.BridgeConfig.DefaultGatewayIPv6, ""), "default-gateway-v6", "Container default gateway IPv6 address") + flags.BoolVar(&conf.BridgeConfig.InterContainerCommunication, "icc", true, "Enable inter-container communication") + flags.Var(opts.NewIPOpt(&conf.BridgeConfig.DefaultIP, "0.0.0.0"), "ip", "Default IP when binding container ports") flags.BoolVar(&conf.BridgeConfig.EnableUserlandProxy, "userland-proxy", true, "Use userland proxy for loopback traffic") defaultUserlandProxyPath := "" if rootless.RunningWithRootlessKit() { From 5d352f6a8754e8d1f2758669336280e5bae47968 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 27 Apr 2022 00:52:49 +0200 Subject: [PATCH 6/8] cmd/dockerd: remove config_common_unix.go This file was originally part of the work to support Solaris, and there's nothing "not common unix" anymmore, so merging the files. Signed-off-by: Sebastiaan van Stijn --- cmd/dockerd/config_common_unix.go | 43 ------------------------------- cmd/dockerd/config_unix.go | 33 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 43 deletions(-) delete mode 100644 cmd/dockerd/config_common_unix.go diff --git a/cmd/dockerd/config_common_unix.go b/cmd/dockerd/config_common_unix.go deleted file mode 100644 index ab64539fe3..0000000000 --- a/cmd/dockerd/config_common_unix.go +++ /dev/null @@ -1,43 +0,0 @@ -//go:build linux || freebsd -// +build linux freebsd - -package main - -import ( - "path/filepath" - - "github.com/docker/docker/pkg/homedir" -) - -func getDefaultPidFile() (string, error) { - if !honorXDG { - return "/var/run/docker.pid", nil - } - runtimeDir, err := homedir.GetRuntimeDir() - if err != nil { - return "", err - } - return filepath.Join(runtimeDir, "docker.pid"), nil -} - -func getDefaultDataRoot() (string, error) { - if !honorXDG { - return "/var/lib/docker", nil - } - dataHome, err := homedir.GetDataHome() - if err != nil { - return "", err - } - return filepath.Join(dataHome, "docker"), nil -} - -func getDefaultExecRoot() (string, error) { - if !honorXDG { - return "/var/run/docker", nil - } - runtimeDir, err := homedir.GetRuntimeDir() - if err != nil { - return "", err - } - return filepath.Join(runtimeDir, "docker"), nil -} diff --git a/cmd/dockerd/config_unix.go b/cmd/dockerd/config_unix.go index 6e6aa81bed..51a27eb326 100644 --- a/cmd/dockerd/config_unix.go +++ b/cmd/dockerd/config_unix.go @@ -97,3 +97,36 @@ func configureCertsDir() { } } } + +func getDefaultPidFile() (string, error) { + if !honorXDG { + return "/var/run/docker.pid", nil + } + runtimeDir, err := homedir.GetRuntimeDir() + if err != nil { + return "", err + } + return filepath.Join(runtimeDir, "docker.pid"), nil +} + +func getDefaultDataRoot() (string, error) { + if !honorXDG { + return "/var/lib/docker", nil + } + dataHome, err := homedir.GetDataHome() + if err != nil { + return "", err + } + return filepath.Join(dataHome, "docker"), nil +} + +func getDefaultExecRoot() (string, error) { + if !honorXDG { + return "/var/run/docker", nil + } + runtimeDir, err := homedir.GetRuntimeDir() + if err != nil { + return "", err + } + return filepath.Join(runtimeDir, "docker"), nil +} From ccb75439fff3f09ac4fa4daea787db5cb5438468 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 23 Apr 2022 23:22:36 +0200 Subject: [PATCH 7/8] cmd/dockerd: use flags.IPVar() instead of custom type spf13/pflag now provides this out of the box, so no need to implement and use our own value-type for this. Signed-off-by: Sebastiaan van Stijn --- cmd/dockerd/config.go | 2 +- cmd/dockerd/config_unix.go | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/dockerd/config.go b/cmd/dockerd/config.go index 8c2578339f..f39ce8ed54 100644 --- a/cmd/dockerd/config.go +++ b/cmd/dockerd/config.go @@ -54,7 +54,7 @@ func installCommonConfigFlags(conf *config.Config, flags *pflag.FlagSet) error { flags.Var(opts.NewListOptsRef(&conf.DNS, opts.ValidateIPAddress), "dns", "DNS server to use") flags.Var(opts.NewNamedListOptsRef("dns-opts", &conf.DNSOptions, nil), "dns-opt", "DNS options to use") flags.Var(opts.NewListOptsRef(&conf.DNSSearch, opts.ValidateDNSSearch), "dns-search", "DNS search domains to use") - flags.Var(opts.NewIPOpt(&conf.HostGatewayIP, ""), "host-gateway-ip", "IP address that the special 'host-gateway' string in --add-host resolves to. Defaults to the IP address of the default bridge") + flags.IPVar(&conf.HostGatewayIP, "host-gateway-ip", nil, "IP address that the special 'host-gateway' string in --add-host resolves to. Defaults to the IP address of the default bridge") flags.Var(opts.NewNamedListOptsRef("labels", &conf.Labels, opts.ValidateLabel), "label", "Set key=value labels to the daemon") flags.StringVar(&conf.LogConfig.Type, "log-driver", "json-file", "Default driver for container logs") flags.Var(opts.NewNamedMapOpts("log-opts", conf.LogConfig.Config, nil), "log-opt", "Default log driver options for containers") diff --git a/cmd/dockerd/config_unix.go b/cmd/dockerd/config_unix.go index 51a27eb326..6f7d5daee2 100644 --- a/cmd/dockerd/config_unix.go +++ b/cmd/dockerd/config_unix.go @@ -4,6 +4,7 @@ package main import ( + "net" "os/exec" "path/filepath" @@ -48,10 +49,10 @@ func installConfigFlags(conf *config.Config, flags *pflag.FlagSet) error { flags.StringVarP(&conf.BridgeConfig.Iface, "bridge", "b", "", "Attach containers to a network bridge") flags.StringVar(&conf.BridgeConfig.FixedCIDR, "fixed-cidr", "", "IPv4 subnet for fixed IPs") flags.StringVar(&conf.BridgeConfig.FixedCIDRv6, "fixed-cidr-v6", "", "IPv6 subnet for fixed IPs") - flags.Var(opts.NewIPOpt(&conf.BridgeConfig.DefaultGatewayIPv4, ""), "default-gateway", "Container default gateway IPv4 address") - flags.Var(opts.NewIPOpt(&conf.BridgeConfig.DefaultGatewayIPv6, ""), "default-gateway-v6", "Container default gateway IPv6 address") + flags.IPVar(&conf.BridgeConfig.DefaultGatewayIPv4, "default-gateway", nil, "Container default gateway IPv4 address") + flags.IPVar(&conf.BridgeConfig.DefaultGatewayIPv6, "default-gateway-v6", nil, "Container default gateway IPv6 address") flags.BoolVar(&conf.BridgeConfig.InterContainerCommunication, "icc", true, "Enable inter-container communication") - flags.Var(opts.NewIPOpt(&conf.BridgeConfig.DefaultIP, "0.0.0.0"), "ip", "Default IP when binding container ports") + flags.IPVar(&conf.BridgeConfig.DefaultIP, "ip", net.IPv4zero, "Default IP when binding container ports") flags.BoolVar(&conf.BridgeConfig.EnableUserlandProxy, "userland-proxy", true, "Use userland proxy for loopback traffic") defaultUserlandProxyPath := "" if rootless.RunningWithRootlessKit() { From 21dac5e441d608260e700984fd6219753f038b28 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 23 Apr 2022 23:29:26 +0200 Subject: [PATCH 8/8] opts: remove IPOpt as it's no longer used This option type was only used by us, and had no external consumers, so we can remove it. Signed-off-by: Sebastiaan van Stijn --- opts/ip.go | 47 ------------------------------------------ opts/ip_test.go | 54 ------------------------------------------------- 2 files changed, 101 deletions(-) delete mode 100644 opts/ip.go delete mode 100644 opts/ip_test.go diff --git a/opts/ip.go b/opts/ip.go deleted file mode 100644 index cfbff3a9fd..0000000000 --- a/opts/ip.go +++ /dev/null @@ -1,47 +0,0 @@ -package opts // import "github.com/docker/docker/opts" - -import ( - "fmt" - "net" -) - -// IPOpt holds an IP. It is used to store values from CLI flags. -type IPOpt struct { - *net.IP -} - -// NewIPOpt creates a new IPOpt from a reference net.IP and a -// string representation of an IP. If the string is not a valid -// IP it will fallback to the specified reference. -func NewIPOpt(ref *net.IP, defaultVal string) *IPOpt { - o := &IPOpt{ - IP: ref, - } - o.Set(defaultVal) - return o -} - -// Set sets an IPv4 or IPv6 address from a given string. If the given -// string is not parsable as an IP address it returns an error. -func (o *IPOpt) Set(val string) error { - ip := net.ParseIP(val) - if ip == nil { - return fmt.Errorf("%s is not an ip address", val) - } - *o.IP = ip - return nil -} - -// String returns the IP address stored in the IPOpt. If stored IP is a -// nil pointer, it returns an empty string. -func (o *IPOpt) String() string { - if *o.IP == nil { - return "" - } - return o.IP.String() -} - -// Type returns the type of the option -func (o *IPOpt) Type() string { - return "ip" -} diff --git a/opts/ip_test.go b/opts/ip_test.go deleted file mode 100644 index 966d7f21ec..0000000000 --- a/opts/ip_test.go +++ /dev/null @@ -1,54 +0,0 @@ -package opts // import "github.com/docker/docker/opts" - -import ( - "net" - "testing" -) - -func TestIpOptString(t *testing.T) { - addresses := []string{"", "0.0.0.0"} - var ip net.IP - - for _, address := range addresses { - stringAddress := NewIPOpt(&ip, address).String() - if stringAddress != address { - t.Fatalf("IpOpt string should be `%s`, not `%s`", address, stringAddress) - } - } -} - -func TestNewIpOptInvalidDefaultVal(t *testing.T) { - ip := net.IPv4(127, 0, 0, 1) - defaultVal := "Not an ip" - - ipOpt := NewIPOpt(&ip, defaultVal) - - expected := "127.0.0.1" - if ipOpt.String() != expected { - t.Fatalf("Expected [%v], got [%v]", expected, ipOpt.String()) - } -} - -func TestNewIpOptValidDefaultVal(t *testing.T) { - ip := net.IPv4(127, 0, 0, 1) - defaultVal := "192.168.1.1" - - ipOpt := NewIPOpt(&ip, defaultVal) - - expected := "192.168.1.1" - if ipOpt.String() != expected { - t.Fatalf("Expected [%v], got [%v]", expected, ipOpt.String()) - } -} - -func TestIpOptSetInvalidVal(t *testing.T) { - ip := net.IPv4(127, 0, 0, 1) - ipOpt := &IPOpt{IP: &ip} - - invalidIP := "invalid ip" - expectedError := "invalid ip is not an ip address" - err := ipOpt.Set(invalidIP) - if err == nil || err.Error() != expectedError { - t.Fatalf("Expected an Error with [%v], got [%v]", expectedError, err.Error()) - } -}