Skip to content

Commit 310daf2

Browse files
authored
Merge pull request #4259 from gmargaritis/373-support-detach-flag-in-stack-rm
Add support for --detach flag in stack rm
2 parents 4a43b8e + 238d659 commit 310daf2

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

cli/command/stack/options/opts.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type PS struct {
3838
// Remove holds docker stack remove options
3939
type Remove struct {
4040
Namespaces []string
41+
Detach bool
4142
}
4243

4344
// Services holds docker stack services options

cli/command/stack/remove.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
2727
return completeNames(dockerCli)(cmd, args, toComplete)
2828
},
2929
}
30+
31+
flags := cmd.Flags()
32+
flags.BoolVarP(&opts.Detach, "detach", "d", true, "Do not wait for stack removal")
3033
return cmd
3134
}

cli/command/stack/swarm/common.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ func getStackSecrets(ctx context.Context, apiclient client.APIClient, namespace
4444
func getStackConfigs(ctx context.Context, apiclient client.APIClient, namespace string) ([]swarm.Config, error) {
4545
return apiclient.ConfigList(ctx, types.ConfigListOptions{Filters: getStackFilter(namespace)})
4646
}
47+
48+
func getStackTasks(ctx context.Context, apiclient client.APIClient, namespace string) ([]swarm.Task, error) {
49+
return apiclient.TaskList(ctx, types.TaskListOptions{Filters: getStackFilter(namespace)})
50+
}

cli/command/stack/swarm/remove.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/docker/docker/api/types"
1212
"github.com/docker/docker/api/types/swarm"
1313
"github.com/docker/docker/api/types/versions"
14+
apiclient "github.com/docker/docker/client"
1415
"github.com/pkg/errors"
1516
)
1617

@@ -58,6 +59,14 @@ func RunRemove(ctx context.Context, dockerCli command.Cli, opts options.Remove)
5859

5960
if hasError {
6061
errs = append(errs, fmt.Sprintf("Failed to remove some resources from stack: %s", namespace))
62+
continue
63+
}
64+
65+
if !opts.Detach {
66+
err = waitOnTasks(ctx, client, namespace)
67+
if err != nil {
68+
errs = append(errs, fmt.Sprintf("Failed to wait on tasks of stack: %s: %s", namespace, err))
69+
}
6170
}
6271
}
6372

@@ -137,3 +146,45 @@ func removeConfigs(
137146
}
138147
return hasError
139148
}
149+
150+
var numberedStates = map[swarm.TaskState]int64{
151+
swarm.TaskStateNew: 1,
152+
swarm.TaskStateAllocated: 2,
153+
swarm.TaskStatePending: 3,
154+
swarm.TaskStateAssigned: 4,
155+
swarm.TaskStateAccepted: 5,
156+
swarm.TaskStatePreparing: 6,
157+
swarm.TaskStateReady: 7,
158+
swarm.TaskStateStarting: 8,
159+
swarm.TaskStateRunning: 9,
160+
swarm.TaskStateComplete: 10,
161+
swarm.TaskStateShutdown: 11,
162+
swarm.TaskStateFailed: 12,
163+
swarm.TaskStateRejected: 13,
164+
}
165+
166+
func terminalState(state swarm.TaskState) bool {
167+
return numberedStates[state] > numberedStates[swarm.TaskStateRunning]
168+
}
169+
170+
func waitOnTasks(ctx context.Context, client apiclient.APIClient, namespace string) error {
171+
terminalStatesReached := 0
172+
for {
173+
tasks, err := getStackTasks(ctx, client, namespace)
174+
if err != nil {
175+
return fmt.Errorf("failed to get tasks: %w", err)
176+
}
177+
178+
for _, task := range tasks {
179+
if terminalState(task.Status.State) {
180+
terminalStatesReached++
181+
break
182+
}
183+
}
184+
185+
if terminalStatesReached == len(tasks) {
186+
break
187+
}
188+
}
189+
return nil
190+
}

docs/reference/commandline/stack_rm.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ Remove one or more stacks
77

88
`docker stack rm`, `docker stack remove`, `docker stack down`
99

10+
### Options
11+
12+
| Name | Type | Default | Description |
13+
|:-----------------|:-------|:--------|:------------------------------|
14+
| `-d`, `--detach` | `bool` | `true` | Do not wait for stack removal |
15+
1016

1117
<!---MARKER_GEN_END-->
1218

0 commit comments

Comments
 (0)