diff options
author | Matthew Heon <matthew.heon@pm.me> | 2019-04-01 19:45:23 -0400 |
---|---|---|
committer | Matthew Heon <matthew.heon@pm.me> | 2019-05-03 10:36:16 -0400 |
commit | 7ba1b609aad678f458951c978455ea44c5b4d3ec (patch) | |
tree | cd9b6322c44fbb15579ff40f3ca33ea8d63285a1 | |
parent | 357e4c37e92e7426a66f1a1142a9650bb0d81ca0 (diff) | |
download | podman-7ba1b609aad678f458951c978455ea44c5b4d3ec.tar.gz podman-7ba1b609aad678f458951c978455ea44c5b4d3ec.tar.bz2 podman-7ba1b609aad678f458951c978455ea44c5b4d3ec.zip |
Move to using constants for valid restart policy types
Signed-off-by: Matthew Heon <matthew.heon@pm.me>
-rw-r--r-- | libpod/container.go | 14 | ||||
-rw-r--r-- | libpod/container_api.go | 6 | ||||
-rw-r--r-- | libpod/container_internal.go | 2 | ||||
-rw-r--r-- | libpod/options.go | 2 |
4 files changed, 19 insertions, 5 deletions
diff --git a/libpod/container.go b/libpod/container.go index 769c4f69c..c07f4c78d 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -102,6 +102,20 @@ func (ns LinuxNS) String() string { } } +// Valid restart policy types. +const ( + // RestartPolicyNone indicates that no restart policy has been requested + // by a container. + RestartPolicyNone = "" + // RestartPolicyNo is identical in function to RestartPolicyNone. + RestartPolicyNo = "no" + // RestartPolicyAlways unconditionally restarts the container. + RestartPolicyAlways = "always" + // RestartPolicyOnFailure restarts the container on non-0 exit code, + // with an optional maximum number of retries. + RestartPolicyOnFailure = "on-failure" +) + // Container is a single OCI container. // All operations on a Container that access state must begin with a call to // syncContainer(). diff --git a/libpod/container_api.go b/libpod/container_api.go index 7132a82c2..faf01eccb 100644 --- a/libpod/container_api.go +++ b/libpod/container_api.go @@ -605,13 +605,13 @@ func (c *Container) Cleanup(ctx context.Context) error { // restart the container. // However, perform a full validation of restart policy first. if c.state.RestartPolicyMatch { - if c.config.RestartPolicy == "on-failure" && c.state.ExitCode != 0 { + if c.config.RestartPolicy == RestartPolicyOnFailure && c.state.ExitCode != 0 { logrus.Debugf("Container %s restart policy trigger: on retry %d (of %d)", c.ID(), c.state.RestartCount, c.config.RestartRetries) } - if (c.config.RestartPolicy == "on-failure" && c.state.ExitCode != 0 && + if (c.config.RestartPolicy == RestartPolicyOnFailure && c.state.ExitCode != 0 && (c.config.RestartRetries > 0 && c.state.RestartCount < c.config.RestartRetries)) || - c.config.RestartPolicy == "always" { + c.config.RestartPolicy == RestartPolicyAlways { // The container stopped. We need to restart it. return c.handleRestartPolicy(ctx) } diff --git a/libpod/container_internal.go b/libpod/container_internal.go index b1b7a090f..76de37a09 100644 --- a/libpod/container_internal.go +++ b/libpod/container_internal.go @@ -275,7 +275,7 @@ func (c *Container) syncContainer() error { // Only save back to DB if state changed if c.state.State != oldState { // Check for a restart policy match - if c.config.RestartPolicy != "" && c.config.RestartPolicy != "no" && + if c.config.RestartPolicy != RestartPolicyNone && c.config.RestartPolicy != RestartPolicyNo && (oldState == ContainerStateRunning || oldState == ContainerStatePaused) && (c.state.State == ContainerStateStopped || c.state.State == ContainerStateExited) && !c.state.StoppedByUser { diff --git a/libpod/options.go b/libpod/options.go index e83515822..7ec7dfe63 100644 --- a/libpod/options.go +++ b/libpod/options.go @@ -1249,7 +1249,7 @@ func WithRestartPolicy(policy string) CtrCreateOption { } switch policy { - case "", "no", "on-failure", "always": + case RestartPolicyNone, RestartPolicyNo, RestartPolicyOnFailure, RestartPolicyAlways: ctr.config.RestartPolicy = policy default: return errors.Wrapf(ErrInvalidArg, "%q is not a valid restart policy", policy) |