Skip to content

Commit

Permalink
Merge pull request #879 from HusterWan/zr/add-resize-interface
Browse files Browse the repository at this point in the history
feature: add resize interface
  • Loading branch information
allencloud authored Mar 16, 2018
2 parents 0a3508e + b9fb5eb commit b97bd2b
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 0 deletions.
20 changes: 20 additions & 0 deletions apis/server/container_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,5 +354,25 @@ func (s *Server) logsContainer(ctx context.Context, rw http.ResponseWriter, req

// TODO
return nil
}

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 {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}
// validate request body
if err := opts.Validate(strfmt.NewFormats()); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

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

if err := s.ContainerMgr.Resize(ctx, name, opts); err != nil {
return err
}

rw.WriteHeader(http.StatusOK)
return nil
}
1 change: 1 addition & 0 deletions apis/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func initRoute(s *Server) http.Handler {
r.Path(versionMatcher + "/containers/{name:.*}/upgrade").Methods(http.MethodPost).Handler(s.filter(s.upgradeContainer))
r.Path(versionMatcher + "/containers/{name:.*}/top").Methods(http.MethodGet).Handler(s.filter(s.topContainer))
r.Path(versionMatcher + "/containers/{name:.*}/logs").Methods(http.MethodGet).Handler(s.filter(s.logsContainer))
r.Path(versionMatcher + "/containers/{name:.*}/resize").Methods(http.MethodPost).Handler(s.filter(s.resizeContainer))

// image
r.Path(versionMatcher + "/images/create").Methods(http.MethodPost).Handler(s.filter(s.pullImage))
Expand Down
32 changes: 32 additions & 0 deletions apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,29 @@ paths:
default: "all"
tags: ["Container"]

/containers/{id}/resize:
post:
summary: "changes the size of the tty for a container"
operationId: "ContainerResize"
parameters:
- $ref: "#/parameters/id"
- name: "height"
in: "query"
description: "height of the tty"
type: "string"
- name: "width"
in: "query"
description: "width of the tty"
type: "string"
responses:
200:
description: "no error"
400:
description: "bad parameter"
schema:
$ref: "#/definitions/Error"
tags: ["Container"]

/exec/{id}/start:
post:
summary: "Start an exec instance"
Expand Down Expand Up @@ -3034,6 +3057,15 @@ definitions:
additionalProperties:
type: "string"

ResizeOptions:
description: "options of resizing container tty size"
type: "object"
properties:
Height:
type: "integer"
Width:
type: "integer"

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

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

14 changes: 14 additions & 0 deletions client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net"
"net/url"
"strconv"
"strings"

"github.com/alibaba/pouch/apis/types"
Expand Down Expand Up @@ -192,6 +193,7 @@ func (client *APIClient) ContainerUpdate(ctx context.Context, name string, confi

// ContainerUpgrade upgrade a container with new image and args.
func (client *APIClient) ContainerUpgrade(ctx context.Context, name string, config types.ContainerConfig, hostConfig *types.HostConfig) error {
// TODO
return nil
}

Expand Down Expand Up @@ -252,3 +254,15 @@ func (client *APIClient) ContainerLogs(ctx context.Context, name string, options
ensureCloseReader(resp)
return resp.Body, nil
}

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

resp, err := client.post(ctx, "/containers/"+name+"/resize", query, nil, nil)
ensureCloseReader(resp)

return err
}
1 change: 1 addition & 0 deletions client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +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
}

// ImageAPIClient defines methods of Image client.
Expand Down
9 changes: 9 additions & 0 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ type ContainerMgr interface {

// Top lists the processes running inside of the given container
Top(ctx context.Context, name string, psArgs string) (*types.ContainerProcessList, error)

// Resize resizes the size of container tty.
Resize(ctx context.Context, name string, opts types.ResizeOptions) error
}

// ContainerManager is the default implement of interface ContainerMgr.
Expand Down Expand Up @@ -842,6 +845,12 @@ func (mgr *ContainerManager) Top(ctx context.Context, name string, psArgs string
return procList, nil
}

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

func (mgr *ContainerManager) openContainerIO(id string, attach *AttachConfig) (*containerio.IO, error) {
return mgr.openIO(id, attach, false)
}
Expand Down
22 changes: 22 additions & 0 deletions test/api_container_resize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"github.com/alibaba/pouch/test/environment"

"github.com/go-check/check"
)

// APIContainerResizeSuite is the test suite for container upgrade API.
type APIContainerResizeSuite struct{}

func init() {
check.Suite(&APIContainerResizeSuite{})
}

// SetUpTest does common setup in the beginning of each test.
func (suite *APIContainerResizeSuite) SetUpTest(c *check.C) {
SkipIfFalse(c, environment.IsLinux)

}

// TODO add test case.

0 comments on commit b97bd2b

Please sign in to comment.