Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update container state according to containerd events #2680

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}