reload.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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/sirupsen/logrus"
  7. )
  8. // Reload reads configuration changes and modifies the
  9. // daemon according to those changes.
  10. // These are the settings that Reload changes:
  11. // - Platform runtime
  12. // - Daemon debug log level
  13. // - Daemon max concurrent downloads
  14. // - Daemon max concurrent uploads
  15. // - Daemon max download attempts
  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. if err == nil {
  27. jsonString, _ := json.Marshal(&struct {
  28. *config.Config
  29. config.ProxyConfig
  30. }{
  31. Config: daemon.configStore,
  32. ProxyConfig: config.ProxyConfig{
  33. HTTPProxy: config.MaskCredentials(daemon.configStore.HTTPProxy),
  34. HTTPSProxy: config.MaskCredentials(daemon.configStore.HTTPSProxy),
  35. NoProxy: config.MaskCredentials(daemon.configStore.NoProxy),
  36. },
  37. })
  38. logrus.Infof("Reloaded configuration: %s", jsonString)
  39. }
  40. // we're unlocking here, because
  41. // LogDaemonEventWithAttributes() -> SystemInfo() -> GetAllRuntimes()
  42. // holds that lock too.
  43. daemon.configStore.Unlock()
  44. if err == nil {
  45. daemon.LogDaemonEventWithAttributes("reload", attributes)
  46. }
  47. }()
  48. if err := daemon.reloadPlatform(conf, attributes); err != nil {
  49. return err
  50. }
  51. daemon.reloadDebug(conf, attributes)
  52. daemon.reloadMaxConcurrentDownloadsAndUploads(conf, attributes)
  53. if err := daemon.reloadMaxDownloadAttempts(conf, attributes); err != nil {
  54. return err
  55. }
  56. daemon.reloadShutdownTimeout(conf, attributes)
  57. daemon.reloadFeatures(conf, attributes)
  58. if err := daemon.reloadLabels(conf, attributes); err != nil {
  59. return err
  60. }
  61. if err := daemon.reloadAllowNondistributableArtifacts(conf, attributes); err != nil {
  62. return err
  63. }
  64. if err := daemon.reloadInsecureRegistries(conf, attributes); err != nil {
  65. return err
  66. }
  67. if err := daemon.reloadRegistryMirrors(conf, attributes); err != nil {
  68. return err
  69. }
  70. if err := daemon.reloadLiveRestore(conf, attributes); err != nil {
  71. return err
  72. }
  73. return daemon.reloadNetworkDiagnosticPort(conf, attributes)
  74. }
  75. // reloadDebug updates configuration with Debug option
  76. // and updates the passed attributes
  77. func (daemon *Daemon) reloadDebug(conf *config.Config, attributes map[string]string) {
  78. // update corresponding configuration
  79. if conf.IsValueSet("debug") {
  80. daemon.configStore.Debug = conf.Debug
  81. }
  82. // prepare reload event attributes with updatable configurations
  83. attributes["debug"] = fmt.Sprintf("%t", daemon.configStore.Debug)
  84. }
  85. // reloadMaxConcurrentDownloadsAndUploads updates configuration with max concurrent
  86. // download and upload options and updates the passed attributes
  87. func (daemon *Daemon) reloadMaxConcurrentDownloadsAndUploads(conf *config.Config, attributes map[string]string) {
  88. // If no value is set for max-concurrent-downloads we assume it is the default value
  89. // We always "reset" as the cost is lightweight and easy to maintain.
  90. maxConcurrentDownloads := config.DefaultMaxConcurrentDownloads
  91. if conf.IsValueSet("max-concurrent-downloads") && conf.MaxConcurrentDownloads != nil {
  92. maxConcurrentDownloads = *conf.MaxConcurrentDownloads
  93. }
  94. daemon.configStore.MaxConcurrentDownloads = &maxConcurrentDownloads
  95. logrus.Debugf("Reset Max Concurrent Downloads: %d", *daemon.configStore.MaxConcurrentDownloads)
  96. // If no value is set for max-concurrent-upload we assume it is the default value
  97. // We always "reset" as the cost is lightweight and easy to maintain.
  98. maxConcurrentUploads := config.DefaultMaxConcurrentUploads
  99. if conf.IsValueSet("max-concurrent-uploads") && conf.MaxConcurrentUploads != nil {
  100. maxConcurrentUploads = *conf.MaxConcurrentUploads
  101. }
  102. daemon.configStore.MaxConcurrentUploads = &maxConcurrentUploads
  103. logrus.Debugf("Reset Max Concurrent Uploads: %d", *daemon.configStore.MaxConcurrentUploads)
  104. if daemon.imageService != nil {
  105. daemon.imageService.UpdateConfig(&maxConcurrentDownloads, &maxConcurrentUploads)
  106. }
  107. // prepare reload event attributes with updatable configurations
  108. attributes["max-concurrent-downloads"] = fmt.Sprintf("%d", *daemon.configStore.MaxConcurrentDownloads)
  109. // prepare reload event attributes with updatable configurations
  110. attributes["max-concurrent-uploads"] = fmt.Sprintf("%d", *daemon.configStore.MaxConcurrentUploads)
  111. }
  112. // reloadMaxDownloadAttempts updates configuration with max concurrent
  113. // download attempts when a connection is lost and updates the passed attributes
  114. func (daemon *Daemon) reloadMaxDownloadAttempts(conf *config.Config, attributes map[string]string) error {
  115. if err := config.ValidateMaxDownloadAttempts(conf); err != nil {
  116. return err
  117. }
  118. // If no value is set for max-download-attempts we assume it is the default value
  119. // We always "reset" as the cost is lightweight and easy to maintain.
  120. maxDownloadAttempts := config.DefaultDownloadAttempts
  121. if conf.IsValueSet("max-download-attempts") && conf.MaxDownloadAttempts != nil {
  122. maxDownloadAttempts = *conf.MaxDownloadAttempts
  123. }
  124. daemon.configStore.MaxDownloadAttempts = &maxDownloadAttempts
  125. logrus.Debugf("Reset Max Download Attempts: %d", *daemon.configStore.MaxDownloadAttempts)
  126. // prepare reload event attributes with updatable configurations
  127. attributes["max-download-attempts"] = fmt.Sprintf("%d", *daemon.configStore.MaxDownloadAttempts)
  128. return nil
  129. }
  130. // reloadShutdownTimeout updates configuration with daemon shutdown timeout option
  131. // and updates the passed attributes
  132. func (daemon *Daemon) reloadShutdownTimeout(conf *config.Config, attributes map[string]string) {
  133. // update corresponding configuration
  134. if conf.IsValueSet("shutdown-timeout") {
  135. daemon.configStore.ShutdownTimeout = conf.ShutdownTimeout
  136. logrus.Debugf("Reset Shutdown Timeout: %d", daemon.configStore.ShutdownTimeout)
  137. }
  138. // prepare reload event attributes with updatable configurations
  139. attributes["shutdown-timeout"] = fmt.Sprintf("%d", daemon.configStore.ShutdownTimeout)
  140. }
  141. // reloadLabels updates configuration with engine labels
  142. // and updates the passed attributes
  143. func (daemon *Daemon) reloadLabels(conf *config.Config, attributes map[string]string) error {
  144. // update corresponding configuration
  145. if conf.IsValueSet("labels") {
  146. daemon.configStore.Labels = conf.Labels
  147. }
  148. // prepare reload event attributes with updatable configurations
  149. if daemon.configStore.Labels != nil {
  150. labels, err := json.Marshal(daemon.configStore.Labels)
  151. if err != nil {
  152. return err
  153. }
  154. attributes["labels"] = string(labels)
  155. } else {
  156. attributes["labels"] = "[]"
  157. }
  158. return nil
  159. }
  160. // reloadAllowNondistributableArtifacts updates the configuration with allow-nondistributable-artifacts options
  161. // and updates the passed attributes.
  162. func (daemon *Daemon) reloadAllowNondistributableArtifacts(conf *config.Config, attributes map[string]string) error {
  163. // Update corresponding configuration.
  164. if conf.IsValueSet("allow-nondistributable-artifacts") {
  165. daemon.configStore.AllowNondistributableArtifacts = conf.AllowNondistributableArtifacts
  166. if err := daemon.RegistryService.LoadAllowNondistributableArtifacts(conf.AllowNondistributableArtifacts); err != nil {
  167. return err
  168. }
  169. }
  170. // Prepare reload event attributes with updatable configurations.
  171. if daemon.configStore.AllowNondistributableArtifacts != nil {
  172. v, err := json.Marshal(daemon.configStore.AllowNondistributableArtifacts)
  173. if err != nil {
  174. return err
  175. }
  176. attributes["allow-nondistributable-artifacts"] = string(v)
  177. } else {
  178. attributes["allow-nondistributable-artifacts"] = "[]"
  179. }
  180. return nil
  181. }
  182. // reloadInsecureRegistries updates configuration with insecure registry option
  183. // and updates the passed attributes
  184. func (daemon *Daemon) reloadInsecureRegistries(conf *config.Config, attributes map[string]string) error {
  185. // update corresponding configuration
  186. if conf.IsValueSet("insecure-registries") {
  187. daemon.configStore.InsecureRegistries = conf.InsecureRegistries
  188. if err := daemon.RegistryService.LoadInsecureRegistries(conf.InsecureRegistries); err != nil {
  189. return err
  190. }
  191. }
  192. // prepare reload event attributes with updatable configurations
  193. if daemon.configStore.InsecureRegistries != nil {
  194. insecureRegistries, err := json.Marshal(daemon.configStore.InsecureRegistries)
  195. if err != nil {
  196. return err
  197. }
  198. attributes["insecure-registries"] = string(insecureRegistries)
  199. } else {
  200. attributes["insecure-registries"] = "[]"
  201. }
  202. return nil
  203. }
  204. // reloadRegistryMirrors updates configuration with registry mirror options
  205. // and updates the passed attributes
  206. func (daemon *Daemon) reloadRegistryMirrors(conf *config.Config, attributes map[string]string) error {
  207. // update corresponding configuration
  208. if conf.IsValueSet("registry-mirrors") {
  209. daemon.configStore.Mirrors = conf.Mirrors
  210. if err := daemon.RegistryService.LoadMirrors(conf.Mirrors); err != nil {
  211. return err
  212. }
  213. }
  214. // prepare reload event attributes with updatable configurations
  215. if daemon.configStore.Mirrors != nil {
  216. mirrors, err := json.Marshal(daemon.configStore.Mirrors)
  217. if err != nil {
  218. return err
  219. }
  220. attributes["registry-mirrors"] = string(mirrors)
  221. } else {
  222. attributes["registry-mirrors"] = "[]"
  223. }
  224. return nil
  225. }
  226. // reloadLiveRestore updates configuration with live restore option
  227. // and updates the passed attributes
  228. func (daemon *Daemon) reloadLiveRestore(conf *config.Config, attributes map[string]string) error {
  229. // update corresponding configuration
  230. if conf.IsValueSet("live-restore") {
  231. daemon.configStore.LiveRestoreEnabled = conf.LiveRestoreEnabled
  232. }
  233. // prepare reload event attributes with updatable configurations
  234. attributes["live-restore"] = fmt.Sprintf("%t", daemon.configStore.LiveRestoreEnabled)
  235. return nil
  236. }
  237. // reloadNetworkDiagnosticPort updates the network controller starting the diagnostic if the config is valid
  238. func (daemon *Daemon) reloadNetworkDiagnosticPort(conf *config.Config, attributes map[string]string) error {
  239. if conf == nil || daemon.netController == nil || !conf.IsValueSet("network-diagnostic-port") ||
  240. conf.NetworkDiagnosticPort < 1 || conf.NetworkDiagnosticPort > 65535 {
  241. // If there is no config make sure that the diagnostic is off
  242. if daemon.netController != nil {
  243. daemon.netController.StopDiagnostic()
  244. }
  245. return nil
  246. }
  247. // Enable the network diagnostic if the flag is set with a valid port within the range
  248. logrus.WithFields(logrus.Fields{"port": conf.NetworkDiagnosticPort, "ip": "127.0.0.1"}).Warn("Starting network diagnostic server")
  249. daemon.netController.StartDiagnostic(conf.NetworkDiagnosticPort)
  250. return nil
  251. }
  252. // reloadFeatures updates configuration with enabled/disabled features
  253. func (daemon *Daemon) reloadFeatures(conf *config.Config, attributes map[string]string) {
  254. // update corresponding configuration
  255. // note that we allow features option to be entirely unset
  256. daemon.configStore.Features = conf.Features
  257. // prepare reload event attributes with updatable configurations
  258. attributes["features"] = fmt.Sprintf("%v", daemon.configStore.Features)
  259. }