reload.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/docker/docker/daemon/config"
  6. "github.com/docker/docker/daemon/discovery"
  7. "github.com/sirupsen/logrus"
  8. )
  9. // Reload reads configuration changes and modifies the
  10. // daemon according to those changes.
  11. // These are the settings that Reload changes:
  12. // - Platform runtime
  13. // - Daemon debug log level
  14. // - Daemon max concurrent downloads
  15. // - Daemon max concurrent uploads
  16. // - Daemon shutdown timeout (in seconds)
  17. // - Cluster discovery (reconfigure and restart)
  18. // - Daemon labels
  19. // - Insecure registries
  20. // - Registry mirrors
  21. // - Daemon live restore
  22. func (daemon *Daemon) Reload(conf *config.Config) (err error) {
  23. daemon.configStore.Lock()
  24. attributes := map[string]string{}
  25. defer func() {
  26. jsonString, _ := json.Marshal(daemon.configStore)
  27. // we're unlocking here, because
  28. // LogDaemonEventWithAttributes() -> SystemInfo() -> GetAllRuntimes()
  29. // holds that lock too.
  30. daemon.configStore.Unlock()
  31. if err == nil {
  32. logrus.Infof("Reloaded configuration: %s", jsonString)
  33. daemon.LogDaemonEventWithAttributes("reload", attributes)
  34. }
  35. }()
  36. if err := daemon.reloadPlatform(conf, attributes); err != nil {
  37. return err
  38. }
  39. daemon.reloadDebug(conf, attributes)
  40. daemon.reloadMaxConcurrentDownloadsAndUploads(conf, attributes)
  41. daemon.reloadShutdownTimeout(conf, attributes)
  42. if err := daemon.reloadClusterDiscovery(conf, attributes); err != nil {
  43. return err
  44. }
  45. if err := daemon.reloadLabels(conf, attributes); err != nil {
  46. return err
  47. }
  48. if err := daemon.reloadAllowNondistributableArtifacts(conf, attributes); err != nil {
  49. return err
  50. }
  51. if err := daemon.reloadInsecureRegistries(conf, attributes); err != nil {
  52. return err
  53. }
  54. if err := daemon.reloadRegistryMirrors(conf, attributes); err != nil {
  55. return err
  56. }
  57. if err := daemon.reloadLiveRestore(conf, attributes); err != nil {
  58. return err
  59. }
  60. return daemon.reloadNetworkDiagnosticPort(conf, attributes)
  61. }
  62. // reloadDebug updates configuration with Debug option
  63. // and updates the passed attributes
  64. func (daemon *Daemon) reloadDebug(conf *config.Config, attributes map[string]string) {
  65. // update corresponding configuration
  66. if conf.IsValueSet("debug") {
  67. daemon.configStore.Debug = conf.Debug
  68. }
  69. // prepare reload event attributes with updatable configurations
  70. attributes["debug"] = fmt.Sprintf("%t", daemon.configStore.Debug)
  71. }
  72. // reloadMaxConcurrentDownloadsAndUploads updates configuration with max concurrent
  73. // download and upload options and updates the passed attributes
  74. func (daemon *Daemon) reloadMaxConcurrentDownloadsAndUploads(conf *config.Config, attributes map[string]string) {
  75. // If no value is set for max-concurrent-downloads we assume it is the default value
  76. // We always "reset" as the cost is lightweight and easy to maintain.
  77. if conf.IsValueSet("max-concurrent-downloads") && conf.MaxConcurrentDownloads != nil {
  78. *daemon.configStore.MaxConcurrentDownloads = *conf.MaxConcurrentDownloads
  79. } else {
  80. maxConcurrentDownloads := config.DefaultMaxConcurrentDownloads
  81. daemon.configStore.MaxConcurrentDownloads = &maxConcurrentDownloads
  82. }
  83. logrus.Debugf("Reset Max Concurrent Downloads: %d", *daemon.configStore.MaxConcurrentDownloads)
  84. // If no value is set for max-concurrent-upload we assume it is the default value
  85. // We always "reset" as the cost is lightweight and easy to maintain.
  86. if conf.IsValueSet("max-concurrent-uploads") && conf.MaxConcurrentUploads != nil {
  87. *daemon.configStore.MaxConcurrentUploads = *conf.MaxConcurrentUploads
  88. } else {
  89. maxConcurrentUploads := config.DefaultMaxConcurrentUploads
  90. daemon.configStore.MaxConcurrentUploads = &maxConcurrentUploads
  91. }
  92. logrus.Debugf("Reset Max Concurrent Uploads: %d", *daemon.configStore.MaxConcurrentUploads)
  93. daemon.imageService.UpdateConfig(conf.MaxConcurrentDownloads, conf.MaxConcurrentUploads)
  94. // prepare reload event attributes with updatable configurations
  95. attributes["max-concurrent-downloads"] = fmt.Sprintf("%d", *daemon.configStore.MaxConcurrentDownloads)
  96. // prepare reload event attributes with updatable configurations
  97. attributes["max-concurrent-uploads"] = fmt.Sprintf("%d", *daemon.configStore.MaxConcurrentUploads)
  98. }
  99. // reloadShutdownTimeout updates configuration with daemon shutdown timeout option
  100. // and updates the passed attributes
  101. func (daemon *Daemon) reloadShutdownTimeout(conf *config.Config, attributes map[string]string) {
  102. // update corresponding configuration
  103. if conf.IsValueSet("shutdown-timeout") {
  104. daemon.configStore.ShutdownTimeout = conf.ShutdownTimeout
  105. logrus.Debugf("Reset Shutdown Timeout: %d", daemon.configStore.ShutdownTimeout)
  106. }
  107. // prepare reload event attributes with updatable configurations
  108. attributes["shutdown-timeout"] = fmt.Sprintf("%d", daemon.configStore.ShutdownTimeout)
  109. }
  110. // reloadClusterDiscovery updates configuration with cluster discovery options
  111. // and updates the passed attributes
  112. func (daemon *Daemon) reloadClusterDiscovery(conf *config.Config, attributes map[string]string) (err error) {
  113. defer func() {
  114. // prepare reload event attributes with updatable configurations
  115. attributes["cluster-store"] = conf.ClusterStore
  116. attributes["cluster-advertise"] = conf.ClusterAdvertise
  117. attributes["cluster-store-opts"] = "{}"
  118. if daemon.configStore.ClusterOpts != nil {
  119. opts, err2 := json.Marshal(conf.ClusterOpts)
  120. if err != nil {
  121. err = err2
  122. }
  123. attributes["cluster-store-opts"] = string(opts)
  124. }
  125. }()
  126. newAdvertise := conf.ClusterAdvertise
  127. newClusterStore := daemon.configStore.ClusterStore
  128. if conf.IsValueSet("cluster-advertise") {
  129. if conf.IsValueSet("cluster-store") {
  130. newClusterStore = conf.ClusterStore
  131. }
  132. newAdvertise, err = config.ParseClusterAdvertiseSettings(newClusterStore, conf.ClusterAdvertise)
  133. if err != nil && err != discovery.ErrDiscoveryDisabled {
  134. return err
  135. }
  136. }
  137. if daemon.clusterProvider != nil {
  138. if err := conf.IsSwarmCompatible(); err != nil {
  139. return err
  140. }
  141. }
  142. // check discovery modifications
  143. if !config.ModifiedDiscoverySettings(daemon.configStore, newClusterStore, newAdvertise, conf.ClusterOpts) {
  144. return nil
  145. }
  146. // enable discovery for the first time if it was not previously enabled
  147. if daemon.discoveryWatcher == nil {
  148. discoveryWatcher, err := discovery.Init(newClusterStore, newAdvertise, conf.ClusterOpts)
  149. if err != nil {
  150. return fmt.Errorf("failed to initialize discovery: %v", err)
  151. }
  152. daemon.discoveryWatcher = discoveryWatcher
  153. } else if err == discovery.ErrDiscoveryDisabled {
  154. // disable discovery if it was previously enabled and it's disabled now
  155. daemon.discoveryWatcher.Stop()
  156. } else if err = daemon.discoveryWatcher.Reload(conf.ClusterStore, newAdvertise, conf.ClusterOpts); err != nil {
  157. // reload discovery
  158. return err
  159. }
  160. daemon.configStore.ClusterStore = newClusterStore
  161. daemon.configStore.ClusterOpts = conf.ClusterOpts
  162. daemon.configStore.ClusterAdvertise = newAdvertise
  163. if daemon.netController == nil {
  164. return nil
  165. }
  166. netOptions, err := daemon.networkOptions(daemon.configStore, daemon.PluginStore, nil)
  167. if err != nil {
  168. logrus.WithError(err).Warn("failed to get options with network controller")
  169. return nil
  170. }
  171. err = daemon.netController.ReloadConfiguration(netOptions...)
  172. if err != nil {
  173. logrus.Warnf("Failed to reload configuration with network controller: %v", err)
  174. }
  175. return nil
  176. }
  177. // reloadLabels updates configuration with engine labels
  178. // and updates the passed attributes
  179. func (daemon *Daemon) reloadLabels(conf *config.Config, attributes map[string]string) error {
  180. // update corresponding configuration
  181. if conf.IsValueSet("labels") {
  182. daemon.configStore.Labels = conf.Labels
  183. }
  184. // prepare reload event attributes with updatable configurations
  185. if daemon.configStore.Labels != nil {
  186. labels, err := json.Marshal(daemon.configStore.Labels)
  187. if err != nil {
  188. return err
  189. }
  190. attributes["labels"] = string(labels)
  191. } else {
  192. attributes["labels"] = "[]"
  193. }
  194. return nil
  195. }
  196. // reloadAllowNondistributableArtifacts updates the configuration with allow-nondistributable-artifacts options
  197. // and updates the passed attributes.
  198. func (daemon *Daemon) reloadAllowNondistributableArtifacts(conf *config.Config, attributes map[string]string) error {
  199. // Update corresponding configuration.
  200. if conf.IsValueSet("allow-nondistributable-artifacts") {
  201. daemon.configStore.AllowNondistributableArtifacts = conf.AllowNondistributableArtifacts
  202. if err := daemon.RegistryService.LoadAllowNondistributableArtifacts(conf.AllowNondistributableArtifacts); err != nil {
  203. return err
  204. }
  205. }
  206. // Prepare reload event attributes with updatable configurations.
  207. if daemon.configStore.AllowNondistributableArtifacts != nil {
  208. v, err := json.Marshal(daemon.configStore.AllowNondistributableArtifacts)
  209. if err != nil {
  210. return err
  211. }
  212. attributes["allow-nondistributable-artifacts"] = string(v)
  213. } else {
  214. attributes["allow-nondistributable-artifacts"] = "[]"
  215. }
  216. return nil
  217. }
  218. // reloadInsecureRegistries updates configuration with insecure registry option
  219. // and updates the passed attributes
  220. func (daemon *Daemon) reloadInsecureRegistries(conf *config.Config, attributes map[string]string) error {
  221. // update corresponding configuration
  222. if conf.IsValueSet("insecure-registries") {
  223. daemon.configStore.InsecureRegistries = conf.InsecureRegistries
  224. if err := daemon.RegistryService.LoadInsecureRegistries(conf.InsecureRegistries); err != nil {
  225. return err
  226. }
  227. }
  228. // prepare reload event attributes with updatable configurations
  229. if daemon.configStore.InsecureRegistries != nil {
  230. insecureRegistries, err := json.Marshal(daemon.configStore.InsecureRegistries)
  231. if err != nil {
  232. return err
  233. }
  234. attributes["insecure-registries"] = string(insecureRegistries)
  235. } else {
  236. attributes["insecure-registries"] = "[]"
  237. }
  238. return nil
  239. }
  240. // reloadRegistryMirrors updates configuration with registry mirror options
  241. // and updates the passed attributes
  242. func (daemon *Daemon) reloadRegistryMirrors(conf *config.Config, attributes map[string]string) error {
  243. // update corresponding configuration
  244. if conf.IsValueSet("registry-mirrors") {
  245. daemon.configStore.Mirrors = conf.Mirrors
  246. if err := daemon.RegistryService.LoadMirrors(conf.Mirrors); err != nil {
  247. return err
  248. }
  249. }
  250. // prepare reload event attributes with updatable configurations
  251. if daemon.configStore.Mirrors != nil {
  252. mirrors, err := json.Marshal(daemon.configStore.Mirrors)
  253. if err != nil {
  254. return err
  255. }
  256. attributes["registry-mirrors"] = string(mirrors)
  257. } else {
  258. attributes["registry-mirrors"] = "[]"
  259. }
  260. return nil
  261. }
  262. // reloadLiveRestore updates configuration with live retore option
  263. // and updates the passed attributes
  264. func (daemon *Daemon) reloadLiveRestore(conf *config.Config, attributes map[string]string) error {
  265. // update corresponding configuration
  266. if conf.IsValueSet("live-restore") {
  267. daemon.configStore.LiveRestoreEnabled = conf.LiveRestoreEnabled
  268. }
  269. // prepare reload event attributes with updatable configurations
  270. attributes["live-restore"] = fmt.Sprintf("%t", daemon.configStore.LiveRestoreEnabled)
  271. return nil
  272. }
  273. // reloadNetworkDiagnosticPort updates the network controller starting the diagnostic if the config is valid
  274. func (daemon *Daemon) reloadNetworkDiagnosticPort(conf *config.Config, attributes map[string]string) error {
  275. if conf == nil || daemon.netController == nil || !conf.IsValueSet("network-diagnostic-port") ||
  276. conf.NetworkDiagnosticPort < 1 || conf.NetworkDiagnosticPort > 65535 {
  277. // If there is no config make sure that the diagnostic is off
  278. if daemon.netController != nil {
  279. daemon.netController.StopDiagnostic()
  280. }
  281. return nil
  282. }
  283. // Enable the network diagnostic if the flag is set with a valid port within the range
  284. logrus.WithFields(logrus.Fields{"port": conf.NetworkDiagnosticPort, "ip": "127.0.0.1"}).Warn("Starting network diagnostic server")
  285. daemon.netController.StartDiagnostic(conf.NetworkDiagnosticPort)
  286. return nil
  287. }