From 6a97441a44378839aa8d5112439fdd2a19ae4409 Mon Sep 17 00:00:00 2001 From: Ziomal12 Date: Wed, 10 Apr 2024 18:25:15 +0200 Subject: [PATCH 1/4] Assign sequential ips --- management/server/network.go | 45 ++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/management/server/network.go b/management/server/network.go index e8c40242b79..53d3a353c29 100644 --- a/management/server/network.go +++ b/management/server/network.go @@ -113,30 +113,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")) - 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) + + 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()) + } + + // 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)) + // pick a random IP + s := rand.NewSource(time.Now().Unix()) + r := rand.New(s) + intn := r.Intn(len(ips)) - return ips[intn], nil + 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) { From 9752eaa14ccc16556c793f0976b2017689fb472e Mon Sep 17 00:00:00 2001 From: Ziomal12 Date: Wed, 10 Apr 2024 18:34:42 +0200 Subject: [PATCH 2/4] Update network.go --- management/server/network.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/management/server/network.go b/management/server/network.go index 53d3a353c29..1a17286579f 100644 --- a/management/server/network.go +++ b/management/server/network.go @@ -125,7 +125,11 @@ func AllocatePeerIP(ipNet net.IPNet, takenIps []net.IP) (net.IP, error) { } // Check if sequential IP assignment is enabled - assignSequentialIPs, _ := strconv.ParseBool(os.Getenv("NETBIRD_ASSIGN_SEQUENTIAL_IPS")) + assignSequentialIPsStr := os.Getenv("NETBIRD_ASSIGN_SEQUENTIAL_IPS") + assignSequentialIPs, err := strconv.Atoi(assignSequentialIPsStr) + if err != nil { + return nil, err + } ips, _ := generateIPs(&ipNet, takenIPMap) From b57a5bebe05564d6962c927905ee942be2e90ea5 Mon Sep 17 00:00:00 2001 From: Ziomal12 Date: Wed, 10 Apr 2024 18:46:48 +0200 Subject: [PATCH 3/4] strconv --- management/server/network.go | 1 + 1 file changed, 1 insertion(+) diff --git a/management/server/network.go b/management/server/network.go index 1a17286579f..5c04156e96e 100644 --- a/management/server/network.go +++ b/management/server/network.go @@ -6,6 +6,7 @@ import ( "sync" "time" "os" + "strconv" "github.com/c-robinson/iplib" "github.com/rs/xid" From 9dc091a5f72186a667ff30931f7dd4a9fe34a1f7 Mon Sep 17 00:00:00 2001 From: Ziomal12 Date: Wed, 10 Apr 2024 18:51:46 +0200 Subject: [PATCH 4/4] update if --- management/server/network.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/management/server/network.go b/management/server/network.go index 5c04156e96e..c7df1041e55 100644 --- a/management/server/network.go +++ b/management/server/network.go @@ -139,9 +139,9 @@ func AllocatePeerIP(ipNet net.IPNet, takenIps []net.IP) (net.IP, error) { } // pick the smallest available IP if sequential assignment is enabled - if assignSequentialIPs { + if assignSequentialIPs == 1 { return ips[0], nil - } + } else { // pick a random IP s := rand.NewSource(time.Now().Unix()) @@ -149,6 +149,7 @@ func AllocatePeerIP(ipNet net.IPNet, takenIps []net.IP) (net.IP, error) { intn := r.Intn(len(ips)) return ips[intn], nil + } }