Skip to content

Commit

Permalink
refactor set-node api
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Jul 9, 2019
1 parent 276729c commit 8cfb874
Show file tree
Hide file tree
Showing 9 changed files with 469 additions and 451 deletions.
4 changes: 2 additions & 2 deletions cluster/calcium/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func (c *Calcium) ControlContainer(ctx context.Context, IDs []string, t string,
var message []*bytes.Buffer
defer func() {
if err == nil {
log.Infof("[ControlContainer] Control container %s %s", container.ID, t)
log.Info("[ControlContainer] Output:")
log.Infof("[ControlContainer] Container %s %s", container.ID, t)
log.Info("[ControlContainer] Hook Output:")
log.Info(string(types.HookOutput(message)))
}
ch <- &types.ControlContainerMessage{
Expand Down
36 changes: 24 additions & 12 deletions cluster/calcium/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ package calcium
import (
"context"

"github.com/sanity-io/litter"

"github.com/projecteru2/core/utils"

"github.com/projecteru2/core/cluster"
"github.com/projecteru2/core/types"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -96,8 +99,13 @@ func (c *Calcium) SetNode(ctx context.Context, opts *types.SetNodeOptions) (*typ
var n *types.Node
return n, c.withNodeLocked(ctx, opts.Podname, opts.Nodename, func(node *types.Node) error {
n = node
n.Available = n.Available || opts.Available
if !n.Available {
litter.Dump(opts)
// status
switch opts.Status {
case cluster.NodeUp:
n.Available = true
case cluster.NodeDown:
n.Available = false
containers, err := c.store.ListNodeContainers(ctx, opts.Nodename)
if err != nil {
return err
Expand Down Expand Up @@ -132,17 +140,21 @@ func (c *Calcium) SetNode(ctx context.Context, opts *types.SetNodeOptions) (*typ
}
}
}
// update storage
n.StorageCap += opts.DeltaStorage
n.InitStorageCap += opts.DeltaStorage
if n.StorageCap < 0 {
return types.ErrBadStorage
if opts.DeltaStorage != 0 {
// update storage
n.StorageCap += opts.DeltaStorage
n.InitStorageCap += opts.DeltaStorage
if n.StorageCap < 0 {
return types.ErrBadStorage
}
}
// update memory
n.MemCap += opts.DeltaMemory
n.InitMemCap += opts.DeltaMemory
if n.MemCap < 0 {
return types.ErrBadStorage
if opts.DeltaMemory != 0 {
// update memory
n.MemCap += opts.DeltaMemory
n.InitMemCap += opts.DeltaMemory
if n.MemCap < 0 {
return types.ErrBadStorage
}
}
// update cpu
for cpuID, cpuShare := range opts.DeltaCPU {
Expand Down
12 changes: 6 additions & 6 deletions cluster/calcium/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,30 +243,30 @@ func TestSetNode(t *testing.T) {
store.On("GetNode", mock.Anything, mock.Anything, mock.Anything).Return(node, nil)
// failed by updatenode
store.On("UpdateNode", mock.Anything, mock.Anything).Return(types.ErrCannotGetEngine).Once()
_, err = c.SetNode(ctx, &types.SetNodeOptions{Available: true})
_, err = c.SetNode(ctx, &types.SetNodeOptions{Status: 2})
assert.Error(t, err)
store.On("UpdateNode", mock.Anything, mock.Anything).Return(nil)
// succ when node available
n, err := c.SetNode(ctx, &types.SetNodeOptions{Available: true})
n, err := c.SetNode(ctx, &types.SetNodeOptions{Status: 2})
assert.NoError(t, err)
assert.Equal(t, n.Name, name)
// not available
// failed by list node containers
store.On("ListNodeContainers", mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD).Once()
_, err = c.SetNode(ctx, &types.SetNodeOptions{Available: false})
_, err = c.SetNode(ctx, &types.SetNodeOptions{Status: 0})
assert.Error(t, err)
containers := []*types.Container{&types.Container{Name: "wrong_name"}, &types.Container{Name: "a_b_c"}}
store.On("ListNodeContainers", mock.Anything, mock.Anything).Return(containers, nil)
store.On("ContainerDeployed",
mock.Anything, mock.Anything, mock.Anything,
mock.Anything, mock.Anything, mock.Anything, mock.Anything,
).Return(types.ErrNoETCD)
_, err = c.SetNode(ctx, &types.SetNodeOptions{Available: false})
_, err = c.SetNode(ctx, &types.SetNodeOptions{Status: 0})
assert.NoError(t, err)
// test modify
setOpts := &types.SetNodeOptions{
Available: true,
Labels: map[string]string{"some": "1"},
Status: 1,
Labels: map[string]string{"some": "1"},
}
// set label
n, err = c.SetNode(ctx, setOpts)
Expand Down
6 changes: 6 additions & 0 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ const (
ContainerLock = "clock_%s"
// NodeLock for lock node
NodeLock = "cnode_%s_%s"
// NodeUp for node up
NodeUp = 1
// NodeDown for node down
NodeDown = 0
// KeepNodeStatus for no change node status
KeepNodeStatus = 2
)

// Cluster define all interface
Expand Down
Loading

0 comments on commit 8cfb874

Please sign in to comment.