Simplify filter block

Signed-off-by: Yann Ramin <atrus@stackworks.net>
This commit is contained in:
Yann Ramin 2018-02-02 15:38:45 -08:00
parent 50c94a9335
commit a2fc41358a
2 changed files with 10 additions and 24 deletions

View file

@ -30,9 +30,7 @@ func filterContainer(c *pb.Container, filter *pb.ContainerFilter) bool {
// filterContainerList applies a protobuf-defined filter to retrieve only intended containers. Not matching // filterContainerList applies a protobuf-defined filter to retrieve only intended containers. Not matching
// the filter is not considered an error but will return an empty response. // the filter is not considered an error but will return an empty response.
func (s *Server) filterContainerList(filter *pb.ContainerFilter, origCtrList []*oci.Container) (ctrList []*oci.Container, err error) { func (s *Server) filterContainerList(filter *pb.ContainerFilter, origCtrList []*oci.Container) []*oci.Container {
ctrList = origCtrList
// Filter using container id and pod id first. // Filter using container id and pod id first.
if filter.Id != "" { if filter.Id != "" {
id, err := s.CtrIDIndex().Get(filter.Id) id, err := s.CtrIDIndex().Get(filter.Id)
@ -40,32 +38,26 @@ func (s *Server) filterContainerList(filter *pb.ContainerFilter, origCtrList []*
// If we don't find a container ID with a filter, it should not // If we don't find a container ID with a filter, it should not
// be considered an error. Log a warning and return an empty struct // be considered an error. Log a warning and return an empty struct
logrus.Warn("unable to find container ID %s", filter.Id) logrus.Warn("unable to find container ID %s", filter.Id)
return []*oci.Container{}, nil return []*oci.Container{}
} }
c := s.ContainerServer.GetContainer(id) c := s.ContainerServer.GetContainer(id)
if c != nil { if c != nil {
if filter.PodSandboxId != "" { if filter.PodSandboxId == "" || c.Sandbox() == filter.PodSandboxId {
if c.Sandbox() == filter.PodSandboxId { return []*oci.Container{}
ctrList = []*oci.Container{c}
} else {
ctrList = []*oci.Container{}
}
} else {
ctrList = []*oci.Container{c}
} }
return []*oci.Container{c}
} }
} else { } else {
if filter.PodSandboxId != "" { if filter.PodSandboxId != "" {
pod := s.ContainerServer.GetSandbox(filter.PodSandboxId) pod := s.ContainerServer.GetSandbox(filter.PodSandboxId)
if pod == nil { if pod == nil {
ctrList = []*oci.Container{} return []*oci.Container{}
} else { } else {
ctrList = pod.Containers().List() return pod.Containers().List()
} }
} }
} }
return return origCtrList
} }
// ListContainers lists all containers by filters. // ListContainers lists all containers by filters.
@ -85,10 +77,7 @@ func (s *Server) ListContainers(ctx context.Context, req *pb.ListContainersReque
} }
if filter != nil { if filter != nil {
ctrList, err = s.filterContainerList(filter, ctrList) ctrList = s.filterContainerList(filter, ctrList)
if err != nil {
return nil, err
}
} }
for _, ctr := range ctrList { for _, ctr := range ctrList {

View file

@ -28,10 +28,7 @@ func (s *Server) ListContainerStats(ctx context.Context, req *pb.ListContainerSt
PodSandboxId: req.Filter.PodSandboxId, PodSandboxId: req.Filter.PodSandboxId,
LabelSelector: req.Filter.LabelSelector, LabelSelector: req.Filter.LabelSelector,
} }
ctrList, err = s.filterContainerList(cFilter, ctrList) ctrList = s.filterContainerList(cFilter, ctrList)
if err != nil {
return nil, err
}
} }
var allStats []*pb.ContainerStats var allStats []*pb.ContainerStats