-
Notifications
You must be signed in to change notification settings - Fork 274
/
ipmanager.go
62 lines (54 loc) 路 1.65 KB
/
ipmanager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package e2e
import (
"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/networkutils"
)
type E2EIPManager struct {
vspherenetworkCidr string
privateNetworkCidr string
vsphereNetworkIPs map[string]bool
privateNetworkIPs map[string]bool
}
func newE2EIPManager(networkCidr, privateNetworkCidr string) *E2EIPManager {
ipman := &E2EIPManager{
vspherenetworkCidr: networkCidr,
privateNetworkCidr: privateNetworkCidr,
vsphereNetworkIPs: make(map[string]bool),
privateNetworkIPs: make(map[string]bool),
}
return ipman
}
func (ipman *E2EIPManager) reserveIP() string {
return getUniqueIP(ipman.vspherenetworkCidr, ipman.vsphereNetworkIPs)
}
func (ipMan *E2EIPManager) reservePrivateIP() string {
return getUniqueIP(ipMan.privateNetworkCidr, ipMan.privateNetworkIPs)
}
func (ipman *E2EIPManager) reservePrivateIPPool(count int) networkutils.IPPool {
pool := networkutils.NewIPPool()
for i := 0; i < count; i++ {
pool.AddIP(ipman.reservePrivateIP())
}
return pool
}
func (ipman *E2EIPManager) reserveIPPool(count int) networkutils.IPPool {
pool := networkutils.NewIPPool()
for i := 0; i < count; i++ {
pool.AddIP(ipman.reserveIP())
}
return pool
}
func getUniqueIP(cidr string, usedIPs map[string]bool) string {
ipgen := networkutils.NewIPGenerator(&networkutils.DefaultNetClient{})
ip, err := ipgen.GenerateUniqueIP(cidr)
for ; err != nil || usedIPs[ip]; ip, err = ipgen.GenerateUniqueIP(cidr) {
if err != nil {
logger.V(2).Info("Warning: getting unique IP for vsphere failed", "error", err)
}
if usedIPs[ip] {
logger.V(2).Info("Warning: generated IP is already taken", "IP", ip)
}
}
usedIPs[ip] = true
return ip
}