forked from containerd/nerdctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrm.go
More file actions
204 lines (185 loc) · 5.59 KB
/
Copy pathrm.go
File metadata and controls
204 lines (185 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"fmt"
"os"
"syscall"
"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/nerdctl/pkg/dnsutil/hostsstore"
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
"github.com/containerd/nerdctl/pkg/labels"
"github.com/containerd/nerdctl/pkg/namestore"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var rmCommand = &cli.Command{
Name: "rm",
Usage: "Remove one or more containers",
ArgsUsage: "[flags] CONTAINER [CONTAINER, ...]",
Action: rmAction,
BashComplete: rmBashComplete,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "Force the removal of a running|paused|unknown container (uses SIGKILL)",
},
},
}
func rmAction(clicontext *cli.Context) error {
if clicontext.NArg() == 0 {
return errors.Errorf("requires at least 1 argument")
}
client, ctx, cancel, err := newClient(clicontext)
if err != nil {
return err
}
defer cancel()
dataStore, err := getDataStore(clicontext)
if err != nil {
return err
}
force := clicontext.Bool("force")
ns := clicontext.String("namespace")
containerNameStore, err := namestore.New(dataStore, ns)
if err != nil {
return err
}
walker := &containerwalker.ContainerWalker{
Client: client,
OnFound: func(ctx context.Context, found containerwalker.Found) error {
stateDir, err := getContainerStateDirPath(clicontext, dataStore, found.Container.ID())
if err != nil {
return err
}
err = removeContainer(clicontext, ctx, client, ns, found.Container.ID(), found.Req, force, dataStore, stateDir, containerNameStore)
return err
},
}
for _, req := range clicontext.Args().Slice() {
n, err := walker.Walk(ctx, req)
if err != nil {
return err
} else if n == 0 {
return errors.Errorf("no such container %s", req)
}
}
return nil
}
// removeContainer returns nil when the container cannot be found
// FIXME: refactoring
func removeContainer(clicontext *cli.Context, ctx context.Context, client *containerd.Client, ns, id, req string, force bool, dataStore, stateDir string, namst namestore.NameStore) (retErr error) {
var name string
defer func() {
if errdefs.IsNotFound(retErr) {
retErr = nil
}
if retErr == nil {
retErr = os.RemoveAll(stateDir)
} else {
logrus.WithError(retErr).Warnf("failed to remove container %q", id)
}
if retErr == nil {
if name != "" {
retErr = namst.Release(name, id)
}
} else {
logrus.WithError(retErr).Warnf("failed to remove container %q", id)
}
if retErr == nil {
retErr = hostsstore.DeallocHostsFile(dataStore, ns, id)
} else {
logrus.WithError(retErr).Warnf("failed to release name store for container %q", id)
}
}()
container, err := client.LoadContainer(ctx, id)
if err != nil {
return err
}
l, err := container.Labels(ctx)
if err != nil {
return err
}
name = l[labels.Name]
task, err := container.Task(ctx, cio.Load)
if err != nil {
if errdefs.IsNotFound(err) {
if container.Delete(ctx, containerd.WithSnapshotCleanup) != nil {
return container.Delete(ctx)
}
}
return err
}
status, err := task.Status(ctx)
if err != nil {
if errdefs.IsNotFound(err) {
return nil
}
return err
}
switch status.Status {
case containerd.Created, containerd.Stopped:
if _, err := task.Delete(ctx); err != nil && !errdefs.IsNotFound(err) {
return errors.Wrapf(err, "failed to delete task %v", id)
}
case containerd.Paused:
if !force {
_, err := fmt.Fprintf(clicontext.App.Writer, "You cannot remove a %v container %v. Unpause the container before attempting removal or force remove\n", status.Status, id)
return err
}
_, err := task.Delete(ctx, containerd.WithProcessKill)
if err != nil && !errdefs.IsNotFound(err) {
return errors.Wrapf(err, "failed to delete task %v", id)
}
// default is the case, when status.Status = containerd.Running
default:
if !force {
_, err := fmt.Fprintf(clicontext.App.Writer, "You cannot remove a %v container %v. Stop the container before attempting removal or force remove\n", status.Status, id)
return err
}
if err := task.Kill(ctx, syscall.SIGKILL); err != nil {
logrus.WithError(err).Warnf("failed to send SIGKILL")
}
es, err := task.Wait(ctx)
if err == nil {
<-es
}
_, err = task.Delete(ctx, containerd.WithProcessKill)
if err != nil && !errdefs.IsNotFound(err) {
logrus.WithError(err).Warnf("failed to delete task %v", id)
}
}
var delOpts []containerd.DeleteOpts
if _, err := container.Image(ctx); err == nil {
delOpts = append(delOpts, containerd.WithSnapshotCleanup)
}
if err := container.Delete(ctx, delOpts...); err != nil {
return err
}
_, err = fmt.Fprintf(clicontext.App.Writer, "%s\n", req)
return err
}
func rmBashComplete(clicontext *cli.Context) {
coco := parseCompletionContext(clicontext)
if coco.boring || coco.flagTakesValue {
defaultBashComplete(clicontext)
return
}
// show container names
bashCompleteContainerNames(clicontext)
}