|
1 | 1 | package libnetwork |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "container/heap" |
4 | 5 | "encoding/json" |
5 | | - "sync" |
6 | 6 |
|
7 | 7 | "github.com/docker/libnetwork/datastore" |
8 | 8 | "github.com/docker/libnetwork/osl" |
@@ -207,6 +207,40 @@ func (c *controller) sandboxCleanup(activeSandboxes map[string]interface{}) { |
207 | 207 | return |
208 | 208 | } |
209 | 209 |
|
| 210 | + // Get all the endpoints |
| 211 | + // Use the network as the source of truth so that if there was an issue before the sandbox registered the endpoint |
| 212 | + // this will be taken anyway |
| 213 | + endpointsInSandboxID := map[string][]*endpoint{} |
| 214 | + nl, err := c.getNetworksForScope(datastore.LocalScope) |
| 215 | + if err != nil { |
| 216 | + logrus.Warnf("Could not get list of networks during sandbox cleanup: %v", err) |
| 217 | + return |
| 218 | + } |
| 219 | + |
| 220 | + for _, n := range nl { |
| 221 | + var epl []*endpoint |
| 222 | + epl, err = n.getEndpointsFromStore() |
| 223 | + if err != nil { |
| 224 | + logrus.Warnf("Could not get list of endpoints in network %s during sandbox cleanup: %v", n.name, err) |
| 225 | + continue |
| 226 | + } |
| 227 | + for _, ep := range epl { |
| 228 | + ep, err = n.getEndpointFromStore(ep.id) |
| 229 | + if err != nil { |
| 230 | + logrus.Warnf("Could not get endpoint in network %s during sandbox cleanup: %v", n.name, err) |
| 231 | + continue |
| 232 | + } |
| 233 | + if ep.sandboxID == "" { |
| 234 | + logrus.Warnf("Endpoint %s not associated to any sandbox, deleting it", ep.id) |
| 235 | + ep.Delete(true) |
| 236 | + continue |
| 237 | + } |
| 238 | + |
| 239 | + // Append the endpoint to the corresponding sandboxID |
| 240 | + endpointsInSandboxID[ep.sandboxID] = append(endpointsInSandboxID[ep.sandboxID], ep) |
| 241 | + } |
| 242 | + } |
| 243 | + |
210 | 244 | for _, kvo := range kvol { |
211 | 245 | sbs := kvo.(*sbState) |
212 | 246 |
|
@@ -252,25 +286,11 @@ func (c *controller) sandboxCleanup(activeSandboxes map[string]interface{}) { |
252 | 286 | c.sandboxes[sb.id] = sb |
253 | 287 | c.Unlock() |
254 | 288 |
|
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 |
| 289 | + // Restore all the endpoints that are supposed to be in this sandbox |
| 290 | + if eps, ok := endpointsInSandboxID[sb.id]; ok { |
| 291 | + for _, ep := range eps { |
| 292 | + heap.Push(&sb.endpoints, ep) |
272 | 293 | } |
273 | | - sb.addEndpoint(ep) |
274 | 294 | } |
275 | 295 |
|
276 | 296 | if _, ok := activeSandboxes[sb.ID()]; !ok { |
|
0 commit comments