@@ -3,12 +3,8 @@ package container
33import (
44 "context"
55 "errors"
6- "strconv"
76
87 "github.com/moby/moby/api/types/container"
9- "github.com/moby/moby/api/types/events"
10- "github.com/moby/moby/api/types/filters"
11- "github.com/moby/moby/api/types/versions"
128 "github.com/moby/moby/client"
139 "github.com/sirupsen/logrus"
1410)
@@ -19,13 +15,6 @@ func waitExitOrRemoved(ctx context.Context, apiClient client.APIClient, containe
1915 panic ("Internal Error: waitExitOrRemoved needs a containerID as parameter" )
2016 }
2117
22- // Older versions used the Events API, and even older versions did not
23- // support server-side removal. This legacyWaitExitOrRemoved method
24- // preserves that old behavior and any issues it may have.
25- if versions .LessThan (apiClient .ClientVersion (), "1.30" ) {
26- return legacyWaitExitOrRemoved (ctx , apiClient , containerID , waitRemove )
27- }
28-
2918 condition := container .WaitConditionNextExit
3019 if waitRemove {
3120 condition = container .WaitConditionRemoved
@@ -58,81 +47,6 @@ func waitExitOrRemoved(ctx context.Context, apiClient client.APIClient, containe
5847 return statusC
5948}
6049
61- func legacyWaitExitOrRemoved (ctx context.Context , apiClient client.APIClient , containerID string , waitRemove bool ) <- chan int {
62- var removeErr error
63- statusChan := make (chan int )
64- exitCode := 125
65-
66- // Get events via Events API
67- f := filters .NewArgs ()
68- f .Add ("type" , "container" )
69- f .Add ("container" , containerID )
70-
71- eventCtx , cancel := context .WithCancel (ctx )
72- eventq , errq := apiClient .Events (eventCtx , client.EventsListOptions {
73- Filters : f ,
74- })
75-
76- eventProcessor := func (e events.Message ) bool {
77- stopProcessing := false
78- switch e .Action { //nolint:exhaustive // TODO(thaJeztah): make exhaustive
79- case events .ActionDie :
80- if v , ok := e .Actor .Attributes ["exitCode" ]; ok {
81- code , cerr := strconv .Atoi (v )
82- if cerr != nil {
83- logrus .Errorf ("failed to convert exitcode '%q' to int: %v" , v , cerr )
84- } else {
85- exitCode = code
86- }
87- }
88- if ! waitRemove {
89- stopProcessing = true
90- } else if versions .LessThan (apiClient .ClientVersion (), "1.25" ) {
91- // If we are talking to an older daemon, `AutoRemove` is not supported.
92- // We need to fall back to the old behavior, which is client-side removal
93- go func () {
94- removeErr = apiClient .ContainerRemove (ctx , containerID , client.ContainerRemoveOptions {RemoveVolumes : true })
95- if removeErr != nil {
96- logrus .Errorf ("error removing container: %v" , removeErr )
97- cancel () // cancel the event Q
98- }
99- }()
100- }
101- case events .ActionDetach :
102- exitCode = 0
103- stopProcessing = true
104- case events .ActionDestroy :
105- stopProcessing = true
106- }
107- return stopProcessing
108- }
109-
110- go func () {
111- defer func () {
112- statusChan <- exitCode // must always send an exit code or the caller will block
113- cancel ()
114- }()
115-
116- for {
117- select {
118- case <- eventCtx .Done ():
119- if removeErr != nil {
120- return
121- }
122- case evt := <- eventq :
123- if eventProcessor (evt ) {
124- return
125- }
126- case err := <- errq :
127- logrus .Errorf ("error getting events from daemon: %v" , err )
128- return
129- }
130- }
131- }()
132-
133- return statusChan
134- }
135-
13650func parallelOperation (ctx context.Context , containers []string , op func (ctx context.Context , containerID string ) error ) chan error {
13751 if len (containers ) == 0 {
13852 return nil
0 commit comments