Skip to content

Commit e0e9ee8

Browse files
committed
rm: fail with --force when a container that exists cannot be removed
A container that is not found, or that disappears while it is being removed, still counts as removed in force mode. Signed-off-by: Srinivas Chippagiri <cvas22@gmail.com>
1 parent 52b705e commit e0e9ee8

2 files changed

Lines changed: 126 additions & 5 deletions

File tree

‎pkg/cmd/container/remove.go‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"errors"
2323
"fmt"
2424
"os"
25+
"strings"
2526
"syscall"
2627

2728
containerd "github.com/containerd/containerd/v2/client"
@@ -84,12 +85,36 @@ func Remove(ctx context.Context, client *containerd.Client, containers []string,
8485
},
8586
}
8687

87-
err := walker.WalkAll(ctx, containers, true)
88-
if err != nil && options.Force {
89-
log.G(ctx).Error(err)
90-
return nil
88+
if !options.Force {
89+
return walker.WalkAll(ctx, containers, true)
9190
}
92-
return err
91+
return walkAllForce(ctx, containers, walker.Walk)
92+
}
93+
94+
// walkAllForce walks every request for "rm --force". Like "docker rm -f", a container that does not
95+
// exist is only logged, but a container that exists and cannot be removed fails the command, so the
96+
// caller can tell the difference. It keeps going after a failure, to remove as many as it can.
97+
func walkAllForce(ctx context.Context, reqs []string, walk func(ctx context.Context, req string) (int, error)) error {
98+
var errs []string
99+
for _, req := range reqs {
100+
n, err := walk(ctx, req)
101+
if err == nil && n == 0 {
102+
log.G(ctx).Errorf("no such container: %s", req)
103+
continue
104+
}
105+
if errdefs.IsNotFound(err) {
106+
// it disappeared between the lookup and the removal
107+
log.G(ctx).Errorf("no such container: %s", req)
108+
continue
109+
}
110+
if err != nil {
111+
errs = append(errs, err.Error())
112+
}
113+
}
114+
if len(errs) > 0 {
115+
return fmt.Errorf("%d errors:\n%s", len(errs), strings.Join(errs, "\n"))
116+
}
117+
return nil
93118
}
94119

95120
// RemoveContainer removes a container from containerd store.

‎pkg/cmd/container/remove_test.go‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package container
18+
19+
import (
20+
"context"
21+
"errors"
22+
"testing"
23+
24+
"gotest.tools/v3/assert"
25+
26+
"github.com/containerd/errdefs"
27+
)
28+
29+
func TestWalkAllForce(t *testing.T) {
30+
t.Parallel()
31+
32+
walkResults := map[string]struct {
33+
count int
34+
err error
35+
}{
36+
"found": {count: 1},
37+
"missing": {count: 0},
38+
"broken": {count: -1, err: errors.New("failed to delete task: no such directory")},
39+
"other": {count: -1, err: errors.New("cannot kill")},
40+
"gone": {count: -1, err: errdefs.ErrNotFound},
41+
}
42+
var walked []string
43+
walk := func(_ context.Context, req string) (int, error) {
44+
walked = append(walked, req)
45+
res := walkResults[req]
46+
return res.count, res.err
47+
}
48+
49+
tests := []struct {
50+
name string
51+
reqs []string
52+
expectedErr string
53+
expectedRun []string
54+
}{
55+
{
56+
name: "removed containers succeed",
57+
reqs: []string{"found"},
58+
expectedRun: []string{"found"},
59+
},
60+
{
61+
name: "a container that does not exist is not an error",
62+
reqs: []string{"missing"},
63+
expectedRun: []string{"missing"},
64+
},
65+
{
66+
name: "a container that vanishes during removal is not an error",
67+
reqs: []string{"gone"},
68+
expectedRun: []string{"gone"},
69+
},
70+
{
71+
name: "a container that cannot be removed fails",
72+
reqs: []string{"broken"},
73+
expectedErr: "1 errors:\nfailed to delete task: no such directory",
74+
expectedRun: []string{"broken"},
75+
},
76+
{
77+
name: "it keeps going after a failure and reports every one",
78+
reqs: []string{"broken", "found", "missing", "other"},
79+
expectedErr: "2 errors:\nfailed to delete task: no such directory\ncannot kill",
80+
expectedRun: []string{"broken", "found", "missing", "other"},
81+
},
82+
}
83+
84+
for _, tc := range tests {
85+
t.Run(tc.name, func(t *testing.T) {
86+
walked = nil
87+
err := walkAllForce(context.Background(), tc.reqs, walk)
88+
if tc.expectedErr == "" {
89+
assert.NilError(t, err)
90+
} else {
91+
assert.Error(t, err, tc.expectedErr)
92+
}
93+
assert.DeepEqual(t, walked, tc.expectedRun)
94+
})
95+
}
96+
}

0 commit comments

Comments
 (0)