Browse Source

Extract restart-policy-validation to a function

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 6 years ago
parent
commit
e1809510ca
1 changed files with 21 additions and 16 deletions
  1. 21 16
      daemon/container.go

+ 21 - 16
daemon/container.go

@@ -308,23 +308,9 @@ func (daemon *Daemon) verifyContainerSettings(platform string, hostConfig *conta
 		}
 	}
 
-	p := hostConfig.RestartPolicy
-
-	switch p.Name {
-	case "always", "unless-stopped", "no":
-		if p.MaximumRetryCount != 0 {
-			return nil, errors.Errorf("maximum retry count cannot be used with restart policy '%s'", p.Name)
-		}
-	case "on-failure":
-		if p.MaximumRetryCount < 0 {
-			return nil, errors.Errorf("maximum retry count cannot be negative")
-		}
-	case "":
-		// do nothing
-	default:
-		return nil, errors.Errorf("invalid restart policy '%s'", p.Name)
+	if err := validateRestartPolicy(hostConfig.RestartPolicy); err != nil {
+		return nil, err
 	}
-
 	if !hostConfig.Isolation.IsValid() {
 		return nil, errors.Errorf("invalid isolation '%s' on %s", hostConfig.Isolation, runtime.GOOS)
 	}
@@ -356,3 +342,22 @@ func validateHealthCheck(healthConfig *containertypes.HealthConfig) error {
 	}
 	return nil
 }
+
+func validateRestartPolicy(policy containertypes.RestartPolicy) error {
+	switch policy.Name {
+	case "always", "unless-stopped", "no":
+		if policy.MaximumRetryCount != 0 {
+			return errors.Errorf("maximum retry count cannot be used with restart policy '%s'", policy.Name)
+		}
+	case "on-failure":
+		if policy.MaximumRetryCount < 0 {
+			return errors.Errorf("maximum retry count cannot be negative")
+		}
+	case "":
+		// do nothing
+		return nil
+	default:
+		return errors.Errorf("invalid restart policy '%s'", policy.Name)
+	}
+	return nil
+}