Skip to content

Commit

Permalink
Merge branch 'refactor/log' into 'master'
Browse files Browse the repository at this point in the history
put error log inside functions, instead of printing outside

See merge request !64
  • Loading branch information
timfeirg committed Mar 6, 2017
2 parents f591414 + 6d6197f commit 3104fc1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.7.20
0.7.21
39 changes: 20 additions & 19 deletions cluster/calcium/create_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func (c *calcium) createContainerWithCPUPeriod(specs types.Specs, opts *types.De

cpuandmem, _, err := c.getCPUAndMem(opts.Podname, opts.Nodename, 1.0)
if err != nil {
log.Errorf("Got error %v after getCPUAndMem", err)
return ch, err
}
go utils.SendMemCap(cpuandmem, "before-alloc")
Expand Down Expand Up @@ -100,7 +99,6 @@ func (c *calcium) doCreateContainerWithCPUPeriod(nodename string, connum int, qu
ms[i].Nodename = node.Name
ms[i].Memory = opts.Memory
if err != nil {
log.Errorf("Error when creating CreateContainerOptions, %v", err)
c.store.UpdateNodeMem(opts.Podname, nodename, opts.Memory, "+") // 创建容器失败就要把资源还回去对不对?
ms[i].Error = err.Error()
continue
Expand All @@ -109,7 +107,7 @@ func (c *calcium) doCreateContainerWithCPUPeriod(nodename string, connum int, qu
//create container
container, err := node.Engine.ContainerCreate(context.Background(), config, hostConfig, networkConfig, containerName)
if err != nil {
log.Errorf("Error when creating container, %v", err)
log.Errorf("Error during ContainerCreate, %v", err)
ms[i].Error = err.Error()
c.store.UpdateNodeMem(opts.Podname, nodename, opts.Memory, "+")
continue
Expand All @@ -125,7 +123,7 @@ func (c *calcium) doCreateContainerWithCPUPeriod(nodename string, connum int, qu
for networkID, ipv4 := range opts.Networks {
if err = c.network.ConnectToNetwork(ctx, container.ID, networkID, ipv4); err != nil {
c.store.UpdateNodeMem(opts.Podname, nodename, opts.Memory, "+")
log.Errorf("Error when connecting container %q to network %q, %q", container.ID, networkID, err.Error())
log.Errorf("Error during connecting container %q to network %q, %v", container.ID, networkID, err)
breaked = true
break
}
Expand All @@ -135,7 +133,7 @@ func (c *calcium) doCreateContainerWithCPUPeriod(nodename string, connum int, qu
// only when user defined networks is given
if len(opts.Networks) != 0 {
if err := c.network.DisconnectFromNetwork(ctx, container.ID, "bridge"); err != nil {
log.Errorf("Error when disconnecting container %q from network %q, %q", container.ID, "bridge", err.Error())
log.Errorf("Error during disconnecting container %q from network %q, %v", container.ID, "bridge", err)
}
}

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

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

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

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

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

if len(nodes) == 0 {
return result, nil, fmt.Errorf("No available nodes")
err := fmt.Errorf("No available nodes")
log.Errorf("Error during getCPUAndMem: %v", err)
return result, nil, err
}

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

resp, err := node.Engine.ImagePull(context.Background(), image, enginetypes.ImagePullOptions{})
if err != nil {
log.Errorf("Error during pulling image %s: %v", image, err)
return err
}
ensureReaderClosed(resp)
log.Debugf("Done pulling image %s", image)
return nil
}

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

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

Expand All @@ -386,7 +387,6 @@ func (c *calcium) doCreateContainerWithScheduler(nodename string, cpumap []types
ms[i].Nodename = node.Name
ms[i].Memory = opts.Memory
if err != nil {
log.Errorf("Error when creating CreateContainerOptions, %v", err)
ms[i].Error = err.Error()
c.releaseQuota(node, quota)
continue
Expand All @@ -410,7 +410,7 @@ func (c *calcium) doCreateContainerWithScheduler(nodename string, cpumap []types
// need to ensure all networks are correctly connected
for networkID, ipv4 := range opts.Networks {
if err = c.network.ConnectToNetwork(ctx, container.ID, networkID, ipv4); err != nil {
log.Errorf("Error when connecting container %q to network %q, %q", container.ID, networkID, err.Error())
log.Errorf("Error when connecting container %q to network %q, %v", container.ID, networkID, err)
breaked = true
break
}
Expand All @@ -420,7 +420,7 @@ func (c *calcium) doCreateContainerWithScheduler(nodename string, cpumap []types
// only when user defined networks is given
if len(opts.Networks) != 0 {
if err := c.network.DisconnectFromNetwork(ctx, container.ID, "bridge"); err != nil {
log.Errorf("Error when disconnecting container %q from network %q, %q", container.ID, "bridge", err.Error())
log.Errorf("Error when disconnecting container %q from network %q, %v", container.ID, "bridge", err)
}
}

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

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

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

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

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

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

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

// after start
if err := runExec(engine, newInfo, AFTER_START); err != nil {
log.Errorf("Run exec at %s error: %s", AFTER_START, err.Error())
log.Errorf("Run exec at %s error: %v", AFTER_START, err)
}

// if so, add a new container in etcd
Expand Down
5 changes: 4 additions & 1 deletion cluster/calcium/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"strings"

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

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

Expand Down

0 comments on commit 3104fc1

Please sign in to comment.