Skip to content

Commit

Permalink
Add support for rmi --force for nerdctl images
Browse files Browse the repository at this point in the history
Signed-off-by: Manu Gupta <[email protected]>
  • Loading branch information
manugupt1 committed Oct 23, 2024
1 parent beaa39d commit 1858622
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 23 deletions.
24 changes: 3 additions & 21 deletions cmd/nerdctl/image/image_remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,8 @@ func TestRemove(t *testing.T) {
{
Description: "Remove image with running container - with -f",
NoParallel: true,
// FIXME: nerdctl is broken
// https://github.com/containerd/nerdctl/issues/3454
// If an image is associated with a running/paused containers, `docker rmi -f imageName`
// untags `imageName` (left a `<none>` image) without deletion; `docker rmi -rf imageID` fails.
// In both cases, `nerdctl rmi -f` will fail.
Require: test.Require(
test.Not(test.Windows),
test.Not(nerdtest.Docker),
),
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "--pull", "always", "-d", "--name", data.Identifier(), testutil.CommonImage, "sleep", "infinity")
Expand All @@ -133,11 +127,9 @@ func TestRemove(t *testing.T) {
Command: test.Command("rmi", "-f", testutil.CommonImage),
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 1,
Errors: []error{errors.New("image is being used")},
Output: func(stdout string, info string, t *testing.T) {
helpers.Command("images").Run(&test.Expected{
Output: test.Contains(repoName),
Output: test.DoesNotContain(repoName),
})
},
}
Expand Down Expand Up @@ -225,12 +217,6 @@ func TestRemove(t *testing.T) {
Require: test.Require(
test.Not(test.Windows),
nerdtest.CGroup,
// FIXME: nerdctl is broken
// https://github.com/containerd/nerdctl/issues/3454
// If an image is associated with a running/paused containers, `docker rmi -f imageName`
// untags `imageName` (left a `<none>` image) without deletion; `docker rmi -rf imageID` fails.
// In both cases, `nerdctl rmi -f` will fail.
test.Not(nerdtest.Docker),
),
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "--pull", "always", "-d", "--name", data.Identifier(), testutil.CommonImage, "sleep", "infinity")
Expand All @@ -242,11 +228,9 @@ func TestRemove(t *testing.T) {
Command: test.Command("rmi", "-f", testutil.CommonImage),
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 1,
Errors: []error{errors.New("image is being used")},
Output: func(stdout string, info string, t *testing.T) {
helpers.Command("images").Run(&test.Expected{
Output: test.Contains(repoName),
Output: test.DoesNotContain(repoName),
})
},
}
Expand Down Expand Up @@ -334,11 +318,9 @@ func TestIssue3016(t *testing.T) {
},
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 0,
Errors: []error{},
Errors: []error{},
Output: func(stdout string, info string, t *testing.T) {
helpers.Command("images", data.Get(tagIDKey)).Run(&test.Expected{
ExitCode: 0,
Output: func(stdout string, info string, t *testing.T) {
assert.Equal(t, len(strings.Split(stdout, "\n")), 2)
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/image/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func Remove(ctx context.Context, client *containerd.Client, args []string, optio
return nil
}

if cid, ok := runningImages[found.Image.Name]; ok {
return fmt.Errorf("conflict: unable to delete %s (cannot be forced) - image is being used by running container %s", found.Req, cid)
if cid, ok := runningImages[found.Image.Name]; !options.Force && ok {
return fmt.Errorf("conflict: unable to delete %s - image is being used by running container %s", found.Req, cid)
}
if cid, ok := usedImages[found.Image.Name]; ok && !options.Force {
return fmt.Errorf("conflict: unable to delete %s (must be forced) - image is being used by stopped container %s", found.Req, cid)
Expand Down

0 comments on commit 1858622

Please sign in to comment.