github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/daemon/reload.go (about)

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