From 4cf904494e221d2a9846f4d720b6701a14215cb2 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 24 Apr 2022 22:42:05 +0200 Subject: [PATCH] daemon: reloadMaxDownloadAttempts() remove validation reloadMaxDownloadAttempts() is used to reload the configuration, but validation happened before merging the config with the defaults. This removes the validation from this function, instead centralizing validation in config.Validate(). NOTE: Currently this validation is "ok", as it checks for "nil" values; I am working on changes to reduce the use of pointers in the config, and instead provide a mechanism to fill in defaults. This change is in preparation of that. Signed-off-by: Sebastiaan van Stijn --- daemon/config/config.go | 16 ++++------------ daemon/config/config_test.go | 17 ++++++++++------- daemon/reload.go | 14 +++----------- 3 files changed, 17 insertions(+), 30 deletions(-) diff --git a/daemon/config/config.go b/daemon/config/config.go index 80dd0bc740..339acf2fc3 100644 --- a/daemon/config/config.go +++ b/daemon/config/config.go @@ -600,16 +600,16 @@ func Validate(config *Config) error { return err } } - // validate MaxConcurrentDownloads + + // TODO(thaJeztah) Validations below should not accept "0" to be valid; see Validate() for a more in-depth description of this problem if config.MaxConcurrentDownloads != nil && *config.MaxConcurrentDownloads < 0 { return fmt.Errorf("invalid max concurrent downloads: %d", *config.MaxConcurrentDownloads) } - // validate MaxConcurrentUploads if config.MaxConcurrentUploads != nil && *config.MaxConcurrentUploads < 0 { return fmt.Errorf("invalid max concurrent uploads: %d", *config.MaxConcurrentUploads) } - if err := ValidateMaxDownloadAttempts(config); err != nil { - return err + if config.MaxDownloadAttempts != nil && *config.MaxDownloadAttempts < 0 { + return fmt.Errorf("invalid max download attempts: %d", *config.MaxDownloadAttempts) } // validate that "default" runtime is not reset @@ -642,14 +642,6 @@ func Validate(config *Config) error { return config.ValidatePlatformConfig() } -// ValidateMaxDownloadAttempts validates if the max-download-attempts is within the valid range -func ValidateMaxDownloadAttempts(config *Config) error { - if config.MaxDownloadAttempts != nil && *config.MaxDownloadAttempts <= 0 { - return fmt.Errorf("invalid max download attempts: %d", *config.MaxDownloadAttempts) - } - return nil -} - // GetDefaultRuntimeName returns the current default runtime func (conf *Config) GetDefaultRuntimeName() string { conf.Lock() diff --git a/daemon/config/config_test.go b/daemon/config/config_test.go index b5ec03cf13..a982ea2920 100644 --- a/daemon/config/config_test.go +++ b/daemon/config/config_test.go @@ -309,15 +309,18 @@ func TestValidateConfigurationErrors(t *testing.T) { }, expectedErr: "invalid max download attempts: -10", }, - { - name: "zero max-download-attempts", - config: &Config{ - CommonConfig: CommonConfig{ - MaxDownloadAttempts: intPtr(0), + // TODO(thaJeztah) temporarily excluding this test as it assumes defaults are set before validating and applying updated configs + /* + { + name: "zero max-download-attempts", + config: &Config{ + CommonConfig: CommonConfig{ + MaxDownloadAttempts: intPtr(0), + }, }, + expectedErr: "invalid max download attempts: 0", }, - expectedErr: "invalid max download attempts: 0", - }, + */ { name: "generic resource without =", config: &Config{ diff --git a/daemon/reload.go b/daemon/reload.go index edfd623f8d..368ec53ff2 100644 --- a/daemon/reload.go +++ b/daemon/reload.go @@ -56,9 +56,7 @@ func (daemon *Daemon) Reload(conf *config.Config) (err error) { } daemon.reloadDebug(conf, attributes) daemon.reloadMaxConcurrentDownloadsAndUploads(conf, attributes) - if err := daemon.reloadMaxDownloadAttempts(conf, attributes); err != nil { - return err - } + daemon.reloadMaxDownloadAttempts(conf, attributes) daemon.reloadShutdownTimeout(conf, attributes) daemon.reloadFeatures(conf, attributes) @@ -124,23 +122,17 @@ func (daemon *Daemon) reloadMaxConcurrentDownloadsAndUploads(conf *config.Config // reloadMaxDownloadAttempts updates configuration with max concurrent // download attempts when a connection is lost and updates the passed attributes -func (daemon *Daemon) reloadMaxDownloadAttempts(conf *config.Config, attributes map[string]string) error { - if err := config.ValidateMaxDownloadAttempts(conf); err != nil { - return err - } - - // If no value is set for max-download-attempts we assume it is the default value +func (daemon *Daemon) reloadMaxDownloadAttempts(conf *config.Config, attributes map[string]string) { // We always "reset" as the cost is lightweight and easy to maintain. maxDownloadAttempts := config.DefaultDownloadAttempts if conf.IsValueSet("max-download-attempts") && conf.MaxDownloadAttempts != nil { maxDownloadAttempts = *conf.MaxDownloadAttempts } daemon.configStore.MaxDownloadAttempts = &maxDownloadAttempts - logrus.Debugf("Reset Max Download Attempts: %d", *daemon.configStore.MaxDownloadAttempts) // prepare reload event attributes with updatable configurations attributes["max-download-attempts"] = fmt.Sprintf("%d", *daemon.configStore.MaxDownloadAttempts) - return nil + logrus.Debug("Reset Max Download Attempts: ", attributes["max-download-attempts"]) } // reloadShutdownTimeout updates configuration with daemon shutdown timeout option