Skip to content
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

Add support for --detach flag in stack rm #4259

Merged

Conversation

gmargaritis
Copy link
Contributor

@gmargaritis gmargaritis commented May 5, 2023

Part of #373 along with #4258

Added --detach to stack rm. Setting --detach=false waits until all of the stack tasks have reached a terminal state.

@neersighted
Copy link
Member

PTAL @corhere @dperny

Comment on lines +150 to +164
var numberedStates = map[swarm.TaskState]int64{
swarm.TaskStateNew: 1,
swarm.TaskStateAllocated: 2,
swarm.TaskStatePending: 3,
swarm.TaskStateAssigned: 4,
swarm.TaskStateAccepted: 5,
swarm.TaskStatePreparing: 6,
swarm.TaskStateReady: 7,
swarm.TaskStateStarting: 8,
swarm.TaskStateRunning: 9,
swarm.TaskStateComplete: 10,
swarm.TaskStateShutdown: 11,
swarm.TaskStateFailed: 12,
swarm.TaskStateRejected: 13,
}
Copy link
Member

Choose a reason for hiding this comment

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

We should rely on importing SwarmKit here instead of incorporating magic enums.

Also it's unclear if this logic even belongs on the client; this looks dangerously close to leaking concerns across layers.

Copy link
Contributor Author

@gmargaritis gmargaritis May 26, 2023

Choose a reason for hiding this comment

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

Hey @neersighted, thanks a lot for getting back to me!

This map was inspired by the progress code for Service:

var (
numberedStates = map[swarm.TaskState]int64{
swarm.TaskStateNew: 1,
swarm.TaskStateAllocated: 2,
swarm.TaskStatePending: 3,
swarm.TaskStateAssigned: 4,
swarm.TaskStateAccepted: 5,
swarm.TaskStatePreparing: 6,
swarm.TaskStateReady: 7,
swarm.TaskStateStarting: 8,
swarm.TaskStateRunning: 9,
// The following states are not actually shown in progress
// output, but are used internally for ordering.
swarm.TaskStateComplete: 10,
swarm.TaskStateShutdown: 11,
swarm.TaskStateFailed: 12,
swarm.TaskStateRejected: 13,
}
longestState int
)

func terminalState(state swarm.TaskState) bool {
return numberedStates[state] > numberedStates[swarm.TaskStateRunning]
}

An alternative would be to create a set with all the terminal states and check against that instead.

By design, the server instantly acknowledges any changes (e.g. removing a stack) and then the action is asynchronous. Thus, I believe that this logic belongs to the client, as the client is the one that waits for the actions to complete.

Copy link
Contributor

Choose a reason for hiding this comment

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

The reason Swarm states are numbered like they are is so that intermediate states can be inserted at any time without disrupting the ordering of states committed to the object store already. If the code handling states isn't persistent, then there's no such concern. If we ever added an intermediate state, we can just renumber all of these harmlessly.

@dperny
Copy link
Contributor

dperny commented Sep 1, 2023

Looks fine to me.

@gmargaritis gmargaritis force-pushed the 373-support-detach-flag-in-stack-rm branch from 074f182 to 08204d8 Compare September 14, 2023 12:29
@gmargaritis
Copy link
Contributor Author

Hey @thaJeztah and @neersighted

Is there anything else I can do in order to move this forward?

@gmargaritis gmargaritis force-pushed the 373-support-detach-flag-in-stack-rm branch 2 times, most recently from bda291b to 153b2f3 Compare January 24, 2024 13:53
@gmargaritis
Copy link
Contributor Author

👋 @thaJeztah @neersighted

I have rebased this PR along with #4258, and it's ready for review!

Anything I can do to move this forward in order to close #373?

@@ -27,5 +27,8 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
return completeNames(dockerCli)(cmd, args, toComplete)
},
}

flags := cmd.Flags()
flags.BoolVarP(&opts.Detach, "detach", "d", true, "Exit immediately instead of waiting for the stack tasks to converge")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
flags.BoolVarP(&opts.Detach, "detach", "d", true, "Exit immediately instead of waiting for the stack tasks to converge")
flags.BoolVarP(&opts.Detach, "detach", "d", true, "Do not wait for stack removal")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated it in b040ca8!

@gmargaritis gmargaritis force-pushed the 373-support-detach-flag-in-stack-rm branch from 153b2f3 to b040ca8 Compare January 29, 2024 20:43
@gmargaritis gmargaritis requested a review from cpuguy83 January 29, 2024 20:44
@cpuguy83
Copy link
Collaborator

It looks like this is changing the default behavior of stack rm to wait for removal?

@cpuguy83
Copy link
Collaborator

Oh nevermind I see the default for the flag is true.
This makes usage a little weird because you have to --detach=false.

🤔

@gmargaritis
Copy link
Contributor Author

gmargaritis commented Jan 31, 2024

@cpuguy83 Thank you for your feedback!

I see where you're coming from regarding the --detach flag behavior. We set --detach to true by default, in order to keep the user experience of the command the same. This approach was actually based on the implementation of the --detach flag in service create and service update (moby/moby#31144). In this case, the default was --detach=true and in a later release it was decided to change the default behavior of the commands to --detach=false.

If the plan is to switch the default behavior of stack rm to non-detachable in a future release, we can add a message indicating so, just like we did in moby/moby#31144 (comment) and in #4258 (comment).

I hope this clears things up and takes us closer to also merging #4258 and closing #373 😄

/cc @thaJeztah

@codecov-commenter
Copy link

codecov-commenter commented Feb 8, 2024

Codecov Report

Merging #4259 (238d659) into master (4a43b8e) will decrease coverage by 0.06%.
The diff coverage is 8.33%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4259      +/-   ##
==========================================
- Coverage   61.18%   61.13%   -0.06%     
==========================================
  Files         287      287              
  Lines       20112    20136      +24     
==========================================
+ Hits        12306    12310       +4     
- Misses       6912     6933      +21     
+ Partials      894      893       -1     


| Name | Type | Default | Description |
|:-----------------|:-----|:--------|:------------------------------|
| `-d`, `--detach` | | | Do not wait for stack removal |
Copy link
Member

Choose a reason for hiding this comment

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

Interesting; for some reason it doesn't show the default here 🤔

--help shows the correct output, but the markdown generator doesn't 🤔 Any ideas @crazy-max ?

./build/docker stack rm --help

Usage:  docker stack rm [OPTIONS] STACK [STACK...]

Remove one or more stacks

Aliases:
  docker stack rm, docker stack remove, docker stack down

Options:
  -d, --detach   Do not wait for stack removal (default true)

Copy link
Member

Choose a reason for hiding this comment

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

I think it makes sense to not show default as this is a bool flag anyway? (like --pull or --push)

Copy link
Member

Choose a reason for hiding this comment

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

Oh my bad this is true by default so opt-out flag right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@crazy-max Yes, it's true by default and the user can use --detach=false if they want to wait for stack removal to complete

Copy link
Contributor Author

@gmargaritis gmargaritis Feb 8, 2024

Choose a reason for hiding this comment

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

It's the same for service create/update where we already have implemented this flag

### Options
| Name | Type | Default | Description |
|:----------------------------------------------------|:------------------|:-------------|:----------------------------------------------------------------------------------------------------|
| `--cap-add` | `list` | | Add Linux capabilities |
| `--cap-drop` | `list` | | Drop Linux capabilities |
| [`--config`](#config) | `config` | | Specify configurations to expose to the service |
| [`--constraint`](#constraint) | `list` | | Placement constraints |
| `--container-label` | `list` | | Container labels |
| `--credential-spec` | `credential-spec` | | Credential spec for managed service account (Windows only) |
| `-d`, `--detach` | | | Exit immediately instead of waiting for the service to converge |

Copy link
Collaborator

Choose a reason for hiding this comment

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

Crazy thought, how about "--wait"?

Copy link
Member

Choose a reason for hiding this comment

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

So the intent here is to (like docker service create) flip the default at some point, so make stack deploy act the same as docker compose up.

I don't think the markdown here is a real blocker for the PR (we can fix in "post"), but I noticed it, and was curious what went wrong here (looks like a bug in the markdown generator potentially?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, we can investigate further in a separate issue, as it seems like a generalized bug in the markdown generator.

Is there anything else left to be done in this PR?

@gmargaritis
Copy link
Contributor Author

@thaJeztah @crazy-max 👋

Is there anything left to be done in this one?

Copy link
Member

@neersighted neersighted left a comment

Choose a reason for hiding this comment

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

Overall LGTM/agreed that we can worry about the markdown later, given this is no more broken then other commands.

@neersighted neersighted added this to the 26.0.0 milestone Feb 20, 2024
@gmargaritis
Copy link
Contributor Author

@neersighted We have fixed the markdown issue in docker/cli-docs-tool#48 ✌️

Added --detach/-d to stack rm. Setting --detach=false waits until
all of the stack tasks have reached a terminal state.

Co-authored-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: George Margaritis <[email protected]>
@thaJeztah thaJeztah force-pushed the 373-support-detach-flag-in-stack-rm branch from 23e079d to 238d659 Compare March 4, 2024 08:57
@thaJeztah thaJeztah dismissed cpuguy83’s stale review March 4, 2024 09:04

this was addressed

@thaJeztah
Copy link
Member

Did a quick rebase and re-generated the markdown as we updated the cli-docs-tool with a fix for the boolean flags;

diff --git a/docs/reference/commandline/stack_rm.md b/docs/reference/commandline/stack_rm.md
index 8dd872b7c..5cfbfaab1 100644
--- a/docs/reference/commandline/stack_rm.md
+++ b/docs/reference/commandline/stack_rm.md
@@ -9,9 +9,9 @@ Remove one or more stacks

 ### Options

-| Name             | Type | Default | Description                   |
-|:-----------------|:-----|:--------|:------------------------------|
-| `-d`, `--detach` |      |         | Do not wait for stack removal |
+| Name             | Type   | Default | Description                   |
+|:-----------------|:-------|:--------|:------------------------------|
+| `-d`, `--detach` | `bool` | `true`  | Do not wait for stack removal |

I also opened a tracking ticket to look at follow-up improvements discussed above; #4907

@thaJeztah thaJeztah merged commit 310daf2 into docker:master Mar 4, 2024
88 checks passed
matt9ucci added a commit to matt9ucci/DockerCompletion that referenced this pull request Apr 8, 2024
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.

7 participants