Skip to content

Commit

Permalink
support get engine by node, only show which engine type (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS authored Mar 7, 2022
1 parent 339e5f0 commit 36d1c03
Show file tree
Hide file tree
Showing 15 changed files with 1,943 additions and 1,738 deletions.
16 changes: 16 additions & 0 deletions cluster/calcium/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/projecteru2/core/types"
"github.com/projecteru2/core/utils"

enginetypes "github.com/projecteru2/core/engine/types"

"github.com/pkg/errors"
)

Expand Down Expand Up @@ -84,6 +86,20 @@ func (c *Calcium) GetNode(ctx context.Context, nodename string) (*types.Node, er
return node, logger.Err(ctx, errors.WithStack(err))
}

// GetNodeEngine get node engine
func (c *Calcium) GetNodeEngine(ctx context.Context, nodename string) (*enginetypes.Info, error) {
logger := log.WithField("Calcium", "GetNodeEngine").WithField("nodename", nodename)
if nodename == "" {
return nil, logger.Err(ctx, errors.WithStack(types.ErrEmptyNodeName))
}
node, err := c.store.GetNode(ctx, nodename)
if err != nil {
return nil, logger.Err(ctx, errors.WithStack(err))
}
engineInfo, err := node.Engine.Info(ctx)
return engineInfo, logger.Err(ctx, errors.WithStack(err))
}

// SetNode set node available or not
func (c *Calcium) SetNode(ctx context.Context, opts *types.SetNodeOptions) (*types.Node, error) {
logger := log.WithField("Calcium", "SetNode").WithField("opts", opts)
Expand Down
27 changes: 27 additions & 0 deletions cluster/calcium/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"testing"

enginemocks "github.com/projecteru2/core/engine/mocks"
enginetypes "github.com/projecteru2/core/engine/types"
lockmocks "github.com/projecteru2/core/lock/mocks"
storemocks "github.com/projecteru2/core/store/mocks"
"github.com/projecteru2/core/types"
Expand Down Expand Up @@ -135,6 +137,31 @@ func TestGetNode(t *testing.T) {
assert.Equal(t, n.Name, name)
}

func TestGetNodeEngine(t *testing.T) {
c := NewTestCluster()
ctx := context.Background()

// fail by validating
_, err := c.GetNodeEngine(ctx, "")
assert.Error(t, err)

engine := &enginemocks.API{}
engine.On("Info", mock.Anything).Return(&enginetypes.Info{Type: "fake"}, nil)
name := "test"
node := &types.Node{
NodeMeta: types.NodeMeta{Name: name},
Engine: engine,
}

store := &storemocks.Store{}
store.On("GetNode", mock.Anything, mock.Anything).Return(node, nil)
c.store = store

e, err := c.GetNodeEngine(ctx, name)
assert.NoError(t, err)
assert.Equal(t, e.Type, "fake")
}

func TestSetNode(t *testing.T) {
c := NewTestCluster()
ctx := context.Background()
Expand Down
1 change: 1 addition & 0 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type Cluster interface {
RemoveNode(ctx context.Context, nodename string) error
ListPodNodes(context.Context, *types.ListNodesOptions) (<-chan *types.Node, error)
GetNode(ctx context.Context, nodename string) (*types.Node, error)
GetNodeEngine(ctx context.Context, nodename string) (*enginetypes.Info, error)
SetNode(ctx context.Context, opts *types.SetNodeOptions) (*types.Node, error)
SetNodeStatus(ctx context.Context, nodename string, ttl int64) error
GetNodeStatus(ctx context.Context, nodename string) (*types.NodeStatus, error)
Expand Down
25 changes: 24 additions & 1 deletion cluster/mocks/Cluster.go

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

4 changes: 3 additions & 1 deletion engine/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const (
TCPPrefixKey = "tcp://"
// SockPrefixKey indicate sock prefix
SockPrefixKey = "unix://"
// Type indicate docker
Type = "docker"
)

// Engine is engine for docker
Expand Down Expand Up @@ -80,7 +82,7 @@ func (e *Engine) Info(ctx context.Context) (*enginetypes.Info, error) {
if err != nil {
return nil, err
}
return &enginetypes.Info{ID: r.ID, NCPU: r.NCPU, MemTotal: r.MemTotal}, nil
return &enginetypes.Info{Type: Type, ID: r.ID, NCPU: r.NCPU, MemTotal: r.MemTotal}, nil
}

// ResourceValidate validate resource usage
Expand Down
1 change: 1 addition & 0 deletions engine/types/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

// Info define info response
type Info struct {
Type string
ID string
NCPU int
MemTotal int64
Expand Down
3 changes: 3 additions & 0 deletions engine/virt/virt.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const (
DmiUUIDKey = "DMIUUID"
// ImageUserKey indicates the image's owner
ImageUserKey = "ImageUser"
// Type indicate type
Type = "virt"

ttyFlag = "tty"
)
Expand Down Expand Up @@ -69,6 +71,7 @@ func (v *Virt) Info(ctx context.Context) (*enginetypes.Info, error) {
}

return &enginetypes.Info{
Type: Type,
ID: resp.ID,
NCPU: resp.CPU,
MemTotal: resp.Mem,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/alexcesaro/statsd v2.0.0+incompatible // indirect
github.com/alicebob/miniredis/v2 v2.14.3
github.com/cenkalti/backoff/v4 v4.0.2
github.com/containerd/containerd v1.4.12 // indirect
github.com/containerd/containerd v1.4.13 // indirect
github.com/containerd/continuity v0.0.0-20200710164510-efbc4488d8fe // indirect
github.com/docker/distribution v2.8.0+incompatible
github.com/docker/docker v20.10.0+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ github.com/containerd/cgroups v0.0.0-20200531161412-0dbf7f05ba59/go.mod h1:pA0z1
github.com/containerd/console v0.0.0-20180822173158-c12b1e7919c1/go.mod h1:Tj/on1eG8kiEhd0+fhSDzsPAFESxzBBvdyEgyryXffw=
github.com/containerd/console v1.0.2/go.mod h1:ytZPjGgY2oeTkAONYafi2kSj0aYggsf8acV1PGKCbzQ=
github.com/containerd/containerd v1.3.2/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA=
github.com/containerd/containerd v1.4.12 h1:V+SHzYmhng/iju6M5nFrpTTusrhidoxKTwdwLw+u4c4=
github.com/containerd/containerd v1.4.12/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA=
github.com/containerd/containerd v1.4.13 h1:Z0CbagVdn9VN4K6htOCY/jApSw8YKP+RdLZ5dkXF8PM=
github.com/containerd/containerd v1.4.13/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA=
github.com/containerd/continuity v0.0.0-20190426062206-aaeac12a7ffc/go.mod h1:GL3xCUCBDV3CZiTSEKksMWbLE66hEyuu9qyDOOqM47Y=
github.com/containerd/continuity v0.0.0-20200710164510-efbc4488d8fe h1:PEmIrUvwG9Yyv+0WKZqjXfSFDeZjs/q15g0m08BYS9k=
github.com/containerd/continuity v0.0.0-20200710164510-efbc4488d8fe/go.mod h1:cECdGN1O8G9bgKTlLhuPJimka6Xb/Gg7vYzCTNVxhvo=
Expand Down
2 changes: 2 additions & 0 deletions rpc/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const (
GetNodeStatus codes.Code = 1038
// GetNodeResource .
GetNodeResource codes.Code = 1037
// GetNodeEngine .
GetNodeEngine codes.Code = 1038

// CalculateCapacity .
CalculateCapacity codes.Code = 1041
Expand Down
Loading

0 comments on commit 36d1c03

Please sign in to comment.