Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions pkg/cmd/container/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"os"
"strings"
"syscall"

containerd "github.com/containerd/containerd/v2/client"
Expand Down Expand Up @@ -84,12 +85,32 @@ func Remove(ctx context.Context, client *containerd.Client, containers []string,
},
}

err := walker.WalkAll(ctx, containers, true)
if err != nil && options.Force {
log.G(ctx).Error(err)
return nil
return removeAll(ctx, containers, walker.Walk, options.Force)
}

// removeAll calls walk for every request in reqs and returns all errors joined by `\n`.
// In force mode, a request that matches no container is only logged, so that
// `rm -f` stays idempotent, but a failure to remove an existing container is still
// returned to the caller.
func removeAll(ctx context.Context, reqs []string, walk func(context.Context, string) (int, error), force bool) error {
var errs []string
for _, req := range reqs {
n, err := walk(ctx, req)
if err == nil && n == 0 {
err = fmt.Errorf("no such container: %s", req)
if force {
log.G(ctx).Error(err)
continue
}
}
if err != nil {
errs = append(errs, err.Error())
}
}
return err
if len(errs) > 0 {
return fmt.Errorf("%d errors:\n%s", len(errs), strings.Join(errs, "\n"))
}
return nil
}

// RemoveContainer removes a container from containerd store.
Expand Down
64 changes: 64 additions & 0 deletions pkg/cmd/container/remove_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
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 container

import (
"context"
"errors"
"testing"

"gotest.tools/v3/assert"
)

func TestRemoveAll(t *testing.T) {
walk := func(_ context.Context, req string) (int, error) {
switch req {
case "missing":
return 0, nil
case "broken":
return -1, errors.New("failed to delete task")
default:
return 1, nil
}
}

testCases := []struct {
name string
reqs []string
force bool
wantErr string
}{
{name: "removed", reqs: []string{"ok"}},
{name: "missing without force", reqs: []string{"missing"}, wantErr: "1 errors:\nno such container: missing"},
{name: "missing with force", reqs: []string{"missing"}, force: true},
{name: "failure without force", reqs: []string{"broken"}, wantErr: "1 errors:\nfailed to delete task"},
{name: "failure with force", reqs: []string{"broken"}, force: true, wantErr: "1 errors:\nfailed to delete task"},
{name: "mixed with force", reqs: []string{"ok", "missing", "broken"}, force: true, wantErr: "1 errors:\nfailed to delete task"},
{name: "mixed without force", reqs: []string{"ok", "missing", "broken"}, wantErr: "2 errors:\nno such container: missing\nfailed to delete task"},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := removeAll(context.Background(), tc.reqs, walk, tc.force)
if tc.wantErr == "" {
assert.NilError(t, err)
return
}
assert.Error(t, err, tc.wantErr)
})
}
}
Loading