Skip to content

Commit

Permalink
support annotation in container update api
Browse files Browse the repository at this point in the history
Signed-off-by: allen.wang <allen.wq@alipay.com>
  • Loading branch information
wangforthinker committed Feb 25, 2019
1 parent fc95f8f commit 9f96857
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 5 deletions.
6 changes: 6 additions & 0 deletions apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2520,6 +2520,12 @@ definitions:
x-nullable: true
additionalProperties:
type: "string"
SpecAnnotation:
type: "object"
description: "update specAnnotation for container"
x-nullable: true
additionalProperties:
type: "string"

ContainerUpgradeConfig:
description: |
Expand Down
11 changes: 11 additions & 0 deletions apis/types/update_config.go

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

17 changes: 12 additions & 5 deletions cli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (uc *UpdateCommand) addFlags() {
flagSet.StringSliceVarP(&uc.labels, "label", "l", nil, "Set label for container")
flagSet.StringVar(&uc.restartPolicy, "restart", "", "Restart policy to apply when container exits")
flagSet.StringSliceVar(&uc.diskQuota, "disk-quota", nil, "Update disk quota for container(/=10g)")
flagSet.StringSliceVar(&uc.specAnnotation, "annotation", nil, "Update annotation for runtime spec")
}

// updateRun is the entry of update command.
Expand Down Expand Up @@ -97,12 +98,18 @@ func (uc *UpdateCommand) updateRun(args []string) error {
return err
}

annotation, err := opts.ParseAnnotation(uc.specAnnotation)
if err != nil {
return err
}

updateConfig := &types.UpdateConfig{
Env: uc.env,
Label: uc.labels,
RestartPolicy: restartPolicy,
Resources: resource,
DiskQuota: diskQuota,
Env: uc.env,
Label: uc.labels,
RestartPolicy: restartPolicy,
Resources: resource,
DiskQuota: diskQuota,
SpecAnnotation: annotation,
}

apiClient := uc.cli.Client()
Expand Down
6 changes: 6 additions & 0 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,12 @@ func (mgr *ContainerManager) Update(ctx context.Context, name string, config *ty
}
}

c.Lock()
if len(config.SpecAnnotation) > 0 {
c.Config.SpecAnnotation = mergeAnnotation(config.SpecAnnotation, c.Config.SpecAnnotation)
}
c.Unlock()

if mgr.containerPlugin != nil && len(config.Env) > 0 {
if err = mgr.containerPlugin.PostUpdate(c.BaseFS, c.Config.Env); err != nil {
return err
Expand Down
16 changes: 16 additions & 0 deletions daemon/mgr/container_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,19 @@ func mergeEnvSlice(newEnv, oldEnv []string) ([]string, error) {

return newEnvSlice, nil
}

func mergeAnnotation(newAnnotation, oldAnnotation map[string]string) map[string]string {
if len(newAnnotation) == 0 {
return oldAnnotation
}

if len(oldAnnotation) == 0 {
oldAnnotation = make(map[string]string)
}

for k, v := range newAnnotation {
oldAnnotation[k] = v
}

return oldAnnotation
}
37 changes: 37 additions & 0 deletions test/cli_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,40 @@ func (suite *PouchUpdateSuite) TestUpdateBlkIOLimit(c *check.C) {
out := res.Stdout()
c.Assert(out, check.Equals, Expected)
}

func checkContainerAnnotation(c *check.C, cName string, annotationKey string, expect string) {
output := command.PouchRun("inspect", cName).Stdout()
result := []types.ContainerJSON{}
if err := json.Unmarshal([]byte(output), &result); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}

annotations := result[0].Config.SpecAnnotation
v, found := annotations[annotationKey]
c.Assert(found, check.Equals, true)
c.Assert(v, check.Equals, expect)
}

// TestUpdateAnnotation is to verity the correctness of update the annotation
func (suite *PouchUpdateSuite) TestUpdateAnnotation(c *check.C) {
cname := "TestUpdateAnnotation1"
annotation1 := "key1=value1"
annotation2 := "key2=value2"
command.PouchRun("run", "-d", "--name", cname, "--annotation", annotation1, "--annotation", annotation2, busyboxImage, "top").Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, cname)

annotation1Update := "key1=value1.new"
annotation2Update := "key2=value2.new"

command.PouchRun("update", "--annotation", annotation1Update, cname).Assert(c, icmd.Success)
checkContainerAnnotation(c, cname, "key1", "value1.new")
checkContainerAnnotation(c, cname, "key2", "value2")

command.PouchRun("update", "--annotation", annotation2Update, cname).Assert(c, icmd.Success)
checkContainerAnnotation(c, cname, "key1", "value1.new")
checkContainerAnnotation(c, cname, "key2", "value2.new")

command.PouchRun("restart", cname).Assert(c, icmd.Success)
checkContainerAnnotation(c, cname, "key1", "value1.new")
checkContainerAnnotation(c, cname, "key2", "value2.new")
}

0 comments on commit 9f96857

Please sign in to comment.