-
Notifications
You must be signed in to change notification settings - Fork 3k
Add ensureState helper for checking container state #4355
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -328,7 +328,7 @@ func (c *Container) handleRestartPolicy(ctx context.Context) (restarted bool, er | |
|
|
||
| // Is the container running again? | ||
| // If so, we don't have to do anything | ||
| if c.state.State == define.ContainerStateRunning || c.state.State == define.ContainerStatePaused { | ||
| if c.ensureState(define.ContainerStateRunning, define.ContainerStatePaused) { | ||
| return false, nil | ||
| } else if c.state.State == define.ContainerStateUnknown { | ||
| return false, errors.Wrapf(define.ErrInternal, "invalid container state encountered in restart attempt!") | ||
|
|
@@ -359,8 +359,7 @@ func (c *Container) handleRestartPolicy(ctx context.Context) (restarted bool, er | |
| if err := c.reinit(ctx, true); err != nil { | ||
| return false, err | ||
| } | ||
| } else if c.state.State == define.ContainerStateConfigured || | ||
| c.state.State == define.ContainerStateExited { | ||
| } else if c.ensureState(define.ContainerStateConfigured, define.ContainerStateExited) { | ||
| // Initialize the container | ||
| if err := c.init(ctx, true); err != nil { | ||
| return false, err | ||
|
|
@@ -372,6 +371,18 @@ func (c *Container) handleRestartPolicy(ctx context.Context) (restarted bool, er | |
| return true, nil | ||
| } | ||
|
|
||
| // Ensure that the container is in a specific state or state. | ||
| // Returns true if the container is in one of the given states, | ||
| // or false otherwise. | ||
| func (c *Container) ensureState(states ...define.ContainerStatus) bool { | ||
| for _, state := range states { | ||
| if state == c.state.State { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice little convenience function! |
||
|
|
||
| // Sync this container with on-disk state and runtime status | ||
| // Should only be called with container lock held | ||
| // This function should suffice to ensure a container's state is accurate and | ||
|
|
@@ -382,9 +393,7 @@ func (c *Container) syncContainer() error { | |
| } | ||
| // If runtime knows about the container, update its status in runtime | ||
| // And then save back to disk | ||
| if (c.state.State != define.ContainerStateUnknown) && | ||
| (c.state.State != define.ContainerStateConfigured) && | ||
| (c.state.State != define.ContainerStateExited) { | ||
| if c.ensureState(define.ContainerStateCreated, define.ContainerStateRunning, define.ContainerStateStopped, define.ContainerStatePaused) { | ||
| oldState := c.state.State | ||
|
|
||
| if err := c.checkExitFile(); err != nil { | ||
|
|
@@ -516,7 +525,7 @@ func (c *Container) setupStorage(ctx context.Context) error { | |
|
|
||
| // Tear down a container's storage prior to removal | ||
| func (c *Container) teardownStorage() error { | ||
| if c.state.State == define.ContainerStateRunning || c.state.State == define.ContainerStatePaused { | ||
| if c.ensureState(define.ContainerStateRunning, define.ContainerStatePaused) { | ||
| return errors.Wrapf(define.ErrCtrStateInvalid, "cannot remove storage for container %s as it is running or paused", c.ID()) | ||
| } | ||
|
|
||
|
|
@@ -721,10 +730,7 @@ func (c *Container) save() error { | |
| // Otherwise, this function will return with error if there are dependencies of this container that aren't running. | ||
| func (c *Container) prepareToStart(ctx context.Context, recursive bool) (err error) { | ||
| // Container must be created or stopped to be started | ||
| if !(c.state.State == define.ContainerStateConfigured || | ||
| c.state.State == define.ContainerStateCreated || | ||
| c.state.State == define.ContainerStateStopped || | ||
| c.state.State == define.ContainerStateExited) { | ||
| if !c.ensureState(define.ContainerStateConfigured, define.ContainerStateCreated, define.ContainerStateStopped, define.ContainerStateExited) { | ||
| return errors.Wrapf(define.ErrCtrStateInvalid, "container %s must be in Created or Stopped state to be started", c.ID()) | ||
| } | ||
|
|
||
|
|
@@ -755,8 +761,7 @@ func (c *Container) prepareToStart(ctx context.Context, recursive bool) (err err | |
| if err := c.reinit(ctx, false); err != nil { | ||
| return err | ||
| } | ||
| } else if c.state.State == define.ContainerStateConfigured || | ||
| c.state.State == define.ContainerStateExited { | ||
| } else if c.ensureState(define.ContainerStateConfigured, define.ContainerStateExited) { | ||
| // Or initialize it if necessary | ||
| if err := c.init(ctx, false); err != nil { | ||
| return err | ||
|
|
@@ -987,7 +992,7 @@ func (c *Container) cleanupRuntime(ctx context.Context) error { | |
|
|
||
| // If the container is not ContainerStateStopped or | ||
| // ContainerStateCreated, do nothing. | ||
| if c.state.State != define.ContainerStateStopped && c.state.State != define.ContainerStateCreated { | ||
| if !c.ensureState(define.ContainerStateStopped, define.ContainerStateCreated) { | ||
| return nil | ||
| } | ||
|
|
||
|
|
@@ -1078,8 +1083,7 @@ func (c *Container) initAndStart(ctx context.Context) (err error) { | |
| if err := c.reinit(ctx, false); err != nil { | ||
| return err | ||
| } | ||
| } else if c.state.State == define.ContainerStateConfigured || | ||
| c.state.State == define.ContainerStateExited { | ||
| } else if c.ensureState(define.ContainerStateConfigured, define.ContainerStateExited) { | ||
| if err := c.init(ctx, false); err != nil { | ||
| return err | ||
| } | ||
|
|
@@ -1205,7 +1209,7 @@ func (c *Container) unpause() error { | |
|
|
||
| // Internal, non-locking function to restart a container | ||
| func (c *Container) restartWithTimeout(ctx context.Context, timeout uint) (err error) { | ||
| if c.state.State == define.ContainerStateUnknown || c.state.State == define.ContainerStatePaused { | ||
| if !c.ensureState(define.ContainerStateConfigured, define.ContainerStateCreated, define.ContainerStateRunning, define.ContainerStateStopped, define.ContainerStateExited) { | ||
| return errors.Wrapf(define.ErrCtrStateInvalid, "unable to restart a container in a paused or unknown state") | ||
| } | ||
|
|
||
|
|
@@ -1733,9 +1737,8 @@ func (c *Container) checkReadyForRemoval() error { | |
| return errors.Wrapf(define.ErrCtrStateInvalid, "container %s is in invalid state", c.ID()) | ||
| } | ||
|
|
||
| if c.state.State == define.ContainerStateRunning || | ||
| c.state.State == define.ContainerStatePaused { | ||
| return errors.Wrapf(define.ErrCtrStateInvalid, "cannot remove container %s as it is %s - running or paused containers cannot be removed", c.ID(), c.state.State.String()) | ||
| if c.ensureState(define.ContainerStateRunning, define.ContainerStatePaused) { | ||
| return errors.Wrapf(define.ErrCtrStateInvalid, "cannot remove container %s as it is %s - running or paused containers cannot be removed without force", c.ID(), c.state.State.String()) | ||
| } | ||
|
|
||
| if len(c.state.ExecSessions) != 0 { | ||
|
|
@@ -1816,7 +1819,7 @@ func (c *Container) sortUserVolumes(ctrSpec *spec.Spec) ([]*ContainerNamedVolume | |
| // Check for an exit file, and handle one if present | ||
| func (c *Container) checkExitFile() error { | ||
| // If the container's not running, nothing to do. | ||
| if c.state.State != define.ContainerStateRunning && c.state.State != define.ContainerStatePaused { | ||
| if !c.ensureState(define.ContainerStateRunning, define.ContainerStatePaused) { | ||
| return nil | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
A paused container shouldn't be running, so I'm not sure how you'd stop the process there. If you could, I don't think you should unless there was some kind of
--forceoption added to the kill command.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.
Paused processes can still receive signals, which has some interesting applications - you can pause a container, SIGKILL every process in it, and unpause it, to cause it to immediately exit without any possibility of a race condition around
fork()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.
Thanks for the 411, I wasn't aware that paused containers could receive signals still.