From 5e7d96bd6aff9fcbac9a9fc32f5b4216378cd293 Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Mon, 17 Oct 2016 12:24:57 -0700 Subject: [PATCH] Add server side pod filtering support Signed-off-by: Mrunal Patel --- server/sandbox.go | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/server/sandbox.go b/server/sandbox.go index b69e946a..9035d9db 100644 --- a/server/sandbox.go +++ b/server/sandbox.go @@ -463,10 +463,46 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR }, nil } +// filterSandbox returns whether passed container matches filtering criteria +func filterSandbox(p *pb.PodSandbox, filter *pb.PodSandboxFilter) bool { + if filter != nil { + if filter.State != nil { + if *p.State != *filter.State { + 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(context.Context, *pb.ListPodSandboxRequest) (*pb.ListPodSandboxResponse, error) { +func (s *Server) ListPodSandbox(ctx context.Context, req *pb.ListPodSandboxRequest) (*pb.ListPodSandboxResponse, error) { 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 { + if filter.Id != nil { + sb := s.getSandbox(*filter.Id) + if sb == nil { + podList = []*sandbox{} + } else { + podList = []*sandbox{sb} + } + } + } + + for _, sb := range podList { podInfraContainerName := sb.name + "-infra" podInfraContainer := sb.getContainer(podInfraContainerName) if podInfraContainer == nil { @@ -493,7 +529,10 @@ func (s *Server) ListPodSandbox(context.Context, *pb.ListPodSandboxRequest) (*pb Metadata: sb.metadata, } - pods = append(pods, pod) + // Filter by other criteria such as state and labels. + if filterSandbox(pod, req.Filter) { + pods = append(pods, pod) + } } return &pb.ListPodSandboxResponse{