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/Sirupsen/logrus"
|
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
2017-04-11 12:09:17 +00:00
|
|
|
rspec "github.com/opencontainers/runtime-spec/specs-go"
|
2016-11-22 16:49:54 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ContainerStatus returns status of the container.
|
|
|
|
func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusRequest) (*pb.ContainerStatusResponse, error) {
|
|
|
|
logrus.Debugf("ContainerStatusRequest %+v", req)
|
2016-10-18 14:48:33 +00:00
|
|
|
s.Update()
|
2017-02-03 14:41:28 +00:00
|
|
|
c, err := s.getContainerFromRequest(req.ContainerId)
|
2016-11-22 16:49:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-16 09:42:31 +00:00
|
|
|
if err = s.runtime.UpdateStatus(c); err != nil {
|
2016-11-22 16:49:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
containerID := c.ID()
|
2017-03-16 09:42:31 +00:00
|
|
|
image := c.Image()
|
2016-11-22 16:49:54 +00:00
|
|
|
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(),
|
|
|
|
Image: image,
|
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
|
|
|
|
}
|
|
|
|
resp.Status.Mounts = mounts
|
|
|
|
|
2017-04-19 19:17:10 +00:00
|
|
|
status, err := s.storageImageServer.ImageStatus(s.imageContext, image.Image)
|
2017-03-16 09:42:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Status.ImageRef = status.ID
|
|
|
|
|
2016-11-22 16:49:54 +00:00
|
|
|
cState := s.runtime.ContainerStatus(c)
|
|
|
|
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
|
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) {
|
|
|
|
config, err := s.store.GetFromContainerDirectory(id, "config.json")
|
|
|
|
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
|
|
|
|
}
|