Merge pull request #99585 from Iceber/fix-finished-at

dockershim: fix started and finished timestamp of the container status
This commit is contained in:
Kubernetes Prow Robot 2021-03-08 20:47:28 -08:00 committed by GitHub
commit 40d8aed6f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 12 deletions

View File

@ -387,12 +387,15 @@ func (ds *dockerService) ContainerStatus(_ context.Context, req *runtimeapi.Cont
// Note: Can't set SeLinuxRelabel // Note: Can't set SeLinuxRelabel
}) })
} }
// Interpret container states. // Interpret container states and convert time to unix timestamps.
var state runtimeapi.ContainerState var state runtimeapi.ContainerState
var reason, message string var reason, message string
ct, st, ft := createdAt.UnixNano(), int64(0), int64(0)
if r.State.Running { if r.State.Running {
// Container is running. // Container is running.
state = runtimeapi.ContainerState_CONTAINER_RUNNING state = runtimeapi.ContainerState_CONTAINER_RUNNING
// If container is not in the exited state, not set finished timestamp
st = startedAt.UnixNano()
} else { } else {
// Container is *not* running. We need to get more details. // Container is *not* running. We need to get more details.
// * Case 1: container has run and exited with non-zero finishedAt // * Case 1: container has run and exited with non-zero finishedAt
@ -402,6 +405,7 @@ func (ds *dockerService) ContainerStatus(_ context.Context, req *runtimeapi.Cont
// * Case 3: container has been created, but not started (yet). // * Case 3: container has been created, but not started (yet).
if !finishedAt.IsZero() { // Case 1 if !finishedAt.IsZero() { // Case 1
state = runtimeapi.ContainerState_CONTAINER_EXITED state = runtimeapi.ContainerState_CONTAINER_EXITED
st, ft = startedAt.UnixNano(), finishedAt.UnixNano()
switch { switch {
case r.State.OOMKilled: case r.State.OOMKilled:
// TODO: consider exposing OOMKilled via the runtimeAPI. // TODO: consider exposing OOMKilled via the runtimeAPI.
@ -415,18 +419,15 @@ func (ds *dockerService) ContainerStatus(_ context.Context, req *runtimeapi.Cont
} }
} else if r.State.ExitCode != 0 { // Case 2 } else if r.State.ExitCode != 0 { // Case 2
state = runtimeapi.ContainerState_CONTAINER_EXITED state = runtimeapi.ContainerState_CONTAINER_EXITED
// Adjust finshedAt and startedAt time to createdAt time to avoid // Adjust finished and started timestamp to createdAt time to avoid
// the confusion. // the confusion.
finishedAt, startedAt = createdAt, createdAt st, ft = createdAt.UnixNano(), createdAt.UnixNano()
reason = "ContainerCannotRun" reason = "ContainerCannotRun"
} else { // Case 3 } else { // Case 3
state = runtimeapi.ContainerState_CONTAINER_CREATED state = runtimeapi.ContainerState_CONTAINER_CREATED
} }
message = r.State.Error message = r.State.Error
} }
// Convert to unix timestamps.
ct, st, ft := createdAt.UnixNano(), startedAt.UnixNano(), finishedAt.UnixNano()
exitCode := int32(r.State.ExitCode) exitCode := int32(r.State.ExitCode)
metadata, err := parseContainerName(r.Name) metadata, err := parseContainerName(r.Name)

View File

@ -182,9 +182,6 @@ func TestContainerStatus(t *testing.T) {
imageName := "iamimage" imageName := "iamimage"
config := makeContainerConfig(sConfig, "pause", imageName, 0, labels, annotations) config := makeContainerConfig(sConfig, "pause", imageName, 0, labels, annotations)
var defaultTime time.Time
dt := defaultTime.UnixNano()
ct, st, ft := dt, dt, dt
state := runtimeapi.ContainerState_CONTAINER_CREATED state := runtimeapi.ContainerState_CONTAINER_CREATED
imageRef := DockerImageIDPrefix + imageName imageRef := DockerImageIDPrefix + imageName
// The following variables are not set in FakeDockerClient. // The following variables are not set in FakeDockerClient.
@ -193,9 +190,6 @@ func TestContainerStatus(t *testing.T) {
expected := &runtimeapi.ContainerStatus{ expected := &runtimeapi.ContainerStatus{
State: state, State: state,
CreatedAt: ct,
StartedAt: st,
FinishedAt: ft,
Metadata: config.Metadata, Metadata: config.Metadata,
Image: config.Image, Image: config.Image,
ImageRef: imageRef, ImageRef: imageRef,