Skip to content

Commit 954cd17

Browse files
committed
feature: add "volume" flag when remove container
Add "volume" flag when remove container, it is used to remove these volumes that is created by container. Signed-off-by: Rudy Zhang <[email protected]>
1 parent da348c6 commit 954cd17

File tree

15 files changed

+195
-42
lines changed

15 files changed

+195
-42
lines changed

apis/server/container_bridge.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ import (
2323
func (s *Server) removeContainers(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
2424
name := mux.Vars(req)["name"]
2525

26-
option := &mgr.ContainerRemoveOption{
27-
Force: httputils.BoolValue(req, "force"),
28-
// TODO Volume and Link will be supported in the future.
29-
Volume: httputils.BoolValue(req, "v"),
30-
Link: httputils.BoolValue(req, "link"),
26+
option := &types.ContainerRemoveOptions{
27+
Force: httputils.BoolValue(req, "force"),
28+
Volumes: httputils.BoolValue(req, "v"),
29+
// TODO: Link will be supported in the future.
30+
Link: httputils.BoolValue(req, "link"),
3131
}
3232

3333
if err := s.ContainerMgr.Remove(ctx, name, option); err != nil {

apis/server/volume_bridge.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package server
33
import (
44
"context"
55
"encoding/json"
6-
"fmt"
76
"net/http"
87

98
"github.com/alibaba/pouch/apis/types"
@@ -100,16 +99,6 @@ func (s *Server) getVolume(ctx context.Context, rw http.ResponseWriter, req *htt
10099
func (s *Server) removeVolume(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
101100
name := mux.Vars(req)["name"]
102101

103-
volume, err := s.VolumeMgr.Get(ctx, name)
104-
if err != nil {
105-
return err
106-
}
107-
108-
ref := volume.Option("ref")
109-
if ref != "" {
110-
return fmt.Errorf("failed to remove volume: %s, using by: %s", name, ref)
111-
}
112-
113102
if err := s.VolumeMgr.Remove(ctx, name); err != nil {
114103
return err
115104
}

apis/swagger.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3376,6 +3376,17 @@ definitions:
33763376
Width:
33773377
type: "integer"
33783378

3379+
ContainerRemoveOptions:
3380+
description: "options of remove container"
3381+
type: "object"
3382+
properties:
3383+
Force:
3384+
type: "boolean"
3385+
Volumes:
3386+
type: "boolean"
3387+
Link:
3388+
type: "boolean"
3389+
33793390
parameters:
33803391
id:
33813392
name: id

apis/types/container_remove_options.go

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/rm.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/alibaba/pouch/apis/types"
8+
79
"github.com/spf13/cobra"
810
)
911

@@ -18,7 +20,8 @@ be released.
1820
// RmCommand is used to implement 'rm' command.
1921
type RmCommand struct {
2022
baseCommand
21-
force bool
23+
force bool
24+
removeVolume bool
2225
}
2326

2427
// Init initializes RmCommand command.
@@ -39,16 +42,24 @@ func (r *RmCommand) Init(c *Cli) {
3942

4043
// addFlags adds flags for specific command.
4144
func (r *RmCommand) addFlags() {
42-
r.cmd.Flags().BoolVarP(&r.force, "force", "f", false, "if the container is running, force to remove it")
45+
flagSet := r.cmd.Flags()
46+
47+
flagSet.BoolVarP(&r.force, "force", "f", false, "if the container is running, force to remove it")
48+
flagSet.BoolVarP(&r.removeVolume, "volumes", "v", false, "remove container's volumes that create by the container")
4349
}
4450

4551
// runRm is the entry of RmCommand command.
4652
func (r *RmCommand) runRm(args []string) error {
4753
ctx := context.Background()
4854
apiClient := r.cli.Client()
4955

56+
options := &types.ContainerRemoveOptions{
57+
Force: r.force,
58+
Volumes: r.removeVolume,
59+
}
60+
5061
for _, name := range args {
51-
if err := apiClient.ContainerRemove(ctx, name, r.force); err != nil {
62+
if err := apiClient.ContainerRemove(ctx, name, options); err != nil {
5263
return fmt.Errorf("failed to remove container: %v", err)
5364
}
5465
fmt.Printf("%s\n", name)

cli/run.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"strings"
99

10+
"github.com/alibaba/pouch/apis/types"
1011
"github.com/spf13/cobra"
1112
)
1213

@@ -145,7 +146,7 @@ func (rc *RunCommand) runRun(args []string) error {
145146
}
146147

147148
if rc.rm {
148-
if err := apiClient.ContainerRemove(ctx, containerName, true); err != nil {
149+
if err := apiClient.ContainerRemove(ctx, containerName, &types.ContainerRemoveOptions{Force: true}); err != nil {
149150
return fmt.Errorf("failed to remove container %s: %v", containerName, err)
150151
}
151152
}

client/container_remove.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ package client
33
import (
44
"context"
55
"net/url"
6+
7+
"github.com/alibaba/pouch/apis/types"
68
)
79

810
// ContainerRemove removes a container.
9-
func (client *APIClient) ContainerRemove(ctx context.Context, name string, force bool) error {
11+
func (client *APIClient) ContainerRemove(ctx context.Context, name string, options *types.ContainerRemoveOptions) error {
1012
q := url.Values{}
11-
if force {
13+
if options.Force {
1214
q.Set("force", "true")
1315
}
16+
if options.Volumes {
17+
q.Set("v", "true")
18+
}
1419

1520
resp, err := client.delete(ctx, "/containers/"+name, q, nil)
1621
if err != nil {

client/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type ContainerAPIClient interface {
2323
ContainerCreate(ctx context.Context, config types.ContainerConfig, hostConfig *types.HostConfig, networkConfig *types.NetworkingConfig, containerName string) (*types.ContainerCreateResp, error)
2424
ContainerStart(ctx context.Context, name, detachKeys string) error
2525
ContainerStop(ctx context.Context, name, timeout string) error
26-
ContainerRemove(ctx context.Context, name string, force bool) error
26+
ContainerRemove(ctx context.Context, name string, options *types.ContainerRemoveOptions) error
2727
ContainerList(ctx context.Context, all bool) ([]*types.Container, error)
2828
ContainerAttach(ctx context.Context, name string, stdin bool) (net.Conn, *bufio.Reader, error)
2929
ContainerCreateExec(ctx context.Context, name string, config *types.ExecCreateConfig) (*types.ExecCreateResp, error)

daemon/mgr/container.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ type ContainerMgr interface {
7777
GetExecConfig(ctx context.Context, execid string) (*ContainerExecConfig, error)
7878

7979
// Remove removes a container, it may be running or stopped and so on.
80-
Remove(ctx context.Context, name string, option *ContainerRemoveOption) error
80+
Remove(ctx context.Context, name string, option *types.ContainerRemoveOptions) error
8181

8282
// Rename renames a container.
8383
Rename(ctx context.Context, oldName string, newName string) error
@@ -209,20 +209,20 @@ func (mgr *ContainerManager) Restore(ctx context.Context) error {
209209
}
210210

211211
// Remove removes a container, it may be running or stopped and so on.
212-
func (mgr *ContainerManager) Remove(ctx context.Context, name string, option *ContainerRemoveOption) error {
212+
func (mgr *ContainerManager) Remove(ctx context.Context, name string, options *types.ContainerRemoveOptions) error {
213213
c, err := mgr.container(name)
214214
if err != nil {
215215
return err
216216
}
217217
c.Lock()
218218
defer c.Unlock()
219219

220-
if !c.IsStopped() && !c.IsExited() && !c.IsCreated() && !option.Force {
220+
if !c.IsStopped() && !c.IsExited() && !c.IsCreated() && !options.Force {
221221
return fmt.Errorf("container: %s is not stopped, can't remove it without flag force", c.ID())
222222
}
223223

224224
// if the container is running, force to stop it.
225-
if c.IsRunning() && option.Force {
225+
if c.IsRunning() && options.Force {
226226
msg, err := mgr.Client.DestroyContainer(ctx, c.ID(), c.StopTimeout())
227227
if err != nil && !errtypes.IsNotfound(err) {
228228
return errors.Wrapf(err, "failed to destroy container: %s", c.ID())
@@ -232,7 +232,7 @@ func (mgr *ContainerManager) Remove(ctx context.Context, name string, option *Co
232232
}
233233
}
234234

235-
if err := mgr.detachVolumes(ctx, c.meta); err != nil {
235+
if err := mgr.detachVolumes(ctx, c.meta, options.Volumes); err != nil {
236236
logrus.Errorf("failed to detach volume: %v", err)
237237
}
238238

@@ -1854,7 +1854,7 @@ func (mgr *ContainerManager) generateMountPoints(ctx context.Context, meta *Cont
18541854

18551855
defer func() {
18561856
if err != nil {
1857-
if err := mgr.detachVolumes(ctx, meta); err != nil {
1857+
if err := mgr.detachVolumes(ctx, meta, false); err != nil {
18581858
logrus.Errorf("failed to detach volume, err: %v", err)
18591859
}
18601860
}
@@ -2235,7 +2235,7 @@ func (mgr *ContainerManager) setMountPointDiskQuota(ctx context.Context, c *Cont
22352235
return nil
22362236
}
22372237

2238-
func (mgr *ContainerManager) detachVolumes(ctx context.Context, c *ContainerMeta) error {
2238+
func (mgr *ContainerManager) detachVolumes(ctx context.Context, c *ContainerMeta, remove bool) error {
22392239
for _, mount := range c.Mounts {
22402240
name := mount.Name
22412241
if name == "" {
@@ -2271,6 +2271,12 @@ func (mgr *ContainerManager) detachVolumes(ctx context.Context, c *ContainerMeta
22712271
}
22722272

22732273
mgr.VolumeMgr.Detach(ctx, name, option)
2274+
2275+
if remove {
2276+
if err := mgr.VolumeMgr.Remove(ctx, name); err != nil && !errtypes.IsUsing(err) {
2277+
logrus.Warnf("failed to remove volume: %s when remove container", name)
2278+
}
2279+
}
22742280
}
22752281

22762282
return nil

daemon/mgr/container_types.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,6 @@ type AttachConfig struct {
7575
CriLogFile *os.File
7676
}
7777

78-
// ContainerRemoveOption wraps the container remove interface params.
79-
type ContainerRemoveOption struct {
80-
Force bool
81-
Volume bool
82-
Link bool
83-
}
84-
8578
// ContainerListOption wraps the container list interface params.
8679
type ContainerListOption struct {
8780
All bool

0 commit comments

Comments
 (0)