/
container_state.go
137 lines (119 loc) · 3.88 KB
/
container_state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package mgr
import (
"time"
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/pkg/utils"
)
// IsRunning returns container is running or not.
func (c *Container) IsRunning() bool {
return c.State.Running
}
// IsRunningOrPaused returns true of container is running or paused.
func (c *Container) IsRunningOrPaused() bool {
return c.State.Running || c.State.Paused
}
// ExitCode returns container's ExitCode.
func (c *Container) ExitCode() int64 {
return c.State.ExitCode
}
// IsCreated returns container is created or not.
func (c *Container) IsCreated() bool {
return c.State.Status == types.StatusCreated
}
// IsRemoving returns container is removing or not.
// TODO: actually the pouchd do not set removing status for a container.
func (c *Container) IsRemoving() bool {
return c.State.Status == types.StatusRemoving
}
// SetStatusRunning sets a container to be status running.
// When a container's status turns to StatusStopped, the following fields need updated:
// Status -> StatusRunning
// StartAt -> time.Now()
// Pid -> input param
// ExitCode -> 0
func (c *Container) SetStatusRunning(pid int64) {
c.State.Status = types.StatusRunning
c.State.StartedAt = time.Now().UTC().Format(utils.TimeLayout)
c.State.Pid = pid
c.State.ExitCode = 0
c.setStatusFlags(types.StatusRunning)
}
// SetStatusStopped sets a container to be status stopped.
// When a container's status turns to StatusStopped, the following fields need updated:
// Status -> StatusStopped
// FinishedAt -> time.Now()
// Pid -> 0
// ExitCode -> input param
// Error -> input param
func (c *Container) SetStatusStopped(exitCode int64, errMsg string) {
c.State.Status = types.StatusStopped
c.State.FinishedAt = time.Now().UTC().Format(utils.TimeLayout)
c.State.Pid = 0
c.State.ExitCode = exitCode
c.State.Error = errMsg
c.setStatusFlags(types.StatusStopped)
}
// SetStatusExited sets a container to be status exited.
func (c *Container) SetStatusExited(exitCode int64, errMsg string) {
c.State.Status = types.StatusExited
c.State.FinishedAt = time.Now().UTC().Format(utils.TimeLayout)
c.State.Pid = 0
c.State.ExitCode = exitCode
c.State.Error = errMsg
c.setStatusFlags(types.StatusExited)
}
// SetStatusPaused sets a container to be status paused.
func (c *Container) SetStatusPaused() {
c.State.Status = types.StatusPaused
c.setStatusFlags(types.StatusPaused)
}
// SetStatusUnpaused sets a container to be status running.
// Unpaused is treated running.
func (c *Container) SetStatusUnpaused() {
c.State.Status = types.StatusRunning
c.setStatusFlags(types.StatusRunning)
}
// SetStatusDead sets a container to be status dead.
func (c *Container) SetStatusDead() {
c.State.Status = types.StatusDead
c.setStatusFlags(types.StatusDead)
}
// IsDead returns container is dead or not.
// NOTE: ContainerMgmt.Remove action will set Dead to container's meta config
// before removing the meta config json file.
func (c *Container) IsDead() bool {
return c.State.Status == types.StatusDead
}
// SetStatusOOM sets a container to be status exit because of OOM.
func (c *Container) SetStatusOOM() {
c.State.OOMKilled = true
c.State.Error = "OOMKilled"
}
// 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) {
statusFlags := map[types.Status]bool{
types.StatusDead: false,
types.StatusRunning: false,
types.StatusPaused: false,
types.StatusRestarting: false,
types.StatusExited: false,
}
if _, exists := statusFlags[status]; exists {
statusFlags[status] = true
}
for k, v := range statusFlags {
switch k {
case types.StatusDead:
c.State.Dead = v
case types.StatusPaused:
c.State.Paused = v
case types.StatusRunning:
c.State.Running = v
case types.StatusRestarting:
c.State.Restarting = v
case types.StatusExited:
c.State.Exited = v
}
}
}