2015-05-31 14:49:11 -04:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-06-18 11:18:17 -04:00
|
|
|
"github.com/docker/libkv/store"
|
2015-05-31 14:49:11 -04:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
2015-06-01 00:19:10 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-05-31 14:49:11 -04:00
|
|
|
)
|
|
|
|
|
2015-06-11 09:52:46 -04:00
|
|
|
func (c *controller) validateDatastoreConfig() bool {
|
2015-06-25 12:58:16 -04:00
|
|
|
return c.cfg != nil && c.cfg.Datastore.Client.Provider != "" && c.cfg.Datastore.Client.Address != ""
|
2015-06-11 09:52:46 -04:00
|
|
|
}
|
|
|
|
|
2015-05-31 14:49:11 -04:00
|
|
|
func (c *controller) initDataStore() error {
|
2015-06-05 16:31:12 -04:00
|
|
|
c.Lock()
|
|
|
|
cfg := c.cfg
|
|
|
|
c.Unlock()
|
2015-06-11 09:52:46 -04:00
|
|
|
if !c.validateDatastoreConfig() {
|
2015-05-31 14:49:11 -04:00
|
|
|
return fmt.Errorf("datastore initialization requires a valid configuration")
|
|
|
|
}
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
store, err := datastore.NewDataStore(&cfg.Datastore)
|
2015-05-31 14:49:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Lock()
|
|
|
|
c.store = store
|
|
|
|
c.Unlock()
|
2015-06-18 11:18:17 -04:00
|
|
|
|
|
|
|
nws, err := c.getNetworksFromStore()
|
|
|
|
if err == nil {
|
|
|
|
c.processNetworkUpdate(nws, nil)
|
|
|
|
} else if err != datastore.ErrKeyNotFound {
|
|
|
|
log.Warnf("failed to read networks from datastore during init : %v", err)
|
|
|
|
}
|
2015-06-17 12:13:31 -04:00
|
|
|
return c.watchNetworks()
|
2015-05-31 14:49:11 -04:00
|
|
|
}
|
|
|
|
|
2015-06-18 11:18:17 -04:00
|
|
|
func (c *controller) getNetworksFromStore() ([]*store.KVPair, error) {
|
|
|
|
c.Lock()
|
|
|
|
cs := c.store
|
|
|
|
c.Unlock()
|
|
|
|
return cs.KVStore().List(datastore.Key(datastore.NetworkKeyPrefix))
|
|
|
|
}
|
|
|
|
|
2015-06-01 00:19:10 -04:00
|
|
|
func (c *controller) newNetworkFromStore(n *network) error {
|
2015-06-05 16:31:12 -04:00
|
|
|
n.Lock()
|
2015-05-31 14:49:11 -04:00
|
|
|
n.ctrlr = c
|
|
|
|
n.endpoints = endpointTable{}
|
2015-06-05 16:31:12 -04:00
|
|
|
n.Unlock()
|
2015-05-31 14:49:11 -04:00
|
|
|
|
2015-06-01 00:19:10 -04:00
|
|
|
return c.addNetwork(n)
|
2015-05-31 14:49:11 -04:00
|
|
|
}
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
func (c *controller) updateNetworkToStore(n *network) error {
|
2015-06-06 13:21:51 -04:00
|
|
|
global, err := n.isGlobalScoped()
|
|
|
|
if err != nil || !global {
|
|
|
|
return err
|
2015-05-31 14:49:11 -04:00
|
|
|
}
|
|
|
|
c.Lock()
|
|
|
|
cs := c.store
|
|
|
|
c.Unlock()
|
|
|
|
if cs == nil {
|
|
|
|
log.Debugf("datastore not initialized. Network %s is not added to the store", n.Name())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
return cs.PutObjectAtomic(n)
|
2015-05-31 14:49:11 -04:00
|
|
|
}
|
|
|
|
|
2015-06-01 12:43:24 -04:00
|
|
|
func (c *controller) deleteNetworkFromStore(n *network) error {
|
2015-06-06 13:21:51 -04:00
|
|
|
global, err := n.isGlobalScoped()
|
|
|
|
if err != nil || !global {
|
|
|
|
return err
|
2015-06-01 12:43:24 -04:00
|
|
|
}
|
|
|
|
c.Lock()
|
|
|
|
cs := c.store
|
|
|
|
c.Unlock()
|
|
|
|
if cs == nil {
|
|
|
|
log.Debugf("datastore not initialized. Network %s is not deleted from datastore", n.Name())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
if err := cs.DeleteObjectAtomic(n); err != nil {
|
2015-06-01 12:43:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-01 00:19:10 -04:00
|
|
|
func (c *controller) getNetworkFromStore(nid types.UUID) (*network, error) {
|
|
|
|
n := network{id: nid}
|
|
|
|
if err := c.store.GetObject(datastore.Key(n.Key()...), &n); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &n, nil
|
|
|
|
}
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
func (c *controller) newEndpointFromStore(key string, ep *endpoint) error {
|
|
|
|
ep.Lock()
|
|
|
|
n := ep.network
|
|
|
|
id := ep.id
|
|
|
|
ep.Unlock()
|
2015-06-01 00:19:10 -04:00
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
_, err := n.EndpointByID(string(id))
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(ErrNoSuchEndpoint); ok {
|
|
|
|
return n.addEndpoint(ep)
|
|
|
|
}
|
2015-05-31 14:49:11 -04:00
|
|
|
}
|
2015-06-05 16:31:12 -04:00
|
|
|
return err
|
2015-05-31 14:49:11 -04:00
|
|
|
}
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
func (c *controller) updateEndpointToStore(ep *endpoint) error {
|
2015-06-01 12:43:24 -04:00
|
|
|
ep.Lock()
|
2015-06-06 13:21:51 -04:00
|
|
|
n := ep.network
|
2015-06-05 16:31:12 -04:00
|
|
|
name := ep.name
|
2015-06-01 12:43:24 -04:00
|
|
|
ep.Unlock()
|
2015-06-06 13:21:51 -04:00
|
|
|
global, err := n.isGlobalScoped()
|
|
|
|
if err != nil || !global {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-31 14:49:11 -04:00
|
|
|
c.Lock()
|
|
|
|
cs := c.store
|
|
|
|
c.Unlock()
|
|
|
|
if cs == nil {
|
2015-06-05 16:31:12 -04:00
|
|
|
log.Debugf("datastore not initialized. endpoint %s is not added to the store", name)
|
2015-05-31 14:49:11 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
return cs.PutObjectAtomic(ep)
|
2015-05-31 14:49:11 -04:00
|
|
|
}
|
|
|
|
|
2015-06-01 00:19:10 -04:00
|
|
|
func (c *controller) getEndpointFromStore(eid types.UUID) (*endpoint, error) {
|
|
|
|
ep := endpoint{id: eid}
|
|
|
|
if err := c.store.GetObject(datastore.Key(ep.Key()...), &ep); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ep, nil
|
|
|
|
}
|
|
|
|
|
2015-06-01 12:43:24 -04:00
|
|
|
func (c *controller) deleteEndpointFromStore(ep *endpoint) error {
|
2015-06-06 13:21:51 -04:00
|
|
|
ep.Lock()
|
|
|
|
n := ep.network
|
|
|
|
ep.Unlock()
|
|
|
|
global, err := n.isGlobalScoped()
|
|
|
|
if err != nil || !global {
|
|
|
|
return err
|
2015-06-01 12:43:24 -04:00
|
|
|
}
|
2015-06-06 13:21:51 -04:00
|
|
|
|
2015-06-01 12:43:24 -04:00
|
|
|
c.Lock()
|
|
|
|
cs := c.store
|
|
|
|
c.Unlock()
|
|
|
|
if cs == nil {
|
|
|
|
log.Debugf("datastore not initialized. endpoint %s is not deleted from datastore", ep.Name())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
if err := cs.DeleteObjectAtomic(ep); err != nil {
|
2015-06-01 12:43:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-17 12:13:31 -04:00
|
|
|
func (c *controller) watchNetworks() error {
|
|
|
|
if !c.validateDatastoreConfig() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-31 14:49:11 -04:00
|
|
|
c.Lock()
|
|
|
|
cs := c.store
|
|
|
|
c.Unlock()
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
nwPairs, err := cs.KVStore().WatchTree(datastore.Key(datastore.NetworkKeyPrefix), nil)
|
2015-05-31 14:49:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case nws := <-nwPairs:
|
2015-06-17 12:13:31 -04:00
|
|
|
c.Lock()
|
|
|
|
tmpview := networkTable{}
|
|
|
|
lview := c.networks
|
|
|
|
c.Unlock()
|
|
|
|
for k, v := range lview {
|
2015-06-19 16:34:16 -04:00
|
|
|
global, _ := v.isGlobalScoped()
|
|
|
|
if global {
|
|
|
|
tmpview[k] = v
|
|
|
|
}
|
2015-06-17 12:13:31 -04:00
|
|
|
}
|
2015-06-18 11:18:17 -04:00
|
|
|
c.processNetworkUpdate(nws, &tmpview)
|
2015-06-18 18:13:38 -04:00
|
|
|
|
2015-06-17 12:13:31 -04:00
|
|
|
// Delete processing
|
|
|
|
for k := range tmpview {
|
|
|
|
c.Lock()
|
|
|
|
existing, ok := c.networks[k]
|
|
|
|
c.Unlock()
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tmp := network{}
|
|
|
|
if err := c.store.GetObject(datastore.Key(existing.Key()...), &tmp); err != datastore.ErrKeyNotFound {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := existing.deleteNetwork(); err != nil {
|
|
|
|
log.Debugf("Delete failed %s: %s", existing.name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) watchEndpoints() error {
|
|
|
|
if !n.ctrlr.validateDatastoreConfig() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
n.Lock()
|
|
|
|
cs := n.ctrlr.store
|
|
|
|
tmp := endpoint{network: n}
|
|
|
|
n.stopWatchCh = make(chan struct{})
|
|
|
|
stopCh := n.stopWatchCh
|
|
|
|
n.Unlock()
|
|
|
|
|
|
|
|
epPairs, err := cs.KVStore().WatchTree(datastore.Key(tmp.KeyPrefix()...), stopCh)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
return
|
2015-05-31 14:49:11 -04:00
|
|
|
case eps := <-epPairs:
|
2015-06-17 12:13:31 -04:00
|
|
|
n.Lock()
|
|
|
|
tmpview := endpointTable{}
|
|
|
|
lview := n.endpoints
|
|
|
|
n.Unlock()
|
|
|
|
for k, v := range lview {
|
2015-06-19 16:34:16 -04:00
|
|
|
global, _ := v.network.isGlobalScoped()
|
|
|
|
if global {
|
|
|
|
tmpview[k] = v
|
|
|
|
}
|
2015-06-17 12:13:31 -04:00
|
|
|
}
|
2015-05-31 14:49:11 -04:00
|
|
|
for _, epe := range eps {
|
|
|
|
var ep endpoint
|
|
|
|
err := json.Unmarshal(epe.Value, &ep)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
2015-06-17 12:13:31 -04:00
|
|
|
delete(tmpview, ep.id)
|
2015-06-18 18:13:38 -04:00
|
|
|
ep.SetIndex(epe.LastIndex)
|
2015-06-17 12:13:31 -04:00
|
|
|
ep.network = n
|
|
|
|
if n.ctrlr.processEndpointUpdate(&ep) {
|
|
|
|
err = n.ctrlr.newEndpointFromStore(epe.Key, &ep)
|
|
|
|
if err != nil {
|
2015-06-05 16:31:12 -04:00
|
|
|
log.Error(err)
|
2015-05-31 14:49:11 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-17 12:13:31 -04:00
|
|
|
}
|
|
|
|
// Delete processing
|
|
|
|
for k := range tmpview {
|
|
|
|
n.Lock()
|
|
|
|
existing, ok := n.endpoints[k]
|
|
|
|
n.Unlock()
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tmp := endpoint{}
|
|
|
|
if err := cs.GetObject(datastore.Key(existing.Key()...), &tmp); err != datastore.ErrKeyNotFound {
|
|
|
|
continue
|
2015-06-05 16:31:12 -04:00
|
|
|
}
|
2015-06-17 12:13:31 -04:00
|
|
|
if err := existing.deleteEndpoint(); err != nil {
|
|
|
|
log.Debugf("Delete failed %s: %s", existing.name, err)
|
2015-06-05 16:31:12 -04:00
|
|
|
}
|
2015-05-31 14:49:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-05 16:31:12 -04:00
|
|
|
|
2015-06-17 12:13:31 -04:00
|
|
|
func (n *network) stopWatch() {
|
|
|
|
n.Lock()
|
|
|
|
if n.stopWatchCh != nil {
|
|
|
|
close(n.stopWatchCh)
|
|
|
|
n.stopWatchCh = nil
|
2015-06-05 16:31:12 -04:00
|
|
|
}
|
2015-06-17 12:13:31 -04:00
|
|
|
n.Unlock()
|
2015-06-05 16:31:12 -04:00
|
|
|
}
|
|
|
|
|
2015-06-18 11:18:17 -04:00
|
|
|
func (c *controller) processNetworkUpdate(nws []*store.KVPair, prune *networkTable) {
|
|
|
|
for _, kve := range nws {
|
|
|
|
var n network
|
|
|
|
err := json.Unmarshal(kve.Value, &n)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if prune != nil {
|
|
|
|
delete(*prune, n.id)
|
|
|
|
}
|
2015-06-18 18:13:38 -04:00
|
|
|
n.SetIndex(kve.LastIndex)
|
2015-06-18 11:18:17 -04:00
|
|
|
c.Lock()
|
|
|
|
existing, ok := c.networks[n.id]
|
|
|
|
c.Unlock()
|
|
|
|
if ok {
|
|
|
|
existing.Lock()
|
|
|
|
// Skip existing network update
|
2015-06-18 18:13:38 -04:00
|
|
|
if existing.dbIndex != n.Index() {
|
|
|
|
// Can't use SetIndex() since existing is locked.
|
|
|
|
existing.dbIndex = n.Index()
|
|
|
|
existing.dbExists = true
|
2015-06-18 11:18:17 -04:00
|
|
|
existing.endpointCnt = n.endpointCnt
|
|
|
|
}
|
|
|
|
existing.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = c.newNetworkFromStore(&n); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
func (c *controller) processEndpointUpdate(ep *endpoint) bool {
|
|
|
|
nw := ep.network
|
|
|
|
if nw == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
nw.Lock()
|
|
|
|
id := nw.id
|
|
|
|
nw.Unlock()
|
|
|
|
|
|
|
|
c.Lock()
|
|
|
|
n, ok := c.networks[id]
|
|
|
|
c.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
existing, _ := n.EndpointByID(string(ep.id))
|
|
|
|
if existing == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
ee := existing.(*endpoint)
|
|
|
|
ee.Lock()
|
2015-06-18 18:13:38 -04:00
|
|
|
if ee.dbIndex != ep.Index() {
|
|
|
|
// Can't use SetIndex() because ee is locked.
|
|
|
|
ee.dbIndex = ep.Index()
|
|
|
|
ee.dbExists = true
|
2015-06-05 16:31:12 -04:00
|
|
|
if ee.container != nil && ep.container != nil {
|
|
|
|
// we care only about the container id
|
|
|
|
ee.container.id = ep.container.id
|
|
|
|
} else {
|
|
|
|
// we still care only about the container id, but this is a short-cut to communicate join or leave operation
|
|
|
|
ee.container = ep.container
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ee.Unlock()
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|