db63f937 |
package config |
1cbdaeba |
import ( |
677a6b35 |
"bytes"
"encoding/json" |
934328d8 |
"errors" |
677a6b35 |
"fmt"
"io"
"io/ioutil" |
0f8119a8 |
"os" |
db63f937 |
"reflect" |
fb833947 |
"runtime" |
677a6b35 |
"strings"
"sync"
|
db63f937 |
daemondiscovery "github.com/docker/docker/daemon/discovery" |
353b7c8e |
"github.com/docker/docker/opts" |
38de272b |
"github.com/docker/docker/pkg/authorization" |
677a6b35 |
"github.com/docker/docker/pkg/discovery" |
59586d02 |
"github.com/docker/docker/registry" |
677a6b35 |
"github.com/imdario/mergo" |
1009e6a4 |
"github.com/sirupsen/logrus" |
fb833947 |
"github.com/spf13/pflag" |
1cbdaeba |
)
|
c712e74b |
const ( |
db63f937 |
// DefaultMaxConcurrentDownloads is the default value for |
7368e41c |
// maximum number of downloads that
// may take place at a time for each pull. |
db63f937 |
DefaultMaxConcurrentDownloads = 3
// DefaultMaxConcurrentUploads is the default value for |
7368e41c |
// maximum number of uploads that
// may take place at a time for each push. |
db63f937 |
DefaultMaxConcurrentUploads = 5
// StockRuntimeName is the reserved name/alias used to represent the |
69af7d0d |
// OCI runtime being shipped with the docker daemon package. |
db63f937 |
StockRuntimeName = "runc"
// DefaultShmSize is the default value for container's shm size
DefaultShmSize = int64(67108864)
// DefaultNetworkMtu is the default value for network MTU
DefaultNetworkMtu = 1500
// DisableNetworkBridge is the default value of the option to disable network bridge
DisableNetworkBridge = "none" |
17b12887 |
// DefaultInitBinary is the name of the default init binary
DefaultInitBinary = "docker-init" |
d7be6b2d |
)
|
b6766e30 |
// flatOptions contains configuration keys
// that MUST NOT be parsed as deep structures.
// Use this to differentiate these options
// with others like the ones in CommonTLSOptions.
var flatOptions = map[string]bool{
"cluster-store-opts": true,
"log-opts": true, |
7b2e5216 |
"runtimes": true, |
e3164842 |
"default-ulimits": true, |
b6766e30 |
}
|
677a6b35 |
// LogConfig represents the default log configuration.
// It includes json tags to deserialize configuration from a file |
5c161ade |
// using the same names that the flags in the command line use. |
677a6b35 |
type LogConfig struct {
Type string `json:"log-driver,omitempty"`
Config map[string]string `json:"log-opts,omitempty"`
}
|
ff3525c8 |
// commonBridgeConfig stores all the platform-common bridge driver specific
// configuration.
type commonBridgeConfig struct {
Iface string `json:"bridge,omitempty"`
FixedCIDR string `json:"fixed-cidr,omitempty"`
}
|
677a6b35 |
// CommonTLSOptions defines TLS configuration for the daemon server.
// It includes json tags to deserialize configuration from a file |
5c161ade |
// using the same names that the flags in the command line use. |
677a6b35 |
type CommonTLSOptions struct {
CAFile string `json:"tlscacert,omitempty"`
CertFile string `json:"tlscert,omitempty"`
KeyFile string `json:"tlskey,omitempty"`
}
|
5c161ade |
// CommonConfig defines the configuration of a docker daemon which is |
b3bca3af |
// common across platforms. |
677a6b35 |
// It includes json tags to deserialize configuration from a file |
5c161ade |
// using the same names that the flags in the command line use. |
b3bca3af |
type CommonConfig struct { |
a97e4579 |
AuthzMiddleware *authorization.Middleware `json:"-"`
AuthorizationPlugins []string `json:"authorization-plugins,omitempty"` // AuthorizationPlugins holds list of authorization plugins
AutoRestart bool `json:"-"`
Context map[string][]string `json:"-"`
DisableBridge bool `json:"-"`
DNS []string `json:"dns,omitempty"`
DNSOptions []string `json:"dns-opts,omitempty"`
DNSSearch []string `json:"dns-search,omitempty"`
ExecOptions []string `json:"exec-opts,omitempty"`
GraphDriver string `json:"storage-driver,omitempty"`
GraphOptions []string `json:"storage-opts,omitempty"`
Labels []string `json:"labels,omitempty"`
Mtu int `json:"mtu,omitempty"`
NetworkDiagnosticPort int `json:"network-diagnostic-port,omitempty"`
Pidfile string `json:"pidfile,omitempty"`
RawLogs bool `json:"raw-logs,omitempty"`
RootDeprecated string `json:"graph,omitempty"`
Root string `json:"data-root,omitempty"`
ExecRoot string `json:"exec-root,omitempty"`
SocketGroup string `json:"group,omitempty"`
CorsHeaders string `json:"api-cors-header,omitempty"` |
b9454223 |
|
e428c824 |
// TrustKeyPath is used to generate the daemon ID and for signing schema 1 manifests
// when pushing to a registry which does not support schema 2. This field is marked as
// deprecated because schema 1 manifests are deprecated in favor of schema 2 and the
// daemon ID will use a dedicated identifier not shared with exported signatures.
TrustKeyPath string `json:"deprecated-key-path,omitempty"`
|
b9454223 |
// LiveRestoreEnabled determines whether we should keep containers
// alive upon daemon shutdown/start
LiveRestoreEnabled bool `json:"live-restore,omitempty"` |
7d193ef1 |
// ClusterStore is the storage backend used for the cluster information. It is used by both
// multihost networking (to store networks and endpoints information) and by the node discovery
// mechanism. |
677a6b35 |
ClusterStore string `json:"cluster-store,omitempty"` |
7d193ef1 |
|
124792a8 |
// ClusterOpts is used to pass options to the discovery package for tuning libkv settings, such
// as TLS configuration settings. |
677a6b35 |
ClusterOpts map[string]string `json:"cluster-store-opts,omitempty"` |
124792a8 |
|
7d193ef1 |
// ClusterAdvertise is the network endpoint that the Engine advertises for the purpose of node
// discovery. This should be a 'host:port' combination on which that daemon instance is
// reachable by other hosts. |
677a6b35 |
ClusterAdvertise string `json:"cluster-advertise,omitempty"`
|
7368e41c |
// MaxConcurrentDownloads is the maximum number of downloads that
// may take place at a time for each pull.
MaxConcurrentDownloads *int `json:"max-concurrent-downloads,omitempty"`
// MaxConcurrentUploads is the maximum number of uploads that
// may take place at a time for each push.
MaxConcurrentUploads *int `json:"max-concurrent-uploads,omitempty"`
|
d7be6b2d |
// ShutdownTimeout is the timeout value (in seconds) the daemon will wait for the container
// to stop when daemon is being shutdown
ShutdownTimeout int `json:"shutdown-timeout,omitempty"`
|
5e80ac0d |
Debug bool `json:"debug,omitempty"`
Hosts []string `json:"hosts,omitempty"`
LogLevel string `json:"log-level,omitempty"`
TLS bool `json:"tls,omitempty"`
TLSVerify bool `json:"tlsverify,omitempty"`
// Embedded structs that allow config
// deserialization without the full struct.
CommonTLSOptions |
a0ccd0d4 |
// SwarmDefaultAdvertiseAddr is the default host/IP or network interface
// to use if a wildcard address is specified in the ListenAddr value
// given to the /swarm/init endpoint and no advertise address is
// specified.
SwarmDefaultAdvertiseAddr string `json:"swarm-default-advertise-addr"` |
3343d234 |
MetricsAddress string `json:"metrics-addr"` |
a0ccd0d4 |
|
5e80ac0d |
LogConfig |
db63f937 |
BridgeConfig // bridgeConfig holds bridge network specific configuration. |
59586d02 |
registry.ServiceOptions |
677a6b35 |
|
db63f937 |
sync.Mutex
// FIXME(vdemeester) This part is not that clear and is mainly dependent on cli flags
// It should probably be handled outside this package. |
8378dcf4 |
ValuesSet map[string]interface{} `json:"-"` |
7781a1bf |
Experimental bool `json:"experimental"` // Experimental indicates whether experimental features should be exposed or not |
87e1464c |
// Exposed node Generic Resources |
ebe14310 |
// e.g: ["orange=red", "orange=green", "orange=blue", "apple=3"]
NodeGenericResources []string `json:"node-generic-resources,omitempty"` |
f9f25ca5 |
// NetworkControlPlaneMTU allows to specify the control plane MTU, this will allow to optimize the network use in some components
NetworkControlPlaneMTU int `json:"network-control-plane-mtu,omitempty"` |
ddae20c0 |
// ContainerAddr is the address used to connect to containerd if we're
// not starting it ourselves
ContainerdAddr string `json:"containerd,omitempty"` |
1cbdaeba |
} |
0d1a8251 |
|
cd344697 |
// IsValueSet returns true if a configuration value
// was explicitly set in the configuration file. |
db63f937 |
func (conf *Config) IsValueSet(name string) bool {
if conf.ValuesSet == nil { |
cd344697 |
return false
} |
db63f937 |
_, ok := conf.ValuesSet[name] |
cd344697 |
return ok
}
|
db63f937 |
// New returns a new fully initialized Config struct
func New() *Config { |
fb833947 |
config := Config{}
config.LogConfig.Config = make(map[string]string)
config.ClusterOpts = make(map[string]string)
if runtime.GOOS != "linux" {
config.V2Only = true
}
return &config
}
|
db63f937 |
// ParseClusterAdvertiseSettings parses the specified advertise settings
func ParseClusterAdvertiseSettings(clusterStore, clusterAdvertise string) (string, error) { |
677a6b35 |
if clusterAdvertise == "" { |
db63f937 |
return "", daemondiscovery.ErrDiscoveryDisabled |
677a6b35 |
}
if clusterStore == "" { |
514adcf4 |
return "", errors.New("invalid cluster configuration. --cluster-advertise must be accompanied by --cluster-store configuration") |
677a6b35 |
}
advertise, err := discovery.ParseAdvertise(clusterAdvertise)
if err != nil {
return "", fmt.Errorf("discovery advertise parsing failed (%v)", err)
}
return advertise, nil
}
|
6fb05778 |
// GetConflictFreeLabels validates Labels for conflict |
e4c9079d |
// In swarm the duplicates for labels are removed
// so we only take same values here, no conflict values
// If the key-value is the same we will only take the last label
func GetConflictFreeLabels(labels []string) ([]string, error) {
labelMap := map[string]string{}
for _, label := range labels {
stringSlice := strings.SplitN(label, "=", 2)
if len(stringSlice) > 1 {
// If there is a conflict we will return an error
if v, ok := labelMap[stringSlice[0]]; ok && v != stringSlice[1] {
return nil, fmt.Errorf("conflict labels for %s=%s and %s=%s", stringSlice[0], stringSlice[1], stringSlice[0], v)
}
labelMap[stringSlice[0]] = stringSlice[1]
}
}
newLabels := []string{}
for k, v := range labelMap {
newLabels = append(newLabels, fmt.Sprintf("%s=%s", k, v))
}
return newLabels, nil
}
|
db63f937 |
// Reload reads the configuration in the host and reloads the daemon and server.
func Reload(configFile string, flags *pflag.FlagSet, reload func(*Config)) error { |
677a6b35 |
logrus.Infof("Got signal to reload configuration, reloading from: %s", configFile)
newConfig, err := getConflictFreeConfiguration(configFile, flags)
if err != nil { |
0f8119a8 |
if flags.Changed("config-file") || !os.IsNotExist(err) {
return fmt.Errorf("unable to configure the Docker daemon with file %s: %v", configFile, err)
}
newConfig = New() |
677a6b35 |
} |
825b5822 |
|
db63f937 |
if err := Validate(newConfig); err != nil { |
825b5822 |
return fmt.Errorf("file configuration validation failed (%v)", err)
}
|
8c632233 |
// Check if duplicate label-keys with different values are found
newLabels, err := GetConflictFreeLabels(newConfig.Labels)
if err != nil {
return err |
e4c9079d |
} |
8c632233 |
newConfig.Labels = newLabels |
e4c9079d |
|
31cb96dc |
reload(newConfig)
return nil
}
// boolValue is an interface that boolean value flags implement
// to tell the command line how to make -name equivalent to -name=true.
type boolValue interface {
IsBoolFlag() bool |
677a6b35 |
}
// MergeDaemonConfigurations reads a configuration file,
// loads the file configuration in an isolated structure,
// and merges the configuration provided from flags on top
// if there are no conflicts. |
fb833947 |
func MergeDaemonConfigurations(flagsConfig *Config, flags *pflag.FlagSet, configFile string) (*Config, error) { |
677a6b35 |
fileConfig, err := getConflictFreeConfiguration(configFile, flags)
if err != nil {
return nil, err
}
|
db63f937 |
if err := Validate(fileConfig); err != nil { |
e59af2ab |
return nil, fmt.Errorf("configuration validation from file failed (%v)", err) |
825b5822 |
}
|
677a6b35 |
// merge flags configuration on top of the file configuration
if err := mergo.Merge(fileConfig, flagsConfig); err != nil {
return nil, err
}
|
7b2e5216 |
// We need to validate again once both fileConfig and flagsConfig
// have been merged |
db63f937 |
if err := Validate(fileConfig); err != nil { |
e59af2ab |
return nil, fmt.Errorf("merged configuration validation from file and command line flags failed (%v)", err) |
7b2e5216 |
}
|
677a6b35 |
return fileConfig, nil
}
// getConflictFreeConfiguration loads the configuration from a JSON file.
// It compares that configuration with the one provided by the flags,
// and returns an error if there are conflicts. |
fb833947 |
func getConflictFreeConfiguration(configFile string, flags *pflag.FlagSet) (*Config, error) { |
677a6b35 |
b, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, err
}
|
cd344697 |
var config Config |
677a6b35 |
var reader io.Reader
if flags != nil {
var jsonConfig map[string]interface{}
reader = bytes.NewReader(b)
if err := json.NewDecoder(reader).Decode(&jsonConfig); err != nil {
return nil, err
}
|
cd344697 |
configSet := configValuesSet(jsonConfig)
if err := findConfigurationConflicts(configSet, flags); err != nil { |
677a6b35 |
return nil, err
} |
cd344697 |
|
31cb96dc |
// Override flag values to make sure the values set in the config file with nullable values, like `false`, |
644a7426 |
// are not overridden by default truthy values from the flags that were not explicitly set. |
31cb96dc |
// See https://github.com/docker/docker/issues/20289 for an example.
//
// TODO: Rewrite configuration logic to avoid same issue with other nullable values, like numbers.
namedOptions := make(map[string]interface{})
for key, value := range configSet { |
31bf9ca0 |
f := flags.Lookup(key) |
31cb96dc |
if f == nil { // ignore named flags that don't match
namedOptions[key] = value
continue
}
if _, ok := f.Value.(boolValue); ok {
f.Value.Set(fmt.Sprintf("%v", value))
}
}
if len(namedOptions) > 0 {
// set also default for mergeVal flags that are boolValue at the same time. |
fb833947 |
flags.VisitAll(func(f *pflag.Flag) { |
31cb96dc |
if opt, named := f.Value.(opts.NamedOption); named {
v, set := namedOptions[opt.Name()]
_, boolean := f.Value.(boolValue)
if set && boolean {
f.Value.Set(fmt.Sprintf("%v", v))
}
}
})
}
|
db63f937 |
config.ValuesSet = configSet |
677a6b35 |
}
reader = bytes.NewReader(b) |
261ef1fa |
if err := json.NewDecoder(reader).Decode(&config); err != nil {
return nil, err
}
if config.RootDeprecated != "" {
logrus.Warn(`The "graph" config file option is deprecated. Please use "data-root" instead.`)
if config.Root != "" {
return nil, fmt.Errorf(`cannot specify both "graph" and "data-root" config file options`)
}
config.Root = config.RootDeprecated
}
return &config, nil |
677a6b35 |
}
|
cd344697 |
// configValuesSet returns the configuration values explicitly set in the file.
func configValuesSet(config map[string]interface{}) map[string]interface{} { |
677a6b35 |
flatten := make(map[string]interface{})
for k, v := range config { |
b6766e30 |
if m, isMap := v.(map[string]interface{}); isMap && !flatOptions[k] { |
677a6b35 |
for km, vm := range m {
flatten[km] = vm
} |
b6766e30 |
continue |
677a6b35 |
} |
b6766e30 |
flatten[k] = v |
677a6b35 |
} |
cd344697 |
return flatten
}
// findConfigurationConflicts iterates over the provided flags searching for |
ed403867 |
// duplicated configurations and unknown keys. It returns an error with all the conflicts if |
cd344697 |
// it finds any. |
fb833947 |
func findConfigurationConflicts(config map[string]interface{}, flags *pflag.FlagSet) error { |
ed403867 |
// 1. Search keys from the file that we don't recognize as flags.
unknownKeys := make(map[string]interface{})
for key, value := range config { |
31bf9ca0 |
if flag := flags.Lookup(key); flag == nil { |
ed403867 |
unknownKeys[key] = value
}
}
|
5e80ac0d |
// 2. Discard values that implement NamedOption.
// Their configuration name differs from their flag name, like `labels` and `label`. |
31cb96dc |
if len(unknownKeys) > 0 { |
fb833947 |
unknownNamedConflicts := func(f *pflag.Flag) { |
31cb96dc |
if namedOption, ok := f.Value.(opts.NamedOption); ok {
if _, valid := unknownKeys[namedOption.Name()]; valid {
delete(unknownKeys, namedOption.Name())
} |
ed403867 |
}
} |
31cb96dc |
flags.VisitAll(unknownNamedConflicts) |
ed403867 |
}
if len(unknownKeys) > 0 {
var unknown []string
for key := range unknownKeys {
unknown = append(unknown, key)
}
return fmt.Errorf("the following directives don't match any configuration option: %s", strings.Join(unknown, ", "))
} |
677a6b35 |
|
ed403867 |
var conflicts []string |
677a6b35 |
printConflict := func(name string, flagValue, fileValue interface{}) string {
return fmt.Sprintf("%s: (from flag: %v, from file: %v)", name, flagValue, fileValue)
}
|
ed403867 |
// 3. Search keys that are present as a flag and as a file option. |
fb833947 |
duplicatedConflicts := func(f *pflag.Flag) { |
677a6b35 |
// search option name in the json configuration payload if the value is a named option
if namedOption, ok := f.Value.(opts.NamedOption); ok { |
cd344697 |
if optsValue, ok := config[namedOption.Name()]; ok { |
677a6b35 |
conflicts = append(conflicts, printConflict(namedOption.Name(), f.Value.String(), optsValue))
}
} else { |
fb833947 |
// search flag name in the json configuration payload
for _, name := range []string{f.Name, f.Shorthand} { |
cd344697 |
if value, ok := config[name]; ok { |
677a6b35 |
conflicts = append(conflicts, printConflict(name, f.Value.String(), value))
break
}
}
}
}
|
ed403867 |
flags.Visit(duplicatedConflicts) |
677a6b35 |
if len(conflicts) > 0 {
return fmt.Errorf("the following directives are specified both as a flag and in the configuration file: %s", strings.Join(conflicts, ", "))
}
return nil |
353b7c8e |
} |
825b5822 |
|
db63f937 |
// Validate validates some specific configs. |
7368e41c |
// such as config.DNS, config.Labels, config.DNSSearch,
// as well as config.MaxConcurrentDownloads, config.MaxConcurrentUploads. |
db63f937 |
func Validate(config *Config) error { |
825b5822 |
// validate DNS
for _, dns := range config.DNS {
if _, err := opts.ValidateIPAddress(dns); err != nil {
return err
}
}
// validate DNSSearch
for _, dnsSearch := range config.DNSSearch {
if _, err := opts.ValidateDNSSearch(dnsSearch); err != nil {
return err
}
}
// validate Labels
for _, label := range config.Labels {
if _, err := opts.ValidateLabel(label); err != nil {
return err
}
} |
7368e41c |
// validate MaxConcurrentDownloads |
e59af2ab |
if config.MaxConcurrentDownloads != nil && *config.MaxConcurrentDownloads < 0 { |
7368e41c |
return fmt.Errorf("invalid max concurrent downloads: %d", *config.MaxConcurrentDownloads)
}
// validate MaxConcurrentUploads |
e59af2ab |
if config.MaxConcurrentUploads != nil && *config.MaxConcurrentUploads < 0 { |
7368e41c |
return fmt.Errorf("invalid max concurrent uploads: %d", *config.MaxConcurrentUploads)
} |
7b2e5216 |
// validate that "default" runtime is not reset
if runtimes := config.GetAllRuntimes(); len(runtimes) > 0 { |
db63f937 |
if _, ok := runtimes[StockRuntimeName]; ok {
return fmt.Errorf("runtime name '%s' is reserved", StockRuntimeName) |
7b2e5216 |
}
}
|
b68221c3 |
if _, err := ParseGenericResources(config.NodeGenericResources); err != nil { |
87e1464c |
return err
}
|
db63f937 |
if defaultRuntime := config.GetDefaultRuntimeName(); defaultRuntime != "" && defaultRuntime != StockRuntimeName { |
7b2e5216 |
runtimes := config.GetAllRuntimes()
if _, ok := runtimes[defaultRuntime]; !ok {
return fmt.Errorf("specified default runtime '%s' does not exist", defaultRuntime)
}
}
|
7120976d |
// validate platform-specific settings |
b4a63139 |
return config.ValidatePlatformConfig() |
825b5822 |
} |
1d1362bd |
|
db63f937 |
// ModifiedDiscoverySettings returns whether the discovery configuration has been modified or not.
func ModifiedDiscoverySettings(config *Config, backendType, advertise string, clusterOpts map[string]string) bool {
if config.ClusterStore != backendType || config.ClusterAdvertise != advertise {
return true
}
if (config.ClusterOpts == nil && clusterOpts == nil) ||
(config.ClusterOpts == nil && len(clusterOpts) == 0) ||
(len(config.ClusterOpts) == 0 && clusterOpts == nil) {
return false
}
return !reflect.DeepEqual(config.ClusterOpts, clusterOpts)
} |