From 0ab6590a67aaa3adc57151367bf18b6d41480525 Mon Sep 17 00:00:00 2001 From: CMGS Date: Wed, 1 Feb 2023 11:37:26 +0800 Subject: [PATCH] move types.resources to resource --- Makefile | 1 + cluster/calcium/capacity_test.go | 3 +- cluster/calcium/create.go | 75 +++++++++++----------- cluster/calcium/create_test.go | 7 +- cluster/calcium/dissociate.go | 5 +- cluster/calcium/dissociate_test.go | 7 +- cluster/calcium/lambda_test.go | 15 +++-- cluster/calcium/node.go | 5 +- cluster/calcium/node_test.go | 5 +- cluster/calcium/realloc.go | 7 +- cluster/calcium/realloc_test.go | 13 ++-- cluster/calcium/remap_test.go | 9 +-- cluster/calcium/remove.go | 5 +- cluster/calcium/remove_test.go | 5 +- cluster/calcium/resource_test.go | 17 ++--- cluster/calcium/wal_test.go | 23 +++---- engine/mocks/fakeengine/mock.go | 6 +- engine/transform.go | 13 +++- engine/types/virtualization.go | 2 +- resource/cobalt/alloc.go | 14 ++-- resource/cobalt/node.go | 39 +++++------ resource/cobalt/realloc.go | 16 ++--- resource/cobalt/remap.go | 7 +- resource/manager.go | 21 +++--- resource/mocks/Manager.go | 26 ++++---- resource/plugins/cpumem/types/node.go | 7 +- resource/plugins/cpumem/types/workload.go | 6 +- resource/plugins/mocks/Plugin.go | 2 +- resource/plugins/types/engine.go | 6 +- resource/plugins/types/node.go | 6 +- resource/plugins/types/workload.go | 8 +-- {types => resource/types}/resource.go | 0 {types => resource/types}/resource_test.go | 0 rpc/transform.go | 7 +- types/message.go | 6 +- types/node.go | 11 ++-- types/options.go | 10 +-- types/options_test.go | 3 +- types/workload.go | 31 ++++----- 39 files changed, 244 insertions(+), 205 deletions(-) rename {types => resource/types}/resource.go (100%) rename {types => resource/types}/resource_test.go (100%) diff --git a/Makefile b/Makefile index 44811fc9a..60362bf9c 100644 --- a/Makefile +++ b/Makefile @@ -61,6 +61,7 @@ unit-test: ./lock/etcdlock/... \ ./auth/simple/... \ ./discovery/helium... \ + ./resource/types/. \ ./resource/plugins/cpumem/. \ ./resource/plugins/cpumem/schedule/. \ ./wal/. \ diff --git a/cluster/calcium/capacity_test.go b/cluster/calcium/capacity_test.go index d8af43649..a49537a98 100644 --- a/cluster/calcium/capacity_test.go +++ b/cluster/calcium/capacity_test.go @@ -8,6 +8,7 @@ import ( lockmocks "github.com/projecteru2/core/lock/mocks" resourcemocks "github.com/projecteru2/core/resource/mocks" plugintypes "github.com/projecteru2/core/resource/plugins/types" + resourcetypes "github.com/projecteru2/core/resource/types" storemocks "github.com/projecteru2/core/store/mocks" "github.com/projecteru2/core/strategy" "github.com/projecteru2/core/types" @@ -40,7 +41,7 @@ func TestCalculateCapacity(t *testing.T) { Entrypoint: &types.Entrypoint{ Name: "entry", }, - Resources: types.Resources{}, + Resources: resourcetypes.Resources{}, DeployStrategy: strategy.Auto, NodeFilter: &types.NodeFilter{ Includes: []string{name}, diff --git a/cluster/calcium/create.go b/cluster/calcium/create.go index 890fe55fc..bfed10a38 100644 --- a/cluster/calcium/create.go +++ b/cluster/calcium/create.go @@ -13,6 +13,7 @@ import ( enginetypes "github.com/projecteru2/core/engine/types" "github.com/projecteru2/core/log" "github.com/projecteru2/core/metrics" + resourcetypes "github.com/projecteru2/core/resource/types" "github.com/projecteru2/core/types" "github.com/projecteru2/core/utils" "github.com/projecteru2/core/wal" @@ -52,9 +53,9 @@ func (c *Calcium) doCreateWorkloads(ctx context.Context, opts *types.DeployOptio deployMap map[string]int rollbackMap map[string][]int // map[nodename][]Resources - engineParamsMap = map[string][]types.Resources{} + engineParamsMap = map[string][]resourcetypes.Resources{} // map[nodename][]Resources - workloadResourcesMap = map[string][]types.Resources{} + workloadResourcesMap = map[string][]resourcetypes.Resources{} ) _ = c.pool.Invoke(func() { @@ -151,7 +152,7 @@ func (c *Calcium) doCreateWorkloads(ctx context.Context, opts *types.DeployOptio } for nodename, rollbackIndices := range rollbackMap { if e := c.withNodePodLocked(ctx, nodename, func(ctx context.Context, node *types.Node) error { - rollbackResources := utils.Map(rollbackIndices, func(idx int) types.Resources { + rollbackResources := utils.Map(rollbackIndices, func(idx int) resourcetypes.Resources { return workloadResourcesMap[nodename][idx] }) return c.rmgr.RollbackAlloc(ctx, nodename, rollbackResources) @@ -173,8 +174,8 @@ func (c *Calcium) doCreateWorkloads(ctx context.Context, opts *types.DeployOptio func (c *Calcium) doDeployWorkloads(ctx context.Context, ch chan *types.CreateWorkloadMessage, opts *types.DeployOptions, - engineParamsMap map[string][]types.Resources, - workloadResourcesMap map[string][]types.Resources, + engineParamsMap map[string][]resourcetypes.Resources, + workloadResourcesMap map[string][]resourcetypes.Resources, deployMap map[string]int) (_ map[string][]int, err error) { wg := sync.WaitGroup{} @@ -220,8 +221,8 @@ func (c *Calcium) doDeployWorkloadsOnNode(ctx context.Context, nodename string, opts *types.DeployOptions, deploy int, - engineParams []types.Resources, - workloadResources []types.Resources, + engineParams []resourcetypes.Resources, + workloadResources []resourcetypes.Resources, seq int) (indices []int, err error) { logger := log.WithFunc("calcium.doDeployWorkloadsOnNode").WithField("node", nodename).WithField("ident", opts.ProcessIdent).WithField("deploy", deploy).WithField("seq", seq) @@ -292,15 +293,15 @@ func (c *Calcium) doDeployOneWorkload( node *types.Node, opts *types.DeployOptions, msg *types.CreateWorkloadMessage, - config *enginetypes.VirtualizationCreateOptions, + createOpts *enginetypes.VirtualizationCreateOptions, decrProcessing bool, ) (err error) { logger := log.WithFunc("calcium.doDeployWorkload").WithField("node", node.Name).WithField("ident", opts.ProcessIdent).WithField("msg", msg) workload := &types.Workload{ Resources: msg.Resources, EngineParams: msg.EngineParams, - Name: config.Name, - Labels: config.Labels, + Name: createOpts.Name, + Labels: createOpts.Labels, Podname: opts.Podname, Nodename: node.Name, Hook: opts.Entrypoint.Hook, @@ -324,7 +325,7 @@ func (c *Calcium) doDeployOneWorkload( ctx, // create workload func(ctx context.Context) error { - created, err := node.Engine.VirtualizationCreate(ctx, config) + created, err := node.Engine.VirtualizationCreate(ctx, createOpts) if err != nil { return err } @@ -437,48 +438,48 @@ func (c *Calcium) doDeployOneWorkload( } func (c *Calcium) doMakeWorkloadOptions(ctx context.Context, no int, msg *types.CreateWorkloadMessage, opts *types.DeployOptions, node *types.Node) *enginetypes.VirtualizationCreateOptions { - config := &enginetypes.VirtualizationCreateOptions{} + createOpts := &enginetypes.VirtualizationCreateOptions{} // general - config.EngineParams = msg.EngineParams - config.RawArgs = opts.RawArgs - config.Lambda = opts.Lambda - config.User = opts.User - config.DNS = opts.DNS - config.Image = opts.Image - config.Stdin = opts.OpenStdin - config.Hosts = opts.ExtraHosts - config.Debug = opts.Debug - config.Networks = opts.Networks + createOpts.EngineParams = msg.EngineParams + createOpts.RawArgs = opts.RawArgs + createOpts.Lambda = opts.Lambda + createOpts.User = opts.User + createOpts.DNS = opts.DNS + createOpts.Image = opts.Image + createOpts.Stdin = opts.OpenStdin + createOpts.Hosts = opts.ExtraHosts + createOpts.Debug = opts.Debug + createOpts.Networks = opts.Networks // entry entry := opts.Entrypoint - config.WorkingDir = entry.Dir - config.Privileged = entry.Privileged - config.Sysctl = entry.Sysctls - config.Publish = entry.Publish - config.Restart = entry.Restart + createOpts.WorkingDir = entry.Dir + createOpts.Privileged = entry.Privileged + createOpts.Sysctl = entry.Sysctls + createOpts.Publish = entry.Publish + createOpts.Restart = entry.Restart if entry.Log != nil { - config.LogType = entry.Log.Type - config.LogConfig = map[string]string{} + createOpts.LogType = entry.Log.Type + createOpts.LogConfig = map[string]string{} for k, v := range entry.Log.Config { - config.LogConfig[k] = v + createOpts.LogConfig[k] = v } } // name suffix := utils.RandomString(6) - config.Name = utils.MakeWorkloadName(opts.Name, opts.Entrypoint.Name, suffix) - msg.WorkloadName = config.Name + createOpts.Name = utils.MakeWorkloadName(opts.Name, opts.Entrypoint.Name, suffix) + msg.WorkloadName = createOpts.Name // command and user // extra args is dynamically - config.Cmd = opts.Entrypoint.Commands + createOpts.Cmd = opts.Entrypoint.Commands // env env := append(opts.Env, fmt.Sprintf("APP_NAME=%s", opts.Name)) //nolint env = append(env, fmt.Sprintf("ERU_POD=%s", opts.Podname)) env = append(env, fmt.Sprintf("ERU_NODE_NAME=%s", node.Name)) env = append(env, fmt.Sprintf("ERU_WORKLOAD_SEQ=%d", no)) - config.Env = env + createOpts.Env = env // basic labels, bind to LabelMeta - config.Labels = map[string]string{ + createOpts.Labels = map[string]string{ cluster.ERUMark: "1", cluster.LabelMeta: utils.EncodeMetaInLabel(ctx, &types.LabelMeta{ Publish: opts.Entrypoint.Publish, @@ -488,8 +489,8 @@ func (c *Calcium) doMakeWorkloadOptions(ctx context.Context, no int, msg *types. cluster.LabelCoreID: c.identifier, } for key, value := range opts.Labels { - config.Labels[key] = value + createOpts.Labels[key] = value } - return config + return createOpts } diff --git a/cluster/calcium/create_test.go b/cluster/calcium/create_test.go index 000bb324e..af4efe60b 100644 --- a/cluster/calcium/create_test.go +++ b/cluster/calcium/create_test.go @@ -13,6 +13,7 @@ import ( lockmocks "github.com/projecteru2/core/lock/mocks" resourcemocks "github.com/projecteru2/core/resource/mocks" plugintypes "github.com/projecteru2/core/resource/plugins/types" + resourcetypes "github.com/projecteru2/core/resource/types" storemocks "github.com/projecteru2/core/store/mocks" "github.com/projecteru2/core/strategy" "github.com/projecteru2/core/types" @@ -68,7 +69,7 @@ func TestCreateWorkloadTxn(t *testing.T) { Count: 2, DeployStrategy: strategy.Auto, Podname: "p1", - Resources: types.Resources{}, + Resources: resourcetypes.Resources{}, Image: "zc:test", Entrypoint: &types.Entrypoint{ Name: "good-entrypoint", @@ -149,8 +150,8 @@ func TestCreateWorkloadTxn(t *testing.T) { assert.True(t, walCommitted) walCommitted = false rmgr.On("Alloc", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( - []types.Resources{{}, {}}, - []types.Resources{ + []resourcetypes.Resources{{}, {}}, + []resourcetypes.Resources{ {node1.Name: {}}, {node2.Name: {}}, }, diff --git a/cluster/calcium/dissociate.go b/cluster/calcium/dissociate.go index 25b3bf526..538096d7d 100644 --- a/cluster/calcium/dissociate.go +++ b/cluster/calcium/dissociate.go @@ -5,6 +5,7 @@ import ( "github.com/projecteru2/core/log" "github.com/projecteru2/core/resource/plugins" + resourcetypes "github.com/projecteru2/core/resource/types" "github.com/projecteru2/core/types" "github.com/projecteru2/core/utils" ) @@ -32,7 +33,7 @@ func (c *Calcium) DissociateWorkload(ctx context.Context, IDs []string) (chan *t ctx, // if func(ctx context.Context) (err error) { - _, _, err = c.rmgr.SetNodeResourceUsage(ctx, node.Name, nil, nil, []types.Resources{workload.Resources}, true, plugins.Decr) + _, _, err = c.rmgr.SetNodeResourceUsage(ctx, node.Name, nil, nil, []resourcetypes.Resources{workload.Resources}, true, plugins.Decr) return err }, // then @@ -44,7 +45,7 @@ func (c *Calcium) DissociateWorkload(ctx context.Context, IDs []string) (chan *t if failedByCond { return nil } - _, _, err = c.rmgr.SetNodeResourceUsage(ctx, node.Name, nil, nil, []types.Resources{workload.Resources}, true, plugins.Incr) + _, _, err = c.rmgr.SetNodeResourceUsage(ctx, node.Name, nil, nil, []resourcetypes.Resources{workload.Resources}, true, plugins.Incr) return err }, c.config.GlobalTimeout, diff --git a/cluster/calcium/dissociate_test.go b/cluster/calcium/dissociate_test.go index b843d47f8..8e07998f7 100644 --- a/cluster/calcium/dissociate_test.go +++ b/cluster/calcium/dissociate_test.go @@ -7,6 +7,7 @@ import ( lockmocks "github.com/projecteru2/core/lock/mocks" resourcemocks "github.com/projecteru2/core/resource/mocks" + resourcetypes "github.com/projecteru2/core/resource/types" storemocks "github.com/projecteru2/core/store/mocks" "github.com/projecteru2/core/types" @@ -25,7 +26,7 @@ func TestDissociateWorkload(t *testing.T) { lock.On("Unlock", mock.Anything).Return(nil) c1 := &types.Workload{ - Resources: types.Resources{}, + Resources: resourcetypes.Resources{}, ID: "c1", Podname: "p1", Nodename: "node1", @@ -55,8 +56,8 @@ func TestDissociateWorkload(t *testing.T) { store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil) // failed by RemoveWorkload rmgr.On("SetNodeResourceUsage", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( - types.Resources{}, - types.Resources{}, + resourcetypes.Resources{}, + resourcetypes.Resources{}, nil, ) store.On("RemoveWorkload", mock.Anything, mock.Anything).Return(types.ErrMockError).Once() diff --git a/cluster/calcium/lambda_test.go b/cluster/calcium/lambda_test.go index 5fbf8505c..beaa8adb4 100644 --- a/cluster/calcium/lambda_test.go +++ b/cluster/calcium/lambda_test.go @@ -12,6 +12,7 @@ import ( lockmocks "github.com/projecteru2/core/lock/mocks" resourcemocks "github.com/projecteru2/core/resource/mocks" plugintypes "github.com/projecteru2/core/resource/plugins/types" + resourcetypes "github.com/projecteru2/core/resource/types" storemocks "github.com/projecteru2/core/store/mocks" "github.com/projecteru2/core/strategy" "github.com/projecteru2/core/types" @@ -41,7 +42,7 @@ func TestRunAndWaitFailedThenWALCommitted(t *testing.T) { Count: 2, DeployStrategy: strategy.Auto, Podname: "p1", - Resources: types.Resources{}, + Resources: resourcetypes.Resources{}, Image: "zc:test", Entrypoint: &types.Entrypoint{ Name: "good-entrypoint", @@ -77,7 +78,7 @@ func TestLambdaWithWorkloadIDReturned(t *testing.T) { Count: 2, DeployStrategy: strategy.Auto, Podname: "p1", - Resources: types.Resources{}, + Resources: resourcetypes.Resources{}, Image: "zc:test", Entrypoint: &types.Entrypoint{ Name: "good-entrypoint", @@ -129,7 +130,7 @@ func TestLambdaWithError(t *testing.T) { Count: 2, DeployStrategy: strategy.Auto, Podname: "p1", - Resources: types.Resources{}, + Resources: resourcetypes.Resources{}, Image: "zc:test", Entrypoint: &types.Entrypoint{ Name: "good-entrypoint", @@ -195,8 +196,8 @@ func newLambdaCluster(t *testing.T) (*Calcium, []*types.Node) { store := c.store.(*storemocks.Store) rmgr := c.rmgr.(*resourcemocks.Manager) rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( - types.Resources{}, - types.Resources{}, + resourcetypes.Resources{}, + resourcetypes.Resources{}, []string{}, nil, ) @@ -218,8 +219,8 @@ func newLambdaCluster(t *testing.T) (*Calcium, []*types.Node) { 20, nil, ) rmgr.On("Alloc", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( - []types.Resources{{}, {}}, - []types.Resources{ + []resourcetypes.Resources{{}, {}}, + []resourcetypes.Resources{ {node1.Name: {}}, {node2.Name: {}}, }, diff --git a/cluster/calcium/node.go b/cluster/calcium/node.go index 7d2db0228..ee3b6f114 100644 --- a/cluster/calcium/node.go +++ b/cluster/calcium/node.go @@ -8,6 +8,7 @@ import ( enginetypes "github.com/projecteru2/core/engine/types" "github.com/projecteru2/core/log" "github.com/projecteru2/core/resource/plugins" + resourcetypes "github.com/projecteru2/core/resource/types" "github.com/projecteru2/core/types" "github.com/projecteru2/core/utils" ) @@ -20,7 +21,7 @@ func (c *Calcium) AddNode(ctx context.Context, opts *types.AddNodeOptions) (*typ logger.Error(ctx, err) return nil, err } - var res types.Resources + var res resourcetypes.Resources var node *types.Node var err error @@ -212,7 +213,7 @@ func (c *Calcium) SetNode(ctx context.Context, opts *types.SetNodeOptions) (*typ n.Labels = opts.Labels } - var origin types.Resources + var origin resourcetypes.Resources return utils.Txn(ctx, // if: update node resource capacity success func(ctx context.Context) error { diff --git a/cluster/calcium/node_test.go b/cluster/calcium/node_test.go index d26cdf166..839db530c 100644 --- a/cluster/calcium/node_test.go +++ b/cluster/calcium/node_test.go @@ -12,6 +12,7 @@ import ( lockmocks "github.com/projecteru2/core/lock/mocks" resourcemocks "github.com/projecteru2/core/resource/mocks" plugintypes "github.com/projecteru2/core/resource/plugins/types" + resourcetypes "github.com/projecteru2/core/resource/types" storemocks "github.com/projecteru2/core/store/mocks" "github.com/projecteru2/core/types" @@ -42,7 +43,7 @@ func TestAddNode(t *testing.T) { assert.Error(t, err) rmgr.AssertExpectations(t) rmgr.On("AddNode", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( - types.Resources{}, nil) + resourcetypes.Resources{}, nil) rmgr.On("RemoveNode", mock.Anything, mock.Anything).Return(nil) // failed by store.AddNode @@ -248,7 +249,7 @@ func TestSetNode(t *testing.T) { opts.Labels = labels // failed by SetNodeResourceCapacity - opts.Resources = types.Resources{"a": {"a": 1}} + opts.Resources = resourcetypes.Resources{"a": {"a": 1}} rmgr.On("SetNodeResourceCapacity", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( nil, nil, types.ErrMockError, ).Once() diff --git a/cluster/calcium/realloc.go b/cluster/calcium/realloc.go index 1289fe6b4..b620a18d1 100644 --- a/cluster/calcium/realloc.go +++ b/cluster/calcium/realloc.go @@ -7,6 +7,7 @@ import ( enginetypes "github.com/projecteru2/core/engine/types" "github.com/projecteru2/core/log" + resourcetypes "github.com/projecteru2/core/resource/types" "github.com/projecteru2/core/types" "github.com/projecteru2/core/utils" ) @@ -31,9 +32,9 @@ func (c *Calcium) ReallocResource(ctx context.Context, opts *types.ReallocOption } func (c *Calcium) doReallocOnNode(ctx context.Context, node *types.Node, workload *types.Workload, originWorkload types.Workload, opts *types.ReallocOptions) error { - var resources types.Resources - var deltaResources types.Resources - var engineParams types.Resources + var resources resourcetypes.Resources + var deltaResources resourcetypes.Resources + var engineParams resourcetypes.Resources var err error logger := log.WithFunc("calcium.doReallocOnNode").WithField("opts", opts) diff --git a/cluster/calcium/realloc_test.go b/cluster/calcium/realloc_test.go index 380657b76..c03b45ac8 100644 --- a/cluster/calcium/realloc_test.go +++ b/cluster/calcium/realloc_test.go @@ -10,6 +10,7 @@ import ( lockmocks "github.com/projecteru2/core/lock/mocks" resourcemocks "github.com/projecteru2/core/resource/mocks" plugintypes "github.com/projecteru2/core/resource/plugins/types" + resourcetypes "github.com/projecteru2/core/resource/types" storemocks "github.com/projecteru2/core/store/mocks" "github.com/projecteru2/core/types" @@ -47,7 +48,7 @@ func TestRealloc(t *testing.T) { ID: "c1", Podname: "p1", Engine: engine, - Resources: types.Resources{}, + Resources: resourcetypes.Resources{}, Nodename: "node1", }, } @@ -56,7 +57,7 @@ func TestRealloc(t *testing.T) { store.On("GetWorkload", mock.Anything, "c1").Return(newC1(context.Background(), nil)[0], nil) opts := &types.ReallocOptions{ ID: "c1", - Resources: types.Resources{}, + Resources: resourcetypes.Resources{}, } // failed by GetNode @@ -76,14 +77,14 @@ func TestRealloc(t *testing.T) { // failed by plugin rmgr.On("Realloc", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( - types.Resources{}, nil, nil, types.ErrMockError, + resourcetypes.Resources{}, nil, nil, types.ErrMockError, ).Once() err = c.ReallocResource(ctx, opts) assert.Error(t, err) rmgr.On("Realloc", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( - types.Resources{}, - types.Resources{}, - types.Resources{}, + resourcetypes.Resources{}, + resourcetypes.Resources{}, + resourcetypes.Resources{}, nil, ) rmgr.On("RollbackRealloc", mock.Anything, mock.Anything, mock.Anything).Return(nil) diff --git a/cluster/calcium/remap_test.go b/cluster/calcium/remap_test.go index 13d297e5d..ad25a1d52 100644 --- a/cluster/calcium/remap_test.go +++ b/cluster/calcium/remap_test.go @@ -9,6 +9,7 @@ import ( lockmocks "github.com/projecteru2/core/lock/mocks" "github.com/projecteru2/core/log" resourcemocks "github.com/projecteru2/core/resource/mocks" + resourcetypes "github.com/projecteru2/core/resource/types" storemocks "github.com/projecteru2/core/store/mocks" "github.com/projecteru2/core/types" "github.com/stretchr/testify/assert" @@ -20,19 +21,19 @@ func TestRemapResource(t *testing.T) { store := c.store.(*storemocks.Store) rmgr := c.rmgr.(*resourcemocks.Manager) rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( - types.Resources{"test": {"abc": 123}}, - types.Resources{"test": {"abc": 123}}, + resourcetypes.Resources{"test": {"abc": 123}}, + resourcetypes.Resources{"test": {"abc": 123}}, []string{types.ErrMockError.Error()}, nil) rmgr.On("Remap", mock.Anything, mock.Anything, mock.Anything).Return( - types.Resources{}, + resourcetypes.Resources{}, nil, ) engine := &enginemocks.API{} node := &types.Node{Engine: engine} workload := &types.Workload{ - Resources: types.Resources{}, + Resources: resourcetypes.Resources{}, } store.On("ListNodeWorkloads", mock.Anything, mock.Anything, mock.Anything).Return([]*types.Workload{workload}, nil) ch := make(chan enginetypes.VirtualizationRemapMessage, 1) diff --git a/cluster/calcium/remove.go b/cluster/calcium/remove.go index 8b78f14e2..31d9fa279 100644 --- a/cluster/calcium/remove.go +++ b/cluster/calcium/remove.go @@ -7,6 +7,7 @@ import ( "github.com/projecteru2/core/log" "github.com/projecteru2/core/resource/plugins" + resourcetypes "github.com/projecteru2/core/resource/types" "github.com/projecteru2/core/types" "github.com/projecteru2/core/utils" ) @@ -40,7 +41,7 @@ func (c *Calcium) RemoveWorkload(ctx context.Context, IDs []string, force bool) ctx, // if func(ctx context.Context) error { - _, _, err = c.rmgr.SetNodeResourceUsage(ctx, node.Name, nil, nil, []types.Resources{workload.Resources}, true, plugins.Decr) + _, _, err = c.rmgr.SetNodeResourceUsage(ctx, node.Name, nil, nil, []resourcetypes.Resources{workload.Resources}, true, plugins.Decr) return err }, // then @@ -55,7 +56,7 @@ func (c *Calcium) RemoveWorkload(ctx context.Context, IDs []string, force bool) if failedByCond { return nil } - _, _, err = c.rmgr.SetNodeResourceUsage(ctx, node.Name, nil, nil, []types.Resources{workload.Resources}, true, plugins.Incr) + _, _, err = c.rmgr.SetNodeResourceUsage(ctx, node.Name, nil, nil, []resourcetypes.Resources{workload.Resources}, true, plugins.Incr) return err }, c.config.GlobalTimeout, diff --git a/cluster/calcium/remove_test.go b/cluster/calcium/remove_test.go index c56047d79..cd8343faf 100644 --- a/cluster/calcium/remove_test.go +++ b/cluster/calcium/remove_test.go @@ -13,6 +13,7 @@ import ( "github.com/projecteru2/core/types" "github.com/cockroachdb/errors" + resourcetypes "github.com/projecteru2/core/resource/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) @@ -27,8 +28,8 @@ func TestRemoveWorkload(t *testing.T) { rmgr := c.rmgr.(*resourcemocks.Manager) rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, nil, nil, nil) rmgr.On("SetNodeResourceUsage", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( - types.Resources{}, - types.Resources{}, + resourcetypes.Resources{}, + resourcetypes.Resources{}, nil, ) rmgr.On("GetNodeMetrics", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]*plugintypes.Metrics{}, nil) diff --git a/cluster/calcium/resource_test.go b/cluster/calcium/resource_test.go index 68a586708..ac534274a 100644 --- a/cluster/calcium/resource_test.go +++ b/cluster/calcium/resource_test.go @@ -7,6 +7,7 @@ import ( enginemocks "github.com/projecteru2/core/engine/mocks" lockmocks "github.com/projecteru2/core/lock/mocks" resourcemocks "github.com/projecteru2/core/resource/mocks" + resourcetypes "github.com/projecteru2/core/resource/types" storemocks "github.com/projecteru2/core/store/mocks" "github.com/projecteru2/core/types" @@ -48,8 +49,8 @@ func TestPodResource(t *testing.T) { assert.NotEmpty(t, msg.Diffs) store.AssertExpectations(t) workloads := []*types.Workload{ - {Resources: types.Resources{}}, - {Resources: types.Resources{}}, + {Resources: resourcetypes.Resources{}}, + {Resources: resourcetypes.Resources{}}, } store.On("ListNodeWorkloads", mock.Anything, mock.Anything, mock.Anything).Return(workloads, nil) @@ -63,8 +64,8 @@ func TestPodResource(t *testing.T) { assert.NotEmpty(t, msg.Diffs) store.AssertExpectations(t) rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( - types.Resources{"test": {"abc": 123}}, - types.Resources{"test": {"abc": 123}}, + resourcetypes.Resources{"test": {"abc": 123}}, + resourcetypes.Resources{"test": {"abc": 123}}, []string{}, nil) @@ -98,14 +99,14 @@ func TestNodeResource(t *testing.T) { store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil) rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( - types.Resources{"test": {"abc": 123}}, - types.Resources{"test": {"abc": 123}}, + resourcetypes.Resources{"test": {"abc": 123}}, + resourcetypes.Resources{"test": {"abc": 123}}, []string{}, nil) workloads := []*types.Workload{ - {Resources: types.Resources{}, Engine: engine}, - {Resources: types.Resources{}, Engine: engine}, + {Resources: resourcetypes.Resources{}, Engine: engine}, + {Resources: resourcetypes.Resources{}, Engine: engine}, } store.On("ListNodeWorkloads", mock.Anything, mock.Anything, mock.Anything).Return(workloads, nil) engine.On("VirtualizationInspect", mock.Anything, mock.Anything).Return(nil, types.ErrMockError) diff --git a/cluster/calcium/wal_test.go b/cluster/calcium/wal_test.go index 579bc2586..6c8cdbca1 100644 --- a/cluster/calcium/wal_test.go +++ b/cluster/calcium/wal_test.go @@ -12,6 +12,7 @@ import ( lockmocks "github.com/projecteru2/core/lock/mocks" resourcemocks "github.com/projecteru2/core/resource/mocks" plugintypes "github.com/projecteru2/core/resource/plugins/types" + resourcetypes "github.com/projecteru2/core/resource/types" storemocks "github.com/projecteru2/core/store/mocks" "github.com/projecteru2/core/types" @@ -26,8 +27,8 @@ func TestHandleCreateWorkloadNoHandle(t *testing.T) { c.wal = wal rmgr := c.rmgr.(*resourcemocks.Manager) rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( - types.Resources{}, - types.Resources{}, + resourcetypes.Resources{}, + resourcetypes.Resources{}, []string{}, nil, ) @@ -58,8 +59,8 @@ func TestHandleCreateWorkloadError(t *testing.T) { c.wal = wal rmgr := c.rmgr.(*resourcemocks.Manager) rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( - types.Resources{}, - types.Resources{}, + resourcetypes.Resources{}, + resourcetypes.Resources{}, []string{}, nil, ) @@ -112,8 +113,8 @@ func TestHandleCreateWorkloadHandled(t *testing.T) { c.wal = wal rmgr := c.rmgr.(*resourcemocks.Manager) rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( - types.Resources{}, - types.Resources{}, + resourcetypes.Resources{}, + resourcetypes.Resources{}, []string{}, nil, ) @@ -160,19 +161,19 @@ func TestHandleCreateLambda(t *testing.T) { c.wal = wal rmgr := c.rmgr.(*resourcemocks.Manager) rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( - types.Resources{}, - types.Resources{}, + resourcetypes.Resources{}, + resourcetypes.Resources{}, []string{}, nil, ) rmgr.On("SetNodeResourceUsage", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return( - types.Resources{}, - types.Resources{}, + resourcetypes.Resources{}, + resourcetypes.Resources{}, nil, ) rmgr.On("GetNodeMetrics", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]*plugintypes.Metrics{}, nil) rmgr.On("Remap", mock.Anything, mock.Anything, mock.Anything).Return( - types.Resources{}, + resourcetypes.Resources{}, nil, ) diff --git a/engine/mocks/fakeengine/mock.go b/engine/mocks/fakeengine/mock.go index 9dcdee28c..3ad2d1366 100644 --- a/engine/mocks/fakeengine/mock.go +++ b/engine/mocks/fakeengine/mock.go @@ -7,6 +7,7 @@ import ( "io" "github.com/docker/go-units" + "github.com/sanity-io/litter" mock "github.com/stretchr/testify/mock" "github.com/projecteru2/core/engine" @@ -85,7 +86,9 @@ func MakeClient(ctx context.Context, config coretypes.Config, nodename, endpoint e.On("BuildContent", mock.Anything, mock.Anything, mock.Anything).Return("BuildContent", buildContent, nil) // virtualization var ID string - e.On("VirtualizationCreate", mock.Anything, mock.Anything).Return(func(context.Context, *enginetypes.VirtualizationCreateOptions) *enginetypes.VirtualizationCreated { + e.On("VirtualizationCreate", mock.Anything, mock.Anything).Return(func(_ context.Context, opts *enginetypes.VirtualizationCreateOptions) *enginetypes.VirtualizationCreated { + tmp, _ := engine.MakeVirtualizationResource(opts.EngineParams) + litter.Dump(tmp) ID = utils.RandomString(64) return &enginetypes.VirtualizationCreated{ID: ID, Name: "mock-test-cvm" + utils.RandomString(6)} }, nil) @@ -108,6 +111,5 @@ func MakeClient(ctx context.Context, config coretypes.Config, nodename, endpoint e.On("VirtualizationWait", mock.Anything, mock.Anything, mock.Anything).Return(&enginetypes.VirtualizationWaitResult{Message: "", Code: 0}, nil) e.On("VirtualizationUpdateResource", mock.Anything, mock.Anything, mock.Anything).Return(nil) e.On("VirtualizationCopyFrom", mock.Anything, mock.Anything, mock.Anything).Return([]byte("d1...\nd2...\n"), 0, 0, int64(0), nil) - // e.On("ResourceValidate", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) return e, nil } diff --git a/engine/transform.go b/engine/transform.go index ba8324034..44a46a9c1 100644 --- a/engine/transform.go +++ b/engine/transform.go @@ -8,8 +8,19 @@ import ( // MakeVirtualizationResource . // TODO Werid, should revise func MakeVirtualizationResource(engineParams interface{}) (types.VirtualizationResource, error) { + // trans to Resources first because cycle import not allow here + t := map[string]map[string]interface{}{} var res types.VirtualizationResource - if err := mapstructure.Decode(engineParams, &res); err != nil { + if err := mapstructure.Decode(engineParams, &t); err != nil { + return res, err + } + r := map[string]interface{}{} + for _, p := range t { + for k, v := range p { + r[k] = v + } + } + if err := mapstructure.Decode(r, &res); err != nil { return res, err } res.EngineParams = engineParams diff --git a/engine/types/virtualization.go b/engine/types/virtualization.go index 3450303cf..29f7ec7ec 100644 --- a/engine/types/virtualization.go +++ b/engine/types/virtualization.go @@ -1,6 +1,6 @@ package types -// VirtualizationResource define resources +// VirtualizationResource1 define resources type VirtualizationResource struct { EngineParams interface{} `json:"-" mapstructure:"-"` CPU map[string]int64 `json:"cpu_map" mapstructure:"cpu_map"` // for cpu binding diff --git a/resource/cobalt/alloc.go b/resource/cobalt/alloc.go index 25a5f5616..a2873d605 100644 --- a/resource/cobalt/alloc.go +++ b/resource/cobalt/alloc.go @@ -6,7 +6,7 @@ import ( "github.com/projecteru2/core/log" "github.com/projecteru2/core/resource/plugins" plugintypes "github.com/projecteru2/core/resource/plugins/types" - "github.com/projecteru2/core/types" + resourcetypes "github.com/projecteru2/core/resource/types" "github.com/projecteru2/core/utils" ) @@ -23,17 +23,17 @@ opts struct }, } */ -func (m Manager) Alloc(ctx context.Context, nodename string, deployCount int, opts types.Resources) ([]types.Resources, []types.Resources, error) { +func (m Manager) Alloc(ctx context.Context, nodename string, deployCount int, opts resourcetypes.Resources) ([]resourcetypes.Resources, []resourcetypes.Resources, error) { logger := log.WithFunc("resource.coblat.Alloc") // index -> no, map by plugin name - workloadsParams := make([]types.Resources, deployCount) - engineParams := make([]types.Resources, deployCount) + workloadsParams := make([]resourcetypes.Resources, deployCount) + engineParams := make([]resourcetypes.Resources, deployCount) // init engine args for i := 0; i < deployCount; i++ { - workloadsParams[i] = types.Resources{} - engineParams[i] = types.Resources{} + workloadsParams[i] = resourcetypes.Resources{} + engineParams[i] = resourcetypes.Resources{} } return workloadsParams, engineParams, utils.PCR(ctx, @@ -86,7 +86,7 @@ func (m Manager) Alloc(ctx context.Context, nodename string, deployCount int, op } // RollbackAlloc rollbacks the allocated resource -func (m Manager) RollbackAlloc(ctx context.Context, nodename string, workloadsParams []types.Resources) error { +func (m Manager) RollbackAlloc(ctx context.Context, nodename string, workloadsParams []resourcetypes.Resources) error { _, _, err := m.SetNodeResourceUsage(ctx, nodename, nil, nil, workloadsParams, true, plugins.Decr) return err } diff --git a/resource/cobalt/node.go b/resource/cobalt/node.go index fd6eb1b5f..4b7150ace 100644 --- a/resource/cobalt/node.go +++ b/resource/cobalt/node.go @@ -7,6 +7,7 @@ import ( "github.com/cockroachdb/errors" enginetypes "github.com/projecteru2/core/engine/types" plugintypes "github.com/projecteru2/core/resource/plugins/types" + resourcetypes "github.com/projecteru2/core/resource/types" "github.com/sanity-io/litter" "golang.org/x/exp/slices" @@ -17,9 +18,9 @@ import ( ) // AddNode . -func (m Manager) AddNode(ctx context.Context, nodename string, opts types.Resources, nodeInfo *enginetypes.Info) (types.Resources, error) { +func (m Manager) AddNode(ctx context.Context, nodename string, opts resourcetypes.Resources, nodeInfo *enginetypes.Info) (resourcetypes.Resources, error) { logger := log.WithFunc("resource.cobalt.AddNode").WithField("node", nodename) - res := types.Resources{} + res := resourcetypes.Resources{} rollbackPlugins := []plugins.Plugin{} return res, utils.PCR(ctx, @@ -73,8 +74,8 @@ func (m Manager) AddNode(ctx context.Context, nodename string, opts types.Resour // RemoveNode . func (m Manager) RemoveNode(ctx context.Context, nodename string) error { logger := log.WithFunc("resource.cobalt.RemoveNode").WithField("node", nodename) - var nodeCapacity types.Resources - var nodeUsage types.Resources + var nodeCapacity resourcetypes.Resources + var nodeUsage resourcetypes.Resources rollbackPlugins := []plugins.Plugin{} return utils.PCR(ctx, @@ -163,9 +164,9 @@ func (m Manager) GetMostIdleNode(ctx context.Context, nodenames []string) (strin } // GetNodeResourceInfo . -func (m Manager) GetNodeResourceInfo(ctx context.Context, nodename string, workloads []*types.Workload, fix bool) (types.Resources, types.Resources, []string, error) { - nodeCapacity := types.Resources{} - nodeUsage := types.Resources{} +func (m Manager) GetNodeResourceInfo(ctx context.Context, nodename string, workloads []*types.Workload, fix bool) (resourcetypes.Resources, resourcetypes.Resources, []string, error) { + nodeCapacity := resourcetypes.Resources{} + nodeUsage := resourcetypes.Resources{} resourceDiffs := []string{} ps := m.plugins @@ -211,27 +212,27 @@ func (m Manager) GetNodeResourceInfo(ctx context.Context, nodename string, workl } // SetNodeResourceUsage . -func (m Manager) SetNodeResourceUsage(ctx context.Context, nodename string, nodeResource types.Resources, nodeResourceRequest types.Resources, workloadsResource []types.Resources, delta bool, incr bool) (types.Resources, types.Resources, error) { +func (m Manager) SetNodeResourceUsage(ctx context.Context, nodename string, nodeResource resourcetypes.Resources, nodeResourceRequest resourcetypes.Resources, workloadsResource []resourcetypes.Resources, delta bool, incr bool) (resourcetypes.Resources, resourcetypes.Resources, error) { logger := log.WithFunc("resource.cobalt.SetNodeResourceUsage") - wrksResource := map[string][]types.RawParams{} + wrksResource := map[string][]resourcetypes.RawParams{} rollbackPlugins := []plugins.Plugin{} - before := types.Resources{} - after := types.Resources{} + before := resourcetypes.Resources{} + after := resourcetypes.Resources{} return before, after, utils.PCR(ctx, func(ctx context.Context) error { - // prepare: covert []types.Resources to map[plugin]types.Resources + // prepare: covert []resourcetypes.Resources to map[plugin]resourcetypes.Resources // [{"cpu-plugin": {"cpu": 1}}, {"cpu-plugin": {"cpu": 1}}] -> {"cpu-plugin": [{"cpu": 1}, {"cpu": 1}]} for _, workloadResource := range workloadsResource { for plugin, params := range workloadResource { if _, ok := wrksResource[plugin]; !ok { - wrksResource[plugin] = []types.RawParams{} + wrksResource[plugin] = []resourcetypes.RawParams{} } wrksResource[plugin] = append(wrksResource[plugin], params) } } if nodeResourceRequest == nil { - nodeResourceRequest = types.Resources{} + nodeResourceRequest = resourcetypes.Resources{} } return nil }, @@ -273,7 +274,7 @@ func (m Manager) SetNodeResourceUsage(ctx context.Context, nodename string, node // GetNodesDeployCapacity returns available nodes which meet all the requirements // the caller should require locks // pure calculation -func (m Manager) GetNodesDeployCapacity(ctx context.Context, nodenames []string, opts types.Resources) (map[string]*plugintypes.NodeDeployCapacity, int, error) { +func (m Manager) GetNodesDeployCapacity(ctx context.Context, nodenames []string, opts resourcetypes.Resources) (map[string]*plugintypes.NodeDeployCapacity, int, error) { logger := log.WithFunc("resource.cobalt.GetNodesDeployCapacity") var resp map[string]*plugintypes.NodeDeployCapacity @@ -310,17 +311,17 @@ func (m Manager) GetNodesDeployCapacity(ctx context.Context, nodenames []string, // SetNodeResourceCapacity updates node resource capacity // receives resource options instead of resource args -func (m Manager) SetNodeResourceCapacity(ctx context.Context, nodename string, nodeResource types.Resources, nodeResourceRequest types.Resources, delta bool, incr bool) (types.Resources, types.Resources, error) { +func (m Manager) SetNodeResourceCapacity(ctx context.Context, nodename string, nodeResource resourcetypes.Resources, nodeResourceRequest resourcetypes.Resources, delta bool, incr bool) (resourcetypes.Resources, resourcetypes.Resources, error) { logger := log.WithFunc("resource.cobalt.SetNodeResourceCapacity").WithField("node", nodename) rollbackPlugins := []plugins.Plugin{} - before := types.Resources{} - after := types.Resources{} + before := resourcetypes.Resources{} + after := resourcetypes.Resources{} return before, after, utils.PCR(ctx, func(ctx context.Context) error { if nodeResourceRequest == nil { - nodeResourceRequest = types.Resources{} + nodeResourceRequest = resourcetypes.Resources{} } return nil }, diff --git a/resource/cobalt/realloc.go b/resource/cobalt/realloc.go index 9d558495d..a475010e3 100644 --- a/resource/cobalt/realloc.go +++ b/resource/cobalt/realloc.go @@ -6,16 +6,16 @@ import ( "github.com/projecteru2/core/log" "github.com/projecteru2/core/resource/plugins" plugintypes "github.com/projecteru2/core/resource/plugins/types" - "github.com/projecteru2/core/types" + resourcetypes "github.com/projecteru2/core/resource/types" "github.com/projecteru2/core/utils" ) // Realloc . -func (m Manager) Realloc(ctx context.Context, nodename string, nodeResource types.Resources, opts types.Resources) (types.Resources, types.Resources, types.Resources, error) { +func (m Manager) Realloc(ctx context.Context, nodename string, nodeResource resourcetypes.Resources, opts resourcetypes.Resources) (resourcetypes.Resources, resourcetypes.Resources, resourcetypes.Resources, error) { logger := log.WithFunc("resource.cobalt.Realloc").WithField("node", nodename) - engineParams := types.Resources{} - deltaResources := types.Resources{} - workloadResource := types.Resources{} + engineParams := resourcetypes.Resources{} + deltaResources := resourcetypes.Resources{} + workloadResource := resourcetypes.Resources{} return engineParams, deltaResources, workloadResource, utils.PCR(ctx, // prepare: calculate engine args, delta node resource args and final workload resource args @@ -46,7 +46,7 @@ func (m Manager) Realloc(ctx context.Context, nodename string, nodeResource type // commit: update node resource func(ctx context.Context) error { // TODO 存在问题??3个参数是完整的变化,差值变化,按照 workloads 的变化 - if _, _, err := m.SetNodeResourceUsage(ctx, nodename, nil, nil, []types.Resources{workloadResource}, true, plugins.Incr); err != nil { + if _, _, err := m.SetNodeResourceUsage(ctx, nodename, nil, nil, []resourcetypes.Resources{workloadResource}, true, plugins.Incr); err != nil { logger.Error(ctx, err, "failed to update nodename resource") return err } @@ -61,7 +61,7 @@ func (m Manager) Realloc(ctx context.Context, nodename string, nodeResource type } // RollbackRealloc . -func (m Manager) RollbackRealloc(ctx context.Context, nodename string, workloadParams types.Resources) error { - _, _, err := m.SetNodeResourceUsage(ctx, nodename, nil, nil, []types.Resources{workloadParams}, true, plugins.Decr) +func (m Manager) RollbackRealloc(ctx context.Context, nodename string, workloadParams resourcetypes.Resources) error { + _, _, err := m.SetNodeResourceUsage(ctx, nodename, nil, nil, []resourcetypes.Resources{workloadParams}, true, plugins.Decr) return err } diff --git a/resource/cobalt/remap.go b/resource/cobalt/remap.go index c6eecff37..ddf12fd74 100644 --- a/resource/cobalt/remap.go +++ b/resource/cobalt/remap.go @@ -6,12 +6,13 @@ import ( "github.com/projecteru2/core/log" "github.com/projecteru2/core/resource/plugins" plugintypes "github.com/projecteru2/core/resource/plugins/types" + resourcetypes "github.com/projecteru2/core/resource/types" "github.com/projecteru2/core/types" ) // Remap remaps resource and returns engine args for workloads. format: {"workload-1": {"cpus": ["1-3"]}} // remap doesn't change resource args -func (m Manager) Remap(ctx context.Context, nodename string, workloads []*types.Workload) (types.Resources, error) { +func (m Manager) Remap(ctx context.Context, nodename string, workloads []*types.Workload) (resourcetypes.Resources, error) { logger := log.WithFunc("resource.cobalt.GetRemapArgs").WithField("node", nodename) // call plugins to remap resps, err := call(ctx, m.plugins, func(plugin plugins.Plugin) (*plugintypes.CalculateRemapResponse, error) { @@ -29,12 +30,12 @@ func (m Manager) Remap(ctx context.Context, nodename string, workloads []*types. return nil, err } - enginesParams := types.Resources{} + enginesParams := resourcetypes.Resources{} // merge engine args for _, resp := range resps { for workloadID, engineParams := range resp.EngineParamsMap { if _, ok := enginesParams[workloadID]; !ok { - enginesParams[workloadID] = types.RawParams{} + enginesParams[workloadID] = resourcetypes.RawParams{} } v := enginesParams[workloadID] vMerged, err := m.mergeEngineParams(ctx, v, engineParams) diff --git a/resource/manager.go b/resource/manager.go index afba868d4..df880f8fb 100644 --- a/resource/manager.go +++ b/resource/manager.go @@ -5,25 +5,26 @@ import ( enginetypes "github.com/projecteru2/core/engine/types" plugintypes "github.com/projecteru2/core/resource/plugins/types" + resourcetypes "github.com/projecteru2/core/resource/types" "github.com/projecteru2/core/types" ) // Manager indicate manages // coretypes --> manager to rawparams --> plugins types type Manager interface { - AddNode(context.Context, string, types.Resources, *enginetypes.Info) (types.Resources, error) + AddNode(context.Context, string, resourcetypes.Resources, *enginetypes.Info) (resourcetypes.Resources, error) RemoveNode(context.Context, string) error - GetNodesDeployCapacity(context.Context, []string, types.Resources) (map[string]*plugintypes.NodeDeployCapacity, int, error) - SetNodeResourceCapacity(context.Context, string, types.Resources, types.Resources, bool, bool) (types.Resources, types.Resources, error) - SetNodeResourceUsage(context.Context, string, types.Resources, types.Resources, []types.Resources, bool, bool) (types.Resources, types.Resources, error) - GetNodeResourceInfo(context.Context, string, []*types.Workload, bool) (types.Resources, types.Resources, []string, error) + GetNodesDeployCapacity(context.Context, []string, resourcetypes.Resources) (map[string]*plugintypes.NodeDeployCapacity, int, error) + SetNodeResourceCapacity(context.Context, string, resourcetypes.Resources, resourcetypes.Resources, bool, bool) (resourcetypes.Resources, resourcetypes.Resources, error) + SetNodeResourceUsage(context.Context, string, resourcetypes.Resources, resourcetypes.Resources, []resourcetypes.Resources, bool, bool) (resourcetypes.Resources, resourcetypes.Resources, error) + GetNodeResourceInfo(context.Context, string, []*types.Workload, bool) (resourcetypes.Resources, resourcetypes.Resources, []string, error) GetMostIdleNode(context.Context, []string) (string, error) - Alloc(context.Context, string, int, types.Resources) ([]types.Resources, []types.Resources, error) - RollbackAlloc(context.Context, string, []types.Resources) error - Realloc(context.Context, string, types.Resources, types.Resources) (types.Resources, types.Resources, types.Resources, error) - RollbackRealloc(context.Context, string, types.Resources) error - Remap(context.Context, string, []*types.Workload) (types.Resources, error) + Alloc(context.Context, string, int, resourcetypes.Resources) ([]resourcetypes.Resources, []resourcetypes.Resources, error) + RollbackAlloc(context.Context, string, []resourcetypes.Resources) error + Realloc(context.Context, string, resourcetypes.Resources, resourcetypes.Resources) (resourcetypes.Resources, resourcetypes.Resources, resourcetypes.Resources, error) + RollbackRealloc(context.Context, string, resourcetypes.Resources) error + Remap(context.Context, string, []*types.Workload) (resourcetypes.Resources, error) GetNodeMetrics(context.Context, *types.Node) ([]*plugintypes.Metrics, error) GetMetricsDescription(context.Context) ([]*plugintypes.MetricsDescription, error) diff --git a/resource/mocks/Manager.go b/resource/mocks/Manager.go index bd7c0a9a2..ade459158 100644 --- a/resource/mocks/Manager.go +++ b/resource/mocks/Manager.go @@ -6,11 +6,13 @@ import ( context "context" enginetypes "github.com/projecteru2/core/engine/types" + coretypes "github.com/projecteru2/core/types" + mock "github.com/stretchr/testify/mock" pluginstypes "github.com/projecteru2/core/resource/plugins/types" - types "github.com/projecteru2/core/types" + types "github.com/projecteru2/core/resource/types" ) // Manager is an autogenerated mock type for the Manager type @@ -118,11 +120,11 @@ func (_m *Manager) GetMostIdleNode(_a0 context.Context, _a1 []string) (string, e } // GetNodeMetrics provides a mock function with given fields: _a0, _a1 -func (_m *Manager) GetNodeMetrics(_a0 context.Context, _a1 *types.Node) ([]*pluginstypes.Metrics, error) { +func (_m *Manager) GetNodeMetrics(_a0 context.Context, _a1 *coretypes.Node) ([]*pluginstypes.Metrics, error) { ret := _m.Called(_a0, _a1) var r0 []*pluginstypes.Metrics - if rf, ok := ret.Get(0).(func(context.Context, *types.Node) []*pluginstypes.Metrics); ok { + if rf, ok := ret.Get(0).(func(context.Context, *coretypes.Node) []*pluginstypes.Metrics); ok { r0 = rf(_a0, _a1) } else { if ret.Get(0) != nil { @@ -131,7 +133,7 @@ func (_m *Manager) GetNodeMetrics(_a0 context.Context, _a1 *types.Node) ([]*plug } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, *types.Node) error); ok { + if rf, ok := ret.Get(1).(func(context.Context, *coretypes.Node) error); ok { r1 = rf(_a0, _a1) } else { r1 = ret.Error(1) @@ -141,11 +143,11 @@ func (_m *Manager) GetNodeMetrics(_a0 context.Context, _a1 *types.Node) ([]*plug } // GetNodeResourceInfo provides a mock function with given fields: _a0, _a1, _a2, _a3 -func (_m *Manager) GetNodeResourceInfo(_a0 context.Context, _a1 string, _a2 []*types.Workload, _a3 bool) (types.Resources, types.Resources, []string, error) { +func (_m *Manager) GetNodeResourceInfo(_a0 context.Context, _a1 string, _a2 []*coretypes.Workload, _a3 bool) (types.Resources, types.Resources, []string, error) { ret := _m.Called(_a0, _a1, _a2, _a3) var r0 types.Resources - if rf, ok := ret.Get(0).(func(context.Context, string, []*types.Workload, bool) types.Resources); ok { + if rf, ok := ret.Get(0).(func(context.Context, string, []*coretypes.Workload, bool) types.Resources); ok { r0 = rf(_a0, _a1, _a2, _a3) } else { if ret.Get(0) != nil { @@ -154,7 +156,7 @@ func (_m *Manager) GetNodeResourceInfo(_a0 context.Context, _a1 string, _a2 []*t } var r1 types.Resources - if rf, ok := ret.Get(1).(func(context.Context, string, []*types.Workload, bool) types.Resources); ok { + if rf, ok := ret.Get(1).(func(context.Context, string, []*coretypes.Workload, bool) types.Resources); ok { r1 = rf(_a0, _a1, _a2, _a3) } else { if ret.Get(1) != nil { @@ -163,7 +165,7 @@ func (_m *Manager) GetNodeResourceInfo(_a0 context.Context, _a1 string, _a2 []*t } var r2 []string - if rf, ok := ret.Get(2).(func(context.Context, string, []*types.Workload, bool) []string); ok { + if rf, ok := ret.Get(2).(func(context.Context, string, []*coretypes.Workload, bool) []string); ok { r2 = rf(_a0, _a1, _a2, _a3) } else { if ret.Get(2) != nil { @@ -172,7 +174,7 @@ func (_m *Manager) GetNodeResourceInfo(_a0 context.Context, _a1 string, _a2 []*t } var r3 error - if rf, ok := ret.Get(3).(func(context.Context, string, []*types.Workload, bool) error); ok { + if rf, ok := ret.Get(3).(func(context.Context, string, []*coretypes.Workload, bool) error); ok { r3 = rf(_a0, _a1, _a2, _a3) } else { r3 = ret.Error(3) @@ -253,11 +255,11 @@ func (_m *Manager) Realloc(_a0 context.Context, _a1 string, _a2 types.Resources, } // Remap provides a mock function with given fields: _a0, _a1, _a2 -func (_m *Manager) Remap(_a0 context.Context, _a1 string, _a2 []*types.Workload) (types.Resources, error) { +func (_m *Manager) Remap(_a0 context.Context, _a1 string, _a2 []*coretypes.Workload) (types.Resources, error) { ret := _m.Called(_a0, _a1, _a2) var r0 types.Resources - if rf, ok := ret.Get(0).(func(context.Context, string, []*types.Workload) types.Resources); ok { + if rf, ok := ret.Get(0).(func(context.Context, string, []*coretypes.Workload) types.Resources); ok { r0 = rf(_a0, _a1, _a2) } else { if ret.Get(0) != nil { @@ -266,7 +268,7 @@ func (_m *Manager) Remap(_a0 context.Context, _a1 string, _a2 []*types.Workload) } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, string, []*types.Workload) error); ok { + if rf, ok := ret.Get(1).(func(context.Context, string, []*coretypes.Workload) error); ok { r1 = rf(_a0, _a1, _a2) } else { r1 = ret.Error(1) diff --git a/resource/plugins/cpumem/types/node.go b/resource/plugins/cpumem/types/node.go index 645a54541..83df7d4b3 100644 --- a/resource/plugins/cpumem/types/node.go +++ b/resource/plugins/cpumem/types/node.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/mitchellh/mapstructure" + resourcetypes "github.com/projecteru2/core/resource/types" coretypes "github.com/projecteru2/core/types" coreutils "github.com/projecteru2/core/utils" ) @@ -20,7 +21,7 @@ type NodeResource struct { } // Parse . -func (r *NodeResource) Parse(rawParams coretypes.RawParams) error { +func (r *NodeResource) Parse(rawParams resourcetypes.RawParams) error { return mapstructure.Decode(rawParams, r) } @@ -175,7 +176,7 @@ type NodeResourceRequest struct { NUMAMemory NUMAMemory `json:"numa_memory"` } -func (n *NodeResourceRequest) Parse(config coretypes.Config, rawParams coretypes.RawParams) error { +func (n *NodeResourceRequest) Parse(config coretypes.Config, rawParams resourcetypes.RawParams) error { var err error if n.CPUMap == nil { @@ -232,7 +233,7 @@ func (n *NodeResourceRequest) Parse(config coretypes.Config, rawParams coretypes return nil } -func (n *NodeResourceRequest) LoadFromOrigin(nodeResource *NodeResource, rawParams coretypes.RawParams) { +func (n *NodeResourceRequest) LoadFromOrigin(nodeResource *NodeResource, rawParams resourcetypes.RawParams) { if n == nil { return } diff --git a/resource/plugins/cpumem/types/workload.go b/resource/plugins/cpumem/types/workload.go index 51e975860..2a6316939 100644 --- a/resource/plugins/cpumem/types/workload.go +++ b/resource/plugins/cpumem/types/workload.go @@ -3,7 +3,7 @@ package types import ( "github.com/cockroachdb/errors" "github.com/mitchellh/mapstructure" - coretypes "github.com/projecteru2/core/types" + resourcetypes "github.com/projecteru2/core/resource/types" coreutils "github.com/projecteru2/core/utils" ) @@ -19,7 +19,7 @@ type WorkloadResource struct { } // ParseFromRawParams . -func (w *WorkloadResource) Parse(rawParams coretypes.RawParams) error { +func (w *WorkloadResource) Parse(rawParams resourcetypes.RawParams) error { return mapstructure.Decode(rawParams, w) } @@ -113,7 +113,7 @@ func (w *WorkloadResourceRequest) Validate() error { } // Parse . -func (w *WorkloadResourceRequest) Parse(rawParams coretypes.RawParams) (err error) { +func (w *WorkloadResourceRequest) Parse(rawParams resourcetypes.RawParams) (err error) { w.KeepCPUBind = rawParams.Bool("keep-cpu-bind") w.CPUBind = rawParams.Bool("cpu-bind") diff --git a/resource/plugins/mocks/Plugin.go b/resource/plugins/mocks/Plugin.go index b374ab269..3edd0ab80 100644 --- a/resource/plugins/mocks/Plugin.go +++ b/resource/plugins/mocks/Plugin.go @@ -10,7 +10,7 @@ import ( pluginstypes "github.com/projecteru2/core/resource/plugins/types" - types "github.com/projecteru2/core/types" + types "github.com/projecteru2/core/resource/types" ) // Plugin is an autogenerated mock type for the Plugin type diff --git a/resource/plugins/types/engine.go b/resource/plugins/types/engine.go index eb6612f92..70be30517 100644 --- a/resource/plugins/types/engine.go +++ b/resource/plugins/types/engine.go @@ -1,8 +1,6 @@ package types -import ( - coretypes "github.com/projecteru2/core/types" -) +import resourcetypes "github.com/projecteru2/core/resource/types" // EngineParams used for engine -type EngineParams = coretypes.RawParams +type EngineParams = resourcetypes.RawParams diff --git a/resource/plugins/types/node.go b/resource/plugins/types/node.go index af34d3be7..b893e3d89 100644 --- a/resource/plugins/types/node.go +++ b/resource/plugins/types/node.go @@ -1,14 +1,14 @@ package types import ( - coretypes "github.com/projecteru2/core/types" + resourcetypes "github.com/projecteru2/core/resource/types" ) // NodeResourceRequest use for raw data to create node -type NodeResourceRequest = coretypes.RawParams +type NodeResourceRequest = resourcetypes.RawParams // NodeResource use for indicate node's resource -type NodeResource = coretypes.RawParams +type NodeResource = resourcetypes.RawParams // AddNodeResponse . type AddNodeResponse struct { diff --git a/resource/plugins/types/workload.go b/resource/plugins/types/workload.go index 14d6d0017..a6840e581 100644 --- a/resource/plugins/types/workload.go +++ b/resource/plugins/types/workload.go @@ -1,11 +1,9 @@ package types -import ( - coretypes "github.com/projecteru2/core/types" -) +import resourcetypes "github.com/projecteru2/core/resource/types" // 带 keepbind -type WorkloadResourceRequest = coretypes.RawParams +type WorkloadResourceRequest = resourcetypes.RawParams // 不带 keepbind -type WorkloadResource = coretypes.RawParams +type WorkloadResource = resourcetypes.RawParams diff --git a/types/resource.go b/resource/types/resource.go similarity index 100% rename from types/resource.go rename to resource/types/resource.go diff --git a/types/resource_test.go b/resource/types/resource_test.go similarity index 100% rename from types/resource_test.go rename to resource/types/resource_test.go diff --git a/rpc/transform.go b/rpc/transform.go index 9447447e7..9f4490bdc 100644 --- a/rpc/transform.go +++ b/rpc/transform.go @@ -8,6 +8,7 @@ import ( enginetypes "github.com/projecteru2/core/engine/types" "github.com/projecteru2/core/log" + resourcetypes "github.com/projecteru2/core/resource/types" pb "github.com/projecteru2/core/rpc/gen" "github.com/projecteru2/core/types" "github.com/projecteru2/core/utils" @@ -533,10 +534,10 @@ func toCoreRemoveImageOptions(opts *pb.RemoveImageOptions) *types.ImageOptions { } } -func toCoreResources(resources map[string][]byte) types.Resources { - r := types.Resources{} +func toCoreResources(resources map[string][]byte) resourcetypes.Resources { + r := resourcetypes.Resources{} for k, v := range resources { - rp := types.RawParams{} + rp := resourcetypes.RawParams{} if err := json.Unmarshal(v, &rp); err != nil { log.WithFunc("toCoreResources").Errorf(nil, err, "%v", string(v)) // nolint continue diff --git a/types/message.go b/types/message.go index 31d3deeff..089edfaff 100644 --- a/types/message.go +++ b/types/message.go @@ -2,6 +2,8 @@ package types import ( "bytes" + + resourcetypes "github.com/projecteru2/core/resource/types" ) // RemoveWorkloadMessage for remove message @@ -79,8 +81,8 @@ type ControlWorkloadMessage struct { // CreateWorkloadMessage for create message type CreateWorkloadMessage struct { - EngineParams Resources - Resources Resources + EngineParams resourcetypes.Resources + Resources resourcetypes.Resources Podname string Nodename string WorkloadID string diff --git a/types/node.go b/types/node.go index 2f004a657..f82ae5891 100644 --- a/types/node.go +++ b/types/node.go @@ -6,6 +6,7 @@ import ( "github.com/mitchellh/mapstructure" engine "github.com/projecteru2/core/engine" + resourcetypes "github.com/projecteru2/core/resource/types" ) // NodeMeta . @@ -27,11 +28,11 @@ func (n NodeMeta) DeepCopy() (nn NodeMeta, err error) { // NodeResourceInfo for node resource info type NodeResourceInfo struct { - Name string `json:"-"` - Capacity Resources `json:"capacity,omitempty"` - Usage Resources `json:"usage,omitempty"` - Diffs []string `json:"diffs,omitempty"` - Workloads []*Workload `json:"-"` + Name string `json:"-"` + Capacity resourcetypes.Resources `json:"capacity,omitempty"` + Usage resourcetypes.Resources `json:"usage,omitempty"` + Diffs []string `json:"diffs,omitempty"` + Workloads []*Workload `json:"-"` } // Node store node info diff --git a/types/options.go b/types/options.go index f36dc4ca8..ae2b0571c 100644 --- a/types/options.go +++ b/types/options.go @@ -3,13 +3,15 @@ package types import ( "fmt" "io" + + resourcetypes "github.com/projecteru2/core/resource/types" ) // TODO should validate options // DeployOptions is options for deploying type DeployOptions struct { - Resources Resources + Resources resourcetypes.Resources Name string // Name of application Entrypoint *Entrypoint // entrypoint Podname string // Name of pod to deploy @@ -190,7 +192,7 @@ type AddNodeOptions struct { Cert string Key string Labels map[string]string - Resources Resources + Resources resourcetypes.Resources } // Validate checks options @@ -212,7 +214,7 @@ type SetNodeOptions struct { Nodename string Endpoint string WorkloadsDown bool - Resources Resources + Resources resourcetypes.Resources Delta bool Labels map[string]string Bypass TriOptions @@ -260,7 +262,7 @@ type ExecuteWorkloadOptions struct { // ReallocOptions . type ReallocOptions struct { ID string - Resources Resources + Resources resourcetypes.Resources } // TriOptions . diff --git a/types/options_test.go b/types/options_test.go index 137999081..5f5207660 100644 --- a/types/options_test.go +++ b/types/options_test.go @@ -4,6 +4,7 @@ import ( "bufio" "testing" + resourcetypes "github.com/projecteru2/core/resource/types" "github.com/stretchr/testify/assert" "go.uber.org/zap/buffer" ) @@ -17,7 +18,7 @@ func TestParseTriOption(t *testing.T) { func TestSetNodeOptions(t *testing.T) { o := &SetNodeOptions{ - Resources: Resources{}, + Resources: resourcetypes.Resources{}, } assert.Equal(t, ErrEmptyNodeName, o.Validate()) diff --git a/types/workload.go b/types/workload.go index c605edcc5..4831e4c0d 100644 --- a/types/workload.go +++ b/types/workload.go @@ -6,6 +6,7 @@ import ( "github.com/projecteru2/core/engine" enginetypes "github.com/projecteru2/core/engine/types" + resourcetypes "github.com/projecteru2/core/resource/types" "github.com/cockroachdb/errors" ) @@ -35,21 +36,21 @@ type LabelMeta struct { // only relationship with pod and node is stored // if you wanna get realtime information, use Inspect method type Workload struct { - Resources Resources `json:"resources"` - EngineParams Resources `json:"engine_params"` - ID string `json:"id"` - Name string `json:"name"` - Podname string `json:"podname"` - Nodename string `json:"nodename"` - Hook *Hook `json:"hook"` - Privileged bool `json:"privileged"` - User string `json:"user"` - Env []string `json:"env"` - Image string `json:"image"` - Labels map[string]string `json:"labels"` - CreateTime int64 `json:"create_time"` - StatusMeta *StatusMeta `json:"-"` - Engine engine.API `json:"-"` + Resources resourcetypes.Resources `json:"resources"` + EngineParams resourcetypes.Resources `json:"engine_params"` + ID string `json:"id"` + Name string `json:"name"` + Podname string `json:"podname"` + Nodename string `json:"nodename"` + Hook *Hook `json:"hook"` + Privileged bool `json:"privileged"` + User string `json:"user"` + Env []string `json:"env"` + Image string `json:"image"` + Labels map[string]string `json:"labels"` + CreateTime int64 `json:"create_time"` + StatusMeta *StatusMeta `json:"-"` + Engine engine.API `json:"-"` } // Inspect a workload