From af05c15703cda92332ff3f97b313a90e67e83e09 Mon Sep 17 00:00:00 2001 From: Michael Wan Date: Mon, 12 Mar 2018 11:28:39 -0400 Subject: [PATCH] feature: add resize interface Signed-off-by: Michael Wan --- apis/server/container_bridge.go | 21 ++++++++++++ apis/server/router.go | 1 + apis/swagger.yml | 32 ++++++++++++++++++ apis/types/resize_options.go | 57 +++++++++++++++++++++++++++++++++ client/container.go | 14 ++++++++ client/interface.go | 1 + daemon/mgr/container.go | 9 ++++++ test/api_container_resize.go | 22 +++++++++++++ 8 files changed, 157 insertions(+) create mode 100644 apis/types/resize_options.go create mode 100644 test/api_container_resize.go diff --git a/apis/server/container_bridge.go b/apis/server/container_bridge.go index 915b47e2ef..56edb34090 100644 --- a/apis/server/container_bridge.go +++ b/apis/server/container_bridge.go @@ -332,3 +332,24 @@ func (s *Server) upgradeContainer(ctx context.Context, rw http.ResponseWriter, r rw.WriteHeader(http.StatusOK) 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 +} diff --git a/apis/server/router.go b/apis/server/router.go index 91ca9bd4f0..015254109c 100644 --- a/apis/server/router.go +++ b/apis/server/router.go @@ -43,6 +43,7 @@ func initRoute(s *Server) http.Handler { r.Path(versionMatcher + "/containers/{name:.*}/unpause").Methods(http.MethodPost).Handler(s.filter(s.unpauseContainer)) r.Path(versionMatcher + "/containers/{name:.*}/update").Methods(http.MethodPost).Handler(s.filter(s.updateContainer)) r.Path(versionMatcher + "/containers/{name:.*}/upgrade").Methods(http.MethodPost).Handler(s.filter(s.upgradeContainer)) + 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)) diff --git a/apis/swagger.yml b/apis/swagger.yml index de201ccd91..33ad60eaee 100644 --- a/apis/swagger.yml +++ b/apis/swagger.yml @@ -500,6 +500,29 @@ paths: required: true tags: ["Exec"] + /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" + 404: + $ref: "#/responses/404ErrorResponse" + 500: + $ref: "#/responses/500ErrorResponse" + tags: ["Container"] + /exec/{id}/start: post: summary: "Start an exec instance" @@ -2447,6 +2470,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 diff --git a/apis/types/resize_options.go b/apis/types/resize_options.go new file mode 100644 index 0000000000..369e3bb277 --- /dev/null +++ b/apis/types/resize_options.go @@ -0,0 +1,57 @@ +// Code generated by go-swagger; DO NOT EDIT. + +package types + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + strfmt "github.com/go-openapi/strfmt" + + "github.com/go-openapi/errors" + "github.com/go-openapi/swag" +) + +// ResizeOptions options of resizing container tty size +// swagger:model ResizeOptions + +type ResizeOptions struct { + + // height + Height int64 `json:"Height,omitempty"` + + // width + Width int64 `json:"Width,omitempty"` +} + +/* polymorph ResizeOptions Height false */ + +/* polymorph ResizeOptions Width false */ + +// Validate validates this resize options +func (m *ResizeOptions) Validate(formats strfmt.Registry) error { + var res []error + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +// MarshalBinary interface implementation +func (m *ResizeOptions) MarshalBinary() ([]byte, error) { + if m == nil { + return nil, nil + } + return swag.WriteJSON(m) +} + +// UnmarshalBinary interface implementation +func (m *ResizeOptions) UnmarshalBinary(b []byte) error { + var res ResizeOptions + if err := swag.ReadJSON(b, &res); err != nil { + return err + } + *m = res + return nil +} diff --git a/client/container.go b/client/container.go index a4820d2955..ea60e134bc 100644 --- a/client/container.go +++ b/client/container.go @@ -5,6 +5,7 @@ import ( "context" "net" "net/url" + "strconv" "github.com/alibaba/pouch/apis/types" ) @@ -184,5 +185,18 @@ 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 } + +// 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 +} diff --git a/client/interface.go b/client/interface.go index e8b1f2a39c..f9dafaefa3 100644 --- a/client/interface.go +++ b/client/interface.go @@ -34,6 +34,7 @@ type ContainerAPIClient interface { ContainerUnpause(ctx context.Context, name string) error ContainerUpdate(ctx context.Context, name string, config *types.UpdateConfig) error ContainerUpgrade(ctx context.Context, name string, config types.ContainerConfig, hostConfig *types.HostConfig) error + ContainerResize(ctx context.Context, name string, options types.ResizeOptions) error } // ImageAPIClient defines methods of Image client. diff --git a/daemon/mgr/container.go b/daemon/mgr/container.go index 0d55e8f516..74e1e22559 100644 --- a/daemon/mgr/container.go +++ b/daemon/mgr/container.go @@ -75,6 +75,9 @@ type ContainerMgr interface { // Upgrade upgrades a container with new image and args. Upgrade(ctx context.Context, name string, config *types.ContainerUpgradeConfig) 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. @@ -801,6 +804,12 @@ func (mgr *ContainerManager) Upgrade(ctx context.Context, name string, config *t return 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) } diff --git a/test/api_container_resize.go b/test/api_container_resize.go new file mode 100644 index 0000000000..2699f0593b --- /dev/null +++ b/test/api_container_resize.go @@ -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.