From 27500b1e3519c732b1bbb052f16dfcabe932861c Mon Sep 17 00:00:00 2001 From: Santhosh Manohar Date: Fri, 11 Nov 2016 00:42:34 -0800 Subject: [PATCH] Separate service LB & SD from network plumbing Signed-off-by: Santhosh Manohar --- libnetwork/agent.go | 71 ++++++++++++++++++++++--------- libnetwork/endpoint.go | 12 +++++- libnetwork/networkdb/networkdb.go | 2 +- libnetwork/sandbox.go | 4 +- 4 files changed, 65 insertions(+), 24 deletions(-) diff --git a/libnetwork/agent.go b/libnetwork/agent.go index f589d7bee2..c4f18ba9a8 100644 --- a/libnetwork/agent.go +++ b/libnetwork/agent.go @@ -381,7 +381,57 @@ func (n *network) leaveCluster() error { return c.agent.networkDB.LeaveNetwork(n.ID()) } -func (ep *endpoint) addToCluster() error { +func (ep *endpoint) addDriverInfoToCluster() error { + n := ep.getNetwork() + if !n.isClusterEligible() { + return nil + } + if ep.joinInfo == nil { + return nil + } + + ctrlr := n.ctrlr + ctrlr.Lock() + agent := ctrlr.agent + ctrlr.Unlock() + if agent == nil { + return nil + } + + for _, te := range ep.joinInfo.driverTableEntries { + if err := agent.networkDB.CreateEntry(te.tableName, n.ID(), te.key, te.value); err != nil { + return err + } + } + return nil +} + +func (ep *endpoint) deleteDriverInfoFromCluster() error { + n := ep.getNetwork() + if !n.isClusterEligible() { + return nil + } + if ep.joinInfo == nil { + return nil + } + + ctrlr := n.ctrlr + ctrlr.Lock() + agent := ctrlr.agent + ctrlr.Unlock() + if agent == nil { + return nil + } + + for _, te := range ep.joinInfo.driverTableEntries { + if err := agent.networkDB.DeleteEntry(te.tableName, n.ID(), te.key); err != nil { + return err + } + } + return nil +} + +func (ep *endpoint) addServiceInfoToCluster() error { n := ep.getNetwork() if !n.isClusterEligible() { return nil @@ -421,16 +471,10 @@ func (ep *endpoint) addToCluster() error { } } - for _, te := range ep.joinInfo.driverTableEntries { - if err := c.agent.networkDB.CreateEntry(te.tableName, n.ID(), te.key, te.value); err != nil { - return err - } - } - return nil } -func (ep *endpoint) deleteFromCluster() error { +func (ep *endpoint) deleteServiceInfoFromCluster() error { n := ep.getNetwork() if !n.isClusterEligible() { return nil @@ -453,17 +497,6 @@ func (ep *endpoint) deleteFromCluster() error { return err } } - - if ep.joinInfo == nil { - return nil - } - - for _, te := range ep.joinInfo.driverTableEntries { - if err := c.agent.networkDB.DeleteEntry(te.tableName, n.ID(), te.key); err != nil { - return err - } - } - return nil } diff --git a/libnetwork/endpoint.go b/libnetwork/endpoint.go index 519e8ede8d..df79477dbb 100644 --- a/libnetwork/endpoint.go +++ b/libnetwork/endpoint.go @@ -515,6 +515,10 @@ func (ep *endpoint) sbJoin(sb *sandbox, options ...EndpointOption) error { return err } + if err = ep.addDriverInfoToCluster(); err != nil { + return err + } + if sb.needDefaultGW() && sb.getEndpointInGWNetwork() == nil { return sb.setupDefaultGW() } @@ -709,8 +713,12 @@ func (ep *endpoint) sbLeave(sb *sandbox, force bool, options ...EndpointOption) return err } - if e := ep.deleteFromCluster(); e != nil { - logrus.Errorf("Could not delete state for endpoint %s from cluster: %v", ep.Name(), e) + if e := ep.deleteServiceInfoFromCluster(); e != nil { + logrus.Errorf("Could not delete service state for endpoint %s from cluster: %v", ep.Name(), e) + } + + if e := ep.deleteDriverInfoFromCluster(); e != nil { + logrus.Errorf("Could not delete endpoint state for endpoint %s from cluster: %v", ep.Name(), e) } sb.deleteHostsEntries(n.getSvcRecords(ep)) diff --git a/libnetwork/networkdb/networkdb.go b/libnetwork/networkdb/networkdb.go index 087398e3cf..71a4e2b9ee 100644 --- a/libnetwork/networkdb/networkdb.go +++ b/libnetwork/networkdb/networkdb.go @@ -265,7 +265,7 @@ func (nDB *NetworkDB) CreateEntry(tname, nid, key string, value []byte) error { } if err := nDB.sendTableEvent(TableEventTypeCreate, nid, tname, key, entry); err != nil { - return fmt.Errorf("cannot send table create event: %v", err) + return fmt.Errorf("cannot send create event for table %s, %v", tname, err) } nDB.Lock() diff --git a/libnetwork/sandbox.go b/libnetwork/sandbox.go index 0329e4b7ee..5512e2a57b 100644 --- a/libnetwork/sandbox.go +++ b/libnetwork/sandbox.go @@ -670,7 +670,7 @@ func (sb *sandbox) SetKey(basePath string) error { func (sb *sandbox) EnableService() error { for _, ep := range sb.getConnectedEndpoints() { if ep.enableService(true) { - if err := ep.addToCluster(); err != nil { + if err := ep.addServiceInfoToCluster(); err != nil { ep.enableService(false) return fmt.Errorf("could not update state for endpoint %s into cluster: %v", ep.Name(), err) } @@ -682,7 +682,7 @@ func (sb *sandbox) EnableService() error { func (sb *sandbox) DisableService() error { for _, ep := range sb.getConnectedEndpoints() { if ep.enableService(false) { - if err := ep.deleteFromCluster(); err != nil { + if err := ep.deleteServiceInfoFromCluster(); err != nil { ep.enableService(true) return fmt.Errorf("could not delete state for endpoint %s from cluster: %v", ep.Name(), err) }