Skip to content

Commit

Permalink
feature: add upgrade interface
Browse files Browse the repository at this point in the history
Signed-off-by: HusterWan <[email protected]>
  • Loading branch information
HusterWan committed Mar 9, 2018
1 parent 190c434 commit f6faefd
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 0 deletions.
21 changes: 21 additions & 0 deletions apis/server/container_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,24 @@ func (s *Server) updateContainer(ctx context.Context, rw http.ResponseWriter, re
rw.WriteHeader(http.StatusOK)
return nil
}

func (s *Server) upgradeContainer(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
config := &types.ContainerUpgradeConfig{}
// decode request body
if err := json.NewDecoder(req.Body).Decode(config); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}
// validate request body
if err := config.Validate(strfmt.NewFormats()); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

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

if err := s.ContainerMgr.Upgrade(ctx, name, config); 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 @@ -39,6 +39,7 @@ func initRoute(s *Server) http.Handler {
r.Path("/containers/{name:.*}/pause").Methods(http.MethodPost).Handler(s.filter(s.pauseContainer))
r.Path("/containers/{name:.*}/unpause").Methods(http.MethodPost).Handler(s.filter(s.unpauseContainer))
r.Path("/containers/{name:.*}/update").Methods(http.MethodPost).Handler(s.filter(s.updateContainer))
r.Path("/containers/{name:.*}/upgrade").Methods(http.MethodPost).Handler(s.filter(s.upgradeContainer))

// image
r.Path("/images/create").Methods(http.MethodPost).Handler(s.filter(s.pullImage))
Expand Down
34 changes: 34 additions & 0 deletions apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,28 @@ paths:
500:
$ref: "#/responses/500ErrorResponse"
tags: ["Container"]
/containers/{id}/upgrade:
post:
summary: "Upgrade a container with new image and args"
operationId: "ContainerUpgrade"
parameters:
- $ref: "#/parameters/id"
- name: "upgradeConfig"
in: "body"
schema:
$ref: "#/definitions/ContainerUpgradeConfig"
responses:
200:
description: "no error"
400:
description: "bad parameter"
schema:
$ref: "#/definitions/Error"
404:
$ref: "#/responses/404ErrorResponse"
500:
$ref: "#/responses/500ErrorResponse"
tags: ["Container"]

/volumes:
get:
Expand Down Expand Up @@ -1365,6 +1387,18 @@ definitions:
additionalProperties:
type: "string"

ContainerUpgradeConfig:
description: |
ContainerUpgradeConfig is used for API "POST /containers/upgrade".
It wraps all kinds of config used in container upgrade.
It can be used to encode client params in client and unmarshal request body in daemon side.
allOf:
- $ref: "#/definitions/ContainerConfig"
- type: "object"
properties:
HostConfig:
$ref: "#/definitions/HostConfig"

Resources:
description: "A container's resources (cgroups config, ulimits, etc)"
type: "object"
Expand Down
104 changes: 104 additions & 0 deletions apis/types/container_upgrade_config.go

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

1 change: 1 addition & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func main() {
cli.AddCommand(base, &LoginCommand{})
cli.AddCommand(base, &UpdateCommand{})
cli.AddCommand(base, &LogoutCommand{})
cli.AddCommand(base, &UpgradeCommand{})

// add generate doc command
cli.AddCommand(base, &GenDocCommand{})
Expand Down
70 changes: 70 additions & 0 deletions cli/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"context"
"fmt"

"github.com/spf13/cobra"
)

// upgradeDescription is used to describe upgrade command in detail and auto generate command doc.
var upgradeDescription = ""

// UpgradeCommand use to implement 'upgrade' command, it is used to upgrade a container.
type UpgradeCommand struct {
baseCommand
*container
}

// Init initialize upgrade command.
func (ug *UpgradeCommand) Init(c *Cli) {
ug.cli = c
ug.cmd = &cobra.Command{
Use: "upgrade [OPTIONS] IMAGE [COMMAND] [ARG...]",
Short: "Upgrade a container with new image and args",
Long: upgradeDescription,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return ug.runUpgrade(args)
},
Example: upgradeExample(),
}
ug.addFlags()
}

// addFlags adds flags for specific command.
func (ug *UpgradeCommand) addFlags() {
flagSet := ug.cmd.Flags()
flagSet.SetInterspersed(false)

c := addCommonFlags(flagSet)
ug.container = c
}

// runUpgrade is the entry of UpgradeCommand command.
func (ug *UpgradeCommand) runUpgrade(args []string) error {
config, err := ug.config()
if err != nil {
return fmt.Errorf("failed to upgrade container: %v", err)
}

config.Image = args[0]
if len(args) > 1 {
config.Cmd = args[1:]
}
containerName := ug.name
if containerName == "" {
return fmt.Errorf("failed to upgrade container: must specify container name")
}

ctx := context.Background()
apiClient := ug.cli.Client()

// TODO if error is image not found, we can pull image, and retry upgrade
return apiClient.ContainerUpgrade(ctx, containerName, config.ContainerConfig, config.HostConfig)
}

//upgradeExample shows examples in exec command, and is used in auto-generated cli docs.
func upgradeExample() string {
return ""
}
5 changes: 5 additions & 0 deletions client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,8 @@ func (client *APIClient) ContainerUpdate(ctx context.Context, name string, confi
return err

}

// 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 {
return nil
}
1 change: 1 addition & 0 deletions client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type ContainerAPIClient interface {
ContainerPause(ctx context.Context, name string) error
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
}

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

// Update updates the configurations of a container.
Update(ctx context.Context, name string, config *types.UpdateConfig) error

// Upgrade upgrades a container with new image and args.
Upgrade(ctx context.Context, name string, config *types.ContainerUpgradeConfig) error
}

// ContainerManager is the default implement of interface ContainerMgr.
Expand Down Expand Up @@ -780,6 +783,11 @@ func (mgr *ContainerManager) Update(ctx context.Context, name string, config *ty
return nil
}

func (mgr *ContainerManager) Upgrade(ctx context.Context, name string, config *types.ContainerUpgradeConfig) error {
// TODO
return nil
}

func (mgr *ContainerManager) openContainerIO(id string, attach *AttachConfig) (*containerio.IO, error) {
return mgr.openIO(id, attach, false)
}
Expand Down

0 comments on commit f6faefd

Please sign in to comment.