Skip to content

Commit 3104fc1

Browse files
committed
Merge branch 'refactor/log' into 'master'
put error log inside functions, instead of printing outside See merge request !64
2 parents f591414 + 6d6197f commit 3104fc1

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.7.20
1+
0.7.21

cluster/calcium/create_container.go

+20-19
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ func (c *calcium) createContainerWithCPUPeriod(specs types.Specs, opts *types.De
4040

4141
cpuandmem, _, err := c.getCPUAndMem(opts.Podname, opts.Nodename, 1.0)
4242
if err != nil {
43-
log.Errorf("Got error %v after getCPUAndMem", err)
4443
return ch, err
4544
}
4645
go utils.SendMemCap(cpuandmem, "before-alloc")
@@ -100,7 +99,6 @@ func (c *calcium) doCreateContainerWithCPUPeriod(nodename string, connum int, qu
10099
ms[i].Nodename = node.Name
101100
ms[i].Memory = opts.Memory
102101
if err != nil {
103-
log.Errorf("Error when creating CreateContainerOptions, %v", err)
104102
c.store.UpdateNodeMem(opts.Podname, nodename, opts.Memory, "+") // 创建容器失败就要把资源还回去对不对?
105103
ms[i].Error = err.Error()
106104
continue
@@ -109,7 +107,7 @@ func (c *calcium) doCreateContainerWithCPUPeriod(nodename string, connum int, qu
109107
//create container
110108
container, err := node.Engine.ContainerCreate(context.Background(), config, hostConfig, networkConfig, containerName)
111109
if err != nil {
112-
log.Errorf("Error when creating container, %v", err)
110+
log.Errorf("Error during ContainerCreate, %v", err)
113111
ms[i].Error = err.Error()
114112
c.store.UpdateNodeMem(opts.Podname, nodename, opts.Memory, "+")
115113
continue
@@ -125,7 +123,7 @@ func (c *calcium) doCreateContainerWithCPUPeriod(nodename string, connum int, qu
125123
for networkID, ipv4 := range opts.Networks {
126124
if err = c.network.ConnectToNetwork(ctx, container.ID, networkID, ipv4); err != nil {
127125
c.store.UpdateNodeMem(opts.Podname, nodename, opts.Memory, "+")
128-
log.Errorf("Error when connecting container %q to network %q, %q", container.ID, networkID, err.Error())
126+
log.Errorf("Error during connecting container %q to network %q, %v", container.ID, networkID, err)
129127
breaked = true
130128
break
131129
}
@@ -135,7 +133,7 @@ func (c *calcium) doCreateContainerWithCPUPeriod(nodename string, connum int, qu
135133
// only when user defined networks is given
136134
if len(opts.Networks) != 0 {
137135
if err := c.network.DisconnectFromNetwork(ctx, container.ID, "bridge"); err != nil {
138-
log.Errorf("Error when disconnecting container %q from network %q, %q", container.ID, "bridge", err.Error())
136+
log.Errorf("Error during disconnecting container %q from network %q, %v", container.ID, "bridge", err)
139137
}
140138
}
141139

@@ -150,7 +148,7 @@ func (c *calcium) doCreateContainerWithCPUPeriod(nodename string, connum int, qu
150148

151149
err = node.Engine.ContainerStart(context.Background(), container.ID, enginetypes.ContainerStartOptions{})
152150
if err != nil {
153-
log.Errorf("Error when starting container, %v", err)
151+
log.Errorf("Error during ContainerStart, %v", err)
154152
ms[i].Error = err.Error()
155153
c.store.UpdateNodeMem(opts.Podname, nodename, opts.Memory, "+")
156154
go node.Engine.ContainerRemove(context.Background(), container.ID, enginetypes.ContainerRemoveOptions{})
@@ -163,7 +161,7 @@ func (c *calcium) doCreateContainerWithCPUPeriod(nodename string, connum int, qu
163161

164162
info, err := node.Engine.ContainerInspect(context.Background(), container.ID)
165163
if err != nil {
166-
log.Errorf("Error when inspecting container, %v", err)
164+
log.Errorf("Error during ContainerInspect, %v", err)
167165
ms[i].Error = err.Error()
168166
c.store.UpdateNodeMem(opts.Podname, nodename, opts.Memory, "+")
169167
continue
@@ -172,7 +170,7 @@ func (c *calcium) doCreateContainerWithCPUPeriod(nodename string, connum int, qu
172170

173171
// after start
174172
if err := runExec(node.Engine, info, AFTER_START); err != nil {
175-
log.Errorf("Run exec at %s error: %s", AFTER_START, err.Error())
173+
log.Errorf("Run exec at %s error: %v", AFTER_START, err)
176174
}
177175

178176
_, err = c.store.AddContainer(info.ID, opts.Podname, node.Name, containerName, nil, opts.Memory)
@@ -189,7 +187,6 @@ func (c *calcium) doCreateContainerWithCPUPeriod(nodename string, connum int, qu
189187
go func(podname string, nodename string) {
190188
cpuandmem, _, err := c.getCPUAndMem(podname, nodename, 1.0)
191189
if err != nil {
192-
log.Errorf("Got error %v after getCPUAndMem", err)
193190
return
194191
}
195192
utils.SendMemCap(cpuandmem, "after-alloc")
@@ -289,7 +286,9 @@ func (c *calcium) getCPUAndMem(podname, nodename string, quota float64) (map[str
289286
}
290287

291288
if len(nodes) == 0 {
292-
return result, nil, fmt.Errorf("No available nodes")
289+
err := fmt.Errorf("No available nodes")
290+
log.Errorf("Error during getCPUAndMem: %v", err)
291+
return result, nil, err
293292
}
294293

295294
result = makeCPUAndMem(nodes)
@@ -349,15 +348,18 @@ func filterNodes(nodes []*types.Node, public bool) []*types.Node {
349348
// Pull an image
350349
// Blocks until it finishes.
351350
func pullImage(node *types.Node, image string) error {
351+
log.Debugf("Pulling image %s", image)
352352
if image == "" {
353-
return fmt.Errorf("No image found for version")
353+
return fmt.Errorf("Goddamn empty image, WTF?")
354354
}
355355

356356
resp, err := node.Engine.ImagePull(context.Background(), image, enginetypes.ImagePullOptions{})
357357
if err != nil {
358+
log.Errorf("Error during pulling image %s: %v", image, err)
358359
return err
359360
}
360361
ensureReaderClosed(resp)
362+
log.Debugf("Done pulling image %s", image)
361363
return nil
362364
}
363365

@@ -374,7 +376,6 @@ func (c *calcium) doCreateContainerWithScheduler(nodename string, cpumap []types
374376
}
375377

376378
if err := pullImage(node, opts.Image); err != nil {
377-
log.Errorf("Pull image error %v", err)
378379
return ms
379380
}
380381

@@ -386,7 +387,6 @@ func (c *calcium) doCreateContainerWithScheduler(nodename string, cpumap []types
386387
ms[i].Nodename = node.Name
387388
ms[i].Memory = opts.Memory
388389
if err != nil {
389-
log.Errorf("Error when creating CreateContainerOptions, %v", err)
390390
ms[i].Error = err.Error()
391391
c.releaseQuota(node, quota)
392392
continue
@@ -410,7 +410,7 @@ func (c *calcium) doCreateContainerWithScheduler(nodename string, cpumap []types
410410
// need to ensure all networks are correctly connected
411411
for networkID, ipv4 := range opts.Networks {
412412
if err = c.network.ConnectToNetwork(ctx, container.ID, networkID, ipv4); err != nil {
413-
log.Errorf("Error when connecting container %q to network %q, %q", container.ID, networkID, err.Error())
413+
log.Errorf("Error when connecting container %q to network %q, %v", container.ID, networkID, err)
414414
breaked = true
415415
break
416416
}
@@ -420,7 +420,7 @@ func (c *calcium) doCreateContainerWithScheduler(nodename string, cpumap []types
420420
// only when user defined networks is given
421421
if len(opts.Networks) != 0 {
422422
if err := c.network.DisconnectFromNetwork(ctx, container.ID, "bridge"); err != nil {
423-
log.Errorf("Error when disconnecting container %q from network %q, %q", container.ID, "bridge", err.Error())
423+
log.Errorf("Error when disconnecting container %q from network %q, %v", container.ID, "bridge", err)
424424
}
425425
}
426426

@@ -457,7 +457,7 @@ func (c *calcium) doCreateContainerWithScheduler(nodename string, cpumap []types
457457

458458
// after start
459459
if err := runExec(node.Engine, info, AFTER_START); err != nil {
460-
log.Errorf("Run exec at %s error: %s", AFTER_START, err.Error())
460+
log.Errorf("Run exec at %s error: %v", AFTER_START, err)
461461
}
462462

463463
_, err = c.store.AddContainer(info.ID, opts.Podname, node.Name, containerName, quota, opts.Memory)
@@ -492,7 +492,9 @@ func (c *calcium) makeContainerOptions(quota map[string]int, specs types.Specs,
492492

493493
entry, ok := specs.Entrypoints[opts.Entrypoint]
494494
if !ok {
495-
return nil, nil, nil, "", fmt.Errorf("Entrypoint %q not found in image %q", opts.Entrypoint, opts.Image)
495+
err := fmt.Errorf("Entrypoint %q not found in image %q", opts.Entrypoint, opts.Image)
496+
log.Errorf("Error during makeContainerOptions: %v", err)
497+
return nil, nil, nil, "", err
496498
}
497499

498500
user := specs.Appname
@@ -781,7 +783,6 @@ func (c *calcium) doUpgradeContainer(containers []*types.Container, image string
781783

782784
// prepare new image
783785
if err := pullImage(node, image); err != nil {
784-
log.Errorf("Pull image error %v", err)
785786
return ms
786787
}
787788

@@ -868,7 +869,7 @@ func (c *calcium) doUpgradeContainer(containers []*types.Container, image string
868869

869870
// after start
870871
if err := runExec(engine, newInfo, AFTER_START); err != nil {
871-
log.Errorf("Run exec at %s error: %s", AFTER_START, err.Error())
872+
log.Errorf("Run exec at %s error: %v", AFTER_START, err)
872873
}
873874

874875
// if so, add a new container in etcd

cluster/calcium/helper.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"strings"
99

10+
log "github.com/Sirupsen/logrus"
1011
enginetypes "github.com/docker/docker/api/types"
1112
enginecontainer "github.com/docker/docker/api/types/container"
1213
enginenetwork "github.com/docker/docker/api/types/network"
@@ -139,13 +140,15 @@ func makeMountPaths(specs types.Specs, config types.Config) ([]string, map[strin
139140
func runExec(client *engineapi.Client, container enginetypes.ContainerJSON, label string) error {
140141
cmd, ok := container.Config.Labels[label]
141142
if !ok || cmd == "" {
142-
return fmt.Errorf("No %q found in container %q", label, container.ID)
143+
log.Debug("No %q found in container %q", label, container.ID)
144+
return nil
143145
}
144146

145147
cmds := utils.MakeCommandLineArgs(cmd)
146148
execConfig := enginetypes.ExecConfig{User: container.Config.User, Cmd: cmds}
147149
resp, err := client.ContainerExecCreate(context.Background(), container.ID, execConfig)
148150
if err != nil {
151+
log.Errorf("Error during runExec: %v", err)
149152
return err
150153
}
151154

0 commit comments

Comments
 (0)