Skip to content

Commit

Permalink
Merge pull request #28 from TrekkieCoder/main
Browse files Browse the repository at this point in the history
Fixes for IPAM range support
  • Loading branch information
TrekkieCoder authored Nov 23, 2024
2 parents 4e9d01b + e9c2cee commit 0caa884
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
21 changes: 20 additions & 1 deletion ipalloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ func addIPIndex(ip net.IP, index uint64) net.IP {
func diffIPIndex(baseIP net.IP, IP net.IP) uint64 {
index := uint64(0)
iplen := 0

if baseIP == nil || IP == nil {
return ^uint64(0)
}

if IsNetIPv4(baseIP.String()) {
iplen = IP4Len
} else {
Expand Down Expand Up @@ -360,6 +365,20 @@ func (ipa *IPAllocator) DeAllocateIP(cluster string, cidr string, idString, IPSt
return nil
}

// Contains - Check if IP is in IPrange
func (i *IPRange) Contains(IP net.IP) bool {
if i.isRange {
d1 := diffIPIndex(i.startIP, i.endIP)
d2 := diffIPIndex(i.startIP, IP)
if d2 > d1 {
return false
}
return true
} else {
return i.ipNet.Contains(IP)
}
}

// AddIPRange - Add a new IP Range for allocation in a cluster
func (ipa *IPAllocator) AddIPRange(cluster string, cidr string) error {
var ipCPool *IPClusterPool
Expand Down Expand Up @@ -406,7 +425,7 @@ func (ipa *IPAllocator) AddIPRange(cluster string, cidr string) error {
}

for _, ipr := range ipCPool.pool {
if ipr.ipNet.Contains(ip) {
if ipr.Contains(ip) {
return errors.New("existing IP Pool")
}
}
Expand Down
9 changes: 9 additions & 0 deletions lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,15 @@ func TestIPAlloc(t *testing.T) {
t.Fatal("Failed to add IP Range for 17.17.17.1-17.17.17.3", err)
}

err = ipa.AddIPRange(IPClusterDefault, "192.169.0.1-192.169.0.1")
if err == nil {
t.Fatal("Failed to add IP Range for 192.169.0.1-192.169.0.1", err)
}

err = ipa.AddIPRange(IPClusterDefault, "192.169.0.3-192.169.0.1")
if err == nil {
t.Fatal("Failed to add IP Range for 192.169.0.3-192.169.0.1", err)
}
}

func TestProber(t *testing.T) {
Expand Down

0 comments on commit 0caa884

Please sign in to comment.