-
Notifications
You must be signed in to change notification settings - Fork 429
/
util.go
147 lines (125 loc) · 3.12 KB
/
util.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package framework
import (
"fmt"
"math/big"
"math/rand"
"net"
"sort"
"strings"
"time"
"github.com/onsi/ginkgo/v2"
"github.com/kubeovn/kube-ovn/pkg/util"
)
const (
KubeOvnNamespace = "kube-system"
DaemonSetOvsOvn = "ovs-ovn"
)
// RandomSuffix provides a random sequence to append to resources.
func RandomSuffix() string {
return fmt.Sprintf("%d%04d", ginkgo.GinkgoParallelProcess(), rand.Intn(10000))
}
func RandomCIDR(family string) string {
fnIPv4 := func() string {
cidr := net.IPNet{
IP: net.ParseIP("10.0.0.0").To4(),
Mask: net.CIDRMask(24, 32),
}
cidr.IP[1] = 0xf0 | byte(ginkgo.GinkgoParallelProcess())
cidr.IP[2] = byte(rand.Intn(0xff + 1))
return cidr.String()
}
fnIPv6 := func() string {
cidr := net.IPNet{
IP: net.ParseIP("fc00:10:ff::").To16(),
Mask: net.CIDRMask(96, 128),
}
cidr.IP[9] = byte(ginkgo.GinkgoParallelProcess())
cidr.IP[10] = byte(rand.Intn(0xff + 1))
cidr.IP[11] = byte(rand.Intn(0xff + 1))
return cidr.String()
}
switch family {
case "ipv4":
return fnIPv4()
case "ipv6":
return fnIPv6()
case "dual":
return fnIPv4() + "," + fnIPv6()
default:
Failf("invalid ip family: %q", family)
return ""
}
}
func sortIPs(ips []string) {
sort.Slice(ips, func(i, j int) bool {
return util.Ip2BigInt(ips[i]).Cmp(util.Ip2BigInt(ips[j])) < 0
})
}
// ipv4/ipv6 only
func RandomExcludeIPs(cidr string, count int) []string {
if cidr == "" || count == 0 {
return nil
}
rangeCount := rand.Intn(count + 1)
ips := strings.Split(RandomIPPool(cidr, ";", rangeCount*2+count-rangeCount), ";")
sortIPs(ips)
var idx int
rangeLeft := rangeCount
ret := make([]string, 0, count)
for i := 0; i < count; i++ {
if rangeLeft != 0 && rand.Intn(count-i) < rangeLeft {
ret = append(ret, fmt.Sprintf("%s..%s", ips[idx], ips[idx+1]))
idx++
rangeLeft--
} else {
ret = append(ret, ips[idx])
}
idx++
}
return ret
}
func RandomIPPool(cidr, sep string, count int) string {
fn := func(cidr string) []string {
if cidr == "" {
return nil
}
firstIP, _ := util.FirstIP(cidr)
_, ipnet, _ := net.ParseCIDR(cidr)
base := util.Ip2BigInt(firstIP)
base.Add(base, big.NewInt(1))
prefix, size := ipnet.Mask.Size()
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
max := big.NewInt(0).Exp(big.NewInt(2), big.NewInt(int64(size-prefix)), nil)
max.Sub(max, big.NewInt(3))
ips := make([]string, 0, count)
for len(ips) != count {
n := big.NewInt(0).Rand(rnd, max)
if ip := util.BigInt2Ip(n.Add(n, base)); !util.ContainsString(ips, ip) {
ips = append(ips, ip)
}
}
return ips
}
cidrV4, cidrV6 := util.SplitStringIP(cidr)
ipsV4, ipsV6 := fn(cidrV4), fn(cidrV6)
dual := make([]string, 0, count)
for i := 0; i < count; i++ {
var ips []string
if i < len(ipsV4) {
ips = append(ips, ipsV4[i])
}
if i < len(ipsV6) {
ips = append(ips, ipsV6[i])
}
dual = append(dual, strings.Join(ips, ","))
}
return strings.Join(dual, sep)
}
func PrevIP(ip string) string {
n := util.Ip2BigInt(ip)
return util.BigInt2Ip(n.Add(n, big.NewInt(-1)))
}
func NextIP(ip string) string {
n := util.Ip2BigInt(ip)
return util.BigInt2Ip(n.Add(n, big.NewInt(1)))
}