@@ -2,7 +2,6 @@ package libnetwork
22
33import (
44 "encoding/json"
5- "sync"
65
76 "github.com/docker/libnetwork/datastore"
87 "github.com/docker/libnetwork/osl"
@@ -207,6 +206,40 @@ func (c *controller) sandboxCleanup(activeSandboxes map[string]interface{}) {
207206 return
208207 }
209208
209+ // Get all the endpoints
210+ // Use the network as the source of truth so that if there was an issue before the sandbox registered the endpoint
211+ // this will be taken anyway
212+ endpointsInSandboxID := map [string ][]* endpoint {}
213+ nl , err := c .getNetworksForScope (datastore .LocalScope )
214+ if err != nil {
215+ logrus .Warnf ("Could not get list of networks during sandbox cleanup: %v" , err )
216+ return
217+ }
218+
219+ for _ , n := range nl {
220+ var epl []* endpoint
221+ epl , err = n .getEndpointsFromStore ()
222+ if err != nil {
223+ logrus .Warnf ("Could not get list of endpoints in network %s during sandbox cleanup: %v" , n .name , err )
224+ continue
225+ }
226+ for _ , ep := range epl {
227+ ep , err = n .getEndpointFromStore (ep .id )
228+ if err != nil {
229+ logrus .Warnf ("Could not get endpoint in network %s during sandbox cleanup: %v" , n .name , err )
230+ continue
231+ }
232+ if ep .sandboxID == "" {
233+ logrus .Warnf ("Endpoint %s not associated to any sandbox, deleting it" , ep .id )
234+ ep .Delete (true )
235+ continue
236+ }
237+
238+ // Append the endpoint to the corresponding sandboxID
239+ endpointsInSandboxID [ep .sandboxID ] = append (endpointsInSandboxID [ep .sandboxID ], ep )
240+ }
241+ }
242+
210243 for _ , kvo := range kvol {
211244 sbs := kvo .(* sbState )
212245
@@ -252,25 +285,11 @@ func (c *controller) sandboxCleanup(activeSandboxes map[string]interface{}) {
252285 c .sandboxes [sb .id ] = sb
253286 c .Unlock ()
254287
255- for _ , eps := range sbs .Eps {
256- n , err := c .getNetworkFromStore (eps .Nid )
257- var ep * endpoint
258- if err != nil {
259- logrus .Errorf ("getNetworkFromStore for nid %s failed while trying to build sandbox for cleanup: %v" , eps .Nid , err )
260- n = & network {id : eps .Nid , ctrlr : c , drvOnce : & sync.Once {}, persist : true }
261- ep = & endpoint {id : eps .Eid , network : n , sandboxID : sbs .ID }
262- } else {
263- ep , err = n .getEndpointFromStore (eps .Eid )
264- if err != nil {
265- logrus .Errorf ("getEndpointFromStore for eid %s failed while trying to build sandbox for cleanup: %v" , eps .Eid , err )
266- ep = & endpoint {id : eps .Eid , network : n , sandboxID : sbs .ID }
267- }
268- }
269- if _ , ok := activeSandboxes [sb .ID ()]; ok && err != nil {
270- logrus .Errorf ("failed to restore endpoint %s in %s for container %s due to %v" , eps .Eid , eps .Nid , sb .ContainerID (), err )
271- continue
288+ // Restore all the endpoints that are supposed to be in this sandbox
289+ if eps , ok := endpointsInSandboxID [sb .id ]; ok {
290+ for _ , ep := range eps {
291+ sb .addEndpoint (ep )
272292 }
273- sb .addEndpoint (ep )
274293 }
275294
276295 if _ , ok := activeSandboxes [sb .ID ()]; ! ok {
0 commit comments