@@ -423,7 +423,105 @@ func GenerateExternalContainerFilterFuncs(filter string, filterValues []string,
423
423
}
424
424
return false
425
425
}, nil
426
- case "restart-policy" , "network" , "pod" , "volume" , "health" , "label" , "exited" , "status" :
426
+ case "status" :
427
+ for _ , filterValue := range filterValues {
428
+ if _ , err := define .StringToContainerStatus (filterValue ); err != nil {
429
+ return nil , err
430
+ }
431
+ }
432
+ return func (listContainer * types.ListContainer ) bool {
433
+ status := listContainer .State
434
+ if status == define .ContainerStateConfigured .String () {
435
+ status = "created"
436
+ } else if status == define .ContainerStateStopped .String () {
437
+ status = "exited"
438
+ }
439
+ for _ , filterValue := range filterValues {
440
+ if filterValue == "stopped" {
441
+ filterValue = "exited"
442
+ }
443
+ if status == filterValue {
444
+ return true
445
+ }
446
+ }
447
+ return false
448
+ }, nil
449
+ case "exited" :
450
+ var exitCodes []int32
451
+ for _ , exitCode := range filterValues {
452
+ ec , err := strconv .ParseInt (exitCode , 10 , 32 )
453
+ if err != nil {
454
+ return nil , fmt .Errorf ("exited code out of range %q: %w" , ec , err )
455
+ }
456
+ exitCodes = append (exitCodes , int32 (ec ))
457
+ }
458
+ return func (listContainer * types.ListContainer ) bool {
459
+ ec := listContainer .ExitCode
460
+ exited := listContainer .Exited
461
+ if exited {
462
+ for _ , exitCode := range exitCodes {
463
+ if ec == exitCode {
464
+ return true
465
+ }
466
+ }
467
+ }
468
+ return false
469
+ }, nil
470
+ case "label" :
471
+ return func (listContainer * types.ListContainer ) bool {
472
+ return ! filters .MatchLabelFilters (filterValues , listContainer .Labels )
473
+ }, nil
474
+ case "pod" :
475
+ var pods []* libpod.Pod
476
+ for _ , podNameOrID := range filterValues {
477
+ p , err := r .LookupPod (podNameOrID )
478
+ if err != nil {
479
+ if errors .Is (err , define .ErrNoSuchPod ) {
480
+ continue
481
+ }
482
+ return nil , err
483
+ }
484
+ pods = append (pods , p )
485
+ }
486
+ return func (listContainer * types.ListContainer ) bool {
487
+ // if no pods match, quick out
488
+ if len (pods ) < 1 {
489
+ return false
490
+ }
491
+ // if the container has no pod id, quick out
492
+ if len (listContainer .ID ) < 1 {
493
+ return false
494
+ }
495
+ for _ , p := range pods {
496
+ // we already looked up by name or id, so id match
497
+ // here is ok
498
+ if p .ID () == listContainer .ID {
499
+ return true
500
+ }
501
+ }
502
+ return false
503
+ }, nil
504
+ case "network" :
505
+ var inputNetNames []string
506
+ for _ , val := range filterValues {
507
+ net , err := r .Network ().NetworkInspect (val )
508
+ if err != nil {
509
+ if errors .Is (err , define .ErrNoSuchNetwork ) {
510
+ continue
511
+ }
512
+ return nil , err
513
+ }
514
+ inputNetNames = append (inputNetNames , net .Name )
515
+ }
516
+ return func (listContainer * types.ListContainer ) bool {
517
+ for _ , net := range listContainer .Networks {
518
+ if slices .Contains (inputNetNames , net ) {
519
+ return true
520
+ }
521
+ }
522
+ return false
523
+ }, nil
524
+ case "restart-policy" , "volume" , "health" :
427
525
return nil , fmt .Errorf ("filter %s is not applicable for external containers" , filter )
428
526
}
429
527
0 commit comments