2016-11-22 16:49:54 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-04-11 12:09:17 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
2016-11-22 16:49:54 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
2017-04-11 12:09:17 +00:00
|
|
|
rspec "github.com/opencontainers/runtime-spec/specs-go"
|
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-04-11 12:09:17 +00:00
|
|
|
mounts, err := s.getMounts(containerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-16 09:42:31 +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
|
|
|
|
}
|
2017-04-11 12:09:17 +00:00
|
|
|
|
|
|
|
func (s *Server) getMounts(id string) ([]*pb.Mount, error) {
|
2017-07-17 12:25:32 +00:00
|
|
|
config, err := s.Store().FromContainerDirectory(id, "config.json")
|
2017-04-11 12:09:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var m rspec.Spec
|
|
|
|
if err = json.Unmarshal(config, &m); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
isRO := func(m rspec.Mount) bool {
|
|
|
|
var ro bool
|
|
|
|
for _, o := range m.Options {
|
|
|
|
if o == "ro" {
|
|
|
|
ro = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ro
|
|
|
|
}
|
|
|
|
isBind := func(m rspec.Mount) bool {
|
|
|
|
var bind bool
|
|
|
|
for _, o := range m.Options {
|
|
|
|
if o == "bind" || o == "rbind" {
|
|
|
|
bind = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bind
|
|
|
|
}
|
|
|
|
mounts := []*pb.Mount{}
|
|
|
|
for _, b := range m.Mounts {
|
|
|
|
if !isBind(b) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mounts = append(mounts, &pb.Mount{
|
|
|
|
ContainerPath: b.Destination,
|
|
|
|
HostPath: b.Source,
|
|
|
|
Readonly: isRO(b),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return mounts, nil
|
|
|
|
}
|