reload.go 12 KB

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