From e1809510ca44393478ee507be95fac2178d67611 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 19 Dec 2018 01:12:08 +0100 Subject: [PATCH] Extract restart-policy-validation to a function Signed-off-by: Sebastiaan van Stijn --- daemon/container.go | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/daemon/container.go b/daemon/container.go index 4213c5cca4..06b11e5065 100644 --- a/daemon/container.go +++ b/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 +}