2016-11-22 16:49:54 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-04-11 12:09:17 +00:00
|
|
|
"encoding/json"
|
2017-05-20 13:16:58 +00:00
|
|
|
"fmt"
|
2017-04-11 12:09:17 +00:00
|
|
|
|
2016-11-22 16:49:54 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2017-05-20 13:16:58 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
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"
|
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)
|
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
|
|
|
|
}
|
2017-05-11 10:41:08 +00:00
|
|
|
s.containerStateToDisk(c)
|
2016-11-22 16:49:54 +00:00
|
|
|
|
|
|
|
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(),
|
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-05-20 13:16:58 +00:00
|
|
|
imageName := c.Image().Image
|
|
|
|
status, err := s.storageImageServer.ImageStatus(s.imageContext, imageName)
|
2017-03-16 09:42:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-05-20 13:16:58 +00:00
|
|
|
// TODO: use status.ID only if no digested names!!!
|
|
|
|
// need to modify ImageStatus to split tagged and digested!
|
2017-03-16 09:42:31 +00:00
|
|
|
resp.Status.ImageRef = status.ID
|
|
|
|
|
2017-05-20 13:16:58 +00:00
|
|
|
for _, n := range status.Names {
|
|
|
|
r, err := reference.ParseNormalizedNamed(n)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to normalize image name for Image: %v", err)
|
|
|
|
}
|
|
|
|
if tagged, isTagged := r.(reference.Tagged); isTagged {
|
|
|
|
imageName = reference.FamiliarString(tagged)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp.Status.Image = &pb.ImageSpec{Image: imageName}
|
|
|
|
|
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) {
|
2017-05-17 17:18:35 +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
|
|
|
|
}
|