reload.go 11 KB

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