Skip to content

API: add "signal" parameter to container stop and restart endpoints#43206

Merged
thaJeztah merged 9 commits intomoby:masterfrom
thaJeztah:having_such_a_good_time_im_having_a_ball
Apr 21, 2022
Merged

API: add "signal" parameter to container stop and restart endpoints#43206
thaJeztah merged 9 commits intomoby:masterfrom
thaJeztah:having_such_a_good_time_im_having_a_ball

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

When reviewing #41548, I recalled I had this branch, but never opened it as a PR, as I was planning to do so after #42774 was merged.

depends on / includes:

API: add "signal" parameter to container stop and restart endpoints

Containers can have a default stop-signal (--stop-signal / STOPSIGNAL) and
timeout (--stop-timeout). It is currently not possible to update either of
these after the container is created (docker update does not allow updating
them), and while either of these can be overridden through some commands, we
currently do not have a command that can override both:

command stop-signal stop-timeout notes
docker kill yes DNA only sends a single signal
docker restart no yes
docker stop no yes

As a result, if a user wants to stop a container with a custom signal and
timeout, the only option is to do this manually:

docker kill -s <custom signal> mycontainer
# wait <desired timeout>
# press ^C to cancel the graceful stop
# forcibly kill the container
docker kill mycontainer

This patch adds a new signal query parameter to the container "stop" and
"restart" endpoints. This parameter can be added as a new flag on the CLI,
which would allow stopping and restarting with a custom timeout and signal,
for example:

docker stop --signal=SIGWINCH --time=120 mycontainer

docker restart --signal=SIGWINCH --time=120 mycontainer

Refactoring / cleanup commits

daemon/delete: rename var that collided with import, remove output var (part of #42774)

Renamed a variable that collided with an import.

Also remove the named return variable from cleanupContainer(), as it wasn't used.

daemon: cleanupContainer(): pass ContainerRmConfig as parameter (part of #42774)

We already have this config, so might as well pass it, instead of passing each option as a separate argument.

daemon: fix inconsistent receiver name and package aliases

My IDE kept complaining about this one, so let's rename it

daemon/stop: rename imports and variables to standard naming

This is in preparation of some changes, but keeping it in a separate commit to make review of other changes easier.

daemon: move default stop-timeout to containerStop()

This avoids having to determine what the default is in various parts of the code. If no custom timeout is passed (nil), the default will be used.

Also remove the named return variable from cleanupContainer(), as it wasn't used.

daemon: containerStop(): use a regular "defer" to log container event

backend: add StopConfig to ContainerRestart and ContainerStop

And while we're modifying the interface, also add a context to both

Copy link
Copy Markdown
Member

@tianon tianon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@thaJeztah thaJeztah force-pushed the having_such_a_good_time_im_having_a_ball branch from f876056 to 36caa69 Compare February 16, 2022 10:12
@thaJeztah
Copy link
Copy Markdown
Member Author

@AkihiroSuda @cpuguy83 ptal

Comment on lines +16 to +32
// StopConfig holds the options to stop, or restart a container.
type StopConfig struct {
StopSignal string `json:",omitempty"` // Signal to stop the container.
StopTimeout *int `json:",omitempty"` // Timeout (in seconds) to stop the container, or '-1' to wait indefinitely.
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙈 and I just realised why I probably didn't push it as a PR before; I see that I didn't change the client code to allow the new options to be set.

Looking at that, I'm a bit on the fence if StopConfig should be used for both stop and restart (or if separate types should be used).

Let me temporarily move this PR back to draft, and work on the client bits

@thaJeztah thaJeztah marked this pull request as draft February 16, 2022 19:51
@thaJeztah thaJeztah force-pushed the having_such_a_good_time_im_having_a_ball branch from 36caa69 to 24957b4 Compare March 11, 2022 19:15
@thaJeztah thaJeztah force-pushed the having_such_a_good_time_im_having_a_ball branch from 24957b4 to e41a0bf Compare April 17, 2022 13:24
@thaJeztah thaJeztah marked this pull request as ready for review April 17, 2022 13:25
@thaJeztah
Copy link
Copy Markdown
Member Author

@tianon @AkihiroSuda - Rebased, pushed the commits with the client-side changes that I didn't include, PTAL

@thaJeztah thaJeztah added this to the 22.04.0 milestone Apr 17, 2022
@thaJeztah thaJeztah force-pushed the having_such_a_good_time_im_having_a_ball branch 2 times, most recently from e9d8f5f to 0aebf51 Compare April 19, 2022 14:26
Copy link
Copy Markdown
Member

@tianon tianon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love it. 👍

daemon.statsCollector.StopCollection(container)

if err := daemon.containerStop(container, 3); err != nil {
var stopTimeout = 3
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO a good (unrelated) follow-up here would be to add a comment of some kind here explaining what this is and why we chose "3" as the hard-coded override value 😬

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright; as I also had to address @samuelkarp's comments, I added a comment; the "bad" news; "who knows??" (but I left references to the commits that set this duration). The good news: you can earn yourself a picture of a cute animal!

Copy link
Copy Markdown
Member

@samuelkarp samuelkarp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two nits (one in-line, one here) and one minor comment.

nit: commit "daemon: move default stop-timeout to containerStop()" has a typo in the commit message ("determin")

But aside from those, LGTM!

- daemon/delete: rename var that collided with import, remove output var
- daemon: fix inconsistent receiver name and package aliases
- daemon/stop: rename imports and variables to standard naming
  This is in preparation of some changes, but keeping it in  a
  separate commit to make review of other changes easier.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
We already have this config, so might as well pass it, instead of passing
each option as a separate argument.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This avoids having to determine what the default is in various
parts of the code. If no custom timeout is passed (nil), the
default will be used.

Also remove the named return variable from cleanupContainer(),
as it wasn't used.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
While we're modifying the interface, also add a context to both.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Containers can have a default stop-signal (`--stop-signal` / `STOPSIGNAL`) and
timeout (`--stop-timeout`). It is currently not possible to update either of
these after the container is created (`docker update` does not allow updating
them), and while either of these can be overridden through some commands, we
currently do not have a command that can override *both*:

command         | stop-signal | stop-timeout | notes
----------------|-------------|--------------|----------------------------
docker kill     | yes         | DNA          | only sends a single signal
docker restart  | no          | yes          |
docker stop     | no          | yes          |

As a result, if a user wants to stop a container with a custom signal and
timeout, the only option is to do this manually:

    docker kill -s <custom signal> mycontainer
    # wait <desired timeout>
    # press ^C to cancel the graceful stop
    # forcibly kill the container
    docker kill mycontainer

This patch adds a new `signal` query parameter to the container "stop" and
"restart" endpoints. This parameter can be added as a new flag on the CLI,
which would allow stopping and restarting with a custom timeout and signal,
for example:

    docker stop --signal=SIGWINCH --time=120 mycontainer

    docker restart --signal=SIGWINCH --time=120 mycontainer

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
…onger used

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah thaJeztah force-pushed the having_such_a_good_time_im_having_a_ball branch from 0aebf51 to 90647e2 Compare April 20, 2022 19:43
@thaJeztah
Copy link
Copy Markdown
Member Author

Updated; fixed the typo in the commit message; changed cleanupContainer() to not use a pointer, and added a comment describing the 3 seconds 😅

Copy link
Copy Markdown
Member

@samuelkarp samuelkarp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thaJeztah
Copy link
Copy Markdown
Member Author

thx! Looking at the review comments, I think they've been addressed; let me get this one in, and work on docker/cli changes to expose these options 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants