-
Notifications
You must be signed in to change notification settings - Fork 3k
Add restart policy for containers #2826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3fb52f4
0d73ee4
dc42304
f4db6d5
357e4c3
7ba1b60
948fb5e
56356d7
cafb68e
e1443fe
d7c367a
4d348d7
5c4fefa
ceaaed7
d328695
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(). | ||
|
|
@@ -179,6 +193,16 @@ type ContainerState struct { | |
| // This maps the path the file will be mounted to in the container to | ||
| // the path of the file on disk outside the container | ||
| BindMounts map[string]string `json:"bindMounts,omitempty"` | ||
| // StoppedByUser indicates whether the container was stopped by an | ||
| // explicit call to the Stop() API. | ||
| StoppedByUser bool `json:"stoppedByUser,omitempty"` | ||
|
||
| // RestartPolicyMatch indicates whether the conditions for restart | ||
| // policy have been met. | ||
| RestartPolicyMatch bool `json:"restartPolicyMatch,omitempty"` | ||
| // RestartCount is how many times the container was restarted by its | ||
| // restart policy. This is NOT incremented by normal container restarts | ||
| // (only by restart policy). | ||
| RestartCount uint `json:"restartCount,omitempty"` | ||
|
|
||
| // ExtensionStageHooks holds hooks which will be executed by libpod | ||
| // and not delegated to the OCI runtime. | ||
|
|
@@ -346,6 +370,17 @@ type ContainerConfig struct { | |
| LogPath string `json:"logPath"` | ||
| // File containing the conmon PID | ||
| ConmonPidFile string `json:"conmonPidFile,omitempty"` | ||
| // RestartPolicy indicates what action the container will take upon | ||
| // exiting naturally. | ||
| // Allowed options are "no" (take no action), "on-failure" (restart on | ||
| // non-zero exit code, up an a maximum of RestartRetries times), | ||
| // and "always" (always restart the container on any exit code). | ||
| // The empty string is treated as the default ("no") | ||
| RestartPolicy string `json:"restart_policy,omitempty"` | ||
| // RestartRetries indicates the number of attempts that will be made to | ||
| // restart the container. Used only if RestartPolicy is set to | ||
| // "on-failure". | ||
| RestartRetries uint `json:"restart_retries,omitempty"` | ||
| // TODO log options for log drivers | ||
|
|
||
| PostConfigureNetNS bool `json:"postConfigureNetNS"` | ||
|
|
@@ -729,6 +764,17 @@ func (c *Container) LogPath() string { | |
| return c.config.LogPath | ||
| } | ||
|
|
||
| // RestartPolicy returns the container's restart policy. | ||
| func (c *Container) RestartPolicy() string { | ||
| return c.config.RestartPolicy | ||
| } | ||
|
|
||
| // RestartRetries returns the number of retries that will be attempted when | ||
| // using the "on-failure" restart policy | ||
| func (c *Container) RestartRetries() uint { | ||
| return c.config.RestartRetries | ||
| } | ||
|
|
||
| // RuntimeName returns the name of the runtime | ||
| func (c *Container) RuntimeName() string { | ||
| return c.runtime.ociRuntime.name | ||
|
|
@@ -1003,6 +1049,21 @@ func (c *Container) BindMounts() (map[string]string, error) { | |
| return newMap, nil | ||
| } | ||
|
|
||
| // StoppedByUser returns whether the container was last stopped by an explicit | ||
| // call to the Stop() API, or whether it exited naturally. | ||
| func (c *Container) StoppedByUser() (bool, error) { | ||
| if !c.batched { | ||
| c.lock.Lock() | ||
| defer c.lock.Unlock() | ||
|
|
||
| if err := c.syncContainer(); err != nil { | ||
| return false, err | ||
| } | ||
| } | ||
|
|
||
| return c.state.StoppedByUser, nil | ||
| } | ||
|
|
||
| // Misc Accessors | ||
| // Most will require locking | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docker also supports
unless-stoppedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless-stopped is very much a "restarted the daemon" thing, so I just throw an error about it in pkg/spec if they try to pass it.
We still want to point people to systemd for cases like that, where the container will be restarted after system reboot, for example. The manpages make this clear, but it might be good to add the warning to Podman itself too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My reading of unless-stopped, seems to be exactly what you are designing. IE If a user stops a container then it will not restart, otherwise if the container fails, it will be restarted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems the interpretation depends on where we look. The man page is referring to the daemon start only while the docs state:
This makes me believe that we can support it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From that description, it's identical to
always- restart policy never triggers after the container is stopped via an API call.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Minus the daemon restart aspect)