-
Notifications
You must be signed in to change notification settings - Fork 4
/
range_pool.go
297 lines (261 loc) · 8.12 KB
/
range_pool.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package iprange
import (
"encoding/json"
"errors"
"fmt"
"math/big"
"net"
"sort"
k8sutilnet "k8s.io/utils/net"
networkingv1alpha1 "github.com/baidubce/baiducloud-cce-cni-driver/pkg/apis/networking/v1alpha1"
"github.com/baidubce/baiducloud-cce-cni-driver/pkg/util/cidr"
)
// IPFilter filte unavailable IP addresses
// this interface is used for filtering IP addresses which have been allocated by ipam
type IPFilter interface {
FilterIP(ip net.IP) bool
}
type subnetUnivaluedIP struct {
cidr *net.IPNet
}
func (p *subnetUnivaluedIP) FilterIP(ip net.IP) bool {
if !p.cidr.Contains(ip) {
return true
}
return cidr.IsUnicastIP(ip, p.cidr.String())
}
// RangePool manager ip pool control by range of ip
type RangePool struct {
sbnRange map[string][]*big.Int
ipFilter []IPFilter
}
// NewCustomRangePool create new ip range manager
// This object is used by custom mode of ipam
func NewCustomRangePool(psts *networkingv1alpha1.PodSubnetTopologySpread, ipFilter ...IPFilter) (*RangePool, error) {
manager := &RangePool{
sbnRange: make(map[string][]*big.Int),
ipFilter: ipFilter,
}
for sbnID, sa := range psts.Spec.Subnets {
if sbnStatus, ok := psts.Status.AvailableSubnets[sbnID]; ok {
_, subnetCIDR, err := net.ParseCIDR(sbnStatus.CIDR)
if err != nil {
return nil, err
}
ipRange, err := ListAllCustomIPRangeIndexs(subnetCIDR, sa.Custom)
if err != nil {
return nil, fmt.Errorf("sbnnet %s %w", sbnID, err)
}
manager.sbnRange[sbnID] = ipRange
manager.ipFilter = append(manager.ipFilter, &subnetUnivaluedIP{cidr: subnetCIDR})
}
}
if len(manager.sbnRange) == 0 {
return nil, errors.New("no index range")
}
return manager, nil
}
// IPInRange Whether to include the specified IP in the custom IPRange
func (pool *RangePool) IPInRange(ip string) bool {
netIP := net.ParseIP(ip)
if netIP == nil {
return false
}
ipIndex := k8sutilnet.BigForIP(netIP)
for _, indexRange := range pool.sbnRange {
if isInRangeIndex(ipIndex, indexRange) {
return true
}
}
return false
}
// FirstAvailableIP try to get the first available IP
// Return: IP address can be use for pod
//
// nil if no ip can be used
func (pool *RangePool) FirstAvailableIP(sbnID string) net.IP {
indexRange, ok := pool.sbnRange[sbnID]
if ok {
for i := 0; i+1 < len(indexRange); i = i + 2 {
for ipInt := indexRange[i]; ipInt.Cmp(indexRange[i+1]) <= 0; ipInt = big.NewInt(0).Add(ipInt, big.NewInt(int64(1))) {
ip := BytesToIP(ipInt.Bytes())
if pool.FilterIP(ip) {
return ip
}
}
}
}
return nil
}
// FilteIP return true if the IP can be used
func (pool *RangePool) FilterIP(ip net.IP) bool {
for _, filter := range pool.ipFilter {
if !filter.FilterIP(ip) {
return false
}
}
return true
}
func isInRangeIndex(origin *big.Int, indexRange []*big.Int) bool {
for i := 0; i+1 < len(indexRange); i = i + 2 {
if origin.Cmp(indexRange[i]) >= 0 && origin.Cmp(indexRange[i+1]) <= 0 {
return true
}
}
return false
}
func (pool *RangePool) String() string {
pooklMap := make(map[string][]string)
for sbnID, rs := range pool.sbnRange {
for _, bint := range rs {
pooklMap[sbnID] = append(pooklMap[sbnID], BytesToIP(bint.Bytes()).String())
}
}
b, _ := json.Marshal(pooklMap)
return string(b)
}
// ListAllCustomIPRangeIndexs The IP address range is listed in a list. Every two elements of the returned array are a group
// eg: [1,10, 10,100, 109,107]
// In the example array:
// if v=2, then v is in this element list.
// If v=101, v is not in this list
func ListAllCustomIPRangeIndexs(subnetCIDR *net.IPNet, customs []networkingv1alpha1.CustomAllocation) (indexRanges []*big.Int, err error) {
var ipVersion k8sutilnet.IPFamily = k8sutilnet.IPv4
if subnetCIDR.IP.To4() == nil {
ipVersion = k8sutilnet.IPv6
}
for _, custom := range customs {
if custom.Family == ipVersion {
for _, r := range custom.CustomIPRange {
start, end := customerRangeToBigInt(r)
if start == nil || end == nil {
return nil, fmt.Errorf("")
}
if !subnetCIDR.Contains(BytesToIP(start.Bytes())) || !subnetCIDR.Contains(BytesToIP(end.Bytes())) {
return nil, fmt.Errorf("ip %s %s not in cidr range %s", r.Start, r.End, subnetCIDR.String())
}
indexRanges = append(indexRanges, start, end)
}
}
}
if !overlappingCheck(indexRanges) {
return nil, fmt.Errorf("overlapping IP Range")
}
// fill ip ranges
sort.Slice(indexRanges, func(i, j int) bool {
return indexRanges[i].Cmp(indexRanges[j]) == -1
})
if len(indexRanges) == 0 {
// first ip of cidr
firstIndex := k8sutilnet.BigForIP(subnetCIDR.IP)
lastIndex := big.NewInt(0).Add(firstIndex, big.NewInt(k8sutilnet.RangeSize(subnetCIDR)))
indexRanges = append(indexRanges, firstIndex, lastIndex)
}
return
}
func customerRangeToBigInt(r networkingv1alpha1.CustomIPRange) (start, end *big.Int) {
if startIP := net.ParseIP(r.Start); startIP != nil {
start = k8sutilnet.BigForIP(startIP)
}
if endIP := net.ParseIP(r.End); endIP != nil {
end = k8sutilnet.BigForIP(endIP)
}
return
}
// BytesToIP translate []bytes to net.IP
func BytesToIP(r []byte) net.IP {
r = append(make([]byte, 16), r...)
return net.IP(r[len(r)-16:])
}
// NewCIDRRangePool create new ip range manager
// This object is used by fixed IP and manually assigned IP
func NewCIDRRangePool(psts *networkingv1alpha1.PodSubnetTopologySpread, ipFilter ...IPFilter) (*RangePool, error) {
manager := &RangePool{
sbnRange: make(map[string][]*big.Int),
ipFilter: ipFilter,
}
for sbnID, sa := range psts.Spec.Subnets {
if sbnStatus, ok := psts.Status.AvailableSubnets[sbnID]; ok {
_, subnetCIDR, err := net.ParseCIDR(sbnStatus.CIDR)
if err != nil {
return nil, err
}
ipRange, err := ListAllCIDRIPRangeIndexs(subnetCIDR, sa)
if err != nil {
return nil, err
}
manager.sbnRange[sbnID] = ipRange
manager.ipFilter = append(manager.ipFilter, &subnetUnivaluedIP{cidr: subnetCIDR})
}
}
if len(manager.sbnRange) == 0 {
return nil, errors.New("no index range")
}
return manager, nil
}
// ListAllCIDRIPRangeIndexs The IP address range is listed in a list. Every two elements of the returned array are a group
// eg: [1,10, 10,100, 109,107]
// In the example array:
// if v=2, then v is in this element list.
// If v=101, v is not in this list
func ListAllCIDRIPRangeIndexs(subnetCIDR *net.IPNet, sa networkingv1alpha1.SubnetAllocation) (indexRanges []*big.Int, err error) {
ipVersion := 4
if subnetCIDR.IP.To4() == nil {
ipVersion = 6
}
if ipVersion == 4 {
for _, str := range sa.IPv4 {
ip := net.ParseIP(str)
if ip != nil {
ipint := k8sutilnet.BigForIP(ip)
if !subnetCIDR.Contains(ip) {
return nil, fmt.Errorf("ip not in cidr range")
}
indexRanges = append(indexRanges, ipint, ipint)
}
}
cidrs, err := k8sutilnet.ParseCIDRs(sa.IPv4Range)
if err != nil {
return nil, err
}
for _, cidr := range cidrs {
first := k8sutilnet.BigForIP(cidr.IP)
last := big.NewInt(0).Add(first, big.NewInt(k8sutilnet.RangeSize(cidr)-1))
if !subnetCIDR.Contains(BytesToIP(first.Bytes())) || !subnetCIDR.Contains(BytesToIP(last.Bytes())) {
return nil, fmt.Errorf("ip not in cidr range")
}
indexRanges = append(indexRanges, first, last)
}
}
if !overlappingCheck(indexRanges) {
return nil, fmt.Errorf("overlapping IP Range")
}
// fill ip ranges
sort.Slice(indexRanges, func(i, j int) bool {
return indexRanges[i].Cmp(indexRanges[j]) == -1
})
if len(indexRanges) == 0 {
// first ip of cidr
firstIndex := k8sutilnet.BigForIP(subnetCIDR.IP)
lastIndex := big.NewInt(0).Add(firstIndex, big.NewInt(k8sutilnet.RangeSize(subnetCIDR)-1))
indexRanges = append(indexRanges, firstIndex, lastIndex)
}
return
}
// Overlapping detection range
// Compare each group of ranges. The start and end of other groups cannot overlap
// return true if each goup does not overlap
func overlappingCheck(indexRanges []*big.Int) bool {
for i := 0; i+1 < len(indexRanges); i = i + 2 {
for j := i + 2; j+1 < len(indexRanges); j = j + 2 {
a := indexRanges[i].Cmp(indexRanges[j])
b := indexRanges[i+1].Cmp(indexRanges[j])
c := indexRanges[i].Cmp(indexRanges[j+1])
d := indexRanges[i+1].Cmp(indexRanges[j+1])
if a != b || b != c || c != d {
return false
}
}
}
return true
}