@@ -3,7 +3,6 @@ package libnetwork
33import (
44 "container/heap"
55 "encoding/json"
6- "sync"
76
87 "github.com/Sirupsen/logrus"
98 "github.com/docker/libnetwork/datastore"
@@ -210,6 +209,40 @@ func (c *controller) sandboxCleanup(activeSandboxes map[string]interface{}) {
210209 return
211210 }
212211
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+
213246 for _ , kvo := range kvol {
214247 sbs := kvo .(* sbState )
215248
@@ -256,25 +289,11 @@ func (c *controller) sandboxCleanup(activeSandboxes map[string]interface{}) {
256289 c .sandboxes [sb .id ] = sb
257290 c .Unlock ()
258291
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 )
276296 }
277- heap .Push (& sb .endpoints , ep )
278297 }
279298
280299 if _ , ok := activeSandboxes [sb .ID ()]; ! ok {
0 commit comments