@@ -21,6 +21,7 @@ import (
21
21
"github.com/containers/podman/v5/libpod/define"
22
22
"github.com/containers/podman/v5/libpod/events"
23
23
"github.com/containers/podman/v5/libpod/shutdown"
24
+ contEntities "github.com/containers/podman/v5/pkg/domain/entities"
24
25
"github.com/containers/podman/v5/pkg/domain/entities/reports"
25
26
"github.com/containers/podman/v5/pkg/rootless"
26
27
"github.com/containers/podman/v5/pkg/specgen"
@@ -44,6 +45,11 @@ type CtrCreateOption func(*Container) error
44
45
// A true return will include the container, a false return will exclude it.
45
46
type ContainerFilter func (* Container ) bool
46
47
48
+ // ExternalContainerFilter is a function to determine whether a container list is included
49
+ // in command output. Container lists to be outputted are tested using the function.
50
+ // A true return will include the container list, a false return will exclude it.
51
+ type ExternalContainerFilter func (* contEntities.ListContainer ) bool
52
+
47
53
// NewContainer creates a new container from a given OCI config.
48
54
func (r * Runtime ) NewContainer (ctx context.Context , rSpec * spec.Spec , spec * specgen.SpecGenerator , infra bool , options ... CtrCreateOption ) (* Container , error ) {
49
55
if ! r .valid {
@@ -1246,9 +1252,16 @@ func (r *Runtime) GetContainers(loadState bool, filters ...ContainerFilter) ([]*
1246
1252
return nil , err
1247
1253
}
1248
1254
1249
- ctrsFiltered := make ([]* Container , 0 , len (ctrs ))
1255
+ ctrsFiltered := r .ApplyContainersFilters (ctrs , filters ... )
1256
+
1257
+ return ctrsFiltered , nil
1258
+ }
1259
+
1260
+ // Apply container filters on bunch of containers
1261
+ func (r * Runtime ) ApplyContainersFilters (containers []* Container , filters ... ContainerFilter ) []* Container {
1262
+ ctrsFiltered := make ([]* Container , 0 , len (containers ))
1250
1263
1251
- for _ , ctr := range ctrs {
1264
+ for _ , ctr := range containers {
1252
1265
include := true
1253
1266
for _ , filter := range filters {
1254
1267
include = include && filter (ctr )
@@ -1259,7 +1272,25 @@ func (r *Runtime) GetContainers(loadState bool, filters ...ContainerFilter) ([]*
1259
1272
}
1260
1273
}
1261
1274
1262
- return ctrsFiltered , nil
1275
+ return ctrsFiltered
1276
+ }
1277
+
1278
+ // Apply container filters on bunch of external container lists
1279
+ func (r * Runtime ) ApplyExternalContainersFilters (containersList []* contEntities.ListContainer , filters ... ExternalContainerFilter ) []contEntities.ListContainer {
1280
+ ctrsFiltered := make ([]contEntities.ListContainer , 0 , len (containersList ))
1281
+
1282
+ for _ , ctr := range containersList {
1283
+ include := true
1284
+ for _ , filter := range filters {
1285
+ include = include && filter (ctr )
1286
+ }
1287
+
1288
+ if include {
1289
+ ctrsFiltered = append (ctrsFiltered , * ctr )
1290
+ }
1291
+ }
1292
+
1293
+ return ctrsFiltered
1263
1294
}
1264
1295
1265
1296
// GetAllContainers is a helper function for GetContainers
0 commit comments