@@ -3,7 +3,6 @@ package libnetwork
3
3
import (
4
4
"container/heap"
5
5
"encoding/json"
6
- "sync"
7
6
8
7
"github.com/Sirupsen/logrus"
9
8
"github.com/docker/libnetwork/datastore"
@@ -210,6 +209,40 @@ func (c *controller) sandboxCleanup(activeSandboxes map[string]interface{}) {
210
209
return
211
210
}
212
211
212
+ // Get all the endpoints
213
+ // Use the network as the source of truth so that if there was an issue before the sandbox registered the endpoint
214
+ // this will be taken anyway
215
+ endpointsInSandboxID := map [string ][]* endpoint {}
216
+ nl , err := c .getNetworksForScope (datastore .LocalScope )
217
+ if err != nil {
218
+ logrus .Warnf ("Could not get list of networks during sandbox cleanup: %v" , err )
219
+ return
220
+ }
221
+
222
+ for _ , n := range nl {
223
+ var epl []* endpoint
224
+ epl , err = n .getEndpointsFromStore ()
225
+ if err != nil {
226
+ logrus .Warnf ("Could not get list of endpoints in network %s during sandbox cleanup: %v" , n .name , err )
227
+ continue
228
+ }
229
+ for _ , ep := range epl {
230
+ ep , err = n .getEndpointFromStore (ep .id )
231
+ if err != nil {
232
+ logrus .Warnf ("Could not get endpoint in network %s during sandbox cleanup: %v" , n .name , err )
233
+ continue
234
+ }
235
+ if ep .sandboxID == "" {
236
+ logrus .Warnf ("Endpoint %s not associated to any sandbox, deleting it" , ep .id )
237
+ ep .Delete (true )
238
+ continue
239
+ }
240
+
241
+ // Append the endpoint to the corresponding sandboxID
242
+ endpointsInSandboxID [ep .sandboxID ] = append (endpointsInSandboxID [ep .sandboxID ], ep )
243
+ }
244
+ }
245
+
213
246
for _ , kvo := range kvol {
214
247
sbs := kvo .(* sbState )
215
248
@@ -256,25 +289,11 @@ func (c *controller) sandboxCleanup(activeSandboxes map[string]interface{}) {
256
289
c .sandboxes [sb .id ] = sb
257
290
c .Unlock ()
258
291
259
- for _ , eps := range sbs .Eps {
260
- n , err := c .getNetworkFromStore (eps .Nid )
261
- var ep * endpoint
262
- if err != nil {
263
- logrus .Errorf ("getNetworkFromStore for nid %s failed while trying to build sandbox for cleanup: %v" , eps .Nid , err )
264
- n = & network {id : eps .Nid , ctrlr : c , drvOnce : & sync.Once {}, persist : true }
265
- ep = & endpoint {id : eps .Eid , network : n , sandboxID : sbs .ID }
266
- } else {
267
- ep , err = n .getEndpointFromStore (eps .Eid )
268
- if err != nil {
269
- logrus .Errorf ("getEndpointFromStore for eid %s failed while trying to build sandbox for cleanup: %v" , eps .Eid , err )
270
- ep = & endpoint {id : eps .Eid , network : n , sandboxID : sbs .ID }
271
- }
272
- }
273
- if _ , ok := activeSandboxes [sb .ID ()]; ok && err != nil {
274
- logrus .Errorf ("failed to restore endpoint %s in %s for container %s due to %v" , eps .Eid , eps .Nid , sb .ContainerID (), err )
275
- continue
292
+ // Restore all the endpoints that are supposed to be in this sandbox
293
+ if eps , ok := endpointsInSandboxID [sb .id ]; ok {
294
+ for _ , ep := range eps {
295
+ heap .Push (& sb .endpoints , ep )
276
296
}
277
- heap .Push (& sb .endpoints , ep )
278
297
}
279
298
280
299
if _ , ok := activeSandboxes [sb .ID ()]; ! ok {
0 commit comments