Skip to content

Commit

Permalink
Assign sequential ips
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziomal12 committed Apr 10, 2024
1 parent 3ed2f08 commit c6600c5
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions management/server/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,41 @@ func (n *Network) Copy() *Network {
}
}

// AllocatePeerIP pics an available IP from an net.IPNet.
// This method considers already taken IPs and reuses IPs if there are gaps in takenIps
// E.g. if ipNet=100.30.0.0/16 and takenIps=[100.30.0.1, 100.30.0.4] then the result would be 100.30.0.2 or 100.30.0.3

// AllocatePeerIP picks an available IP from a net.IPNet.
// If sequential IPs assigning is enabled, it assigns the smallest available IP address.
// Otherwise, it assigns a random available IP address.
func AllocatePeerIP(ipNet net.IPNet, takenIps []net.IP) (net.IP, error) {
takenIPMap := make(map[string]struct{})
takenIPMap[ipNet.IP.String()] = struct{}{}
for _, ip := range takenIps {
takenIPMap[ip.String()] = struct{}{}
}
takenIPMap := make(map[string]struct{})
takenIPMap[ipNet.IP.String()] = struct{}{}
for _, ip := range takenIps {
takenIPMap[ip.String()] = struct{}{}
}

ips, _ := generateIPs(&ipNet, takenIPMap)
// Check if sequential IP assignment is enabled
assignSequentialIPs, _ := strconv.ParseBool(os.Getenv("NETBIRD_ASSIGN_SEQUENTIAL_IPS"))

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test_client_on_docker

undefined: strconv

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test_client_on_docker

undefined: os

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

undefined: strconv

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

undefined: os) (typecheck)

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

undefined: strconv

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

undefined: os) (typecheck)

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

undefined: strconv

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

undefined: os) (typecheck)

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

undefined: strconv

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

undefined: os) (typecheck)

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

undefined: strconv

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

undefined: os) (typecheck)

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

undefined: strconv

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

undefined: os) (typecheck)

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test

undefined: strconv

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test

undefined: os

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (sqlite)

undefined: strconv

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (sqlite)

undefined: os

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (jsonfile)

undefined: strconv

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (jsonfile)

undefined: os

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (amd64, sqlite)

undefined: strconv

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (amd64, sqlite)

undefined: os

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (386, sqlite)

undefined: strconv

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (386, sqlite)

undefined: os

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (amd64, jsonfile)

undefined: strconv

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (amd64, jsonfile)

undefined: os

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (386, jsonfile)

undefined: strconv

Check failure on line 101 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (386, jsonfile)

undefined: os

if len(ips) == 0 {
return nil, status.Errorf(status.PreconditionFailed, "failed allocating new IP for the ipNet %s - network is out of IPs", ipNet.String())
}
ips, _ := generateIPs(&ipNet, takenIPMap)

// pick a random IP
s := rand.NewSource(time.Now().Unix())
r := rand.New(s)
intn := r.Intn(len(ips))
if len(ips) == 0 {
return nil, status.Errorf(status.PreconditionFailed, "failed allocating new IP for the ipNet %s - network is out of IPs", ipNet.String())
}

return ips[intn], nil
// pick the smallest available IP if sequential assignment is enabled
if assignSequentialIPs {
return ips[0], nil
}

// pick a random IP
s := rand.NewSource(time.Now().Unix())
r := rand.New(s)
intn := r.Intn(len(ips))

return ips[intn], nil
}



// generateIPs generates a list of all possible IPs of the given network excluding IPs specified in the exclusion list
func generateIPs(ipNet *net.IPNet, exclusions map[string]struct{}) ([]net.IP, int) {

Expand Down

0 comments on commit c6600c5

Please sign in to comment.