2016-11-22 16:49:54 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-11-09 10:10:35 +00:00
|
|
|
"time"
|
|
|
|
|
2016-11-22 16:49:54 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
2017-08-05 11:40:46 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-11-22 16:49:54 +00:00
|
|
|
"golang.org/x/net/context"
|
2017-08-04 11:13:19 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
2016-11-22 16:49:54 +00:00
|
|
|
)
|
|
|
|
|
2017-06-10 16:18:59 +00:00
|
|
|
const (
|
|
|
|
oomKilledReason = "OOMKilled"
|
|
|
|
completedReason = "Completed"
|
|
|
|
errorReason = "Error"
|
|
|
|
)
|
|
|
|
|
2016-11-22 16:49:54 +00:00
|
|
|
// ContainerStatus returns status of the container.
|
2017-11-09 10:10:35 +00:00
|
|
|
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)
|
|
|
|
}()
|
2016-11-22 16:49:54 +00:00
|
|
|
logrus.Debugf("ContainerStatusRequest %+v", req)
|
2017-07-31 23:23:20 +00:00
|
|
|
c, err := s.GetContainerFromRequest(req.ContainerId)
|
2016-11-22 16:49:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
containerID := c.ID()
|
2017-11-09 10:10:35 +00:00
|
|
|
resp = &pb.ContainerStatusResponse{
|
2016-11-22 16:49:54 +00:00
|
|
|
Status: &pb.ContainerStatus{
|
2017-04-03 22:05:57 +00:00
|
|
|
Id: containerID,
|
|
|
|
Metadata: c.Metadata(),
|
|
|
|
Labels: c.Labels(),
|
|
|
|
Annotations: c.Annotations(),
|
2017-08-14 19:29:53 +00:00
|
|
|
ImageRef: c.ImageRef(),
|
2016-11-22 16:49:54 +00:00
|
|
|
},
|
|
|
|
}
|
2017-08-14 19:29:53 +00:00
|
|
|
resp.Status.Image = &pb.ImageSpec{Image: c.ImageName()}
|
2016-11-22 16:49:54 +00:00
|
|
|
|
2017-08-14 19:58:38 +00:00
|
|
|
mounts := []*pb.Mount{}
|
|
|
|
for _, cv := range c.Volumes() {
|
|
|
|
mounts = append(mounts, &pb.Mount{
|
|
|
|
ContainerPath: cv.ContainerPath,
|
|
|
|
HostPath: cv.HostPath,
|
|
|
|
Readonly: cv.Readonly,
|
|
|
|
})
|
2017-04-11 12:09:17 +00:00
|
|
|
}
|
2017-08-14 19:29:53 +00:00
|
|
|
resp.Status.Mounts = mounts
|
2017-05-20 13:16:58 +00:00
|
|
|
|
2017-07-17 12:25:32 +00:00
|
|
|
cState := s.Runtime().ContainerStatus(c)
|
2016-11-22 16:49:54 +00:00
|
|
|
rStatus := pb.ContainerState_CONTAINER_UNKNOWN
|
|
|
|
|
2017-08-18 04:13:39 +00:00
|
|
|
// 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().UpdateStatus(c)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Failed to UpdateStatus of container %s: %v", c.ID(), err)
|
|
|
|
}
|
|
|
|
cState = s.Runtime().ContainerStatus(c)
|
|
|
|
}
|
|
|
|
|
2016-11-22 16:49:54 +00:00
|
|
|
switch cState.Status {
|
|
|
|
case oci.ContainerStateCreated:
|
|
|
|
rStatus = pb.ContainerState_CONTAINER_CREATED
|
|
|
|
created := cState.Created.UnixNano()
|
2017-03-27 17:14:11 +00:00
|
|
|
resp.Status.CreatedAt = created
|
2016-11-22 16:49:54 +00:00
|
|
|
case oci.ContainerStateRunning:
|
|
|
|
rStatus = pb.ContainerState_CONTAINER_RUNNING
|
|
|
|
created := cState.Created.UnixNano()
|
2017-03-27 17:14:11 +00:00
|
|
|
resp.Status.CreatedAt = created
|
2016-11-22 16:49:54 +00:00
|
|
|
started := cState.Started.UnixNano()
|
2017-03-27 17:14:11 +00:00
|
|
|
resp.Status.StartedAt = started
|
2016-11-22 16:49:54 +00:00
|
|
|
case oci.ContainerStateStopped:
|
|
|
|
rStatus = pb.ContainerState_CONTAINER_EXITED
|
|
|
|
created := cState.Created.UnixNano()
|
2017-03-27 17:14:11 +00:00
|
|
|
resp.Status.CreatedAt = created
|
2016-11-22 16:49:54 +00:00
|
|
|
started := cState.Started.UnixNano()
|
2017-03-27 17:14:11 +00:00
|
|
|
resp.Status.StartedAt = started
|
2016-11-22 16:49:54 +00:00
|
|
|
finished := cState.Finished.UnixNano()
|
2017-03-27 17:14:11 +00:00
|
|
|
resp.Status.FinishedAt = finished
|
|
|
|
resp.Status.ExitCode = cState.ExitCode
|
2017-05-28 17:00:48 +00:00
|
|
|
switch {
|
|
|
|
case cState.OOMKilled:
|
2017-06-10 16:18:59 +00:00
|
|
|
resp.Status.Reason = oomKilledReason
|
2017-05-28 17:00:48 +00:00
|
|
|
case cState.ExitCode == 0:
|
2017-06-10 16:18:59 +00:00
|
|
|
resp.Status.Reason = completedReason
|
2017-05-28 17:00:48 +00:00
|
|
|
default:
|
2017-06-10 16:18:59 +00:00
|
|
|
resp.Status.Reason = errorReason
|
|
|
|
resp.Status.Message = cState.Error
|
2017-05-23 21:04:10 +00:00
|
|
|
}
|
2016-11-22 16:49:54 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 14:41:28 +00:00
|
|
|
resp.Status.State = rStatus
|
2016-11-22 16:49:54 +00:00
|
|
|
|
|
|
|
logrus.Debugf("ContainerStatusResponse: %+v", resp)
|
|
|
|
return resp, nil
|
|
|
|
}
|