config.go 8.2 KB

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