Skip to content

Commit

Permalink
Merge pull request #931 from HusterWan/zr/finish-resize-interface
Browse files Browse the repository at this point in the history
feature: finish resize interface
  • Loading branch information
allencloud authored Mar 22, 2018
2 parents 3790c91 + 5de6fa8 commit 079a92d
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 13 deletions.
15 changes: 10 additions & 5 deletions apis/server/container_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,21 @@ func (s *Server) logsContainer(ctx context.Context, rw http.ResponseWriter, req
}

func (s *Server) resizeContainer(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
opts := types.ResizeOptions{}
// decode request body
if err := json.NewDecoder(req.Body).Decode(opts); err != nil {
height, err := strconv.Atoi(req.FormValue("h"))
if err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}
// validate request body
if err := opts.Validate(strfmt.NewFormats()); err != nil {

width, err := strconv.Atoi(req.FormValue("w"))
if err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

opts := types.ResizeOptions{
Height: int64(height),
Width: int64(width),
}

name := mux.Vars(req)["name"]

if err := s.ContainerMgr.Resize(ctx, name, opts); err != nil {
Expand Down
7 changes: 3 additions & 4 deletions client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"net"
"net/url"
"strconv"
"strings"

"github.com/alibaba/pouch/apis/types"
Expand Down Expand Up @@ -256,10 +255,10 @@ func (client *APIClient) ContainerLogs(ctx context.Context, name string, options
}

// ContainerResize resizes the size of container tty.
func (client *APIClient) ContainerResize(ctx context.Context, name string, opts types.ResizeOptions) error {
func (client *APIClient) ContainerResize(ctx context.Context, name, height, width string) error {
query := url.Values{}
query.Set("h", strconv.Itoa(int(opts.Height)))
query.Set("w", strconv.Itoa(int(opts.Width)))
query.Set("h", height)
query.Set("w", width)

resp, err := client.post(ctx, "/containers/"+name+"/resize", query, nil, nil)
ensureCloseReader(resp)
Expand Down
2 changes: 1 addition & 1 deletion client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ContainerAPIClient interface {
ContainerUpgrade(ctx context.Context, name string, config types.ContainerConfig, hostConfig *types.HostConfig) error
ContainerTop(ctx context.Context, name string, arguments []string) (types.ContainerProcessList, error)
ContainerLogs(ctx context.Context, name string, options types.ContainerLogsOptions) (io.ReadCloser, error)
ContainerResize(ctx context.Context, name string, options types.ResizeOptions) error
ContainerResize(ctx context.Context, name, height, width string) error
}

// ImageAPIClient defines methods of Image client.
Expand Down
16 changes: 16 additions & 0 deletions ctrd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,19 @@ func (c *Client) GetPidsForContainer(ctx context.Context, id string) ([]int, err
}
return pids, nil
}

// ResizeContainer changes the size of the TTY of the init process running
// in the container to the given height and width.
func (c *Client) ResizeContainer(ctx context.Context, id string, opts types.ResizeOptions) error {
if !c.lock.Trylock(id) {
return errtypes.ErrLockfailed
}
defer c.lock.Unlock(id)

pack, err := c.watch.get(id)
if err != nil {
return err
}

return pack.task.Resize(ctx, uint32(opts.Height), uint32(opts.Width))
}
15 changes: 13 additions & 2 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,8 +882,19 @@ func (mgr *ContainerManager) Top(ctx context.Context, name string, psArgs string

// Resize resizes the size of a container tty.
func (mgr *ContainerManager) Resize(ctx context.Context, name string, opts types.ResizeOptions) error {
// TODO
return nil
c, err := mgr.container(name)
if err != nil {
return err
}

c.Lock()
defer c.Unlock()

if !c.IsRunning() && !c.IsPaused() {
return fmt.Errorf("failed to resize container %s: container is not running", c.ID())
}

return mgr.Client.ResizeContainer(ctx, c.ID(), opts)
}

func (mgr *ContainerManager) openContainerIO(id string, attach *AttachConfig) (*containerio.IO, error) {
Expand Down
65 changes: 64 additions & 1 deletion test/api_container_resize.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package main

import (
"net/url"

"github.com/alibaba/pouch/test/environment"
"github.com/alibaba/pouch/test/request"

"github.com/go-check/check"
)
Expand All @@ -19,4 +22,64 @@ func (suite *APIContainerResizeSuite) SetUpTest(c *check.C) {

}

// TODO add test case.
// TestContainerResizeOk is to verify resize container.
func (suite *APIContainerResizeSuite) TestContainerResizeOk(c *check.C) {
cname := "TestContainerResizeOk"

CreateBusyboxContainerOk(c, cname)

resp, err := request.Post("/containers/" + cname + "/start")
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 204)

q := url.Values{}
q.Add("h", "10")
q.Add("w", "10")
query := request.WithQuery(q)

resp, err = request.Post("/containers/"+cname+"/resize", query)
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 200)

DelContainerForceOk(c, cname)
}

// TestContainerResizeWithInvalidSize is to verify resize container with invalid size.
func (suite *APIContainerResizeSuite) TestContainerResizeWithInvalidSize(c *check.C) {
cname := "TestContainerResizeWithInvalidSize"

CreateBusyboxContainerOk(c, cname)

resp, err := request.Post("/containers/" + cname + "/start")
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 204)

q := url.Values{}
q.Add("h", "hi")
q.Add("w", "wo")
query := request.WithQuery(q)

resp, err = request.Post("/containers/"+cname+"/resize", query)
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 400)

DelContainerForceOk(c, cname)
}

// TestResizeStoppedContainer is to verify resize a stopped container.
func (suite *APIContainerResizeSuite) TestResizeStoppedContainer(c *check.C) {
cname := "TestResizeStoppedContainer"

CreateBusyboxContainerOk(c, cname)

q := url.Values{}
q.Add("h", "10")
q.Add("w", "10")
query := request.WithQuery(q)

resp, err := request.Post("/containers/"+cname+"/resize", query)
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 500)

DelContainerForceOk(c, cname)
}

0 comments on commit 079a92d

Please sign in to comment.