Skip to content

Commit e8bc5f5

Browse files
committed
Merge branch 'refactor/add-pod' into 'master'
pod 相关的 grpc 接口优化 See merge request !136
2 parents 3e82a48 + 1437524 commit e8bc5f5

19 files changed

+416
-215
lines changed

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.0
1+
1.0.1

cluster/calcium/create_container.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121
)
2222

2323
const (
24-
MEMORY_PRIOR = "MEM"
25-
CPU_PRIOR = "CPU"
2624
RESTART_ALWAYS = "always"
2725
)
2826

@@ -34,7 +32,7 @@ func (c *calcium) CreateContainer(specs types.Specs, opts *types.DeployOptions)
3432
if err != nil {
3533
return nil, err
3634
}
37-
if pod.Scheduler == CPU_PRIOR {
35+
if pod.Favor == types.CPU_PRIOR {
3836
return c.createContainerWithCPUPrior(specs, opts)
3937
}
4038
log.Infof("Creating container with options: %v", opts)
@@ -110,7 +108,7 @@ func (c *calcium) doCreateContainerWithMemoryPrior(nodeInfo types.NodeInfo, spec
110108
}
111109

112110
for i = 0; i < nodeInfo.Deploy; i++ {
113-
config, hostConfig, networkConfig, containerName, err := c.makeContainerOptions(i+index, nil, specs, opts, node, MEMORY_PRIOR)
111+
config, hostConfig, networkConfig, containerName, err := c.makeContainerOptions(i+index, nil, specs, opts, node, types.MEMORY_PRIOR)
114112
ms[i].ContainerName = containerName
115113
ms[i].Podname = opts.Podname
116114
ms[i].Nodename = node.Name
@@ -281,7 +279,7 @@ func (c *calcium) doCreateContainerWithCPUPrior(nodeName string, cpuMap []types.
281279

282280
for i, quota := range cpuMap {
283281
// create options
284-
config, hostConfig, networkConfig, containerName, err := c.makeContainerOptions(i+index, quota, specs, opts, node, CPU_PRIOR)
282+
config, hostConfig, networkConfig, containerName, err := c.makeContainerOptions(i+index, quota, specs, opts, node, types.CPU_PRIOR)
285283
ms[i].ContainerName = containerName
286284
ms[i].Podname = opts.Podname
287285
ms[i].Nodename = node.Name
@@ -385,7 +383,7 @@ func (c *calcium) releaseQuota(node *types.Node, quota types.CPUMap) {
385383
c.store.UpdateNodeCPU(node.Podname, node.Name, quota, "+")
386384
}
387385

388-
func (c *calcium) makeContainerOptions(index int, quota types.CPUMap, specs types.Specs, opts *types.DeployOptions, node *types.Node, optionMode string) (
386+
func (c *calcium) makeContainerOptions(index int, quota types.CPUMap, specs types.Specs, opts *types.DeployOptions, node *types.Node, favor string) (
389387
*enginecontainer.Config,
390388
*enginecontainer.HostConfig,
391389
*enginenetwork.NetworkingConfig,
@@ -528,7 +526,7 @@ func (c *calcium) makeContainerOptions(index int, quota types.CPUMap, specs type
528526
}
529527

530528
var resource enginecontainer.Resources
531-
if optionMode == CPU_PRIOR {
529+
if favor == types.CPU_PRIOR {
532530
resource = c.makeCPUPriorSetting(quota)
533531
} else {
534532
resource = c.makeMemoryPriorSetting(opts.Memory, opts.CPUQuota)

cluster/calcium/create_container_test.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,10 @@ func TestCreateContainerWithMemPrior(t *testing.T) {
9090
func TestClean(t *testing.T) {
9191
initMockConfig()
9292

93-
// delete pod
94-
err := mockc.store.DeletePod(podname, false)
93+
// delete pod, which will fail because there are remaining nodes
94+
err := mockc.store.RemovePod(podname)
9595
assert.Error(t, err)
96-
assert.Contains(t, err.Error(), "still has nodes, delete the nodes first")
97-
98-
// force delete
99-
err = mockc.store.DeletePod(podname, true)
100-
assert.NoError(t, err)
101-
96+
assert.Contains(t, err.Error(), "still has nodes")
10297
}
10398

10499
func TestCreateContainerWithCPUPrior(t *testing.T) {

cluster/calcium/meta.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ func (c *calcium) ListPods() ([]*types.Pod, error) {
1212
return c.store.GetAllPods()
1313
}
1414

15-
func (c *calcium) AddPod(podname, desc string) (*types.Pod, error) {
16-
return c.store.AddPod(podname, desc)
15+
func (c *calcium) AddPod(podname, favor, desc string) (*types.Pod, error) {
16+
return c.store.AddPod(podname, favor, desc)
17+
}
18+
19+
func (c *calcium) RemovePod(podname string) error {
20+
return c.store.RemovePod(podname)
1721
}
1822

1923
func (c *calcium) GetPod(podname string) (*types.Pod, error) {

cluster/calcium/meta_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@ func TestAddPod(t *testing.T) {
3838
config := types.Config{}
3939
c := &calcium{store: store, config: config, scheduler: simplescheduler.New(), network: calico.New(config), source: gitlab.New(config)}
4040

41-
store.On("AddPod", "pod1", "desc1").Return(&types.Pod{Name: "pod1", Desc: "desc1"}, nil)
42-
store.On("AddPod", "pod2", "desc2").Return(nil, fmt.Errorf("Etcd Error"))
41+
store.On("AddPod", "pod1", "", "desc1").Return(&types.Pod{Name: "pod1", Favor: "MEM", Desc: "desc1"}, nil)
42+
store.On("AddPod", "pod2", "", "desc2").Return(nil, fmt.Errorf("Etcd Error"))
4343

44-
p, err := c.AddPod("pod1", "desc1")
44+
p, err := c.AddPod("pod1", "", "desc1")
4545
assert.Equal(t, p.Name, "pod1")
46+
assert.Equal(t, p.Favor, "MEM")
4647
assert.Equal(t, p.Desc, "desc1")
4748
assert.Nil(t, err)
4849

49-
p, err = c.AddPod("pod2", "desc2")
50+
p, err = c.AddPod("pod2", "", "desc2")
5051
assert.Nil(t, p)
5152
assert.Equal(t, err.Error(), "Etcd Error")
5253
}

cluster/calcium/mock_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ func initMockConfig() {
324324
Engine: clnt,
325325
}
326326

327-
pod := &coretypes.Pod{Name: podname, Desc: desc, Scheduler: "complex"}
327+
pod := &coretypes.Pod{Name: podname, Desc: desc, Favor: "MEM"}
328328
mockStringType := mock.AnythingOfType("string")
329329
mockStore.On("GetPod", mockStringType).Return(pod, nil)
330330
mockStore.On("GetNodesByPod", mockStringType).Return([]*coretypes.Node{n1, n2}, nil)
@@ -344,7 +344,7 @@ func initMockConfig() {
344344
return true
345345
})).Return(nil)
346346

347-
mockStore.On("DeletePod", mockStringType, mock.AnythingOfType("bool")).Return(nil)
347+
mockStore.On("RemovePod", mockStringType).Return(nil)
348348

349349
deployNodeInfo := []coretypes.NodeInfo{
350350
coretypes.NodeInfo{

cluster/calcium/realloc.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (c *calcium) ReallocResource(ids []string, cpu float64, mem int64) (chan *t
4545
return nil, err
4646
}
4747
containersInfo[pod][node] = append(containersInfo[pod][node], container)
48-
if pod.Scheduler == CPU_PRIOR {
48+
if pod.Favor == types.CPU_PRIOR {
4949
if _, ok := cpuContainersInfo[pod]; !ok {
5050
cpuContainersInfo[pod] = CPUNodeContainers{}
5151
}
@@ -67,7 +67,7 @@ func (c *calcium) ReallocResource(ids []string, cpu float64, mem int64) (chan *t
6767
wg := sync.WaitGroup{}
6868
wg.Add(len(containersInfo))
6969
for pod, nodeContainers := range containersInfo {
70-
if pod.Scheduler == CPU_PRIOR {
70+
if pod.Favor == types.CPU_PRIOR {
7171
nodeCPUContainersInfo := cpuContainersInfo[pod]
7272
go func(pod *types.Pod, nodeCPUContainersInfo CPUNodeContainers) {
7373
defer wg.Done()

cluster/cluster.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
type Cluster interface {
1010
// meta data methods
1111
ListPods() ([]*types.Pod, error)
12-
AddPod(podname, desc string) (*types.Pod, error)
12+
AddPod(podname, favor, desc string) (*types.Pod, error)
13+
RemovePod(podname string) error
1314
GetPod(podname string) (*types.Pod, error)
1415
AddNode(nodename, endpoint, podname, cafile, certfile, keyfile string, public bool) (*types.Node, error)
1516
RemoveNode(nodename, podname string) (*types.Pod, error)

devtools/client.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ def list_pods(ctx):
4444

4545
@cli.command('pod:add')
4646
@click.argument('name')
47+
@click.argument('favor')
4748
@click.argument('desc')
4849
@click.pass_context
49-
def create_pod(ctx, name, desc):
50+
def add_pod(ctx, name, favor, desc):
5051
stub = _get_stub(ctx)
51-
opts = pb.AddPodOptions(name=name, desc=desc)
52+
opts = pb.AddPodOptions(name=name, favor=favor, desc=desc)
5253

5354
try:
5455
pod = stub.AddPod(opts, 5)
@@ -63,6 +64,22 @@ def create_pod(ctx, name, desc):
6364
click.echo(click.style('create pod %s successfully' % pod, fg='green'))
6465

6566

67+
@cli.command('pod:remove')
68+
@click.argument('name')
69+
@click.pass_context
70+
def remove_pod(ctx, name):
71+
stub = _get_stub(ctx)
72+
opts = pb.RemovePodOptions(name=name)
73+
74+
try:
75+
stub.RemovePod(opts, 5)
76+
except AbortionError as e:
77+
click.echo(click.style('abortion error: %s' % e.details, fg='red', bold=True))
78+
ctx.exit(-1)
79+
80+
click.echo(click.style('pod %s removed' % name, fg='green'))
81+
82+
6683
@cli.command('pod:get')
6784
@click.argument('name')
6885
@click.pass_context

0 commit comments

Comments
 (0)