Skip to content

Commit 92ab1a6

Browse files
committed
cri: fix container status
Signed-off-by: Iceber Gu <[email protected]>
1 parent bb3fcf6 commit 92ab1a6

2 files changed

Lines changed: 45 additions & 8 deletions

File tree

pkg/cri/server/container_status.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,23 @@ func toCRIContainerStatus(container containerstore.Container, spec *runtime.Imag
9393
}
9494
}
9595

96+
// If container is in the created state, not set started and finished unix timestamps
97+
var st, ft int64
98+
switch status.State() {
99+
case runtime.ContainerState_CONTAINER_RUNNING:
100+
// If container is in the running state, set started unix timestamps
101+
st = status.StartedAt
102+
case runtime.ContainerState_CONTAINER_EXITED, runtime.ContainerState_CONTAINER_UNKNOWN:
103+
st, ft = status.StartedAt, status.FinishedAt
104+
}
105+
96106
return &runtime.ContainerStatus{
97107
Id: meta.ID,
98108
Metadata: meta.Config.GetMetadata(),
99109
State: status.State(),
100110
CreatedAt: status.CreatedAt,
101-
StartedAt: status.StartedAt,
102-
FinishedAt: status.FinishedAt,
111+
StartedAt: st,
112+
FinishedAt: ft,
103113
ExitCode: status.ExitCode,
104114
Image: spec,
105115
ImageRef: imageRef,

pkg/cri/server/container_status_test.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Sta
4747
}
4848

4949
createdAt := time.Now().UnixNano()
50-
startedAt := time.Now().UnixNano()
5150

5251
metadata := &containerstore.Metadata{
5352
ID: testID,
@@ -60,7 +59,6 @@ func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Sta
6059
status := &containerstore.Status{
6160
Pid: 1234,
6261
CreatedAt: createdAt,
63-
StartedAt: startedAt,
6462
}
6563
image := &imagestore.Image{
6664
ID: imageID,
@@ -72,9 +70,8 @@ func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Sta
7270
expected := &runtime.ContainerStatus{
7371
Id: testID,
7472
Metadata: config.GetMetadata(),
75-
State: runtime.ContainerState_CONTAINER_RUNNING,
73+
State: runtime.ContainerState_CONTAINER_CREATED,
7674
CreatedAt: createdAt,
77-
StartedAt: startedAt,
7875
Image: &runtime.ImageSpec{Image: "gcr.io/library/busybox:latest"},
7976
ImageRef: "gcr.io/library/busybox@sha256:e6693c20186f837fc393390135d8a598a96a833917917789d63766cab6c59582",
8077
Reason: completeExitReason,
@@ -89,17 +86,23 @@ func getContainerStatusTestData() (*containerstore.Metadata, *containerstore.Sta
8986

9087
func TestToCRIContainerStatus(t *testing.T) {
9188
for desc, test := range map[string]struct {
89+
startedAt int64
9290
finishedAt int64
9391
exitCode int32
9492
reason string
9593
message string
9694
expectedState runtime.ContainerState
9795
expectedReason string
9896
}{
97+
"container created": {
98+
expectedState: runtime.ContainerState_CONTAINER_CREATED,
99+
},
99100
"container running": {
101+
startedAt: time.Now().UnixNano(),
100102
expectedState: runtime.ContainerState_CONTAINER_RUNNING,
101103
},
102104
"container exited with reason": {
105+
startedAt: time.Now().UnixNano(),
103106
finishedAt: time.Now().UnixNano(),
104107
exitCode: 1,
105108
reason: "test-reason",
@@ -108,13 +111,15 @@ func TestToCRIContainerStatus(t *testing.T) {
108111
expectedReason: "test-reason",
109112
},
110113
"container exited with exit code 0 without reason": {
114+
startedAt: time.Now().UnixNano(),
111115
finishedAt: time.Now().UnixNano(),
112116
exitCode: 0,
113117
message: "test-message",
114118
expectedState: runtime.ContainerState_CONTAINER_EXITED,
115119
expectedReason: completeExitReason,
116120
},
117121
"container exited with non-zero exit code without reason": {
122+
startedAt: time.Now().UnixNano(),
118123
finishedAt: time.Now().UnixNano(),
119124
exitCode: 1,
120125
message: "test-message",
@@ -124,6 +129,7 @@ func TestToCRIContainerStatus(t *testing.T) {
124129
} {
125130
metadata, status, _, expected := getContainerStatusTestData()
126131
// Update status with test case.
132+
status.StartedAt = test.startedAt
127133
status.FinishedAt = test.finishedAt
128134
status.ExitCode = test.exitCode
129135
status.Reason = test.reason
@@ -134,11 +140,12 @@ func TestToCRIContainerStatus(t *testing.T) {
134140
)
135141
assert.NoError(t, err)
136142
// Set expectation based on test case.
137-
expected.State = test.expectedState
138143
expected.Reason = test.expectedReason
144+
expected.StartedAt = test.startedAt
139145
expected.FinishedAt = test.finishedAt
140146
expected.ExitCode = test.exitCode
141147
expected.Message = test.message
148+
patchExceptedWithState(expected, test.expectedState)
142149
containerStatus := toCRIContainerStatus(container,
143150
expected.Image,
144151
expected.ImageRef)
@@ -166,19 +173,27 @@ func TestContainerStatus(t *testing.T) {
166173
for desc, test := range map[string]struct {
167174
exist bool
168175
imageExist bool
176+
startedAt int64
169177
finishedAt int64
170178
reason string
171179
expectedState runtime.ContainerState
172180
expectErr bool
173181
}{
182+
"container created": {
183+
exist: true,
184+
imageExist: true,
185+
expectedState: runtime.ContainerState_CONTAINER_CREATED,
186+
},
174187
"container running": {
175188
exist: true,
176189
imageExist: true,
190+
startedAt: time.Now().UnixNano(),
177191
expectedState: runtime.ContainerState_CONTAINER_RUNNING,
178192
},
179193
"container exited": {
180194
exist: true,
181195
imageExist: true,
196+
startedAt: time.Now().UnixNano(),
182197
finishedAt: time.Now().UnixNano(),
183198
reason: "test-reason",
184199
expectedState: runtime.ContainerState_CONTAINER_EXITED,
@@ -198,6 +213,7 @@ func TestContainerStatus(t *testing.T) {
198213
c := newTestCRIService()
199214
metadata, status, image, expected := getContainerStatusTestData()
200215
// Update status with test case.
216+
status.StartedAt = test.startedAt
201217
status.FinishedAt = test.finishedAt
202218
status.Reason = test.reason
203219
container, err := containerstore.NewContainer(
@@ -219,9 +235,20 @@ func TestContainerStatus(t *testing.T) {
219235
continue
220236
}
221237
// Set expectation based on test case.
238+
expected.StartedAt = test.startedAt
222239
expected.FinishedAt = test.finishedAt
223240
expected.Reason = test.reason
224-
expected.State = test.expectedState
241+
patchExceptedWithState(expected, test.expectedState)
225242
assert.Equal(t, expected, resp.GetStatus())
226243
}
227244
}
245+
246+
func patchExceptedWithState(expected *runtime.ContainerStatus, state runtime.ContainerState) {
247+
expected.State = state
248+
switch state {
249+
case runtime.ContainerState_CONTAINER_CREATED:
250+
expected.StartedAt, expected.FinishedAt = 0, 0
251+
case runtime.ContainerState_CONTAINER_RUNNING:
252+
expected.FinishedAt = 0
253+
}
254+
}

0 commit comments

Comments
 (0)