2015-05-23 00:52:02 -04:00
|
|
|
package config
|
|
|
|
|
2015-06-11 09:52:46 -04:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
2016-11-01 00:26:14 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-09-18 15:54:08 -04:00
|
|
|
"github.com/docker/docker/pkg/discovery"
|
2016-10-07 14:58:10 -04:00
|
|
|
"github.com/docker/docker/pkg/plugingetter"
|
2016-09-06 17:10:58 -04:00
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2015-09-16 07:39:46 -04:00
|
|
|
"github.com/docker/libkv/store"
|
2017-05-25 13:45:38 -04:00
|
|
|
"github.com/docker/libnetwork/cluster"
|
2015-10-05 07:21:15 -04:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
2015-06-14 12:03:42 -04:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
2016-07-15 00:25:52 -04:00
|
|
|
"github.com/docker/libnetwork/osl"
|
2015-06-11 09:52:46 -04:00
|
|
|
)
|
2015-05-23 00:52:02 -04:00
|
|
|
|
|
|
|
// Config encapsulates configurations of various Libnetwork components
|
|
|
|
type Config struct {
|
2016-06-10 20:32:19 -04:00
|
|
|
Daemon DaemonCfg
|
|
|
|
Cluster ClusterCfg
|
|
|
|
Scopes map[string]*datastore.ScopeCfg
|
|
|
|
ActiveSandboxes map[string]interface{}
|
2016-10-07 14:58:10 -04:00
|
|
|
PluginGetter plugingetter.PluginGetter
|
2015-05-23 00:52:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DaemonCfg represents libnetwork core configuration
|
|
|
|
type DaemonCfg struct {
|
2016-05-24 19:17:19 -04:00
|
|
|
Debug bool
|
2016-12-13 16:22:55 -05:00
|
|
|
Experimental bool
|
2016-05-24 19:17:19 -04:00
|
|
|
DataDir string
|
|
|
|
DefaultNetwork string
|
|
|
|
DefaultDriver string
|
|
|
|
Labels []string
|
|
|
|
DriverCfg map[string]interface{}
|
2017-05-25 13:45:38 -04:00
|
|
|
ClusterProvider cluster.Provider
|
2015-05-23 00:52:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClusterCfg represents cluster configuration
|
|
|
|
type ClusterCfg struct {
|
2015-09-18 15:54:08 -04:00
|
|
|
Watcher discovery.Watcher
|
2015-05-23 00:52:02 -04:00
|
|
|
Address string
|
2015-09-18 15:54:08 -04:00
|
|
|
Discovery string
|
2015-05-23 00:52:02 -04:00
|
|
|
Heartbeat uint64
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
// LoadDefaultScopes loads default scope configs for scopes which
|
|
|
|
// doesn't have explicit user specified configs.
|
|
|
|
func (c *Config) LoadDefaultScopes(dataDir string) {
|
|
|
|
for k, v := range datastore.DefaultScopes(dataDir) {
|
|
|
|
if _, ok := c.Scopes[k]; !ok {
|
|
|
|
c.Scopes[k] = v
|
|
|
|
}
|
|
|
|
}
|
2015-05-23 00:52:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseConfig parses the libnetwork configuration file
|
|
|
|
func ParseConfig(tomlCfgFile string) (*Config, error) {
|
2015-10-05 07:21:15 -04:00
|
|
|
cfg := &Config{
|
|
|
|
Scopes: map[string]*datastore.ScopeCfg{},
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := toml.DecodeFile(tomlCfgFile, cfg); err != nil {
|
2015-05-23 00:52:02 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-05 07:21:15 -04:00
|
|
|
|
|
|
|
cfg.LoadDefaultScopes(cfg.Daemon.DataDir)
|
|
|
|
return cfg, nil
|
2015-05-23 00:52:02 -04:00
|
|
|
}
|
2015-06-11 09:52:46 -04:00
|
|
|
|
2016-02-16 18:19:18 -05:00
|
|
|
// ParseConfigOptions parses the configuration options and returns
|
|
|
|
// a reference to the corresponding Config structure
|
|
|
|
func ParseConfigOptions(cfgOptions ...Option) *Config {
|
|
|
|
cfg := &Config{
|
|
|
|
Daemon: DaemonCfg{
|
2017-05-03 14:18:33 -04:00
|
|
|
DriverCfg: make(map[string]interface{}),
|
2016-02-16 18:19:18 -05:00
|
|
|
},
|
|
|
|
Scopes: make(map[string]*datastore.ScopeCfg),
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.ProcessOptions(cfgOptions...)
|
|
|
|
cfg.LoadDefaultScopes(cfg.Daemon.DataDir)
|
|
|
|
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2016-01-11 05:15:20 -05:00
|
|
|
// Option is an option setter function type used to pass various configurations
|
2015-06-11 09:52:46 -04:00
|
|
|
// to the controller
|
|
|
|
type Option func(c *Config)
|
|
|
|
|
|
|
|
// OptionDefaultNetwork function returns an option setter for a default network
|
|
|
|
func OptionDefaultNetwork(dn string) Option {
|
|
|
|
return func(c *Config) {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Option DefaultNetwork: %s", dn)
|
2015-06-11 09:52:46 -04:00
|
|
|
c.Daemon.DefaultNetwork = strings.TrimSpace(dn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionDefaultDriver function returns an option setter for default driver
|
|
|
|
func OptionDefaultDriver(dd string) Option {
|
|
|
|
return func(c *Config) {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Option DefaultDriver: %s", dd)
|
2015-06-11 09:52:46 -04:00
|
|
|
c.Daemon.DefaultDriver = strings.TrimSpace(dd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 17:00:36 -04:00
|
|
|
// OptionDriverConfig returns an option setter for driver configuration.
|
|
|
|
func OptionDriverConfig(networkType string, config map[string]interface{}) Option {
|
|
|
|
return func(c *Config) {
|
|
|
|
c.Daemon.DriverCfg[networkType] = config
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-14 12:03:42 -04:00
|
|
|
// OptionLabels function returns an option setter for labels
|
|
|
|
func OptionLabels(labels []string) Option {
|
|
|
|
return func(c *Config) {
|
|
|
|
for _, label := range labels {
|
|
|
|
if strings.HasPrefix(label, netlabel.Prefix) {
|
|
|
|
c.Daemon.Labels = append(c.Daemon.Labels, label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-11 09:52:46 -04:00
|
|
|
// OptionKVProvider function returns an option setter for kvstore provider
|
|
|
|
func OptionKVProvider(provider string) Option {
|
|
|
|
return func(c *Config) {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Option OptionKVProvider: %s", provider)
|
2015-10-05 07:21:15 -04:00
|
|
|
if _, ok := c.Scopes[datastore.GlobalScope]; !ok {
|
|
|
|
c.Scopes[datastore.GlobalScope] = &datastore.ScopeCfg{}
|
|
|
|
}
|
|
|
|
c.Scopes[datastore.GlobalScope].Client.Provider = strings.TrimSpace(provider)
|
2015-06-11 09:52:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionKVProviderURL function returns an option setter for kvstore url
|
|
|
|
func OptionKVProviderURL(url string) Option {
|
|
|
|
return func(c *Config) {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Option OptionKVProviderURL: %s", url)
|
2015-10-05 07:21:15 -04:00
|
|
|
if _, ok := c.Scopes[datastore.GlobalScope]; !ok {
|
|
|
|
c.Scopes[datastore.GlobalScope] = &datastore.ScopeCfg{}
|
|
|
|
}
|
|
|
|
c.Scopes[datastore.GlobalScope].Client.Address = strings.TrimSpace(url)
|
2015-06-11 09:52:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-07 10:40:59 -04:00
|
|
|
// OptionKVOpts function returns an option setter for kvstore options
|
|
|
|
func OptionKVOpts(opts map[string]string) Option {
|
|
|
|
return func(c *Config) {
|
|
|
|
if opts["kv.cacertfile"] != "" && opts["kv.certfile"] != "" && opts["kv.keyfile"] != "" {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Info("Option Initializing KV with TLS")
|
2015-10-07 10:40:59 -04:00
|
|
|
tlsConfig, err := tlsconfig.Client(tlsconfig.Options{
|
|
|
|
CAFile: opts["kv.cacertfile"],
|
|
|
|
CertFile: opts["kv.certfile"],
|
|
|
|
KeyFile: opts["kv.keyfile"],
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Errorf("Unable to set up TLS: %s", err)
|
2015-10-07 10:40:59 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := c.Scopes[datastore.GlobalScope]; !ok {
|
|
|
|
c.Scopes[datastore.GlobalScope] = &datastore.ScopeCfg{}
|
|
|
|
}
|
|
|
|
if c.Scopes[datastore.GlobalScope].Client.Config == nil {
|
|
|
|
c.Scopes[datastore.GlobalScope].Client.Config = &store.Config{TLS: tlsConfig}
|
|
|
|
} else {
|
|
|
|
c.Scopes[datastore.GlobalScope].Client.Config.TLS = tlsConfig
|
|
|
|
}
|
|
|
|
// Workaround libkv/etcd bug for https
|
|
|
|
c.Scopes[datastore.GlobalScope].Client.Config.ClientTLS = &store.ClientTLSConfig{
|
|
|
|
CACertFile: opts["kv.cacertfile"],
|
|
|
|
CertFile: opts["kv.certfile"],
|
|
|
|
KeyFile: opts["kv.keyfile"],
|
|
|
|
}
|
|
|
|
} else {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Info("Option Initializing KV without TLS")
|
2015-10-07 10:40:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 15:54:08 -04:00
|
|
|
// OptionDiscoveryWatcher function returns an option setter for discovery watcher
|
|
|
|
func OptionDiscoveryWatcher(watcher discovery.Watcher) Option {
|
|
|
|
return func(c *Config) {
|
|
|
|
c.Cluster.Watcher = watcher
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionDiscoveryAddress function returns an option setter for self discovery address
|
|
|
|
func OptionDiscoveryAddress(address string) Option {
|
|
|
|
return func(c *Config) {
|
|
|
|
c.Cluster.Address = address
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
// OptionDataDir function returns an option setter for data folder
|
|
|
|
func OptionDataDir(dataDir string) Option {
|
|
|
|
return func(c *Config) {
|
|
|
|
c.Daemon.DataDir = dataDir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 00:25:52 -04:00
|
|
|
// OptionExecRoot function returns an option setter for exec root folder
|
|
|
|
func OptionExecRoot(execRoot string) Option {
|
|
|
|
return func(c *Config) {
|
|
|
|
osl.SetBasePath(execRoot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 16:54:25 -04:00
|
|
|
// OptionPluginGetter returns a plugingetter for remote drivers.
|
2016-10-07 14:58:10 -04:00
|
|
|
func OptionPluginGetter(pg plugingetter.PluginGetter) Option {
|
2016-09-27 16:54:25 -04:00
|
|
|
return func(c *Config) {
|
|
|
|
c.PluginGetter = pg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 16:22:55 -05:00
|
|
|
// OptionExperimental function returns an option setter for experimental daemon
|
|
|
|
func OptionExperimental(exp bool) Option {
|
|
|
|
return func(c *Config) {
|
|
|
|
logrus.Debugf("Option Experimental: %v", exp)
|
|
|
|
c.Daemon.Experimental = exp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-11 09:52:46 -04:00
|
|
|
// ProcessOptions processes options and stores it in config
|
|
|
|
func (c *Config) ProcessOptions(options ...Option) {
|
|
|
|
for _, opt := range options {
|
|
|
|
if opt != nil {
|
|
|
|
opt(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-14 12:00:27 -04:00
|
|
|
|
2017-02-01 21:17:29 -05:00
|
|
|
// IsValidName validates configuration objects supported by libnetwork
|
|
|
|
func IsValidName(name string) bool {
|
|
|
|
if strings.TrimSpace(name) == "" {
|
|
|
|
return false
|
2015-06-14 12:00:27 -04:00
|
|
|
}
|
2017-02-01 21:17:29 -05:00
|
|
|
return true
|
2015-06-14 12:00:27 -04:00
|
|
|
}
|
2015-09-16 07:39:46 -04:00
|
|
|
|
|
|
|
// OptionLocalKVProvider function returns an option setter for kvstore provider
|
|
|
|
func OptionLocalKVProvider(provider string) Option {
|
|
|
|
return func(c *Config) {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Option OptionLocalKVProvider: %s", provider)
|
2015-10-05 07:21:15 -04:00
|
|
|
if _, ok := c.Scopes[datastore.LocalScope]; !ok {
|
|
|
|
c.Scopes[datastore.LocalScope] = &datastore.ScopeCfg{}
|
|
|
|
}
|
|
|
|
c.Scopes[datastore.LocalScope].Client.Provider = strings.TrimSpace(provider)
|
2015-09-16 07:39:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionLocalKVProviderURL function returns an option setter for kvstore url
|
|
|
|
func OptionLocalKVProviderURL(url string) Option {
|
|
|
|
return func(c *Config) {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Option OptionLocalKVProviderURL: %s", url)
|
2015-10-05 07:21:15 -04:00
|
|
|
if _, ok := c.Scopes[datastore.LocalScope]; !ok {
|
|
|
|
c.Scopes[datastore.LocalScope] = &datastore.ScopeCfg{}
|
|
|
|
}
|
|
|
|
c.Scopes[datastore.LocalScope].Client.Address = strings.TrimSpace(url)
|
2015-09-16 07:39:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionLocalKVProviderConfig function returns an option setter for kvstore config
|
|
|
|
func OptionLocalKVProviderConfig(config *store.Config) Option {
|
|
|
|
return func(c *Config) {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Option OptionLocalKVProviderConfig: %v", config)
|
2015-10-05 07:21:15 -04:00
|
|
|
if _, ok := c.Scopes[datastore.LocalScope]; !ok {
|
|
|
|
c.Scopes[datastore.LocalScope] = &datastore.ScopeCfg{}
|
|
|
|
}
|
|
|
|
c.Scopes[datastore.LocalScope].Client.Config = config
|
2015-09-16 07:39:46 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-10 20:32:19 -04:00
|
|
|
|
|
|
|
// OptionActiveSandboxes function returns an option setter for passing the sandboxes
|
|
|
|
// which were active during previous daemon life
|
|
|
|
func OptionActiveSandboxes(sandboxes map[string]interface{}) Option {
|
|
|
|
return func(c *Config) {
|
|
|
|
c.ActiveSandboxes = sandboxes
|
|
|
|
}
|
|
|
|
}
|