Skip to content

Commit

Permalink
Merge pull request #987 from allencloud/update-daemon-api
Browse files Browse the repository at this point in the history
feature: add daemon update API
  • Loading branch information
allencloud authored Mar 29, 2018
2 parents 1d7d1ce + 475cd57 commit 8ac4b7c
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apis/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func initRoute(s *Server) http.Handler {
r.Path(versionMatcher + "/version").Methods(http.MethodGet).Handler(s.filter(s.version))
r.Path(versionMatcher + "/auth").Methods(http.MethodPost).Handler(s.filter(s.auth))

// daemon, we still list this API into system manager.
r.Path(versionMatcher + "/daemon/update").Methods(http.MethodPost).Handler(s.filter(s.updateDaemon))

// container
r.Path(versionMatcher + "/containers/create").Methods(http.MethodPost).Handler(s.filter(s.createContainer))
r.Path(versionMatcher + "/containers/{name:.*}/start").Methods(http.MethodPost).Handler(s.filter(s.startContainer))
Expand Down
11 changes: 11 additions & 0 deletions apis/server/system_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ func (s *Server) version(ctx context.Context, rw http.ResponseWriter, req *http.
return EncodeResponse(rw, http.StatusOK, version)
}

func (s *Server) updateDaemon(ctx context.Context, rw http.ResponseWriter, req *http.Request) (err error) {
cfg := &types.DaemonUpdateConfig{}
if err := json.NewDecoder(req.Body).Decode(cfg); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

// TODO: validate cfg in details

return s.SystemMgr.UpdateDaemon(cfg)
}

func (s *Server) auth(ctx context.Context, rw http.ResponseWriter, req *http.Request) (err error) {
auth := types.AuthConfig{}
if err := json.NewDecoder(req.Body).Decode(&auth); err != nil {
Expand Down
36 changes: 36 additions & 0 deletions apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ paths:
description: "Authentication to check"
schema:
$ref: "#/definitions/AuthConfig"

/daemon/update:
post:
summary: "Update daemon's labels and image proxy"
consumes:
- "application/json"
produces:
- "application/json"
responses:
200:
description: "no error"
400:
description: "bad parameter"
schema:
$ref: '#/definitions/Error'
500:
$ref: "#/responses/500ErrorResponse"
parameters:
- name: "DaemonUpdateConfig"
in: body
description: "Config used to update daemon, only labels and image proxy are allowed."
schema:
$ref: "#/definitions/DaemonUpdateConfig"

/images/create:
post:
Expand Down Expand Up @@ -1325,6 +1348,19 @@ definitions:
example:
- ["unix:///var/run/pouchd.sock", "tcp://0.0.0.0:4243"]

DaemonUpdateConfig:
type: "object"
properties:
Labels:
description: "Labels indentified the attributes of daemon"
type: "array"
items:
type: "string"
example: ["storage=ssd", "zone=hangzhou"]
ImageProxy:
description: "Image proxy used to pull image."
type: "string"

RegistryServiceConfig:
description: |
RegistryServiceConfig stores daemon registry services configuration.
Expand Down
71 changes: 71 additions & 0 deletions apis/types/daemon_update_config.go

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

3 changes: 3 additions & 0 deletions daemon/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"strings"
"sync"

"github.com/alibaba/pouch/client"
"github.com/alibaba/pouch/cri"
Expand All @@ -13,6 +14,8 @@ import (

// Config refers to daemon's whole configurations.
type Config struct {
sync.Mutex

//Volume config
VolumeConfig volume.Config

Expand Down
40 changes: 40 additions & 0 deletions daemon/mgr/system.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package mgr

import (
"fmt"
"runtime"
"strings"
"sync/atomic"

"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/daemon/config"
"github.com/alibaba/pouch/pkg/errtypes"
"github.com/alibaba/pouch/pkg/kernel"
"github.com/alibaba/pouch/pkg/meta"
"github.com/alibaba/pouch/registry"
"github.com/alibaba/pouch/version"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

Expand All @@ -19,6 +23,7 @@ type SystemMgr interface {
Info() (types.SystemInfo, error)
Version() (types.SystemVersion, error)
Auth(*types.AuthConfig) (string, error)
UpdateDaemon(*types.DaemonUpdateConfig) error
}

// SystemManager is an instance of system management.
Expand Down Expand Up @@ -131,3 +136,38 @@ func (mgr *SystemManager) Version() (types.SystemVersion, error) {
func (mgr *SystemManager) Auth(auth *types.AuthConfig) (string, error) {
return mgr.registry.Auth(auth)
}

// UpdateDaemon updates config of daemon, only label and image proxy are allowed.
func (mgr *SystemManager) UpdateDaemon(cfg *types.DaemonUpdateConfig) error {
if cfg == nil || (len(cfg.Labels) == 0 && cfg.ImageProxy == "") {
return errors.Wrap(errtypes.ErrInvalidParam, fmt.Sprintf("daemon update config cannot be empty"))
}

daemonCfg := mgr.config

daemonCfg.Lock()

daemonCfg.ImageProxy = cfg.ImageProxy

length := len(daemonCfg.Labels)
for _, newLabel := range cfg.Labels {
appearedKey := false
newLabelSlice := strings.SplitN(newLabel, "=", 2)
for i := 0; i < length; i++ {
oldLabelSlice := strings.SplitN(daemonCfg.Labels[i], "=", 2)
if newLabelSlice[0] == oldLabelSlice[0] {
// newLabel's key already appears in daemon's origin labels
daemonCfg.Labels[i] = newLabel
appearedKey = true
continue
}
}
if !appearedKey {
daemonCfg.Labels = append(daemonCfg.Labels, newLabel)
}
}

daemonCfg.Unlock()

return nil
}
48 changes: 48 additions & 0 deletions test/api_daemon_update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/test/environment"
"github.com/alibaba/pouch/test/request"

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

// APIDaemonUpdateSuite is the test suite for daemon update API.
type APIDaemonUpdateSuite struct{}

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

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

// TestStartOk tests starting container could work.
func (suite *APIDaemonUpdateSuite) TestUpdateDaemon(c *check.C) {
labels := []string{
"storage=ssd",
"zone=shanghai",
}

obj := map[string]interface{}{
"Labels": labels,
"ImageProxy": "http://192.168.0.3:2378",
}

body := request.WithJSONBody(obj)
resp, err := request.Post("/daemon/update", body)
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 200)

resp, err = request.Get("/info", body)

info := types.SystemInfo{}
err = request.DecodeBody(&info, resp.Body)
c.Assert(err, check.IsNil)

c.Assert(info.Labels, check.DeepEquals, labels)
// TODO: add checking image proxy
}

0 comments on commit 8ac4b7c

Please sign in to comment.