@@ -41,54 +41,61 @@ import (
41
41
)
42
42
43
43
// DeleteContainersByLabel deletes all containers that have a specific label
44
- // if there no containers found with the given label, it will return nil
44
+ // if there no containers found with the given label, it will return nil
45
45
func DeleteContainersByLabel (ociBin string , label string ) []error {
46
46
var deleteErrs []error
47
47
ctx := context .Background ()
48
- cs , err := ListContainersByLabel (ctx , ociBin , label )
48
+
49
+ // Enable slow warning for container listing during delete operations
50
+ cs , err := ListContainersByLabel (ctx , ociBin , label , true )
49
51
if err != nil {
50
- return []error {fmt .Errorf ("listing containers by label %q" , label )}
52
+ return []error {fmt .Errorf ("listing containers by label %q: %v " , label , err )}
51
53
}
52
54
53
55
if len (cs ) == 0 {
54
56
return nil
55
57
}
56
58
57
59
for _ , c := range cs {
58
- _ , err := ContainerStatus (ociBin , c )
60
+ // Enable slow warning for container status check
61
+ _ , err := ContainerStatus (ociBin , c , true )
59
62
// only try to delete if docker/podman inspect returns
60
63
// if it doesn't it means docker daemon is stuck and needs restart
61
64
if err != nil {
62
65
deleteErrs = append (deleteErrs , errors .Wrapf (err , "delete container %s: %s daemon is stuck. please try again" , c , ociBin ))
63
66
klog .Errorf ("%s daemon seems to be stuck. please try restarting your %s :%v" , ociBin , ociBin , err )
64
67
continue
65
68
}
69
+
66
70
if err := ShutDown (ociBin , c ); err != nil {
67
71
klog .Infof ("couldn't shut down %s (might be okay): %v " , c , err )
68
72
}
69
73
70
- if _ , err := runCmd (exec .Command (ociBin , "rm" , "-f" , "-v" , c )); err != nil {
74
+ // Enable slow warning for container removal
75
+ if _ , err := runCmd (exec .Command (ociBin , "rm" , "-f" , "-v" , c ), true ); err != nil {
71
76
deleteErrs = append (deleteErrs , errors .Wrapf (err , "delete container %s: output %s" , c , err ))
72
77
}
73
-
74
78
}
75
79
return deleteErrs
76
80
}
77
81
78
82
// DeleteContainer deletes a container by ID or Name
79
83
func DeleteContainer (ctx context.Context , ociBin string , name string ) error {
80
- _ , err := ContainerStatus (ociBin , name )
84
+ // Enable slow warning for container status check
85
+ _ , err := ContainerStatus (ociBin , name , true )
81
86
if err == context .DeadlineExceeded {
82
- out .WarningT ("{{.ocibin}} is taking an unusually long time to respond, consider restarting {{.ocibin}}" , out.V {"ociBin " : ociBin })
87
+ out .WarningT ("{{.ocibin}} is taking an unusually long time to respond, consider restarting {{.ocibin}}" , out.V {"ocibin " : ociBin })
83
88
} else if err != nil {
84
89
klog .Warningf ("error getting container status, will try to delete anyways: %v" , err )
85
90
}
91
+
86
92
// try to delete anyways
87
93
if err := ShutDown (ociBin , name ); err != nil {
88
94
klog .Infof ("couldn't shut down %s (might be okay): %v " , name , err )
89
95
}
90
96
91
- if _ , err := runCmd (exec .CommandContext (ctx , ociBin , "rm" , "-f" , "-v" , name )); err != nil {
97
+ // Enable slow warning for container removal
98
+ if _ , err := runCmd (exec .CommandContext (ctx , ociBin , "rm" , "-f" , "-v" , name ), true ); err != nil {
92
99
return errors .Wrapf (err , "delete %s" , name )
93
100
}
94
101
return nil
@@ -368,7 +375,12 @@ func ContainerID(ociBin string, nameOrID string) (string, error) {
368
375
369
376
// ContainerExists checks if container name exists (either running or exited)
370
377
func ContainerExists (ociBin string , name string , warnSlow ... bool ) (bool , error ) {
371
- rr , err := runCmd (exec .Command (ociBin , "ps" , "-a" , "--format" , "{{.Names}}" ), warnSlow ... )
378
+ shouldWarn := false
379
+ if len (warnSlow ) > 0 {
380
+ shouldWarn = warnSlow [0 ]
381
+ }
382
+
383
+ rr , err := runCmd (exec .Command (ociBin , "ps" , "-a" , "--format" , "{{.Names}}" ), shouldWarn )
372
384
if err != nil {
373
385
return false , err
374
386
}
@@ -540,7 +552,13 @@ func withPortMappings(portMappings []PortMapping) createOpt {
540
552
541
553
// ListContainersByLabel returns all the container names with a specified label
542
554
func ListContainersByLabel (ctx context.Context , ociBin string , label string , warnSlow ... bool ) ([]string , error ) {
543
- rr , err := runCmd (exec .CommandContext (ctx , ociBin , "ps" , "-a" , "--filter" , fmt .Sprintf ("label=%s" , label ), "--format" , "{{.Names}}" ), warnSlow ... )
555
+ // Default to enabling warnings for container listing operations during critical operations like delete
556
+ shouldWarn := false
557
+ if len (warnSlow ) > 0 {
558
+ shouldWarn = warnSlow [0 ]
559
+ }
560
+
561
+ rr , err := runCmd (exec .CommandContext (ctx , ociBin , "ps" , "-a" , "--filter" , fmt .Sprintf ("label=%s" , label ), "--format" , "{{.Names}}" ), shouldWarn )
544
562
if err != nil {
545
563
return nil , err
546
564
}
@@ -619,7 +637,12 @@ func PointToHostPodman() error {
619
637
620
638
// ContainerRunning returns running state of a container
621
639
func ContainerRunning (ociBin string , name string , warnSlow ... bool ) (bool , error ) {
622
- rr , err := runCmd (exec .Command (ociBin , "container" , "inspect" , name , "--format={{.State.Running}}" ), warnSlow ... )
640
+ shouldWarn := false
641
+ if len (warnSlow ) > 0 {
642
+ shouldWarn = warnSlow [0 ]
643
+ }
644
+
645
+ rr , err := runCmd (exec .Command (ociBin , "container" , "inspect" , name , "--format={{.State.Running}}" ), shouldWarn )
623
646
if err != nil {
624
647
return false , err
625
648
}
@@ -628,8 +651,13 @@ func ContainerRunning(ociBin string, name string, warnSlow ...bool) (bool, error
628
651
629
652
// ContainerStatus returns status of a container running,exited,...
630
653
func ContainerStatus (ociBin string , name string , warnSlow ... bool ) (state.State , error ) {
654
+ shouldWarn := false
655
+ if len (warnSlow ) > 0 {
656
+ shouldWarn = warnSlow [0 ]
657
+ }
658
+
631
659
cmd := exec .Command (ociBin , "container" , "inspect" , name , "--format={{.State.Status}}" )
632
- rr , err := runCmd (cmd , warnSlow ... )
660
+ rr , err := runCmd (cmd , shouldWarn )
633
661
o := strings .TrimSpace (rr .Stdout .String ())
634
662
switch o {
635
663
case "configured" :
0 commit comments