Skip to content

Commit 97094bd

Browse files
authored
calcuate capacity before creating (#268)
1 parent e3558e4 commit 97094bd

File tree

10 files changed

+543
-302
lines changed

10 files changed

+543
-302
lines changed

.github/workflows/golangci-lint.yml

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ jobs:
1616
uses: actions/checkout@v2
1717
- name: golangci-lint
1818
uses: golangci/golangci-lint-action@v1
19+
env:
20+
ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'
1921
with:
2022
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
2123
version: v1.29

cluster/calcium/capacity.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package calcium
2+
3+
import (
4+
"context"
5+
6+
"github.com/pkg/errors"
7+
"github.com/projecteru2/core/types"
8+
)
9+
10+
// CalculateCapacity calculates capacity
11+
func (c *Calcium) CalculateCapacity(ctx context.Context, opts *types.DeployOptions) (msg *types.CapacityMessage, err error) {
12+
return msg, c.withNodesLocked(ctx, opts.Podname, opts.Nodenames, nil, true, func(nodeMap map[string]*types.Node) error {
13+
var deployMap map[string]int
14+
if _, deployMap, err = c.doAllocResource(ctx, nodeMap, opts); err != nil {
15+
return errors.WithStack(err)
16+
}
17+
18+
msg = &types.CapacityMessage{NodeCapacities: deployMap}
19+
for _, capacity := range deployMap {
20+
msg.Total += capacity
21+
}
22+
return nil
23+
})
24+
}

cluster/calcium/capacity_test.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package calcium
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
enginemocks "github.com/projecteru2/core/engine/mocks"
8+
lockmocks "github.com/projecteru2/core/lock/mocks"
9+
"github.com/projecteru2/core/scheduler"
10+
schedulermocks "github.com/projecteru2/core/scheduler/mocks"
11+
storemocks "github.com/projecteru2/core/store/mocks"
12+
"github.com/projecteru2/core/strategy"
13+
"github.com/projecteru2/core/types"
14+
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/mock"
16+
)
17+
18+
func TestCalculateCapacity(t *testing.T) {
19+
c := NewTestCluster()
20+
scheduler.InitSchedulerV1(c.scheduler)
21+
ctx := context.Background()
22+
store := c.store.(*storemocks.Store)
23+
engine := &enginemocks.API{}
24+
25+
// pod1 := &types.Pod{Name: "p1"}
26+
node1 := &types.Node{
27+
Name: "n1",
28+
Engine: engine,
29+
CPU: types.CPUMap{"0": 100, "1": 100},
30+
}
31+
store.On("GetNode", mock.Anything, mock.Anything).Return(node1, nil)
32+
lock := &lockmocks.DistributedLock{}
33+
lock.On("Lock", mock.Anything).Return(nil)
34+
lock.On("Unlock", mock.Anything).Return(nil)
35+
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
36+
// failed by wrong resource
37+
opts := &types.DeployOptions{
38+
ResourceOpts: types.ResourceOptions{
39+
CPUBind: true,
40+
CPUQuotaRequest: 0,
41+
},
42+
DeployStrategy: strategy.Auto,
43+
Nodenames: []string{"n1"},
44+
}
45+
_, err := c.CalculateCapacity(ctx, opts)
46+
assert.Error(t, err)
47+
opts.ResourceOpts.CPUBind = false
48+
opts.ResourceOpts.CPUQuotaRequest = 0.5
49+
opts.Count = 5
50+
sched := c.scheduler.(*schedulermocks.Scheduler)
51+
// define nodesInfo
52+
nodesInfo := []types.NodeInfo{
53+
{
54+
Name: "n1",
55+
MemCap: 100,
56+
Deploy: 5,
57+
Capacity: 10,
58+
Count: 1,
59+
},
60+
}
61+
sched.On("SelectMemoryNodes", mock.Anything, mock.Anything, mock.Anything).Return(nodesInfo, 5, nil)
62+
sched.On("SelectStorageNodes", mock.Anything, mock.Anything).Return(nodesInfo, 5, nil)
63+
sched.On("SelectVolumeNodes", mock.Anything, mock.Anything).Return(nodesInfo, nil, 5, nil)
64+
store.On("MakeDeployStatus", mock.Anything, mock.Anything, mock.Anything).Return(nil)
65+
r, err := c.CalculateCapacity(ctx, opts)
66+
assert.NoError(t, err)
67+
assert.Equal(t, r.Total, 5)
68+
}

cluster/cluster.go

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ type Cluster interface {
5757
SetNode(ctx context.Context, opts *types.SetNodeOptions) (*types.Node, error)
5858
// node resource
5959
NodeResource(ctx context.Context, nodename string, fix bool) (*types.NodeResource, error)
60+
// calculate capacity
61+
CalculateCapacity(context.Context, *types.DeployOptions) (*types.CapacityMessage, error)
6062
// meta containers
6163
GetContainer(ctx context.Context, ID string) (*types.Container, error)
6264
GetContainers(ctx context.Context, IDs []string) ([]*types.Container, error)

cluster/mocks/Cluster.go

+28-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)