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
// 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 {

View file

@ -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