2015-05-31 14:49:11 -04:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-09-22 13:49:15 -04:00
|
|
|
"time"
|
2015-05-31 14:49:11 -04:00
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-06-18 11:18:17 -04:00
|
|
|
"github.com/docker/libkv/store"
|
2015-09-16 07:39:46 -04:00
|
|
|
"github.com/docker/libnetwork/config"
|
2015-05-31 14:49:11 -04:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
|
|
|
)
|
|
|
|
|
2015-09-16 07:39:46 -04:00
|
|
|
var (
|
2015-09-22 13:49:15 -04:00
|
|
|
defaultBoltTimeout = 3 * time.Second
|
2015-09-16 07:39:46 -04:00
|
|
|
defaultLocalStoreConfig = config.DatastoreCfg{
|
|
|
|
Embedded: true,
|
|
|
|
Client: config.DatastoreClientCfg{
|
|
|
|
Provider: "boltdb",
|
|
|
|
Address: defaultPrefix + "/boltdb.db",
|
|
|
|
Config: &store.Config{
|
2015-09-22 13:49:15 -04:00
|
|
|
Bucket: "libnetwork",
|
|
|
|
ConnectionTimeout: defaultBoltTimeout,
|
2015-09-16 07:39:46 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *controller) validateGlobalStoreConfig() bool {
|
|
|
|
return c.cfg != nil && c.cfg.GlobalStore.Client.Provider != "" && c.cfg.GlobalStore.Client.Address != ""
|
2015-06-11 09:52:46 -04:00
|
|
|
}
|
|
|
|
|
2015-09-16 07:39:46 -04:00
|
|
|
func (c *controller) initGlobalStore() error {
|
2015-06-05 16:31:12 -04:00
|
|
|
c.Lock()
|
|
|
|
cfg := c.cfg
|
|
|
|
c.Unlock()
|
2015-09-16 07:39:46 -04:00
|
|
|
if !c.validateGlobalStoreConfig() {
|
|
|
|
return fmt.Errorf("globalstore initialization requires a valid configuration")
|
2015-05-31 14:49:11 -04:00
|
|
|
}
|
|
|
|
|
2015-09-16 07:39:46 -04:00
|
|
|
store, err := datastore.NewDataStore(&cfg.GlobalStore)
|
2015-05-31 14:49:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Lock()
|
2015-09-16 07:39:46 -04:00
|
|
|
c.globalStore = store
|
2015-05-31 14:49:11 -04:00
|
|
|
c.Unlock()
|
2015-06-18 11:18:17 -04:00
|
|
|
|
2015-09-16 07:42:35 -04:00
|
|
|
nws, err := c.getNetworksFromStore(true)
|
2015-06-18 11:18:17 -04:00
|
|
|
if err == nil {
|
|
|
|
c.processNetworkUpdate(nws, nil)
|
|
|
|
} else if err != datastore.ErrKeyNotFound {
|
2015-09-16 07:39:46 -04:00
|
|
|
log.Warnf("failed to read networks from globalstore during init : %v", err)
|
2015-06-18 11:18:17 -04:00
|
|
|
}
|
2015-06-17 12:13:31 -04:00
|
|
|
return c.watchNetworks()
|
2015-05-31 14:49:11 -04:00
|
|
|
}
|
|
|
|
|
2015-09-16 07:39:46 -04:00
|
|
|
func (c *controller) initLocalStore() error {
|
|
|
|
c.Lock()
|
|
|
|
cfg := c.cfg
|
|
|
|
c.Unlock()
|
|
|
|
localStore, err := datastore.NewDataStore(c.getLocalStoreConfig(cfg))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Lock()
|
|
|
|
c.localStore = localStore
|
|
|
|
c.Unlock()
|
|
|
|
|
2015-09-16 07:42:35 -04:00
|
|
|
nws, err := c.getNetworksFromStore(false)
|
2015-09-16 07:39:46 -04:00
|
|
|
if err == nil {
|
|
|
|
c.processNetworkUpdate(nws, nil)
|
|
|
|
} else if err != datastore.ErrKeyNotFound {
|
|
|
|
log.Warnf("failed to read networks from localstore during init : %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-16 07:42:35 -04:00
|
|
|
func (c *controller) getNetworksFromStore(global bool) ([]*store.KVPair, error) {
|
|
|
|
var cs datastore.DataStore
|
2015-06-18 11:18:17 -04:00
|
|
|
c.Lock()
|
2015-09-16 07:42:35 -04:00
|
|
|
if global {
|
|
|
|
cs = c.globalStore
|
|
|
|
} else {
|
|
|
|
cs = c.localStore
|
|
|
|
}
|
2015-06-18 11:18:17 -04:00
|
|
|
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) 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-07-02 01:00:48 -04:00
|
|
|
_, err := n.EndpointByID(id)
|
2015-06-05 16:31:12 -04:00
|
|
|
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-09-16 07:42:35 -04:00
|
|
|
func (c *controller) updateToStore(kvObject datastore.KV) error {
|
2015-09-22 10:09:39 -04:00
|
|
|
if kvObject.Skip() {
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-16 07:42:35 -04:00
|
|
|
cs := c.getDataStore(kvObject.DataScope())
|
2015-05-31 14:49:11 -04:00
|
|
|
if cs == nil {
|
2015-09-16 07:42:35 -04:00
|
|
|
log.Debugf("datastore not initialized. kv object %s is not added to the store", datastore.Key(kvObject.Key()...))
|
2015-05-31 14:49:11 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-16 07:42:35 -04:00
|
|
|
return cs.PutObjectAtomic(kvObject)
|
2015-05-31 14:49:11 -04:00
|
|
|
}
|
|
|
|
|
2015-09-16 07:42:35 -04:00
|
|
|
func (c *controller) deleteFromStore(kvObject datastore.KV) error {
|
2015-09-22 10:09:39 -04:00
|
|
|
if kvObject.Skip() {
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-16 07:42:35 -04:00
|
|
|
cs := c.getDataStore(kvObject.DataScope())
|
2015-06-01 12:43:24 -04:00
|
|
|
if cs == nil {
|
2015-09-16 07:42:35 -04:00
|
|
|
log.Debugf("datastore not initialized. kv object %s is not deleted from datastore", datastore.Key(kvObject.Key()...))
|
2015-06-01 12:43:24 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-16 07:42:35 -04:00
|
|
|
if err := cs.DeleteObjectAtomic(kvObject); 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 {
|
2015-09-16 07:39:46 -04:00
|
|
|
if !c.validateGlobalStoreConfig() {
|
2015-06-17 12:13:31 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-31 14:49:11 -04:00
|
|
|
c.Lock()
|
2015-09-16 07:39:46 -04:00
|
|
|
cs := c.globalStore
|
2015-05-31 14:49:11 -04:00
|
|
|
c.Unlock()
|
|
|
|
|
2015-08-17 04:07:43 -04:00
|
|
|
networkKey := datastore.Key(datastore.NetworkKeyPrefix)
|
|
|
|
if err := ensureKeys(networkKey, cs); err != nil {
|
|
|
|
return fmt.Errorf("failed to ensure if the network keys are valid and present in store: %v", err)
|
|
|
|
}
|
|
|
|
nwPairs, err := cs.KVStore().WatchTree(networkKey, 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-09-16 07:39:46 -04:00
|
|
|
if v.isGlobalScoped() {
|
2015-06-19 16:34:16 -04:00
|
|
|
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{}
|
2015-09-16 07:39:46 -04:00
|
|
|
if err := c.globalStore.GetObject(datastore.Key(existing.Key()...), &tmp); err != datastore.ErrKeyNotFound {
|
2015-06-17 12:13:31 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := existing.deleteNetwork(); err != nil {
|
|
|
|
log.Debugf("Delete failed %s: %s", existing.name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) watchEndpoints() error {
|
2015-09-22 10:09:39 -04:00
|
|
|
if n.Skip() || !n.ctrlr.validateGlobalStoreConfig() {
|
2015-06-17 12:13:31 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
n.Lock()
|
2015-09-16 07:39:46 -04:00
|
|
|
cs := n.ctrlr.globalStore
|
2015-06-17 12:13:31 -04:00
|
|
|
tmp := endpoint{network: n}
|
|
|
|
n.stopWatchCh = make(chan struct{})
|
|
|
|
stopCh := n.stopWatchCh
|
|
|
|
n.Unlock()
|
|
|
|
|
2015-08-17 04:07:43 -04:00
|
|
|
endpointKey := datastore.Key(tmp.KeyPrefix()...)
|
|
|
|
if err := ensureKeys(endpointKey, cs); err != nil {
|
|
|
|
return fmt.Errorf("failed to ensure if the endpoint keys are valid and present in store: %v", err)
|
|
|
|
}
|
|
|
|
epPairs, err := cs.KVStore().WatchTree(endpointKey, stopCh)
|
2015-06-17 12:13:31 -04:00
|
|
|
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-09-16 07:39:46 -04:00
|
|
|
if v.network.isGlobalScoped() {
|
2015-06-19 16:34:16 -04:00
|
|
|
tmpview[k] = v
|
|
|
|
}
|
2015-06-17 12:13:31 -04:00
|
|
|
}
|
2015-09-16 07:39:46 -04:00
|
|
|
n.ctrlr.processEndpointsUpdate(eps, &tmpview)
|
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
|
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
existing, _ := n.EndpointByID(ep.id)
|
2015-06-05 16:31:12 -04:00
|
|
|
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-07-02 01:00:48 -04:00
|
|
|
ee.sandboxID = ep.sandboxID
|
2015-06-05 16:31:12 -04:00
|
|
|
}
|
|
|
|
ee.Unlock()
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2015-08-17 04:07:43 -04:00
|
|
|
|
|
|
|
func ensureKeys(key string, cs datastore.DataStore) error {
|
|
|
|
exists, err := cs.KVStore().Exists(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return cs.KVStore().Put(key, []byte{}, nil)
|
|
|
|
}
|
2015-09-16 07:39:46 -04:00
|
|
|
|
|
|
|
func (c *controller) getLocalStoreConfig(cfg *config.Config) *config.DatastoreCfg {
|
|
|
|
if cfg != nil && cfg.LocalStore.Client.Provider != "" && cfg.LocalStore.Client.Address != "" {
|
|
|
|
return &cfg.LocalStore
|
|
|
|
}
|
|
|
|
return &defaultLocalStoreConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) getDataStore(dataScope datastore.DataScope) (dataStore datastore.DataStore) {
|
|
|
|
c.Lock()
|
|
|
|
if dataScope == datastore.GlobalScope {
|
|
|
|
dataStore = c.globalStore
|
|
|
|
} else if dataScope == datastore.LocalScope {
|
|
|
|
dataStore = c.localStore
|
|
|
|
}
|
|
|
|
c.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) processEndpointsUpdate(eps []*store.KVPair, prune *endpointTable) {
|
|
|
|
for _, epe := range eps {
|
|
|
|
var ep endpoint
|
|
|
|
err := json.Unmarshal(epe.Value, &ep)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if prune != nil {
|
|
|
|
delete(*prune, ep.id)
|
|
|
|
}
|
|
|
|
ep.SetIndex(epe.LastIndex)
|
|
|
|
if nid, err := ep.networkIDFromKey(epe.Key); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
if n, err := c.NetworkByID(nid); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
ep.network = n.(*network)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.processEndpointUpdate(&ep) {
|
|
|
|
err = c.newEndpointFromStore(epe.Key, &ep)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|