123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- package config
- import (
- "strings"
- "github.com/BurntSushi/toml"
- "github.com/Sirupsen/logrus"
- "github.com/docker/docker/pkg/discovery"
- "github.com/docker/docker/pkg/plugingetter"
- "github.com/docker/go-connections/tlsconfig"
- "github.com/docker/libkv/store"
- "github.com/docker/libnetwork/cluster"
- "github.com/docker/libnetwork/datastore"
- "github.com/docker/libnetwork/netlabel"
- "github.com/docker/libnetwork/osl"
- )
- // Config encapsulates configurations of various Libnetwork components
- type Config struct {
- Daemon DaemonCfg
- Cluster ClusterCfg
- Scopes map[string]*datastore.ScopeCfg
- ActiveSandboxes map[string]interface{}
- PluginGetter plugingetter.PluginGetter
- }
- // DaemonCfg represents libnetwork core configuration
- type DaemonCfg struct {
- Debug bool
- Experimental bool
- DataDir string
- DefaultNetwork string
- DefaultDriver string
- Labels []string
- DriverCfg map[string]interface{}
- ClusterProvider cluster.Provider
- }
- // ClusterCfg represents cluster configuration
- type ClusterCfg struct {
- Watcher discovery.Watcher
- Address string
- Discovery string
- Heartbeat uint64
- }
- // 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
- }
- }
- }
- // ParseConfig parses the libnetwork configuration file
- func ParseConfig(tomlCfgFile string) (*Config, error) {
- cfg := &Config{
- Scopes: map[string]*datastore.ScopeCfg{},
- }
- if _, err := toml.DecodeFile(tomlCfgFile, cfg); err != nil {
- return nil, err
- }
- cfg.LoadDefaultScopes(cfg.Daemon.DataDir)
- return cfg, nil
- }
- // ParseConfigOptions parses the configuration options and returns
- // a reference to the corresponding Config structure
- func ParseConfigOptions(cfgOptions ...Option) *Config {
- cfg := &Config{
- Daemon: DaemonCfg{
- DriverCfg: make(map[string]interface{}),
- },
- Scopes: make(map[string]*datastore.ScopeCfg),
- }
- cfg.ProcessOptions(cfgOptions...)
- cfg.LoadDefaultScopes(cfg.Daemon.DataDir)
- return cfg
- }
- // Option is an option setter function type used to pass various configurations
- // 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) {
- logrus.Debugf("Option DefaultNetwork: %s", dn)
- c.Daemon.DefaultNetwork = strings.TrimSpace(dn)
- }
- }
- // OptionDefaultDriver function returns an option setter for default driver
- func OptionDefaultDriver(dd string) Option {
- return func(c *Config) {
- logrus.Debugf("Option DefaultDriver: %s", dd)
- c.Daemon.DefaultDriver = strings.TrimSpace(dd)
- }
- }
- // 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
- }
- }
- // 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)
- }
- }
- }
- }
- // OptionKVProvider function returns an option setter for kvstore provider
- func OptionKVProvider(provider string) Option {
- return func(c *Config) {
- logrus.Debugf("Option OptionKVProvider: %s", provider)
- if _, ok := c.Scopes[datastore.GlobalScope]; !ok {
- c.Scopes[datastore.GlobalScope] = &datastore.ScopeCfg{}
- }
- c.Scopes[datastore.GlobalScope].Client.Provider = strings.TrimSpace(provider)
- }
- }
- // OptionKVProviderURL function returns an option setter for kvstore url
- func OptionKVProviderURL(url string) Option {
- return func(c *Config) {
- logrus.Debugf("Option OptionKVProviderURL: %s", url)
- if _, ok := c.Scopes[datastore.GlobalScope]; !ok {
- c.Scopes[datastore.GlobalScope] = &datastore.ScopeCfg{}
- }
- c.Scopes[datastore.GlobalScope].Client.Address = strings.TrimSpace(url)
- }
- }
- // 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"] != "" {
- logrus.Info("Option Initializing KV with TLS")
- tlsConfig, err := tlsconfig.Client(tlsconfig.Options{
- CAFile: opts["kv.cacertfile"],
- CertFile: opts["kv.certfile"],
- KeyFile: opts["kv.keyfile"],
- })
- if err != nil {
- logrus.Errorf("Unable to set up TLS: %s", err)
- 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 {
- logrus.Info("Option Initializing KV without TLS")
- }
- }
- }
- // 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
- }
- }
- // OptionDataDir function returns an option setter for data folder
- func OptionDataDir(dataDir string) Option {
- return func(c *Config) {
- c.Daemon.DataDir = dataDir
- }
- }
- // OptionExecRoot function returns an option setter for exec root folder
- func OptionExecRoot(execRoot string) Option {
- return func(c *Config) {
- osl.SetBasePath(execRoot)
- }
- }
- // OptionPluginGetter returns a plugingetter for remote drivers.
- func OptionPluginGetter(pg plugingetter.PluginGetter) Option {
- return func(c *Config) {
- c.PluginGetter = pg
- }
- }
- // 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
- }
- }
- // ProcessOptions processes options and stores it in config
- func (c *Config) ProcessOptions(options ...Option) {
- for _, opt := range options {
- if opt != nil {
- opt(c)
- }
- }
- }
- // IsValidName validates configuration objects supported by libnetwork
- func IsValidName(name string) bool {
- if strings.TrimSpace(name) == "" {
- return false
- }
- return true
- }
- // OptionLocalKVProvider function returns an option setter for kvstore provider
- func OptionLocalKVProvider(provider string) Option {
- return func(c *Config) {
- logrus.Debugf("Option OptionLocalKVProvider: %s", provider)
- if _, ok := c.Scopes[datastore.LocalScope]; !ok {
- c.Scopes[datastore.LocalScope] = &datastore.ScopeCfg{}
- }
- c.Scopes[datastore.LocalScope].Client.Provider = strings.TrimSpace(provider)
- }
- }
- // OptionLocalKVProviderURL function returns an option setter for kvstore url
- func OptionLocalKVProviderURL(url string) Option {
- return func(c *Config) {
- logrus.Debugf("Option OptionLocalKVProviderURL: %s", url)
- if _, ok := c.Scopes[datastore.LocalScope]; !ok {
- c.Scopes[datastore.LocalScope] = &datastore.ScopeCfg{}
- }
- c.Scopes[datastore.LocalScope].Client.Address = strings.TrimSpace(url)
- }
- }
- // OptionLocalKVProviderConfig function returns an option setter for kvstore config
- func OptionLocalKVProviderConfig(config *store.Config) Option {
- return func(c *Config) {
- logrus.Debugf("Option OptionLocalKVProviderConfig: %v", config)
- if _, ok := c.Scopes[datastore.LocalScope]; !ok {
- c.Scopes[datastore.LocalScope] = &datastore.ScopeCfg{}
- }
- c.Scopes[datastore.LocalScope].Client.Config = config
- }
- }
- // 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
- }
- }
|