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"
|
2017-02-23 02:32:42 +00:00
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
2016-11-22 16:49:54 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
// filterContainer returns whether passed container matches filtering criteria
|
|
|
|
func filterContainer(c *pb.Container, filter *pb.ContainerFilter) bool {
|
|
|
|
if filter != nil {
|
|
|
|
if filter.State != nil {
|
2017-02-03 14:41:28 +00:00
|
|
|
if c.State != filter.State.State {
|
2016-11-22 16:49:54 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if filter.LabelSelector != nil {
|
|
|
|
sel := fields.SelectorFromSet(filter.LabelSelector)
|
|
|
|
if !sel.Matches(fields.Set(c.Labels)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListContainers lists all containers by filters.
|
|
|
|
func (s *Server) ListContainers(ctx context.Context, req *pb.ListContainersRequest) (*pb.ListContainersResponse, error) {
|
|
|
|
logrus.Debugf("ListContainersRequest %+v", req)
|
|
|
|
var ctrs []*pb.Container
|
|
|
|
filter := req.Filter
|
2017-07-18 15:01:08 +00:00
|
|
|
ctrList := s.ContainerServer.ListContainers()
|
2016-11-22 16:49:54 +00:00
|
|
|
|
|
|
|
// Filter using container id and pod id first.
|
|
|
|
if filter != nil {
|
2017-02-03 14:41:28 +00:00
|
|
|
if filter.Id != "" {
|
2017-07-17 12:25:32 +00:00
|
|
|
id, err := s.CtrIDIndex().Get(filter.Id)
|
2016-12-06 11:16:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-18 15:01:08 +00:00
|
|
|
c := s.ContainerServer.GetContainer(id)
|
2016-11-22 16:49:54 +00:00
|
|
|
if c != nil {
|
2017-02-03 14:41:28 +00:00
|
|
|
if filter.PodSandboxId != "" {
|
|
|
|
if c.Sandbox() == filter.PodSandboxId {
|
2016-11-22 16:49:54 +00:00
|
|
|
ctrList = []*oci.Container{c}
|
|
|
|
} else {
|
|
|
|
ctrList = []*oci.Container{}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ctrList = []*oci.Container{c}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-02-03 14:41:28 +00:00
|
|
|
if filter.PodSandboxId != "" {
|
2017-07-25 15:12:53 +00:00
|
|
|
pod := s.ContainerServer.GetSandbox(filter.PodSandboxId)
|
2016-11-22 16:49:54 +00:00
|
|
|
if pod == nil {
|
|
|
|
ctrList = []*oci.Container{}
|
|
|
|
} else {
|
2017-07-19 19:03:22 +00:00
|
|
|
ctrList = pod.Containers().List()
|
2016-11-22 16:49:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ctr := range ctrList {
|
|
|
|
podSandboxID := ctr.Sandbox()
|
2017-07-17 12:25:32 +00:00
|
|
|
cState := s.Runtime().ContainerStatus(ctr)
|
2016-11-22 16:49:54 +00:00
|
|
|
created := cState.Created.UnixNano()
|
|
|
|
rState := pb.ContainerState_CONTAINER_UNKNOWN
|
|
|
|
cID := ctr.ID()
|
2017-07-17 19:42:14 +00:00
|
|
|
img := &pb.ImageSpec{
|
|
|
|
Image: ctr.Image(),
|
|
|
|
}
|
2016-11-22 16:49:54 +00:00
|
|
|
c := &pb.Container{
|
2017-02-03 14:41:28 +00:00
|
|
|
Id: cID,
|
|
|
|
PodSandboxId: podSandboxID,
|
2017-03-27 17:14:11 +00:00
|
|
|
CreatedAt: created,
|
2016-11-22 16:49:54 +00:00
|
|
|
Labels: ctr.Labels(),
|
|
|
|
Metadata: ctr.Metadata(),
|
2016-12-12 10:12:03 +00:00
|
|
|
Annotations: ctr.Annotations(),
|
2017-07-17 19:42:14 +00:00
|
|
|
Image: img,
|
2016-11-22 16:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch cState.Status {
|
|
|
|
case oci.ContainerStateCreated:
|
|
|
|
rState = pb.ContainerState_CONTAINER_CREATED
|
|
|
|
case oci.ContainerStateRunning:
|
|
|
|
rState = pb.ContainerState_CONTAINER_RUNNING
|
|
|
|
case oci.ContainerStateStopped:
|
|
|
|
rState = pb.ContainerState_CONTAINER_EXITED
|
|
|
|
}
|
2017-02-03 14:41:28 +00:00
|
|
|
c.State = rState
|
2016-11-22 16:49:54 +00:00
|
|
|
|
|
|
|
// Filter by other criteria such as state and labels.
|
|
|
|
if filterContainer(c, req.Filter) {
|
|
|
|
ctrs = append(ctrs, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &pb.ListContainersResponse{
|
|
|
|
Containers: ctrs,
|
|
|
|
}
|
|
|
|
logrus.Debugf("ListContainersResponse: %+v", resp)
|
|
|
|
return resp, nil
|
|
|
|
}
|