Преглед на файлове

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 <github@gone.nl>
Sebastiaan van Stijn преди 3 години
родител
ревизия
4cf904494e
променени са 3 файла, в които са добавени 17 реда и са изтрити 30 реда
  1. 4 12
      daemon/config/config.go
  2. 10 7
      daemon/config/config_test.go
  3. 3 11
      daemon/reload.go

+ 4 - 12
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()

+ 10 - 7
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{

+ 3 - 11
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