2015-06-16 17:46:51 -04:00
|
|
|
package ipam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/libnetwork/datastore"
|
|
|
|
"github.com/docker/libnetwork/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Key provides the Key to be used in KV Store
|
2015-10-05 07:24:44 -04:00
|
|
|
func (aSpace *addrSpace) Key() []string {
|
|
|
|
aSpace.Lock()
|
|
|
|
defer aSpace.Unlock()
|
|
|
|
return []string{aSpace.id}
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// KeyPrefix returns the immediate parent key that can be used for tree walk
|
2015-10-05 07:24:44 -04:00
|
|
|
func (aSpace *addrSpace) KeyPrefix() []string {
|
|
|
|
aSpace.Lock()
|
|
|
|
defer aSpace.Unlock()
|
2015-09-22 16:20:55 -04:00
|
|
|
return []string{dsConfigKey}
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value marshals the data to be stored in the KV store
|
2015-10-05 07:24:44 -04:00
|
|
|
func (aSpace *addrSpace) Value() []byte {
|
|
|
|
b, err := json.Marshal(aSpace)
|
2015-06-16 17:46:51 -04:00
|
|
|
if err != nil {
|
2015-10-03 21:51:53 -04:00
|
|
|
log.Warnf("Failed to marshal ipam configured pools: %v", err)
|
2015-06-16 17:46:51 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2015-06-18 18:13:38 -04:00
|
|
|
// SetValue unmarshalls the data from the KV store.
|
2015-10-05 07:24:44 -04:00
|
|
|
func (aSpace *addrSpace) SetValue(value []byte) error {
|
|
|
|
rc := &addrSpace{subnets: make(map[SubnetKey]*PoolData)}
|
2015-10-03 21:51:53 -04:00
|
|
|
if err := json.Unmarshal(value, rc); err != nil {
|
2015-09-22 16:20:55 -04:00
|
|
|
return err
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
2015-10-05 07:24:44 -04:00
|
|
|
aSpace.subnets = rc.subnets
|
2015-09-22 16:20:55 -04:00
|
|
|
return nil
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Index returns the latest DB Index as seen by this object
|
2015-10-05 07:24:44 -04:00
|
|
|
func (aSpace *addrSpace) Index() uint64 {
|
|
|
|
aSpace.Lock()
|
|
|
|
defer aSpace.Unlock()
|
|
|
|
return aSpace.dbIndex
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetIndex method allows the datastore to store the latest DB Index into this object
|
2015-10-05 07:24:44 -04:00
|
|
|
func (aSpace *addrSpace) SetIndex(index uint64) {
|
|
|
|
aSpace.Lock()
|
|
|
|
aSpace.dbIndex = index
|
|
|
|
aSpace.dbExists = true
|
|
|
|
aSpace.Unlock()
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
|
|
|
|
2015-06-18 18:13:38 -04:00
|
|
|
// Exists method is true if this object has been stored in the DB.
|
2015-10-05 07:24:44 -04:00
|
|
|
func (aSpace *addrSpace) Exists() bool {
|
|
|
|
aSpace.Lock()
|
|
|
|
defer aSpace.Unlock()
|
|
|
|
return aSpace.dbExists
|
2015-06-18 18:13:38 -04:00
|
|
|
}
|
|
|
|
|
2015-09-22 10:09:39 -04:00
|
|
|
// Skip provides a way for a KV Object to avoid persisting it in the KV Store
|
2015-10-05 07:24:44 -04:00
|
|
|
func (aSpace *addrSpace) Skip() bool {
|
2015-09-22 10:09:39 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:24:44 -04:00
|
|
|
func (a *Allocator) getStore(as string) datastore.DataStore {
|
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
|
|
|
|
2015-10-12 13:52:58 -04:00
|
|
|
if aSpace, ok := a.addrSpaces[as]; ok {
|
|
|
|
return aSpace.ds
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-10-05 07:24:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Allocator) getAddressSpaceFromStore(as string) (*addrSpace, error) {
|
|
|
|
store := a.getStore(as)
|
2016-03-01 18:05:36 -05:00
|
|
|
|
|
|
|
// IPAM may not have a valid store. In such cases it is just in-memory state.
|
2015-10-05 07:24:44 -04:00
|
|
|
if store == nil {
|
2016-03-01 18:05:36 -05:00
|
|
|
return nil, nil
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
2015-10-05 07:24:44 -04:00
|
|
|
|
|
|
|
pc := &addrSpace{id: dsConfigKey + "/" + as, ds: store, alloc: a}
|
|
|
|
if err := store.GetObject(datastore.Key(pc.Key()...), pc); err != nil {
|
|
|
|
if err == datastore.ErrKeyNotFound {
|
|
|
|
return nil, nil
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
2015-10-05 07:24:44 -04:00
|
|
|
|
2015-10-20 20:05:01 -04:00
|
|
|
return nil, types.InternalErrorf("could not get pools config from store: %v", err)
|
2015-10-05 07:24:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return pc, nil
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 07:24:44 -04:00
|
|
|
func (a *Allocator) writeToStore(aSpace *addrSpace) error {
|
|
|
|
store := aSpace.store()
|
2016-03-01 18:05:36 -05:00
|
|
|
|
|
|
|
// IPAM may not have a valid store. In such cases it is just in-memory state.
|
2015-10-05 07:24:44 -04:00
|
|
|
if store == nil {
|
2016-03-01 18:05:36 -05:00
|
|
|
return nil
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
2015-10-05 07:24:44 -04:00
|
|
|
|
|
|
|
err := store.PutObjectAtomic(aSpace)
|
2015-10-03 21:51:53 -04:00
|
|
|
if err == datastore.ErrKeyModified {
|
|
|
|
return types.RetryErrorf("failed to perform atomic write (%v). retry might fix the error", err)
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 07:24:44 -04:00
|
|
|
return err
|
2015-10-03 21:51:53 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 07:24:44 -04:00
|
|
|
func (a *Allocator) deleteFromStore(aSpace *addrSpace) error {
|
|
|
|
store := aSpace.store()
|
2016-03-01 18:05:36 -05:00
|
|
|
|
|
|
|
// IPAM may not have a valid store. In such cases it is just in-memory state.
|
2015-10-05 07:24:44 -04:00
|
|
|
if store == nil {
|
2016-03-01 18:05:36 -05:00
|
|
|
return nil
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 07:24:44 -04:00
|
|
|
return store.DeleteObjectAtomic(aSpace)
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
2015-09-16 07:39:46 -04:00
|
|
|
|
|
|
|
// DataScope method returns the storage scope of the datastore
|
2015-10-05 07:24:44 -04:00
|
|
|
func (aSpace *addrSpace) DataScope() string {
|
|
|
|
aSpace.Lock()
|
|
|
|
defer aSpace.Unlock()
|
|
|
|
|
|
|
|
return aSpace.scope
|
2015-09-16 07:39:46 -04:00
|
|
|
}
|