Skip to content

Commit 3f086c4

Browse files
committed
add labels, envs, image and user in container type
1 parent 285bec3 commit 3f086c4

File tree

6 files changed

+63
-58
lines changed

6 files changed

+63
-58
lines changed

cluster/calcium/control.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,9 @@ func (c *Calcium) ControlContainer(ctx context.Context, IDs []string, t string,
5959
message, err = c.doStartContainer(ctx, container, containerInfo, force)
6060
return
6161
case cluster.ContainerRestart:
62-
if containerInfo, err := container.Inspect(ctx); err == nil && containerInfo.Running {
63-
message, err = c.doStopContainer(ctx, container, containerInfo, force)
64-
if err != nil {
65-
return
66-
}
67-
68-
} else if err != nil {
69-
log.Errorf("[ControlContainer] Inspect container %s failed %v", container.ID, err)
70-
} else if !containerInfo.Running {
71-
message = append(message, bytes.NewBufferString("container stopped, can't run hook\n"))
62+
message, err = c.doStopContainer(ctx, container, containerInfo, force)
63+
if err != nil {
64+
return
7265
}
7366
m2, e2 := c.doStartContainer(ctx, container, containerInfo, force)
7467
message = append(message, m2...)
@@ -89,6 +82,10 @@ func (c *Calcium) ControlContainer(ctx context.Context, IDs []string, t string,
8982

9083
func (c *Calcium) doStartContainer(ctx context.Context, container *types.Container, containerInfo *enginetypes.VirtualizationInfo, force bool) ([]*bytes.Buffer, error) {
9184
var message []*bytes.Buffer
85+
if containerInfo.Running {
86+
message = append(message, bytes.NewBufferString("container already running, can't run hook\n"))
87+
return message, nil
88+
}
9289
var err error
9390

9491
startCtx, cancel := context.WithTimeout(ctx, c.config.GlobalTimeout)
@@ -100,8 +97,8 @@ func (c *Calcium) doStartContainer(ctx context.Context, container *types.Contain
10097
if container.Hook != nil && len(container.Hook.AfterStart) > 0 {
10198
message, err = c.doHook(
10299
ctx,
103-
container.ID, containerInfo.User,
104-
container.Hook.AfterStart, containerInfo.Env,
100+
container.ID, container.User,
101+
container.Hook.AfterStart, container.Env,
105102
container.Hook.Force, container.Privileged,
106103
force, container.Engine,
107104
)
@@ -111,13 +108,17 @@ func (c *Calcium) doStartContainer(ctx context.Context, container *types.Contain
111108

112109
func (c *Calcium) doStopContainer(ctx context.Context, container *types.Container, containerInfo *enginetypes.VirtualizationInfo, force bool) ([]*bytes.Buffer, error) {
113110
var message []*bytes.Buffer
111+
if !containerInfo.Running {
112+
message = append(message, bytes.NewBufferString("container stopped, can't run hook\n"))
113+
return message, nil
114+
}
114115
var err error
115116

116117
if container.Hook != nil && len(container.Hook.BeforeStop) > 0 {
117118
message, err = c.doHook(
118119
ctx,
119-
container.ID, containerInfo.User,
120-
container.Hook.BeforeStop, containerInfo.Env,
120+
container.ID, container.User,
121+
container.Hook.BeforeStop, container.Env,
121122
container.Hook.Force, container.Privileged,
122123
force, container.Engine,
123124
)

cluster/calcium/control_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,10 @@ func TestControlStop(t *testing.T) {
132132
store.On("GetContainer", mock.Anything, mock.Anything).Return(container, nil)
133133
engine.On("VirtualizationInspect", mock.Anything, mock.Anything).Return(
134134
&enginetypes.VirtualizationInfo{
135-
User: "someuser",
136-
Env: []string{"a=b"},
137-
Image: "testimage",
135+
User: "someuser",
136+
Env: []string{"a=b"},
137+
Image: "testimage",
138+
Running: true,
138139
}, nil)
139140
// failed, hook true, remove always false
140141
ch, err := c.ControlContainer(ctx, []string{"id1"}, cluster.ContainerStop, false)

cluster/calcium/create.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ func (c *Calcium) doCreateAndStartContainer(
151151
Privileged: opts.Entrypoint.Privileged,
152152
Engine: node.Engine,
153153
SoftLimit: opts.SoftLimit,
154+
Image: opts.Image,
154155
}
155156
createContainerMessage := &types.CreateContainerMessage{
156157
Podname: container.Podname,
@@ -187,11 +188,6 @@ func (c *Calcium) doCreateAndStartContainer(
187188
container.ID = containerCreated.ID
188189
createContainerMessage.ContainerID = containerCreated.ID
189190

190-
if err = c.store.AddContainer(ctx, container); err != nil {
191-
createContainerMessage.Error = err
192-
return createContainerMessage
193-
}
194-
195191
// Copy data to container
196192
if len(opts.Data) > 0 {
197193
for dst, src := range opts.Data {
@@ -214,22 +210,30 @@ func (c *Calcium) doCreateAndStartContainer(
214210
}
215211
}
216212

217-
createContainerMessage.Hook, err = c.doStartContainer(ctx, container, &enginetypes.VirtualizationInfo{User: opts.User, Env: opts.Env}, opts.IgnoreHook)
213+
containerInfo, err := container.Inspect(ctx)
218214
if err != nil {
219215
createContainerMessage.Error = err
220216
return createContainerMessage
221217
}
222-
container.Hook = hook
218+
container.User = containerInfo.User
219+
container.Labels = containerInfo.Labels
220+
container.Env = containerInfo.Env
223221

224-
containerAlived, err := container.Inspect(ctx)
222+
createContainerMessage.Hook, err = c.doStartContainer(ctx, container, containerInfo, opts.IgnoreHook)
225223
if err != nil {
226224
createContainerMessage.Error = err
227225
return createContainerMessage
228226
}
227+
container.Hook = hook
229228

230229
// get ips
231-
if containerAlived.Networks != nil {
232-
createContainerMessage.Publish = utils.MakePublishInfo(containerAlived.Networks, opts.Entrypoint.Publish)
230+
if containerInfo.Networks != nil {
231+
createContainerMessage.Publish = utils.MakePublishInfo(containerInfo.Networks, opts.Entrypoint.Publish)
232+
}
233+
234+
if err = c.store.AddContainer(ctx, container); err != nil {
235+
createContainerMessage.Error = err
236+
return createContainerMessage
233237
}
234238

235239
createContainerMessage.Success = true

cluster/calcium/replace.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (c *Calcium) ReplaceContainer(ctx context.Context, opts *types.ReplaceOptio
4747
)
4848
}
4949
// make sure container exists
50-
containerJSON, err := container.Inspect(ctx)
50+
containerInfo, err := container.Inspect(ctx)
5151
if err != nil {
5252
return err
5353
}
@@ -61,17 +61,17 @@ func (c *Calcium) ReplaceContainer(ctx context.Context, opts *types.ReplaceOptio
6161
replaceOpts.Podname = container.Podname
6262
// 继承网络配置
6363
if replaceOpts.NetworkInherit {
64-
if !containerJSON.Running {
64+
if !containerInfo.Running {
6565
return types.NewDetailedErr(types.ErrNotSupport,
6666
fmt.Sprintf("container %s not running, can not inherit", container.ID),
6767
)
6868
}
69-
log.Infof("[ReplaceContainer] Inherit old container network configuration mode %v", containerJSON.Networks)
69+
log.Infof("[ReplaceContainer] Inherit old container network configuration mode %v", containerInfo.Networks)
7070
replaceOpts.NetworkMode = ""
71-
replaceOpts.Networks = containerJSON.Networks
71+
replaceOpts.Networks = containerInfo.Networks
7272
}
7373

74-
createMessage, removeMessage, err = c.doReplaceContainer(ctx, container, containerJSON, &replaceOpts, index)
74+
createMessage, removeMessage, err = c.doReplaceContainer(ctx, container, containerInfo, &replaceOpts, index)
7575
return err
7676
}); err != nil {
7777
log.Errorf("[ReplaceContainer] Replace and remove failed %v, old container restarted", err)
@@ -93,7 +93,7 @@ func (c *Calcium) ReplaceContainer(ctx context.Context, opts *types.ReplaceOptio
9393
func (c *Calcium) doReplaceContainer(
9494
ctx context.Context,
9595
container *types.Container,
96-
containerJSON *enginetypes.VirtualizationInfo,
96+
containerInfo *enginetypes.VirtualizationInfo,
9797
opts *types.ReplaceOptions,
9898
index int,
9999
) (*types.CreateContainerMessage, *types.RemoveContainerMessage, error) {
@@ -103,7 +103,7 @@ func (c *Calcium) doReplaceContainer(
103103
Hook: []*bytes.Buffer{},
104104
}
105105
// label filter
106-
if !utils.FilterContainer(containerJSON.Labels, opts.FilterLabels) {
106+
if !utils.FilterContainer(containerInfo.Labels, opts.FilterLabels) {
107107
return nil, removeMessage, types.ErrNotFitLabels
108108
}
109109
// get node
@@ -128,7 +128,7 @@ func (c *Calcium) doReplaceContainer(
128128
opts.DeployOptions.Data[dst] = fname
129129
}
130130
// 停止容器
131-
removeMessage.Hook, err = c.doStopContainer(ctx, container, containerJSON, opts.IgnoreHook)
131+
removeMessage.Hook, err = c.doStopContainer(ctx, container, containerInfo, opts.IgnoreHook)
132132
if err != nil {
133133
return nil, removeMessage, err
134134
}
@@ -137,8 +137,7 @@ func (c *Calcium) doReplaceContainer(
137137
createMessage := c.doCreateAndStartContainer(ctx, index, node, &opts.DeployOptions, container.CPU)
138138
if createMessage.Error != nil {
139139
// 重启老容器
140-
// TODO consider this force?
141-
message, err := c.doStartContainer(ctx, container, containerJSON, opts.IgnoreHook)
140+
message, err := c.doStartContainer(ctx, container, containerInfo, opts.IgnoreHook)
142141
removeMessage.Hook = append(removeMessage.Hook, message...)
143142
if err != nil {
144143
log.Errorf("[replaceAndRemove] Old container %s restart failed %v", container.ID, err)

rpc/transform.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,11 @@ func toRPCContainer(ctx context.Context, c *types.Container) (*pb.Container, err
396396
log.Errorf("[toRPCContainer] Inspect container %s failed %v", c.ID, err)
397397
}
398398

399+
// 如果不需要网络信息这里可以干掉
399400
publish := map[string]string{}
400401
inspectData := []byte{}
401-
image := ""
402-
labels := map[string]string{}
403402
if verification {
404-
meta := utils.DecodeMetaInLabel(info.Labels)
403+
meta := utils.DecodeMetaInLabel(c.Labels)
405404
if info.Networks != nil && info.Running {
406405
publish = utils.EncodePublishInfo(
407406
utils.MakePublishInfo(info.Networks, meta.Publish),
@@ -411,9 +410,6 @@ func toRPCContainer(ctx context.Context, c *types.Container) (*pb.Container, err
411410
if inspectData, err = json.Marshal(info); err != nil {
412411
return nil, err
413412
}
414-
415-
image = info.Image
416-
labels = info.Labels
417413
}
418414

419415
cpu := toRPCCPUMap(c.CPU)
@@ -429,8 +425,8 @@ func toRPCContainer(ctx context.Context, c *types.Container) (*pb.Container, err
429425
Storage: c.Storage,
430426
Privileged: c.Privileged,
431427
Publish: publish,
432-
Image: image,
433-
Labels: labels,
428+
Image: c.Image,
429+
Labels: c.Labels,
434430
Inspect: inspectData,
435431
StatusData: c.StatusData,
436432
Verification: verification,

types/container.go

+17-13
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,23 @@ import (
1212
// only relationship with pod and node is stored
1313
// if you wanna get realtime information, use Inspect method
1414
type Container struct {
15-
ID string `json:"id"`
16-
Podname string `json:"podname"`
17-
Nodename string `json:"nodename"`
18-
Name string `json:"name"`
19-
CPU CPUMap `json:"cpu"`
20-
Quota float64 `json:"quota"`
21-
Memory int64 `json:"memory"`
22-
Storage int64 `json:"storage"`
23-
Hook *Hook `json:"hook"`
24-
Privileged bool `json:"privileged"`
25-
SoftLimit bool `json:"softlimit"`
26-
StatusData []byte `json:"-"`
27-
Engine engine.API `json:"-"`
15+
ID string `json:"id"`
16+
Podname string `json:"podname"`
17+
Nodename string `json:"nodename"`
18+
Name string `json:"name"`
19+
CPU CPUMap `json:"cpu"`
20+
Quota float64 `json:"quota"`
21+
Memory int64 `json:"memory"`
22+
Storage int64 `json:"storage"`
23+
Hook *Hook `json:"hook"`
24+
Privileged bool `json:"privileged"`
25+
SoftLimit bool `json:"softlimit"`
26+
User string `json:"user"`
27+
Labels map[string]string `json:"labels"`
28+
Env []string `json:"env"`
29+
Image string `json:"image"`
30+
StatusData []byte `json:"-"`
31+
Engine engine.API `json:"-"`
2832
}
2933

3034
// Inspect a container

0 commit comments

Comments
 (0)