github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/libnetwork/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/BurntSushi/toml"
     7  	"github.com/docker/docker/pkg/discovery"
     8  	"github.com/docker/docker/pkg/plugingetter"
     9  	"github.com/docker/go-connections/tlsconfig"
    10  	"github.com/docker/libkv/store"
    11  	"github.com/docker/libnetwork/cluster"
    12  	"github.com/docker/libnetwork/datastore"
    13  	"github.com/docker/libnetwork/ipamutils"
    14  	"github.com/docker/libnetwork/netlabel"
    15  	"github.com/docker/libnetwork/osl"
    16  	"github.com/sirupsen/logrus"
    17  )
    18  
    19  const (
    20  	warningThNetworkControlPlaneMTU = 1500
    21  	minimumNetworkControlPlaneMTU   = 500
    22  )
    23  
    24  // Config encapsulates configurations of various Libnetwork components
    25  type Config struct {
    26  	Daemon          DaemonCfg
    27  	Cluster         ClusterCfg
    28  	Scopes          map[string]*datastore.ScopeCfg
    29  	ActiveSandboxes map[string]interface{}
    30  	PluginGetter    plugingetter.PluginGetter
    31  }
    32  
    33  // DaemonCfg represents libnetwork core configuration
    34  type DaemonCfg struct {
    35  	Debug                  bool
    36  	Experimental           bool
    37  	DataDir                string
    38  	ExecRoot               string
    39  	DefaultNetwork         string
    40  	DefaultDriver          string
    41  	Labels                 []string
    42  	DriverCfg              map[string]interface{}
    43  	ClusterProvider        cluster.Provider
    44  	NetworkControlPlaneMTU int
    45  	DefaultAddressPool     []*ipamutils.NetworkToSplit
    46  }
    47  
    48  // ClusterCfg represents cluster configuration
    49  type ClusterCfg struct {
    50  	Watcher   discovery.Watcher
    51  	Address   string
    52  	Discovery string
    53  	Heartbeat uint64
    54  }
    55  
    56  // LoadDefaultScopes loads default scope configs for scopes which
    57  // doesn't have explicit user specified configs.
    58  func (c *Config) LoadDefaultScopes(dataDir string) {
    59  	for k, v := range datastore.DefaultScopes(dataDir) {
    60  		if _, ok := c.Scopes[k]; !ok {
    61  			c.Scopes[k] = v
    62  		}
    63  	}
    64  }
    65  
    66  // ParseConfig parses the libnetwork configuration file
    67  func ParseConfig(tomlCfgFile string) (*Config, error) {
    68  	cfg := &Config{
    69  		Scopes: map[string]*datastore.ScopeCfg{},
    70  	}
    71  
    72  	if _, err := toml.DecodeFile(tomlCfgFile, cfg); err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	cfg.LoadDefaultScopes(cfg.Daemon.DataDir)
    77  	return cfg, nil
    78  }
    79  
    80  // ParseConfigOptions parses the configuration options and returns
    81  // a reference to the corresponding Config structure
    82  func ParseConfigOptions(cfgOptions ...Option) *Config {
    83  	cfg := &Config{
    84  		Daemon: DaemonCfg{
    85  			DriverCfg: make(map[string]interface{}),
    86  		},
    87  		Scopes: make(map[string]*datastore.ScopeCfg),
    88  	}
    89  
    90  	cfg.ProcessOptions(cfgOptions...)
    91  	cfg.LoadDefaultScopes(cfg.Daemon.DataDir)
    92  
    93  	return cfg
    94  }
    95  
    96  // Option is an option setter function type used to pass various configurations
    97  // to the controller
    98  type Option func(c *Config)
    99  
   100  // OptionDefaultNetwork function returns an option setter for a default network
   101  func OptionDefaultNetwork(dn string) Option {
   102  	return func(c *Config) {
   103  		logrus.Debugf("Option DefaultNetwork: %s", dn)
   104  		c.Daemon.DefaultNetwork = strings.TrimSpace(dn)
   105  	}
   106  }
   107  
   108  // OptionDefaultDriver function returns an option setter for default driver
   109  func OptionDefaultDriver(dd string) Option {
   110  	return func(c *Config) {
   111  		logrus.Debugf("Option DefaultDriver: %s", dd)
   112  		c.Daemon.DefaultDriver = strings.TrimSpace(dd)
   113  	}
   114  }
   115  
   116  // OptionDefaultAddressPoolConfig function returns an option setter for default address pool
   117  func OptionDefaultAddressPoolConfig(addressPool []*ipamutils.NetworkToSplit) Option {
   118  	return func(c *Config) {
   119  		c.Daemon.DefaultAddressPool = addressPool
   120  	}
   121  }
   122  
   123  // OptionDriverConfig returns an option setter for driver configuration.
   124  func OptionDriverConfig(networkType string, config map[string]interface{}) Option {
   125  	return func(c *Config) {
   126  		c.Daemon.DriverCfg[networkType] = config
   127  	}
   128  }
   129  
   130  // OptionLabels function returns an option setter for labels
   131  func OptionLabels(labels []string) Option {
   132  	return func(c *Config) {
   133  		for _, label := range labels {
   134  			if strings.HasPrefix(label, netlabel.Prefix) {
   135  				c.Daemon.Labels = append(c.Daemon.Labels, label)
   136  			}
   137  		}
   138  	}
   139  }
   140  
   141  // OptionKVProvider function returns an option setter for kvstore provider
   142  func OptionKVProvider(provider string) Option {
   143  	return func(c *Config) {
   144  		logrus.Debugf("Option OptionKVProvider: %s", provider)
   145  		if _, ok := c.Scopes[datastore.GlobalScope]; !ok {
   146  			c.Scopes[datastore.GlobalScope] = &datastore.ScopeCfg{}
   147  		}
   148  		c.Scopes[datastore.GlobalScope].Client.Provider = strings.TrimSpace(provider)
   149  	}
   150  }
   151  
   152  // OptionKVProviderURL function returns an option setter for kvstore url
   153  func OptionKVProviderURL(url string) Option {
   154  	return func(c *Config) {
   155  		logrus.Debugf("Option OptionKVProviderURL: %s", url)
   156  		if _, ok := c.Scopes[datastore.GlobalScope]; !ok {
   157  			c.Scopes[datastore.GlobalScope] = &datastore.ScopeCfg{}
   158  		}
   159  		c.Scopes[datastore.GlobalScope].Client.Address = strings.TrimSpace(url)
   160  	}
   161  }
   162  
   163  // OptionKVOpts function returns an option setter for kvstore options
   164  func OptionKVOpts(opts map[string]string) Option {
   165  	return func(c *Config) {
   166  		if opts["kv.cacertfile"] != "" && opts["kv.certfile"] != "" && opts["kv.keyfile"] != "" {
   167  			logrus.Info("Option Initializing KV with TLS")
   168  			tlsConfig, err := tlsconfig.Client(tlsconfig.Options{
   169  				CAFile:   opts["kv.cacertfile"],
   170  				CertFile: opts["kv.certfile"],
   171  				KeyFile:  opts["kv.keyfile"],
   172  			})
   173  			if err != nil {
   174  				logrus.Errorf("Unable to set up TLS: %s", err)
   175  				return
   176  			}
   177  			if _, ok := c.Scopes[datastore.GlobalScope]; !ok {
   178  				c.Scopes[datastore.GlobalScope] = &datastore.ScopeCfg{}
   179  			}
   180  			if c.Scopes[datastore.GlobalScope].Client.Config == nil {
   181  				c.Scopes[datastore.GlobalScope].Client.Config = &store.Config{TLS: tlsConfig}
   182  			} else {
   183  				c.Scopes[datastore.GlobalScope].Client.Config.TLS = tlsConfig
   184  			}
   185  			// Workaround libkv/etcd bug for https
   186  			c.Scopes[datastore.GlobalScope].Client.Config.ClientTLS = &store.ClientTLSConfig{
   187  				CACertFile: opts["kv.cacertfile"],
   188  				CertFile:   opts["kv.certfile"],
   189  				KeyFile:    opts["kv.keyfile"],
   190  			}
   191  		} else {
   192  			logrus.Info("Option Initializing KV without TLS")
   193  		}
   194  	}
   195  }
   196  
   197  // OptionDiscoveryWatcher function returns an option setter for discovery watcher
   198  func OptionDiscoveryWatcher(watcher discovery.Watcher) Option {
   199  	return func(c *Config) {
   200  		c.Cluster.Watcher = watcher
   201  	}
   202  }
   203  
   204  // OptionDiscoveryAddress function returns an option setter for self discovery address
   205  func OptionDiscoveryAddress(address string) Option {
   206  	return func(c *Config) {
   207  		c.Cluster.Address = address
   208  	}
   209  }
   210  
   211  // OptionDataDir function returns an option setter for data folder
   212  func OptionDataDir(dataDir string) Option {
   213  	return func(c *Config) {
   214  		c.Daemon.DataDir = dataDir
   215  	}
   216  }
   217  
   218  // OptionExecRoot function returns an option setter for exec root folder
   219  func OptionExecRoot(execRoot string) Option {
   220  	return func(c *Config) {
   221  		c.Daemon.ExecRoot = execRoot
   222  		osl.SetBasePath(execRoot)
   223  	}
   224  }
   225  
   226  // OptionPluginGetter returns a plugingetter for remote drivers.
   227  func OptionPluginGetter(pg plugingetter.PluginGetter) Option {
   228  	return func(c *Config) {
   229  		c.PluginGetter = pg
   230  	}
   231  }
   232  
   233  // OptionExperimental function returns an option setter for experimental daemon
   234  func OptionExperimental(exp bool) Option {
   235  	return func(c *Config) {
   236  		logrus.Debugf("Option Experimental: %v", exp)
   237  		c.Daemon.Experimental = exp
   238  	}
   239  }
   240  
   241  // OptionNetworkControlPlaneMTU function returns an option setter for control plane MTU
   242  func OptionNetworkControlPlaneMTU(exp int) Option {
   243  	return func(c *Config) {
   244  		logrus.Debugf("Network Control Plane MTU: %d", exp)
   245  		if exp < warningThNetworkControlPlaneMTU {
   246  			logrus.Warnf("Received a MTU of %d, this value is very low, the network control plane can misbehave,"+
   247  				" defaulting to minimum value (%d)", exp, minimumNetworkControlPlaneMTU)
   248  			if exp < minimumNetworkControlPlaneMTU {
   249  				exp = minimumNetworkControlPlaneMTU
   250  			}
   251  		}
   252  		c.Daemon.NetworkControlPlaneMTU = exp
   253  	}
   254  }
   255  
   256  // ProcessOptions processes options and stores it in config
   257  func (c *Config) ProcessOptions(options ...Option) {
   258  	for _, opt := range options {
   259  		if opt != nil {
   260  			opt(c)
   261  		}
   262  	}
   263  }
   264  
   265  // IsValidName validates configuration objects supported by libnetwork
   266  func IsValidName(name string) bool {
   267  	return strings.TrimSpace(name) != ""
   268  }
   269  
   270  // OptionLocalKVProvider function returns an option setter for kvstore provider
   271  func OptionLocalKVProvider(provider string) Option {
   272  	return func(c *Config) {
   273  		logrus.Debugf("Option OptionLocalKVProvider: %s", provider)
   274  		if _, ok := c.Scopes[datastore.LocalScope]; !ok {
   275  			c.Scopes[datastore.LocalScope] = &datastore.ScopeCfg{}
   276  		}
   277  		c.Scopes[datastore.LocalScope].Client.Provider = strings.TrimSpace(provider)
   278  	}
   279  }
   280  
   281  // OptionLocalKVProviderURL function returns an option setter for kvstore url
   282  func OptionLocalKVProviderURL(url string) Option {
   283  	return func(c *Config) {
   284  		logrus.Debugf("Option OptionLocalKVProviderURL: %s", url)
   285  		if _, ok := c.Scopes[datastore.LocalScope]; !ok {
   286  			c.Scopes[datastore.LocalScope] = &datastore.ScopeCfg{}
   287  		}
   288  		c.Scopes[datastore.LocalScope].Client.Address = strings.TrimSpace(url)
   289  	}
   290  }
   291  
   292  // OptionLocalKVProviderConfig function returns an option setter for kvstore config
   293  func OptionLocalKVProviderConfig(config *store.Config) Option {
   294  	return func(c *Config) {
   295  		logrus.Debugf("Option OptionLocalKVProviderConfig: %v", config)
   296  		if _, ok := c.Scopes[datastore.LocalScope]; !ok {
   297  			c.Scopes[datastore.LocalScope] = &datastore.ScopeCfg{}
   298  		}
   299  		c.Scopes[datastore.LocalScope].Client.Config = config
   300  	}
   301  }
   302  
   303  // OptionActiveSandboxes function returns an option setter for passing the sandboxes
   304  // which were active during previous daemon life
   305  func OptionActiveSandboxes(sandboxes map[string]interface{}) Option {
   306  	return func(c *Config) {
   307  		c.ActiveSandboxes = sandboxes
   308  	}
   309  }