diff --git a/server/container_list.go b/server/container_list.go index 85681489..10a451f5 100644 --- a/server/container_list.go +++ b/server/container_list.go @@ -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 // 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) { - ctrList = origCtrList - +func (s *Server) filterContainerList(filter *pb.ContainerFilter, origCtrList []*oci.Container) []*oci.Container { // Filter using container id and pod id first. if 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 // be considered an error. Log a warning and return an empty struct logrus.Warn("unable to find container ID %s", filter.Id) - return []*oci.Container{}, nil + return []*oci.Container{} } c := s.ContainerServer.GetContainer(id) if c != nil { - if filter.PodSandboxId != "" { - if c.Sandbox() == filter.PodSandboxId { - ctrList = []*oci.Container{c} - } else { - ctrList = []*oci.Container{} - } - - } else { - ctrList = []*oci.Container{c} + if filter.PodSandboxId == "" || c.Sandbox() == filter.PodSandboxId { + return []*oci.Container{} } + return []*oci.Container{c} } } else { if filter.PodSandboxId != "" { pod := s.ContainerServer.GetSandbox(filter.PodSandboxId) if pod == nil { - ctrList = []*oci.Container{} + return []*oci.Container{} } else { - ctrList = pod.Containers().List() + return pod.Containers().List() } } } - return + return origCtrList } // ListContainers lists all containers by filters. @@ -85,10 +77,7 @@ func (s *Server) ListContainers(ctx context.Context, req *pb.ListContainersReque } if filter != nil { - ctrList, err = s.filterContainerList(filter, ctrList) - if err != nil { - return nil, err - } + ctrList = s.filterContainerList(filter, ctrList) } for _, ctr := range ctrList { diff --git a/server/container_stats_list.go b/server/container_stats_list.go index b6a81f6b..558b1e82 100644 --- a/server/container_stats_list.go +++ b/server/container_stats_list.go @@ -28,10 +28,7 @@ func (s *Server) ListContainerStats(ctx context.Context, req *pb.ListContainerSt PodSandboxId: req.Filter.PodSandboxId, LabelSelector: req.Filter.LabelSelector, } - ctrList, err = s.filterContainerList(cFilter, ctrList) - if err != nil { - return nil, err - } + ctrList = s.filterContainerList(cFilter, ctrList) } var allStats []*pb.ContainerStats