Skip to content

Commit 9fc9d10

Browse files
committed
Add --force flag to runc delete
Signed-off-by: Kenfe-Mickael Laventure <[email protected]>
1 parent 629e356 commit 9fc9d10

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

delete.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ import (
1313
"github.com/urfave/cli"
1414
)
1515

16+
func killContainer(container libcontainer.Container) error {
17+
container.Signal(syscall.SIGKILL)
18+
for i := 0; i < 100; i++ {
19+
time.Sleep(100 * time.Millisecond)
20+
if err := container.Signal(syscall.Signal(0)); err != nil {
21+
destroy(container)
22+
return nil
23+
}
24+
}
25+
return fmt.Errorf("container init still running")
26+
}
27+
1628
var deleteCommand = cli.Command{
1729
Name: "delete",
1830
Usage: "delete any resources held by the container often used with detached containers",
@@ -23,9 +35,15 @@ Where "<container-id>" is the name for the instance of the container.
2335
EXAMPLE:
2436
For example, if the container id is "ubuntu01" and runc list currently shows the
2537
status of "ubuntu01" as "stopped" the following will delete resources held for
26-
"ubuntu01" removing "ubuntu01" from the runc list of containers:
27-
38+
"ubuntu01" removing "ubuntu01" from the runc list of containers:
39+
2840
# runc delete ubuntu01`,
41+
Flags: []cli.Flag{
42+
cli.BoolFlag{
43+
Name: "force, f",
44+
Usage: "Forcibly kills the container if it is still running",
45+
},
46+
},
2947
Action: func(context *cli.Context) error {
3048
container, err := getContainer(context)
3149
if err != nil {
@@ -47,16 +65,11 @@ status of "ubuntu01" as "stopped" the following will delete resources held for
4765
case libcontainer.Stopped:
4866
destroy(container)
4967
case libcontainer.Created:
50-
container.Signal(syscall.SIGKILL)
51-
for i := 0; i < 100; i++ {
52-
time.Sleep(100 * time.Millisecond)
53-
if err := container.Signal(syscall.Signal(0)); err != nil {
54-
destroy(container)
55-
return nil
56-
}
57-
}
58-
return fmt.Errorf("container init still running")
68+
return killContainer(container)
5969
default:
70+
if context.Bool("force") {
71+
return killContainer(container)
72+
}
6073
return fmt.Errorf("cannot delete container that is not stopped: %s", s)
6174
}
6275
return nil

tests/integration/delete.bats

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,20 @@ function teardown() {
3131
runc state test_busybox
3232
[ "$status" -ne 0 ]
3333
}
34+
35+
@test "runc delete --force" {
36+
# run busybox detached
37+
runc run -d --console /dev/pts/ptmx test_busybox
38+
[ "$status" -eq 0 ]
39+
40+
# check state
41+
wait_for_container 15 1 test_busybox
42+
43+
testcontainer test_busybox running
44+
45+
# force delete test_busybox
46+
runc delete --force test_busybox
47+
48+
runc state test_busybox
49+
[ "$status" -ne 0 ]
50+
}

0 commit comments

Comments
 (0)