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: finish resize interface #931

Merged
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
15 changes: 10 additions & 5 deletions apis/server/container_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,16 +367,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 @@ -867,8 +867,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)
}