Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add "volume" flag when remove container #1255

Merged
merged 1 commit into from
May 3, 2018
Merged
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
10 changes: 5 additions & 5 deletions apis/server/container_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import (
func (s *Server) removeContainers(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
name := mux.Vars(req)["name"]

option := &mgr.ContainerRemoveOption{
Force: httputils.BoolValue(req, "force"),
// TODO Volume and Link will be supported in the future.
Volume: httputils.BoolValue(req, "v"),
Link: httputils.BoolValue(req, "link"),
option := &types.ContainerRemoveOptions{
Force: httputils.BoolValue(req, "force"),
Volumes: httputils.BoolValue(req, "v"),
// TODO: Link will be supported in the future.
Link: httputils.BoolValue(req, "link"),
}

if err := s.ContainerMgr.Remove(ctx, name, option); err != nil {
Expand Down
14 changes: 2 additions & 12 deletions apis/server/volume_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package server
import (
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/pkg/httputils"
"github.com/alibaba/pouch/pkg/randomid"
volumetypes "github.com/alibaba/pouch/storage/volume/types"

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

if driver == "" {
driver = "local"
driver = volumetypes.DefaultBackend
}

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

volume, err := s.VolumeMgr.Get(ctx, name)
if err != nil {
return err
}

ref := volume.Option("ref")
if ref != "" {
return fmt.Errorf("failed to remove volume: %s, using by: %s", name, ref)
}

if err := s.VolumeMgr.Remove(ctx, name); err != nil {
return err
}
Expand Down
11 changes: 11 additions & 0 deletions apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3376,6 +3376,17 @@ definitions:
Width:
type: "integer"

ContainerRemoveOptions:
description: "options of remove container"
type: "object"
properties:
Force:
type: "boolean"
Volumes:
type: "boolean"
Link:
type: "boolean"

parameters:
id:
name: id
Expand Down
62 changes: 62 additions & 0 deletions apis/types/container_remove_options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions cli/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"

"github.com/alibaba/pouch/apis/types"

"github.com/spf13/cobra"
)

Expand All @@ -18,7 +20,8 @@ be released.
// RmCommand is used to implement 'rm' command.
type RmCommand struct {
baseCommand
force bool
force bool
removeVolumes bool
}

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

// addFlags adds flags for specific command.
func (r *RmCommand) addFlags() {
r.cmd.Flags().BoolVarP(&r.force, "force", "f", false, "if the container is running, force to remove it")
flagSet := r.cmd.Flags()

flagSet.BoolVarP(&r.force, "force", "f", false, "if the container is running, force to remove it")
flagSet.BoolVarP(&r.removeVolumes, "volumes", "v", false, "remove container's volumes that create by the container")
}

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

options := &types.ContainerRemoveOptions{
Force: r.force,
Volumes: r.removeVolumes,
}

for _, name := range args {
if err := apiClient.ContainerRemove(ctx, name, r.force); err != nil {
if err := apiClient.ContainerRemove(ctx, name, options); err != nil {
return fmt.Errorf("failed to remove container: %v", err)
}
fmt.Printf("%s\n", name)
Expand Down
4 changes: 3 additions & 1 deletion cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"strings"

"github.com/alibaba/pouch/apis/types"

"github.com/spf13/cobra"
)

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

if rc.rm {
if err := apiClient.ContainerRemove(ctx, containerName, true); err != nil {
if err := apiClient.ContainerRemove(ctx, containerName, &types.ContainerRemoveOptions{Force: true}); err != nil {
return fmt.Errorf("failed to remove container %s: %v", containerName, err)
}
}
Expand Down
9 changes: 7 additions & 2 deletions client/container_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ package client
import (
"context"
"net/url"

"github.com/alibaba/pouch/apis/types"
)

// ContainerRemove removes a container.
func (client *APIClient) ContainerRemove(ctx context.Context, name string, force bool) error {
func (client *APIClient) ContainerRemove(ctx context.Context, name string, options *types.ContainerRemoveOptions) error {
q := url.Values{}
if force {
if options.Force {
q.Set("force", "true")
}
if options.Volumes {
q.Set("v", "true")
}

resp, err := client.delete(ctx, "/containers/"+name, q, nil)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions client/container_remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"net/http"
"strings"
"testing"

"github.com/alibaba/pouch/apis/types"
)

func TestContainerRemoveError(t *testing.T) {
client := &APIClient{
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerRemove(context.Background(), "nothing", true)
err := client.ContainerRemove(context.Background(), "nothing", &types.ContainerRemoveOptions{Force: true})
if err == nil || !strings.Contains(err.Error(), "Server error") {
t.Fatalf("expected a Server Error, got %v", err)
}
Expand All @@ -24,7 +26,7 @@ func TestContainerRemoveNotFoundError(t *testing.T) {
client := &APIClient{
HTTPCli: newMockClient(errorMockResponse(http.StatusNotFound, "Not Found")),
}
err := client.ContainerRemove(context.Background(), "no contaienr", true)
err := client.ContainerRemove(context.Background(), "no container", &types.ContainerRemoveOptions{Force: true})
if err == nil || !strings.Contains(err.Error(), "Not Found") {
t.Fatalf("expected a Not Found Error, got %v", err)
}
Expand All @@ -49,7 +51,7 @@ func TestContainerRemove(t *testing.T) {
client := &APIClient{
HTTPCli: httpClient,
}
err := client.ContainerRemove(context.Background(), "container_id", true)
err := client.ContainerRemove(context.Background(), "container_id", &types.ContainerRemoveOptions{Force: true})
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type ContainerAPIClient interface {
ContainerCreate(ctx context.Context, config types.ContainerConfig, hostConfig *types.HostConfig, networkConfig *types.NetworkingConfig, containerName string) (*types.ContainerCreateResp, error)
ContainerStart(ctx context.Context, name, detachKeys string) error
ContainerStop(ctx context.Context, name, timeout string) error
ContainerRemove(ctx context.Context, name string, force bool) error
ContainerRemove(ctx context.Context, name string, options *types.ContainerRemoveOptions) error
ContainerList(ctx context.Context, all bool) ([]*types.Container, error)
ContainerAttach(ctx context.Context, name string, stdin bool) (net.Conn, *bufio.Reader, error)
ContainerCreateExec(ctx context.Context, name string, config *types.ExecCreateConfig) (*types.ExecCreateResp, error)
Expand Down
Loading