Skip to content

Commit

Permalink
refactor: re-design upgrade feature
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Wan <zirenwan@gmail.com>
  • Loading branch information
HusterWan committed Sep 7, 2018
1 parent 7d8d6fc commit 21c3b71
Show file tree
Hide file tree
Showing 14 changed files with 486 additions and 306 deletions.
31 changes: 22 additions & 9 deletions apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2454,15 +2454,28 @@ definitions:

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"
ContainerUpgradeConfig is used for API "POST /containers/upgrade". when upgrade a container,
we must specify new image used to create a new container, and also can specify `Cmd` and `Entrypoint` for
new container. There is all parameters that upgrade a container, if want to change other parameters, i
think you should use `update` API interface.
required: [Image]
properties:
Image:
type: "string"
x-nullable: false
Cmd:
type: "array"
description: "Execution commands and args"
minItems: 1
items:
type: "string"
Entrypoint:
description: |
The entrypoint for the container as a string or an array of strings.
If the array consists of exactly one empty string (`[""]`) then the entry point is reset to system default.
type: "array"
items:
type: "string"

LogConfig:
description: "The logging configuration for this container"
Expand Down
97 changes: 56 additions & 41 deletions apis/types/container_upgrade_config.go

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

8 changes: 4 additions & 4 deletions apis/types/registry_service_config.go

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

39 changes: 24 additions & 15 deletions cli/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package main
import (
"context"
"fmt"
"strings"

"github.com/alibaba/pouch/apis/types"

"github.com/spf13/cobra"
)
Expand All @@ -13,7 +16,8 @@ var upgradeDescription = ""
// UpgradeCommand use to implement 'upgrade' command, it is used to upgrade a container.
type UpgradeCommand struct {
baseCommand
*container
entrypoint string
name string
}

// Init initialize upgrade command.
Expand All @@ -36,21 +40,26 @@ func (ug *UpgradeCommand) Init(c *Cli) {
func (ug *UpgradeCommand) addFlags() {
flagSet := ug.cmd.Flags()
flagSet.SetInterspersed(false)

c := addCommonFlags(flagSet)
ug.container = c
flagSet.StringVar(&ug.entrypoint, "entrypoint", "", "Overwrite the default ENTRYPOINT of the image")
flagSet.StringVar(&ug.name, "name", "", "Specify name of container")
}

// 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)
}
var (
image string
cmd []string
)

config.Image = args[0]
image = args[0]
if len(args) > 1 {
config.Cmd = args[1:]
cmd = args[1:]
}

upgradeConfig := &types.ContainerUpgradeConfig{
Image: image,
Cmd: cmd,
Entrypoint: strings.Fields(ug.entrypoint),
}

containerName := ug.name
Expand All @@ -61,16 +70,16 @@ func (ug *UpgradeCommand) runUpgrade(args []string) error {
ctx := context.Background()
apiClient := ug.cli.Client()

if err := pullMissingImage(ctx, apiClient, config.Image, false); err != nil {
if err := pullMissingImage(ctx, apiClient, image, false); err != nil {
return err
}

err = apiClient.ContainerUpgrade(ctx, containerName, config.ContainerConfig, config.HostConfig)
if err == nil {
fmt.Println(containerName)
if err := apiClient.ContainerUpgrade(ctx, containerName, upgradeConfig); err != nil {
return err
}

return err
fmt.Println(containerName)
return nil
}

//upgradeExample shows examples in exec command, and is used in auto-generated cli docs.
Expand Down
8 changes: 2 additions & 6 deletions client/container_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ import (
)

// 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 {
upgradeConfig := types.ContainerUpgradeConfig{
ContainerConfig: config,
HostConfig: hostConfig,
}
resp, err := client.post(ctx, "/containers/"+name+"/upgrade", url.Values{}, upgradeConfig, nil)
func (client *APIClient) ContainerUpgrade(ctx context.Context, name string, config *types.ContainerUpgradeConfig) error {
resp, err := client.post(ctx, "/containers/"+name+"/upgrade", url.Values{}, config, nil)
ensureCloseReader(resp)

return err
Expand Down
2 changes: 1 addition & 1 deletion client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,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
ContainerUpgrade(ctx context.Context, name string, config *types.ContainerUpgradeConfig) 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, height, width string) error
Expand Down
4 changes: 2 additions & 2 deletions ctrd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,10 @@ func (c *Client) createContainer(ctx context.Context, ref, id, checkpointDir str
rootFSPath = container.BaseFS
} else { // containers created by pouch must first create snapshot
// check snapshot exist or not.
if _, err := c.GetSnapshot(ctx, id); err != nil {
if _, err := c.GetSnapshot(ctx, container.SnapshotKey); err != nil {
return errors.Wrapf(err, "failed to create container %s", id)
}
options = append(options, containerd.WithSnapshot(id))
options = append(options, containerd.WithSnapshot(container.SnapshotKey))
}

// specify Spec for new container
Expand Down
13 changes: 7 additions & 6 deletions ctrd/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
// then create container by specifying the snapshot;
// The other is create container by specify container rootfs, we use `RootFSProvided` flag to mark it,
type Container struct {
ID string
Image string
Runtime string
Labels map[string]string
IO *containerio.IO
Spec *specs.Spec
ID string
Image string
Runtime string
Labels map[string]string
IO *containerio.IO
Spec *specs.Spec
SnapshotKey string

// BaseFS is rootfs used by containerd container
BaseFS string
Expand Down
Loading

0 comments on commit 21c3b71

Please sign in to comment.