@@ -29,6 +29,7 @@ func GetContainerLists(runtime *libpod.Runtime, options entities.ContainerListOp
2929 pss = []entities.ListContainer {}
3030 )
3131 filterFuncs := make ([]libpod.ContainerFilter , 0 , len (options .Filters ))
32+ filterExtFuncs := make ([]libpod.ExternalContainerFilter , 0 , len (options .Filters ))
3233 all := options .All || options .Last > 0
3334 if len (options .Filters ) > 0 {
3435 for k , v := range options .Filters {
@@ -37,6 +38,14 @@ func GetContainerLists(runtime *libpod.Runtime, options entities.ContainerListOp
3738 return nil , err
3839 }
3940 filterFuncs = append (filterFuncs , generatedFunc )
41+
42+ if options .External {
43+ generatedExtFunc , err := filters .GenerateExternalContainerFilterFuncs (k , v , runtime )
44+ if err != nil {
45+ return nil , err
46+ }
47+ filterExtFuncs = append (filterExtFuncs , generatedExtFunc )
48+ }
4049 }
4150 }
4251
@@ -87,7 +96,7 @@ func GetContainerLists(runtime *libpod.Runtime, options entities.ContainerListOp
8796 }
8897
8998 if options .External {
90- listCon , err := GetExternalContainerLists (runtime )
99+ listCon , err := GetExternalContainerLists (runtime , filterExtFuncs ... )
91100 if err != nil {
92101 return nil , err
93102 }
@@ -107,9 +116,9 @@ func GetContainerLists(runtime *libpod.Runtime, options entities.ContainerListOp
107116}
108117
109118// GetExternalContainerLists returns list of external containers for e.g. created by buildah
110- func GetExternalContainerLists (runtime * libpod.Runtime ) ([]entities.ListContainer , error ) {
119+ func GetExternalContainerLists (runtime * libpod.Runtime , filterExtFuncs ... libpod. ExternalContainerFilter ) ([]entities.ListContainer , error ) {
111120 var (
112- pss = []entities.ListContainer {}
121+ pss = []* entities.ListContainer {}
113122 )
114123
115124 externCons , err := runtime .StorageContainers ()
@@ -128,10 +137,31 @@ func GetExternalContainerLists(runtime *libpod.Runtime) ([]entities.ListContaine
128137 case err != nil :
129138 return nil , err
130139 default :
131- pss = append (pss , listCon )
140+ pss = append (pss , & listCon )
132141 }
133142 }
134- return pss , nil
143+
144+ filteredPss := applyExternalContainersFilters (pss , filterExtFuncs ... )
145+
146+ return filteredPss , nil
147+ }
148+
149+ // Apply container filters on bunch of external container lists
150+ func applyExternalContainersFilters (containersList []* entities.ListContainer , filters ... libpod.ExternalContainerFilter ) []entities.ListContainer {
151+ ctrsFiltered := make ([]entities.ListContainer , 0 , len (containersList ))
152+
153+ for _ , ctr := range containersList {
154+ include := true
155+ for _ , filter := range filters {
156+ include = include && filter (ctr )
157+ }
158+
159+ if include {
160+ ctrsFiltered = append (ctrsFiltered , * ctr )
161+ }
162+ }
163+
164+ return ctrsFiltered
135165}
136166
137167// ListContainerBatch is used in ps to reduce performance hits by "batching"
0 commit comments