Skip to content

Commit

Permalink
Fixed a bug when dynamically allocated IP could come from outside the…
Browse files Browse the repository at this point in the history
… defined pool for a fresh network

UTs added to cover the previously missing scenario
  • Loading branch information
Levovar committed Mar 2, 2020
1 parent 47961ee commit ecaf8a1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pkg/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,23 @@ func allocateAddress(pool *danmtypes.IpPool, alloc, reqType, cidr string) (strin
var allocatedIndex uint32
if reqType == DynamicAllocType {
begin, end := getAllocRangeBasedOnCidr(pool, subnet)
lastIp := net.ParseIP(pool.LastIp)
lastAllocatedIp := GetIndexOfIp(lastIp, subnet)
lastIpIndex := begin-1
if pool.LastIp != "" {
lastIp := net.ParseIP(pool.LastIp)
lastIpIndex = GetIndexOfIp(lastIp, subnet)
}
var doesAnyFreeIpExist bool
for i:=lastAllocatedIp+1; i<=end; i++ {
for i:=lastIpIndex+1; i<=end; i++ {
if !ba.Get(i) {
ba.Set(i)
allocatedIndex = i
doesAnyFreeIpExist = true
break
}
//Now let's look from the beginning until LastIp
if i == end && end != lastAllocatedIp {
if i == end && end != lastIpIndex {
i = begin
end = lastAllocatedIp
end = lastIpIndex
}
}
if !doesAnyFreeIpExist {
Expand Down
4 changes: 4 additions & 0 deletions test/uts/ipam_test/ipam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var testNets = []danmtypes.DanmNet {
danmtypes.DanmNet {ObjectMeta: meta_v1.ObjectMeta {Name: "error"},Spec: danmtypes.DanmNetSpec{NetworkID: "error", Options: danmtypes.DanmNetOption{Cidr: "192.168.1.64/26"}}},
danmtypes.DanmNet {ObjectMeta: meta_v1.ObjectMeta {Name: "staticFirst"},Spec: danmtypes.DanmNetSpec{NetworkID: "cidr", Options: danmtypes.DanmNetOption{Cidr: "192.168.1.64/26"}}},
danmtypes.DanmNet {ObjectMeta: meta_v1.ObjectMeta {Name: "fullinitv6"},Spec: danmtypes.DanmNetSpec{NetworkID: "net6", Options: danmtypes.DanmNetOption{Cidr: "192.168.1.64/26", Net6: "2a00:8a00:a000:1193::/64"}}},
danmtypes.DanmNet {ObjectMeta: meta_v1.ObjectMeta {Name: "v4RestrictedPool"},Spec: danmtypes.DanmNetSpec{NetworkID: "v4RestrictedPool", Options: danmtypes.DanmNetOption{Cidr: "192.168.1.64/26", Pool: danmtypes.IpPool{Start: "192.168.1.70", End: "192.168.1.80"}}}},
danmtypes.DanmNet {ObjectMeta: meta_v1.ObjectMeta {Name: "v4RestrictedPoolWithLastIp"},Spec: danmtypes.DanmNetSpec{NetworkID: "v4RestrictedPoolWithLastIp", Options: danmtypes.DanmNetOption{Cidr: "192.168.1.64/26", Pool: danmtypes.IpPool{Start: "192.168.1.70", End: "192.168.1.80", LastIp: "192.168.1.75"}}}},
}

var reserveTcs = []struct {
Expand Down Expand Up @@ -66,6 +68,8 @@ var reserveTcs = []struct {
{"resolvedConflictDuringUpdate", 5, "dynamic", "", "192.168.1.65/26", "", false, 2},
{"unresolvedConflictAfterUpdate", 6, "dynamic", "", "", "", true, 1},
{"errorUpdate", 10, "dynamic", "", "", "", true, 1},
{"dyanmicV4FromAllocationPool", 13, "dynamic", "", "192.168.1.70/26", "", false, 1},
{"dyanmicV4FromAllocationPoolWithLastIpSet", 14, "dynamic", "", "192.168.1.76/26", "", false, 1},
}

var freeTcs = []struct {
Expand Down

0 comments on commit ecaf8a1

Please sign in to comment.