|
@@ -76,8 +76,7 @@ func NewAllocator(lcDs, glDs datastore.DataStore) (*Allocator, error) {
|
|
func (a *Allocator) refresh(as string) error {
|
|
func (a *Allocator) refresh(as string) error {
|
|
aSpace, err := a.getAddressSpaceFromStore(as)
|
|
aSpace, err := a.getAddressSpaceFromStore(as)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return fmt.Errorf("error getting pools config from store during init: %v",
|
|
|
|
- err)
|
|
|
|
|
|
+ return types.InternalErrorf("error getting pools config from store: %v", err)
|
|
}
|
|
}
|
|
|
|
|
|
if aSpace == nil {
|
|
if aSpace == nil {
|
|
@@ -239,7 +238,7 @@ func (a *Allocator) insertBitMask(key SubnetKey, pool *net.IPNet) error {
|
|
|
|
|
|
store := a.getStore(key.AddressSpace)
|
|
store := a.getStore(key.AddressSpace)
|
|
if store == nil {
|
|
if store == nil {
|
|
- return fmt.Errorf("could not find store for address space %s while inserting bit mask", key.AddressSpace)
|
|
|
|
|
|
+ return types.InternalErrorf("could not find store for address space %s while inserting bit mask", key.AddressSpace)
|
|
}
|
|
}
|
|
|
|
|
|
ipVer := getAddressVersion(pool.IP)
|
|
ipVer := getAddressVersion(pool.IP)
|
|
@@ -279,7 +278,7 @@ func (a *Allocator) retrieveBitmask(k SubnetKey, n *net.IPNet) (*bitseq.Handle,
|
|
if !ok {
|
|
if !ok {
|
|
log.Debugf("Retrieving bitmask (%s, %s)", k.String(), n.String())
|
|
log.Debugf("Retrieving bitmask (%s, %s)", k.String(), n.String())
|
|
if err := a.insertBitMask(k, n); err != nil {
|
|
if err := a.insertBitMask(k, n); err != nil {
|
|
- return nil, fmt.Errorf("could not find bitmask in datastore for %s", k.String())
|
|
|
|
|
|
+ return nil, types.InternalErrorf("could not find bitmask in datastore for %s", k.String())
|
|
}
|
|
}
|
|
a.Lock()
|
|
a.Lock()
|
|
bm = a.addresses[k]
|
|
bm = a.addresses[k]
|
|
@@ -306,7 +305,7 @@ func (a *Allocator) getPredefinedPool(as string, ipV6 bool) (*net.IPNet, error)
|
|
}
|
|
}
|
|
|
|
|
|
if as != localAddressSpace && as != globalAddressSpace {
|
|
if as != localAddressSpace && as != globalAddressSpace {
|
|
- return nil, fmt.Errorf("no default pool available for non-default address spaces")
|
|
|
|
|
|
+ return nil, types.NotImplementedErrorf("no default pool availbale for non-default addresss spaces")
|
|
}
|
|
}
|
|
|
|
|
|
aSpace, err := a.getAddrSpace(as)
|
|
aSpace, err := a.getAddrSpace(as)
|
|
@@ -378,7 +377,7 @@ func (a *Allocator) RequestAddress(poolID string, prefAddress net.IP, opts map[s
|
|
|
|
|
|
bm, err := a.retrieveBitmask(k, c.Pool)
|
|
bm, err := a.retrieveBitmask(k, c.Pool)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, nil, fmt.Errorf("could not find bitmask in datastore for %s on address %v request from pool %s: %v",
|
|
|
|
|
|
+ return nil, nil, types.InternalErrorf("could not find bitmask in datastore for %s on address %v request from pool %s: %v",
|
|
k.String(), prefAddress, poolID, err)
|
|
k.String(), prefAddress, poolID, err)
|
|
}
|
|
}
|
|
ip, err := a.getAddress(p.Pool, bm, prefAddress, p.Range)
|
|
ip, err := a.getAddress(p.Pool, bm, prefAddress, p.Range)
|
|
@@ -410,12 +409,12 @@ func (a *Allocator) ReleaseAddress(poolID string, address net.IP) error {
|
|
p, ok := aSpace.subnets[k]
|
|
p, ok := aSpace.subnets[k]
|
|
if !ok {
|
|
if !ok {
|
|
aSpace.Unlock()
|
|
aSpace.Unlock()
|
|
- return ipamapi.ErrBadPool
|
|
|
|
|
|
+ return types.NotFoundErrorf("cannot find address pool for poolID:%s", poolID)
|
|
}
|
|
}
|
|
|
|
|
|
if address == nil {
|
|
if address == nil {
|
|
aSpace.Unlock()
|
|
aSpace.Unlock()
|
|
- return ipamapi.ErrInvalidRequest
|
|
|
|
|
|
+ return types.BadRequestErrorf("invalid address: nil")
|
|
}
|
|
}
|
|
|
|
|
|
if !p.Pool.Contains(address) {
|
|
if !p.Pool.Contains(address) {
|
|
@@ -434,12 +433,12 @@ func (a *Allocator) ReleaseAddress(poolID string, address net.IP) error {
|
|
|
|
|
|
h, err := types.GetHostPartIP(address, mask)
|
|
h, err := types.GetHostPartIP(address, mask)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return fmt.Errorf("failed to release address %s: %v", address.String(), err)
|
|
|
|
|
|
+ return types.InternalErrorf("failed to release address %s: %v", address.String(), err)
|
|
}
|
|
}
|
|
|
|
|
|
bm, err := a.retrieveBitmask(k, c.Pool)
|
|
bm, err := a.retrieveBitmask(k, c.Pool)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return fmt.Errorf("could not find bitmask in datastore for %s on address %v release from pool %s: %v",
|
|
|
|
|
|
+ return types.InternalErrorf("could not find bitmask in datastore for %s on address %v release from pool %s: %v",
|
|
k.String(), address, poolID, err)
|
|
k.String(), address, poolID, err)
|
|
}
|
|
}
|
|
|
|
|
|
@@ -463,19 +462,25 @@ func (a *Allocator) getAddress(nw *net.IPNet, bitmask *bitseq.Handle, prefAddres
|
|
} else if prefAddress != nil {
|
|
} else if prefAddress != nil {
|
|
hostPart, e := types.GetHostPartIP(prefAddress, base.Mask)
|
|
hostPart, e := types.GetHostPartIP(prefAddress, base.Mask)
|
|
if e != nil {
|
|
if e != nil {
|
|
- return nil, fmt.Errorf("failed to allocate preferred address %s: %v", prefAddress.String(), e)
|
|
|
|
|
|
+ return nil, types.InternalErrorf("failed to allocate preferred address %s: %v", prefAddress.String(), e)
|
|
}
|
|
}
|
|
ordinal = ipToUint64(types.GetMinimalIP(hostPart))
|
|
ordinal = ipToUint64(types.GetMinimalIP(hostPart))
|
|
err = bitmask.Set(ordinal)
|
|
err = bitmask.Set(ordinal)
|
|
} else {
|
|
} else {
|
|
ordinal, err = bitmask.SetAnyInRange(ipr.Start, ipr.End)
|
|
ordinal, err = bitmask.SetAnyInRange(ipr.Start, ipr.End)
|
|
}
|
|
}
|
|
- if err != nil {
|
|
|
|
|
|
+
|
|
|
|
+ switch err {
|
|
|
|
+ case nil:
|
|
|
|
+ // Convert IP ordinal for this subnet into IP address
|
|
|
|
+ return generateAddress(ordinal, base), nil
|
|
|
|
+ case bitseq.ErrBitAllocated:
|
|
|
|
+ return nil, ipamapi.ErrIPAlreadyAllocated
|
|
|
|
+ case bitseq.ErrNoBitAvailable:
|
|
return nil, ipamapi.ErrNoAvailableIPs
|
|
return nil, ipamapi.ErrNoAvailableIPs
|
|
|
|
+ default:
|
|
|
|
+ return nil, err
|
|
}
|
|
}
|
|
-
|
|
|
|
- // Convert IP ordinal for this subnet into IP address
|
|
|
|
- return generateAddress(ordinal, base), nil
|
|
|
|
}
|
|
}
|
|
|
|
|
|
// DumpDatabase dumps the internal info
|
|
// DumpDatabase dumps the internal info
|