2016-11-22 16:49:54 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
|
|
|
)
|
|
|
|
|
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.
|
|
|
|
func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusRequest) (*pb.ContainerStatusResponse, error) {
|
|
|
|
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()
|
|
|
|
resp := &pb.ContainerStatusResponse{
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|