Skip to content

Commit

Permalink
fix: update container state according to containerd events
Browse files Browse the repository at this point in the history
Signed-off-by: zhuangqh <zhuangqhc@gmail.com>
  • Loading branch information
zhuangqh authored and fuweid committed Jan 29, 2019
1 parent 8b2687d commit 42992ba
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func NewContainerManager(ctx context.Context, store *meta.Store, cli ctrd.APICli

mgr.Client.SetExitHooks(mgr.exitedAndRelease)
mgr.Client.SetExecExitHooks(mgr.execExitedAndRelease)
mgr.Client.SetEventsHooks(mgr.publishContainerdEvent)
mgr.Client.SetEventsHooks(mgr.publishContainerdEvent, mgr.updateContainerState)

go mgr.execProcessGC()

Expand Down
12 changes: 12 additions & 0 deletions daemon/mgr/container_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ func (c *Container) SetStatusUnpaused() {
c.setStatusFlags(types.StatusRunning)
}

// SetStatusOOM sets a container to be status exit because of OOM.
func (c *Container) SetStatusOOM() {
c.Lock()
defer c.Unlock()
c.State.OOMKilled = true
c.State.Status = types.StatusExited
c.State.Pid = 0
c.State.ExitCode = 137
c.State.Error = "OOMKilled"
c.setStatusFlags(types.StatusExited)
}

// Notes(ziren): i still feel uncomfortable for a function hasing no return
// setStatusFlags set the specified status flag to true, and unset others
func (c *Container) setStatusFlags(status types.Status) {
Expand Down
33 changes: 33 additions & 0 deletions daemon/mgr/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package mgr

import (
"context"
"strconv"
"strings"

"github.com/alibaba/pouch/apis/types"

"github.com/docker/libnetwork"
"github.com/sirupsen/logrus"
)

// LogContainerEvent generates an event related to a container with only the default attributes.
Expand Down Expand Up @@ -109,3 +112,33 @@ func (mgr *ContainerManager) publishContainerdEvent(ctx context.Context, id, act

return nil
}

// updateContainerState update container's state according to the containerd events.
func (mgr *ContainerManager) updateContainerState(ctx context.Context, id, action string, attributes map[string]string) error {
c, err := mgr.container(id)
if err != nil {
return err
}

dirty := true
switch action {
case "die":
exitCode, err := strconv.ParseInt(attributes["exitCode"], 10, 64)
if err != nil {
logrus.Warnf("failed to parse exitCode: %v", err)
}
c.SetStatusExited(exitCode, "")
case "oom":
c.SetStatusOOM()
default:
dirty = false
}

if dirty {
if err := mgr.Store.Put(c); err != nil {
return err
}
}

return nil
}

0 comments on commit 42992ba

Please sign in to comment.