Skip to content

Commit 9459d46

Browse files
authored
Merge pull request #1255 from rudyfly/remove-volume
feature: add "volume" flag when remove container
2 parents b07e9ec + b06040c commit 9459d46

File tree

18 files changed

+289
-93
lines changed

18 files changed

+289
-93
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: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package server
33
import (
44
"context"
55
"encoding/json"
6-
"fmt"
76
"net/http"
87

98
"github.com/alibaba/pouch/apis/types"
109
"github.com/alibaba/pouch/pkg/httputils"
1110
"github.com/alibaba/pouch/pkg/randomid"
11+
volumetypes "github.com/alibaba/pouch/storage/volume/types"
1212

1313
"github.com/go-openapi/strfmt"
1414
"github.com/gorilla/mux"
@@ -35,7 +35,7 @@ func (s *Server) createVolume(ctx context.Context, rw http.ResponseWriter, req *
3535
}
3636

3737
if driver == "" {
38-
driver = "local"
38+
driver = volumetypes.DefaultBackend
3939
}
4040

4141
if err := s.VolumeMgr.Create(ctx, name, driver, options, labels); err != nil {
@@ -100,16 +100,6 @@ func (s *Server) getVolume(ctx context.Context, rw http.ResponseWriter, req *htt
100100
func (s *Server) removeVolume(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
101101
name := mux.Vars(req)["name"]
102102

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-
113103
if err := s.VolumeMgr.Remove(ctx, name); err != nil {
114104
return err
115105
}

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+
removeVolumes 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.removeVolumes, "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.removeVolumes,
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"os"
88
"strings"
99

10+
"github.com/alibaba/pouch/apis/types"
11+
1012
"github.com/spf13/cobra"
1113
)
1214

@@ -145,7 +147,7 @@ func (rc *RunCommand) runRun(args []string) error {
145147
}
146148

147149
if rc.rm {
148-
if err := apiClient.ContainerRemove(ctx, containerName, true); err != nil {
150+
if err := apiClient.ContainerRemove(ctx, containerName, &types.ContainerRemoveOptions{Force: true}); err != nil {
149151
return fmt.Errorf("failed to remove container %s: %v", containerName, err)
150152
}
151153
}

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/container_remove_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import (
88
"net/http"
99
"strings"
1010
"testing"
11+
12+
"github.com/alibaba/pouch/apis/types"
1113
)
1214

1315
func TestContainerRemoveError(t *testing.T) {
1416
client := &APIClient{
1517
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")),
1618
}
17-
err := client.ContainerRemove(context.Background(), "nothing", true)
19+
err := client.ContainerRemove(context.Background(), "nothing", &types.ContainerRemoveOptions{Force: true})
1820
if err == nil || !strings.Contains(err.Error(), "Server error") {
1921
t.Fatalf("expected a Server Error, got %v", err)
2022
}
@@ -24,7 +26,7 @@ func TestContainerRemoveNotFoundError(t *testing.T) {
2426
client := &APIClient{
2527
HTTPCli: newMockClient(errorMockResponse(http.StatusNotFound, "Not Found")),
2628
}
27-
err := client.ContainerRemove(context.Background(), "no contaienr", true)
29+
err := client.ContainerRemove(context.Background(), "no container", &types.ContainerRemoveOptions{Force: true})
2830
if err == nil || !strings.Contains(err.Error(), "Not Found") {
2931
t.Fatalf("expected a Not Found Error, got %v", err)
3032
}
@@ -49,7 +51,7 @@ func TestContainerRemove(t *testing.T) {
4951
client := &APIClient{
5052
HTTPCli: httpClient,
5153
}
52-
err := client.ContainerRemove(context.Background(), "container_id", true)
54+
err := client.ContainerRemove(context.Background(), "container_id", &types.ContainerRemoveOptions{Force: true})
5355
if err != nil {
5456
t.Fatal(err)
5557
}

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)

0 commit comments

Comments
 (0)