2016-11-22 22:23:01 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-11-09 10:10:35 +00:00
|
|
|
"time"
|
|
|
|
|
2017-11-30 15:46:11 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/lib/sandbox"
|
2016-11-22 22:23:01 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
2017-08-05 11:40:46 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-11-22 22:23:01 +00:00
|
|
|
"golang.org/x/net/context"
|
2017-02-23 02:32:42 +00:00
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
2018-02-12 20:13:07 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
|
2016-11-22 22:23:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// filterSandbox returns whether passed container matches filtering criteria
|
|
|
|
func filterSandbox(p *pb.PodSandbox, filter *pb.PodSandboxFilter) bool {
|
|
|
|
if filter != nil {
|
|
|
|
if filter.State != nil {
|
2017-02-03 14:41:28 +00:00
|
|
|
if p.State != filter.State.State {
|
2016-11-22 22:23:01 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if filter.LabelSelector != nil {
|
|
|
|
sel := fields.SelectorFromSet(filter.LabelSelector)
|
|
|
|
if !sel.Matches(fields.Set(p.Labels)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListPodSandbox returns a list of SandBoxes.
|
2017-11-09 10:10:35 +00:00
|
|
|
func (s *Server) ListPodSandbox(ctx context.Context, req *pb.ListPodSandboxRequest) (resp *pb.ListPodSandboxResponse, err error) {
|
|
|
|
const operation = "list_pod_sandbox"
|
|
|
|
defer func() {
|
|
|
|
recordOperation(operation, time.Now())
|
|
|
|
recordError(operation, err)
|
|
|
|
}()
|
|
|
|
|
2016-11-22 22:23:01 +00:00
|
|
|
logrus.Debugf("ListPodSandboxRequest %+v", req)
|
|
|
|
var pods []*pb.PodSandbox
|
2017-07-19 19:03:22 +00:00
|
|
|
var podList []*sandbox.Sandbox
|
2017-07-25 15:12:53 +00:00
|
|
|
for _, sb := range s.ContainerServer.ListSandboxes() {
|
2016-11-22 22:23:01 +00:00
|
|
|
podList = append(podList, sb)
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := req.Filter
|
|
|
|
// Filter by pod id first.
|
|
|
|
if filter != nil {
|
2017-02-03 14:41:28 +00:00
|
|
|
if filter.Id != "" {
|
2017-07-25 15:36:33 +00:00
|
|
|
id, err := s.PodIDIndex().Get(filter.Id)
|
2016-12-06 11:12:52 +00:00
|
|
|
if err != nil {
|
2017-10-16 17:24:43 +00:00
|
|
|
// Not finding an ID in a filtered list should not be considered
|
|
|
|
// and error; it might have been deleted when stop was done.
|
|
|
|
// Log and return an empty struct.
|
|
|
|
logrus.Warn("unable to find pod %s with filter", filter.Id)
|
|
|
|
return &pb.ListPodSandboxResponse{}, nil
|
2016-12-06 11:12:52 +00:00
|
|
|
}
|
|
|
|
sb := s.getSandbox(id)
|
2016-11-22 22:23:01 +00:00
|
|
|
if sb == nil {
|
2017-07-19 19:03:22 +00:00
|
|
|
podList = []*sandbox.Sandbox{}
|
2016-11-22 22:23:01 +00:00
|
|
|
} else {
|
2017-07-19 19:03:22 +00:00
|
|
|
podList = []*sandbox.Sandbox{sb}
|
2016-11-22 22:23:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sb := range podList {
|
2017-07-19 19:03:22 +00:00
|
|
|
podInfraContainer := sb.InfraContainer()
|
2016-11-22 22:23:01 +00:00
|
|
|
if podInfraContainer == nil {
|
|
|
|
// this can't really happen, but if it does because of a bug
|
|
|
|
// it's better not to panic
|
|
|
|
continue
|
|
|
|
}
|
2017-07-17 12:25:32 +00:00
|
|
|
cState := s.Runtime().ContainerStatus(podInfraContainer)
|
2016-11-22 22:23:01 +00:00
|
|
|
created := cState.Created.UnixNano()
|
|
|
|
rStatus := pb.PodSandboxState_SANDBOX_NOTREADY
|
|
|
|
if cState.Status == oci.ContainerStateRunning {
|
|
|
|
rStatus = pb.PodSandboxState_SANDBOX_READY
|
|
|
|
}
|
|
|
|
|
|
|
|
pod := &pb.PodSandbox{
|
2017-07-19 19:03:22 +00:00
|
|
|
Id: sb.ID(),
|
2017-03-27 17:14:11 +00:00
|
|
|
CreatedAt: created,
|
2017-02-03 14:41:28 +00:00
|
|
|
State: rStatus,
|
2017-07-19 19:03:22 +00:00
|
|
|
Labels: sb.Labels(),
|
|
|
|
Annotations: sb.Annotations(),
|
|
|
|
Metadata: sb.Metadata(),
|
2016-11-22 22:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by other criteria such as state and labels.
|
|
|
|
if filterSandbox(pod, req.Filter) {
|
|
|
|
pods = append(pods, pod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 10:10:35 +00:00
|
|
|
resp = &pb.ListPodSandboxResponse{
|
2016-11-22 22:23:01 +00:00
|
|
|
Items: pods,
|
|
|
|
}
|
|
|
|
logrus.Debugf("ListPodSandboxResponse %+v", resp)
|
|
|
|
return resp, nil
|
|
|
|
}
|