config.go 8.7 KB

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