2015-05-31 18:49:11 +00:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/libnetwork/datastore"
|
|
|
|
)
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (c *controller) initStores() error {
|
2015-06-05 20:31:12 +00:00
|
|
|
c.Lock()
|
2015-10-05 11:21:15 +00:00
|
|
|
if c.cfg == nil {
|
|
|
|
c.Unlock()
|
|
|
|
return nil
|
2015-05-31 18:49:11 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
scopeConfigs := c.cfg.Scopes
|
|
|
|
c.Unlock()
|
2015-05-31 18:49:11 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
for scope, scfg := range scopeConfigs {
|
|
|
|
store, err := datastore.NewDataStore(scope, scfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Lock()
|
|
|
|
c.stores = append(c.stores, store)
|
|
|
|
c.Unlock()
|
2015-05-31 18:49:11 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
c.startWatch()
|
2015-09-22 20:20:55 +00:00
|
|
|
return nil
|
2015-05-31 18:49:11 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (c *controller) closeStores() {
|
|
|
|
for _, store := range c.getStores() {
|
|
|
|
store.Close()
|
2015-09-16 11:39:46 +00:00
|
|
|
}
|
2015-09-22 20:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (c *controller) getStore(scope string) datastore.DataStore {
|
2015-09-22 20:20:55 +00:00
|
|
|
c.Lock()
|
2015-10-05 11:21:15 +00:00
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
for _, store := range c.stores {
|
|
|
|
if store.Scope() == scope {
|
|
|
|
return store
|
|
|
|
}
|
2015-09-22 20:20:55 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
return nil
|
2015-09-22 20:20:55 +00:00
|
|
|
}
|
2015-09-16 11:39:46 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (c *controller) getStores() []datastore.DataStore {
|
2015-09-22 20:20:55 +00:00
|
|
|
c.Lock()
|
2015-10-05 11:21:15 +00:00
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
return c.stores
|
2015-09-22 20:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (c *controller) getNetworkFromStore(nid string) (*network, error) {
|
|
|
|
for _, store := range c.getStores() {
|
|
|
|
n := &network{id: nid, ctrlr: c}
|
|
|
|
err := store.GetObject(datastore.Key(n.Key()...), n)
|
|
|
|
if err != nil && err != datastore.ErrKeyNotFound {
|
|
|
|
return nil, fmt.Errorf("could not find network %s: %v", nid, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue searching in the next store if the key is not found in this store
|
|
|
|
if err == datastore.ErrKeyNotFound {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
2015-09-16 11:39:46 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
return nil, fmt.Errorf("network %s not found", nid)
|
2015-09-16 11:39:46 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (c *controller) getNetworksFromStore() ([]*network, error) {
|
|
|
|
var nl []*network
|
|
|
|
|
|
|
|
for _, store := range c.getStores() {
|
|
|
|
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",
|
|
|
|
store.Scope(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue searching in the next store if no keys found in this store
|
|
|
|
if err == datastore.ErrKeyNotFound {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kvo := range kvol {
|
|
|
|
n := kvo.(*network)
|
|
|
|
n.ctrlr = c
|
|
|
|
nl = append(nl, n)
|
|
|
|
}
|
2015-09-16 11:42:35 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
return nl, nil
|
2015-06-18 15:18:17 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (n *network) getEndpointFromStore(eid string) (*endpoint, error) {
|
|
|
|
for _, store := range n.ctrlr.getStores() {
|
|
|
|
ep := &endpoint{id: eid, network: n}
|
|
|
|
err := store.GetObject(datastore.Key(ep.Key()...), ep)
|
|
|
|
if err != nil && err != datastore.ErrKeyNotFound {
|
|
|
|
return nil, fmt.Errorf("could not find endpoint %s: %v", eid, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue searching in the next store if the key is not found in this store
|
|
|
|
if err == datastore.ErrKeyNotFound {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return ep, nil
|
|
|
|
}
|
2015-05-31 18:49:11 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
return nil, fmt.Errorf("endpoint %s not found", eid)
|
2015-05-31 18:49:11 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (n *network) getEndpointsFromStore() ([]*endpoint, error) {
|
|
|
|
var epl []*endpoint
|
2015-06-01 04:19:10 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
tmp := endpoint{network: n}
|
|
|
|
for _, store := range n.getController().getStores() {
|
|
|
|
kvol, err := store.List(datastore.Key(tmp.KeyPrefix()...), &endpoint{network: n})
|
|
|
|
if err != nil && err != datastore.ErrKeyNotFound {
|
|
|
|
return nil,
|
|
|
|
fmt.Errorf("failed to get endpoints for network %s scope %s: %v",
|
|
|
|
n.Name(), store.Scope(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue searching in the next store if no keys found in this store
|
|
|
|
if err == datastore.ErrKeyNotFound {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kvo := range kvol {
|
|
|
|
ep := kvo.(*endpoint)
|
|
|
|
ep.network = n
|
|
|
|
epl = append(epl, ep)
|
2015-06-05 20:31:12 +00:00
|
|
|
}
|
2015-05-31 18:49:11 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
return epl, nil
|
2015-05-31 18:49:11 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (c *controller) updateToStore(kvObject datastore.KVObject) error {
|
|
|
|
cs := c.getStore(kvObject.DataScope())
|
2015-05-31 18:49:11 +00:00
|
|
|
if cs == nil {
|
2015-10-05 11:21:15 +00:00
|
|
|
log.Warnf("datastore for scope %s not initialized. kv object %s is not added to the store", kvObject.DataScope(), datastore.Key(kvObject.Key()...))
|
2015-05-31 18:49:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
if err := cs.PutObjectAtomic(kvObject); err != nil {
|
|
|
|
return fmt.Errorf("failed to update store for object type %T: %v", kvObject, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-05-31 18:49:11 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (c *controller) deleteFromStore(kvObject datastore.KVObject) error {
|
|
|
|
cs := c.getStore(kvObject.DataScope())
|
2015-06-01 16:43:24 +00:00
|
|
|
if cs == nil {
|
2015-10-05 11:21:15 +00:00
|
|
|
log.Debugf("datastore for scope %s not initialized. kv object %s is not deleted from datastore", kvObject.DataScope(), datastore.Key(kvObject.Key()...))
|
2015-06-01 16:43:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
retry:
|
2015-09-16 11:42:35 +00:00
|
|
|
if err := cs.DeleteObjectAtomic(kvObject); err != nil {
|
2015-10-05 11:21:15 +00:00
|
|
|
if err == datastore.ErrKeyModified {
|
|
|
|
if err := cs.GetObject(datastore.Key(kvObject.Key()...), kvObject); err != nil {
|
|
|
|
return fmt.Errorf("could not update the kvobject to latest when trying to delete: %v", err)
|
|
|
|
}
|
|
|
|
goto retry
|
|
|
|
}
|
2015-06-01 16:43:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
type netWatch struct {
|
|
|
|
localEps map[string]*endpoint
|
|
|
|
remoteEps map[string]*endpoint
|
|
|
|
stopCh chan struct{}
|
|
|
|
}
|
2015-06-17 16:13:31 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (c *controller) getLocalEps(nw *netWatch) []*endpoint {
|
2015-05-31 18:49:11 +00:00
|
|
|
c.Lock()
|
2015-10-05 11:21:15 +00:00
|
|
|
defer c.Unlock()
|
2015-05-31 18:49:11 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
var epl []*endpoint
|
|
|
|
for _, ep := range nw.localEps {
|
|
|
|
epl = append(epl, ep)
|
2015-08-17 08:07:43 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
return epl
|
2015-06-17 16:13:31 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (c *controller) watchSvcRecord(ep *endpoint) {
|
|
|
|
c.watchCh <- ep
|
|
|
|
}
|
2015-06-17 16:13:31 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (c *controller) unWatchSvcRecord(ep *endpoint) {
|
|
|
|
c.unWatchCh <- ep
|
|
|
|
}
|
2015-06-17 16:13:31 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (c *controller) networkWatchLoop(nw *netWatch, ep *endpoint, nCh <-chan datastore.KVObject) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-nw.stopCh:
|
|
|
|
return
|
|
|
|
case o := <-nCh:
|
|
|
|
n := o.(*network)
|
|
|
|
|
|
|
|
epl, err := n.getEndpointsFromStore()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Lock()
|
|
|
|
var addEp []*endpoint
|
|
|
|
|
|
|
|
delEpMap := make(map[string]*endpoint)
|
|
|
|
for k, v := range nw.remoteEps {
|
|
|
|
delEpMap[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, lEp := range epl {
|
|
|
|
if _, ok := nw.localEps[lEp.ID()]; ok {
|
|
|
|
continue
|
2015-06-17 16:13:31 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
if _, ok := nw.remoteEps[lEp.ID()]; ok {
|
|
|
|
delete(delEpMap, lEp.ID())
|
|
|
|
continue
|
2015-05-31 18:49:11 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
nw.remoteEps[lEp.ID()] = lEp
|
|
|
|
addEp = append(addEp, lEp)
|
|
|
|
|
|
|
|
}
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
for _, lEp := range addEp {
|
|
|
|
ep.getNetwork().updateSvcRecord(lEp, c.getLocalEps(nw), true)
|
2015-05-31 18:49:11 +00:00
|
|
|
}
|
2015-06-05 20:31:12 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
for _, lEp := range delEpMap {
|
|
|
|
ep.getNetwork().updateSvcRecord(lEp, c.getLocalEps(nw), false)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2015-06-05 20:31:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (c *controller) processEndpointCreate(nmap map[string]*netWatch, ep *endpoint) {
|
|
|
|
c.Lock()
|
|
|
|
nw, ok := nmap[ep.getNetwork().ID()]
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
// Update the svc db for the local endpoint join right away
|
|
|
|
ep.getNetwork().updateSvcRecord(ep, c.getLocalEps(nw), true)
|
|
|
|
|
2015-06-18 15:18:17 +00:00
|
|
|
c.Lock()
|
2015-10-05 11:21:15 +00:00
|
|
|
nw.localEps[ep.ID()] = ep
|
2015-06-18 15:18:17 +00:00
|
|
|
c.Unlock()
|
2015-10-05 11:21:15 +00:00
|
|
|
return
|
2015-06-18 15:18:17 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
nw = &netWatch{
|
|
|
|
localEps: make(map[string]*endpoint),
|
|
|
|
remoteEps: make(map[string]*endpoint),
|
2015-06-05 20:31:12 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
// Update the svc db for the local endpoint join right away
|
|
|
|
// Do this before adding this ep to localEps so that we don't
|
|
|
|
// try to update this ep's container's svc records
|
|
|
|
ep.getNetwork().updateSvcRecord(ep, c.getLocalEps(nw), true)
|
2015-06-05 20:31:12 +00:00
|
|
|
|
|
|
|
c.Lock()
|
2015-10-05 11:21:15 +00:00
|
|
|
nw.localEps[ep.ID()] = ep
|
|
|
|
nmap[ep.getNetwork().ID()] = nw
|
|
|
|
nw.stopCh = make(chan struct{})
|
2015-06-05 20:31:12 +00:00
|
|
|
c.Unlock()
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
store := c.getStore(ep.getNetwork().DataScope())
|
|
|
|
if store == nil {
|
|
|
|
return
|
2015-06-05 20:31:12 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
if !store.Watchable() {
|
|
|
|
return
|
|
|
|
}
|
2015-08-17 08:07:43 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
ch, err := store.Watch(ep.getNetwork(), nw.stopCh)
|
2015-08-17 08:07:43 +00:00
|
|
|
if err != nil {
|
2015-10-05 11:21:15 +00:00
|
|
|
log.Warnf("Error creating watch for network: %v", err)
|
|
|
|
return
|
2015-08-17 08:07:43 +00:00
|
|
|
}
|
2015-09-16 11:39:46 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
go c.networkWatchLoop(nw, ep, ch)
|
2015-09-16 11:39:46 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (c *controller) processEndpointDelete(nmap map[string]*netWatch, ep *endpoint) {
|
2015-09-16 11:39:46 +00:00
|
|
|
c.Lock()
|
2015-10-05 11:21:15 +00:00
|
|
|
nw, ok := nmap[ep.getNetwork().ID()]
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
delete(nw.localEps, ep.ID())
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
// Update the svc db about local endpoint leave right away
|
|
|
|
// Do this after we remove this ep from localEps so that we
|
|
|
|
// don't try to remove this svc record from this ep's container.
|
|
|
|
ep.getNetwork().updateSvcRecord(ep, c.getLocalEps(nw), false)
|
|
|
|
|
|
|
|
c.Lock()
|
|
|
|
if len(nw.localEps) == 0 {
|
|
|
|
close(nw.stopCh)
|
|
|
|
delete(nmap, ep.getNetwork().ID())
|
|
|
|
}
|
2015-09-16 11:39:46 +00:00
|
|
|
}
|
|
|
|
c.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (c *controller) watchLoop(nmap map[string]*netWatch) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ep := <-c.watchCh:
|
|
|
|
c.processEndpointCreate(nmap, ep)
|
|
|
|
case ep := <-c.unWatchCh:
|
|
|
|
c.processEndpointDelete(nmap, ep)
|
2015-09-16 11:39:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
func (c *controller) startWatch() {
|
|
|
|
c.watchCh = make(chan *endpoint)
|
|
|
|
c.unWatchCh = make(chan *endpoint)
|
|
|
|
nmap := make(map[string]*netWatch)
|
|
|
|
|
|
|
|
go c.watchLoop(nmap)
|
|
|
|
}
|