2016-11-22 22:23:01 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
2017-02-03 14:41:28 +00:00
|
|
|
"k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/fields"
|
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.
|
|
|
|
func (s *Server) ListPodSandbox(ctx context.Context, req *pb.ListPodSandboxRequest) (*pb.ListPodSandboxResponse, error) {
|
|
|
|
logrus.Debugf("ListPodSandboxRequest %+v", req)
|
2016-10-18 14:48:33 +00:00
|
|
|
s.Update()
|
2016-11-22 22:23:01 +00:00
|
|
|
var pods []*pb.PodSandbox
|
|
|
|
var podList []*sandbox
|
|
|
|
for _, sb := range s.state.sandboxes {
|
|
|
|
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 != "" {
|
|
|
|
id, err := s.podIDIndex.Get(filter.Id)
|
2016-12-06 11:12:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sb := s.getSandbox(id)
|
2016-11-22 22:23:01 +00:00
|
|
|
if sb == nil {
|
|
|
|
podList = []*sandbox{}
|
|
|
|
} else {
|
|
|
|
podList = []*sandbox{sb}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sb := range podList {
|
|
|
|
podInfraContainer := sb.infraContainer
|
|
|
|
if podInfraContainer == nil {
|
|
|
|
// this can't really happen, but if it does because of a bug
|
|
|
|
// it's better not to panic
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := s.runtime.UpdateStatus(podInfraContainer); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cState := s.runtime.ContainerStatus(podInfraContainer)
|
|
|
|
created := cState.Created.UnixNano()
|
|
|
|
rStatus := pb.PodSandboxState_SANDBOX_NOTREADY
|
|
|
|
if cState.Status == oci.ContainerStateRunning {
|
|
|
|
rStatus = pb.PodSandboxState_SANDBOX_READY
|
|
|
|
}
|
|
|
|
|
|
|
|
pod := &pb.PodSandbox{
|
2017-02-03 14:41:28 +00:00
|
|
|
Id: sb.id,
|
|
|
|
CreatedAt: int64(created),
|
|
|
|
State: rStatus,
|
2016-11-22 22:23:01 +00:00
|
|
|
Labels: sb.labels,
|
|
|
|
Annotations: sb.annotations,
|
|
|
|
Metadata: sb.metadata,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by other criteria such as state and labels.
|
|
|
|
if filterSandbox(pod, req.Filter) {
|
|
|
|
pods = append(pods, pod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &pb.ListPodSandboxResponse{
|
|
|
|
Items: pods,
|
|
|
|
}
|
|
|
|
logrus.Debugf("ListPodSandboxResponse %+v", resp)
|
|
|
|
return resp, nil
|
|
|
|
}
|