Skip to content

Commit bf02989

Browse files
committed
fix: update container state according to containerd events
Signed-off-by: zhuangqh <[email protected]>
1 parent 6bd339f commit bf02989

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

daemon/mgr/container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func NewContainerManager(ctx context.Context, store *meta.Store, cli ctrd.APICli
221221

222222
mgr.Client.SetExitHooks(mgr.exitedAndRelease)
223223
mgr.Client.SetExecExitHooks(mgr.execExitedAndRelease)
224-
mgr.Client.SetEventsHooks(mgr.publishContainerdEvent)
224+
mgr.Client.SetEventsHooks(mgr.publishContainerdEvent, mgr.updateContainerState)
225225

226226
go mgr.execProcessGC()
227227

daemon/mgr/container_state.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ func (c *Container) SetStatusUnpaused() {
140140
c.setStatusFlags(types.StatusRunning)
141141
}
142142

143+
// SetStatusOOM sets a container to be status exit because of OOM.
144+
func (c *Container) SetStatusOOM() {
145+
c.Lock()
146+
defer c.Unlock()
147+
c.State.OOMKilled = true
148+
c.State.Status = types.StatusExited
149+
c.State.Pid = 0
150+
c.State.ExitCode = 137
151+
c.State.Error = "OOMKilled"
152+
c.setStatusFlags(types.StatusExited)
153+
}
154+
143155
// Notes(ziren): i still feel uncomfortable for a function hasing no return
144156
// setStatusFlags set the specified status flag to true, and unset others
145157
func (c *Container) setStatusFlags(status types.Status) {

daemon/mgr/events.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package mgr
22

33
import (
44
"context"
5+
"strconv"
56
"strings"
67

78
"github.com/alibaba/pouch/apis/types"
9+
810
"github.com/docker/libnetwork"
11+
"github.com/sirupsen/logrus"
912
)
1013

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

110113
return nil
111114
}
115+
116+
// updateContainerState update container's state according to the containerd events.
117+
func (mgr *ContainerManager) updateContainerState(ctx context.Context, id, action string, attributes map[string]string) error {
118+
c, err := mgr.container(id)
119+
if err != nil {
120+
return err
121+
}
122+
123+
dirty := true
124+
switch action {
125+
case "die":
126+
exitCode, err := strconv.ParseInt(attributes["exitCode"], 10, 64)
127+
if err != nil {
128+
logrus.Warnf("failed to parse exitCode: %v", err)
129+
}
130+
c.SetStatusExited(exitCode, "")
131+
case "oom":
132+
c.SetStatusOOM()
133+
default:
134+
dirty = false
135+
}
136+
137+
if dirty {
138+
if err := mgr.Store.Put(c); err != nil {
139+
return err
140+
}
141+
}
142+
143+
return nil
144+
}

0 commit comments

Comments
 (0)