reload.go 11 KB

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