forked from cri-o/cri-o
/
container_status.go
110 lines (98 loc) · 2.78 KB
/
container_status.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
package server
import (
"strconv"
"time"
"github.com/cri-o/cri-o/oci"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
)
const (
oomKilledReason = "OOMKilled"
completedReason = "Completed"
errorReason = "Error"
)
// ContainerStatus returns status of the container.
func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusRequest) (resp *pb.ContainerStatusResponse, err error) {
const operation = "container_status"
defer func() {
recordOperation(operation, time.Now())
recordError(operation, err)
}()
logrus.Debugf("ContainerStatusRequest %+v", req)
c, err := s.GetContainerFromShortID(req.ContainerId)
if err != nil {
return nil, err
}
containerID := c.ID()
resp = &pb.ContainerStatusResponse{
Status: &pb.ContainerStatus{
Id: containerID,
Metadata: c.Metadata(),
Labels: c.Labels(),
Annotations: c.Annotations(),
ImageRef: c.ImageRef(),
Image: &pb.ImageSpec{
Image: c.ImageName(),
},
},
}
mounts := []*pb.Mount{}
for _, cv := range c.Volumes() {
mounts = append(mounts, &pb.Mount{
ContainerPath: cv.ContainerPath,
HostPath: cv.HostPath,
Readonly: cv.Readonly,
})
}
resp.Status.Mounts = mounts
cState := c.State()
rStatus := pb.ContainerState_CONTAINER_UNKNOWN
// If we defaulted to exit code -1 earlier then we attempt to
// get the exit code from the exit file again.
if cState.ExitCode == -1 {
err := s.Runtime().UpdateContainerStatus(c)
if err != nil {
logrus.Warnf("Failed to UpdateStatus of container %s: %v", c.ID(), err)
}
cState = c.State()
}
created := c.CreatedAt().UnixNano()
switch cState.Status {
case oci.ContainerStateCreated:
rStatus = pb.ContainerState_CONTAINER_CREATED
resp.Status.CreatedAt = created
case oci.ContainerStateRunning:
rStatus = pb.ContainerState_CONTAINER_RUNNING
resp.Status.CreatedAt = created
started := cState.Started.UnixNano()
resp.Status.StartedAt = started
case oci.ContainerStateStopped:
rStatus = pb.ContainerState_CONTAINER_EXITED
resp.Status.CreatedAt = created
started := cState.Started.UnixNano()
resp.Status.StartedAt = started
finished := cState.Finished.UnixNano()
resp.Status.FinishedAt = finished
resp.Status.ExitCode = cState.ExitCode
switch {
case cState.OOMKilled:
resp.Status.Reason = oomKilledReason
case cState.ExitCode == 0:
resp.Status.Reason = completedReason
default:
resp.Status.Reason = errorReason
resp.Status.Message = cState.Error
}
}
resp.Status.State = rStatus
resp.Status.LogPath = c.LogPath()
if req.Verbose {
resp.Info = map[string]string{
"pid": strconv.Itoa(c.State().Pid),
"sandboxId": c.Sandbox(),
}
}
logrus.Debugf("ContainerStatusResponse: %+v", resp)
return resp, nil
}