From 33d336ac72872851c2a1cf4b5efb112766f01e18 Mon Sep 17 00:00:00 2001 From: Jana Radhakrishnan Date: Mon, 19 Oct 2015 13:20:23 -0700 Subject: [PATCH] Cleanup dangling local endpoints When we bootup cleanup all dangling local endpoints since they are not needed anymore. The only reason it can happen is when the process went down ungracefully after an endpoint is created but before join is successfull. Signed-off-by: Jana Radhakrishnan --- libnetwork/controller.go | 1 + libnetwork/endpoint.go | 22 ++++++++++++++++++++++ libnetwork/store.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/libnetwork/controller.go b/libnetwork/controller.go index c7f0cd2c6b..be027f4988 100644 --- a/libnetwork/controller.go +++ b/libnetwork/controller.go @@ -192,6 +192,7 @@ func New(cfgOptions ...config.Option) (NetworkController, error) { } c.sandboxCleanup() + c.cleanupLocalEndpoints() if err := c.startExternalKeyListener(); err != nil { return nil, err diff --git a/libnetwork/endpoint.go b/libnetwork/endpoint.go index 707006e33d..6331d93be5 100644 --- a/libnetwork/endpoint.go +++ b/libnetwork/endpoint.go @@ -708,3 +708,25 @@ func (ep *endpoint) releaseAddress() { } } } + +func (c *controller) cleanupLocalEndpoints() { + nl, err := c.getNetworksForScope(datastore.LocalScope) + if err != nil { + log.Warnf("Could not get list of networks during endpoint cleanup: %v", err) + return + } + + for _, n := range nl { + epl, err := n.getEndpointsFromStore() + if err != nil { + log.Warnf("Could not get list of endpoints in network %s during endpoint cleanup: %v", n.name, err) + continue + } + + for _, ep := range epl { + if err := ep.Delete(); err != nil { + log.Warnf("Could not delete local endpoint %s during endpoint cleanup: %v", ep.name, err) + } + } + } +} diff --git a/libnetwork/store.go b/libnetwork/store.go index d5eca874a4..65d3b02a32 100644 --- a/libnetwork/store.go +++ b/libnetwork/store.go @@ -82,6 +82,38 @@ func (c *controller) getNetworkFromStore(nid string) (*network, error) { return nil, fmt.Errorf("network %s not found", nid) } +func (c *controller) getNetworksForScope(scope string) ([]*network, error) { + var nl []*network + + store := c.getStore(scope) + if store == nil { + return nil, nil + } + + kvol, err := store.List(datastore.Key(datastore.NetworkKeyPrefix), + &network{ctrlr: c}) + if err != nil && err != datastore.ErrKeyNotFound { + return nil, fmt.Errorf("failed to get networks for scope %s: %v", + scope, err) + } + + for _, kvo := range kvol { + n := kvo.(*network) + n.ctrlr = c + + ec := &endpointCnt{n: n} + err = store.GetObject(datastore.Key(ec.Key()...), ec) + if err != nil { + return nil, fmt.Errorf("could not find endpoint count key %s for network %s while listing: %v", datastore.Key(ec.Key()...), n.Name(), err) + } + + n.epCnt = ec + nl = append(nl, n) + } + + return nl, nil +} + func (c *controller) getNetworksFromStore() ([]*network, error) { var nl []*network