Skip to content

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

Merged
thaJeztah merged 1 commit into
docker:masterfrom
gmargaritis:373-support-detach-flag-in-stack-rm
Mar 4, 2024
Merged

Add support for --detach flag in stack rm#4259
thaJeztah merged 1 commit into
docker:masterfrom
gmargaritis:373-support-detach-flag-in-stack-rm

Conversation

@gmargaritis

@gmargaritisgmargaritis commented May 5, 2023

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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.

@gmargaritisgmargaritisMay 26, 2023

Copy link
Copy Markdown
ContributorAuthor

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,
}
longestStateint
)

functerminalState(state swarm.TaskState) bool {
returnnumberedStates[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
Copy Markdown
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
Copy Markdown
Contributor

Looks fine to me.

@gmargaritis
gmargaritisforce-pushed the 373-support-detach-flag-in-stack-rm branch from 074f182 to 08204d8CompareSeptember 14, 2023 12:29
@gmargaritis

Copy link
Copy Markdown
ContributorAuthor

Hey @thaJeztah and @neersighted

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

@gmargaritis
gmargaritisforce-pushed the 373-support-detach-flag-in-stack-rm branch 2 times, most recently from bda291b to 153b2f3CompareJanuary 24, 2024 13:53
@gmargaritis

Copy link
Copy Markdown
ContributorAuthor

👋 @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?

cpuguy83
cpuguy83 previously requested changes Jan 29, 2024
Comment threadcli/command/stack/remove.go Outdated
}

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

Copy link
Copy Markdown
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
Copy Markdown
ContributorAuthor

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
gmargaritisforce-pushed the 373-support-detach-flag-in-stack-rm branch from 153b2f3 to b040ca8CompareJanuary 29, 2024 20:43
@cpuguy83

Copy link
Copy Markdown
Collaborator

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

@cpuguy83

Copy link
Copy Markdown
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

gmargaritis commented Jan 31, 2024

Copy link
Copy Markdown
ContributorAuthor

@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

codecov-commenter commented Feb 8, 2024

Copy link
Copy Markdown

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
Copy Markdown
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 waitfor stack removal (default true)

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.

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

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.

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

Copy link
Copy Markdown
ContributorAuthor

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

@gmargaritisgmargaritisFeb 8, 2024

Copy link
Copy Markdown
ContributorAuthor

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
ContributorAuthor

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
Copy Markdown
ContributorAuthor

@thaJeztah@crazy-max 👋

Is there anything left to be done in this one?

@neersightedneersighted left a comment

Copy link
Copy Markdown
Contributor

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.

@neersightedneersighted added this to the 26.0.0 milestone Feb 20, 2024
@gmargaritis

Copy link
Copy Markdown
ContributorAuthor

@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 <github@gone.nl>
Signed-off-by: George Margaritis <gmargaritis@protonmail.com>
@thaJeztah
thaJeztahforce-pushed the 373-support-detach-flag-in-stack-rm branch from 23e079d to 238d659CompareMarch 4, 2024 08:57
@thaJeztah
thaJeztah dismissed cpuguy83’s stale reviewMarch 4, 2024 09:04

this was addressed

@thaJeztah

Copy link
Copy Markdown
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

Sign up for freeto 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

@gmargaritis@neersighted@dperny@cpuguy83@codecov-commenter@thaJeztah@crazy-max