From 4289ea637a9025837327fa733cc4fac27715ff49 Mon Sep 17 00:00:00 2001 From: Jana Radhakrishnan Date: Tue, 1 Mar 2016 15:05:36 -0800 Subject: [PATCH] Make IPAM work even without a backing store In general the core IPAM and bitseq implementation has very little assumptions about the presence of a backing store. But there are a few places where this assumption exists and this makes it not useful as a simple in-memory allocator. This PR removes those false assumptions. Signed-off-by: Jana Radhakrishnan --- libnetwork/ipam/allocator.go | 14 ++++++-------- libnetwork/ipam/store.go | 12 +++++++++--- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/libnetwork/ipam/allocator.go b/libnetwork/ipam/allocator.go index 6641a4cc8b..9ff3301baa 100644 --- a/libnetwork/ipam/allocator.go +++ b/libnetwork/ipam/allocator.go @@ -58,9 +58,6 @@ func NewAllocator(lcDs, glDs datastore.DataStore) (*Allocator, error) { {localAddressSpace, lcDs}, {globalAddressSpace, glDs}, } { - if aspc.ds == nil { - continue - } a.initializeAddressSpace(aspc.as, aspc.ds) } @@ -143,6 +140,11 @@ func (a *Allocator) checkConsistency(as string) { } func (a *Allocator) initializeAddressSpace(as string, ds datastore.DataStore) error { + scope := "" + if ds != nil { + scope = ds.Scope() + } + a.Lock() if _, ok := a.addrSpaces[as]; ok { a.Unlock() @@ -151,7 +153,7 @@ func (a *Allocator) initializeAddressSpace(as string, ds datastore.DataStore) er a.addrSpaces[as] = &addrSpace{ subnets: map[SubnetKey]*PoolData{}, id: dsConfigKey + "/" + as, - scope: ds.Scope(), + scope: scope, ds: ds, alloc: a, } @@ -313,10 +315,6 @@ func (a *Allocator) insertBitMask(key SubnetKey, pool *net.IPNet) error { //log.Debugf("Inserting bitmask (%s, %s)", key.String(), pool.String()) store := a.getStore(key.AddressSpace) - if store == nil { - return types.InternalErrorf("could not find store for address space %s while inserting bit mask", key.AddressSpace) - } - ipVer := getAddressVersion(pool.IP) ones, bits := pool.Mask.Size() numAddresses := uint64(1 << uint(bits-ones)) diff --git a/libnetwork/ipam/store.go b/libnetwork/ipam/store.go index ba44ef9dd9..02f627b66e 100644 --- a/libnetwork/ipam/store.go +++ b/libnetwork/ipam/store.go @@ -82,8 +82,10 @@ func (a *Allocator) getStore(as string) datastore.DataStore { func (a *Allocator) getAddressSpaceFromStore(as string) (*addrSpace, error) { store := a.getStore(as) + + // IPAM may not have a valid store. In such cases it is just in-memory state. if store == nil { - return nil, types.InternalErrorf("store for address space %s not found", as) + return nil, nil } pc := &addrSpace{id: dsConfigKey + "/" + as, ds: store, alloc: a} @@ -100,8 +102,10 @@ func (a *Allocator) getAddressSpaceFromStore(as string) (*addrSpace, error) { func (a *Allocator) writeToStore(aSpace *addrSpace) error { store := aSpace.store() + + // IPAM may not have a valid store. In such cases it is just in-memory state. if store == nil { - return types.InternalErrorf("invalid store while trying to write %s address space", aSpace.DataScope()) + return nil } err := store.PutObjectAtomic(aSpace) @@ -114,8 +118,10 @@ func (a *Allocator) writeToStore(aSpace *addrSpace) error { func (a *Allocator) deleteFromStore(aSpace *addrSpace) error { store := aSpace.store() + + // IPAM may not have a valid store. In such cases it is just in-memory state. if store == nil { - return types.InternalErrorf("invalid store while trying to delete %s address space", aSpace.DataScope()) + return nil } return store.DeleteObjectAtomic(aSpace)