Skip to content

Commit

Permalink
ipalloc implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
PacketCrunch committed Jan 17, 2023
1 parent c8e22a5 commit 1a012a4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 4 additions & 2 deletions ipalloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
type IPRange struct {
ipNet net.IPNet
freeID *Counter
fOK bool
first uint64
ident map[uint32]struct{}
}
Expand Down Expand Up @@ -108,13 +109,14 @@ func (ipa *IPAllocator) AllocateNewIP(cluster string, cidr string, id uint32) (n
}
}

if id == 0 || ipr.first == 0 {
if id == 0 || !ipr.fOK {
newIndex, err = ipr.freeID.GetCounter()
if err != nil {
return net.IP{0, 0, 0, 0}, errors.New("IP Alloc counter failure")
}
if ipr.first == 0 {
if !ipr.fOK {
ipr.first = newIndex
ipr.fOK = true
}
} else {
newIndex = ipr.first
Expand Down
9 changes: 9 additions & 0 deletions lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,13 @@ func TestIPAlloc(t *testing.T) {
t.Fatalf("IP Alloc failed for 74.125.227.24:%s", ip1.String())
}

ip1, err = ipa.AllocateNewIP(IPClusterDefault, "74.125.227.24/29", 1)
if err != nil {
t.Fatalf("IP Alloc failed for 100.100.100.1/32:%s", err)
}

if ip1.String() != "74.125.227.24" {
t.Fatalf("IP Alloc failed for 74.125.227.24:%s", ip1.String())
}

}

0 comments on commit 1a012a4

Please sign in to comment.