Bläddra i källkod

Merge pull request #1119 from mrjana/ipam

Make IPAM work even without a backing store
Alessandro Boch 9 år sedan
förälder
incheckning
45bf52ed79
2 ändrade filer med 15 tillägg och 11 borttagningar
  1. 6 8
      libnetwork/ipam/allocator.go
  2. 9 3
      libnetwork/ipam/store.go

+ 6 - 8
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))

+ 9 - 3
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)