Skip to content

Commit 4ab2bb1

Browse files
committed
FindAvailableIP takes []strings
1 parent c5c9b5e commit 4ab2bb1

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

apiserver/database/cidr.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,25 @@ import (
2020
// fmt.Println(next)
2121
//}
2222

23-
func FindAvailableIP(cidr string, allocated map[string]struct{}) (string, error) {
23+
func FindAvailableIP(cidr string, allocated []string) (string, error) {
24+
allocatedMap := toMap(allocated)
2425
ips, _ := cidrIPs(cidr)
2526
for _, ip := range ips {
2627
fmt.Println(ip)
27-
if _, found := allocated[ip]; !found {
28+
if _, found := allocatedMap[ip]; !found {
2829
return ip, nil
2930
}
3031
}
3132
return "", fmt.Errorf("no available IPs in range %v", cidr)
3233
}
3334

35+
func toMap(strings []string) (m map[string]struct{}) {
36+
for _, s := range strings {
37+
m[s] = struct{}{}
38+
}
39+
return
40+
}
41+
3442
func cidrIPs(cidr string) ([]string, error) {
3543
ip, ipnet, err := net.ParseCIDR(cidr)
3644

0 commit comments

Comments
 (0)