forked from projectcalico/libcalico-go
/
ipam_block.go
408 lines (358 loc) · 11.8 KB
/
ipam_block.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright (c) 2016-2019 Tigera, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ipam
import (
"errors"
"fmt"
"math/big"
"net"
"reflect"
"strings"
"github.com/projectcalico/libcalico-go/lib/apis/v3"
log "github.com/sirupsen/logrus"
"github.com/projectcalico/libcalico-go/lib/backend/model"
cnet "github.com/projectcalico/libcalico-go/lib/net"
)
// Wrap the backend AllocationBlock struct so that we can
// attach methods to it.
type allocationBlock struct {
*model.AllocationBlock
}
func newBlock(cidr cnet.IPNet) allocationBlock {
ones, size := cidr.Mask.Size()
numAddresses := 1 << uint(size-ones)
b := model.AllocationBlock{}
b.Allocations = make([]*int, numAddresses)
b.Unallocated = make([]int, numAddresses)
b.StrictAffinity = false
b.CIDR = cidr
// Initialize unallocated ordinals.
for i := 0; i < numAddresses; i++ {
b.Unallocated[i] = i
}
return allocationBlock{&b}
}
func (b *allocationBlock) autoAssign(
num int, handleID *string, host string, attrs map[string]string, affinityCheck bool) ([]cnet.IPNet, error) {
// Determine if we need to check for affinity.
checkAffinity := b.StrictAffinity || affinityCheck
if checkAffinity && b.Affinity != nil && !hostAffinityMatches(host, b.AllocationBlock) {
// Affinity check is enabled but the host does not match - error.
s := fmt.Sprintf("Block affinity (%s) does not match provided (%s)", *b.Affinity, host)
return nil, errors.New(s)
} else if b.Affinity == nil {
log.Warnf("Attempting to assign IPs from block with no affinity: %v", b)
if checkAffinity {
// If we're checking strict affinity, we can't assign from a block with no affinity.
return nil, fmt.Errorf("Attempt to assign from block %v with no affinity", b.CIDR)
}
}
// Walk the allocations until we find enough addresses.
ordinals := []int{}
for len(b.Unallocated) > 0 && len(ordinals) < num {
ordinals = append(ordinals, b.Unallocated[0])
b.Unallocated = b.Unallocated[1:]
}
// Create slice of IPs and perform the allocations.
ips := []cnet.IPNet{}
_, mask, _ := cnet.ParseCIDR(b.CIDR.String())
for _, o := range ordinals {
attrIndex := b.findOrAddAttribute(handleID, attrs)
b.Allocations[o] = &attrIndex
ipNets := cnet.IPNet(*mask)
ipNets.IP = cnet.IncrementIP(cnet.IP{b.CIDR.IP}, big.NewInt(int64(o))).IP
ips = append(ips, ipNets)
}
log.Debugf("Block %s returned ips: %v", b.CIDR.String(), ips)
return ips, nil
}
func (b *allocationBlock) assign(address cnet.IP, handleID *string, attrs map[string]string, host string) error {
if b.StrictAffinity && b.Affinity != nil && !hostAffinityMatches(host, b.AllocationBlock) {
// Affinity check is enabled but the host does not match - error.
return errors.New("Block host affinity does not match")
} else if b.Affinity == nil {
log.Warnf("Attempting to assign IP from block with no affinity: %v", b)
if b.StrictAffinity {
// If we're checking strict affinity, we can't assign from a block with no affinity.
return fmt.Errorf("Attempt to assign from block %v with no affinity", b.CIDR)
}
}
// Convert to an ordinal.
ordinal, err := b.IPToOrdinal(address)
if err != nil {
return err
}
// Check if already allocated.
if b.Allocations[ordinal] != nil {
return errors.New("Address already assigned in block")
}
// Set up attributes.
attrIndex := b.findOrAddAttribute(handleID, attrs)
b.Allocations[ordinal] = &attrIndex
// Remove from unallocated.
for i, unallocated := range b.Unallocated {
if unallocated == ordinal {
b.Unallocated = append(b.Unallocated[:i], b.Unallocated[i+1:]...)
break
}
}
return nil
}
// hostAffinityMatches checks if the provided host matches the provided affinity.
func hostAffinityMatches(host string, block *model.AllocationBlock) bool {
return *block.Affinity == "host:"+host
}
func getHostAffinity(block *model.AllocationBlock) string {
if block.Affinity != nil && strings.HasPrefix(*block.Affinity, "host:") {
return strings.TrimPrefix(*block.Affinity, "host:")
}
return ""
}
func (b allocationBlock) numFreeAddresses() int {
return len(b.Unallocated)
}
func (b allocationBlock) empty() bool {
return b.numFreeAddresses() == b.NumAddresses()
}
func (b *allocationBlock) release(addresses []cnet.IP) ([]cnet.IP, map[string]int, error) {
// Store return values.
unallocated := []cnet.IP{}
countByHandle := map[string]int{}
// Used internally.
var ordinals []int
delRefCounts := map[int]int{}
attrsToDelete := []int{}
// De-duplicate addresses to ensure reference counting is correcet
uniqueAddresses := make(map[string]struct{})
for _, ip := range addresses {
uniqueAddresses[ip.IP.String()] = struct{}{}
}
// Determine the ordinals that need to be released and the
// attributes that need to be cleaned up.
log.Debugf("Releasing addresses from block: %v", uniqueAddresses)
for ipStr := range uniqueAddresses {
ip := cnet.MustParseIP(ipStr)
// Convert to an ordinal.
ordinal, err := b.IPToOrdinal(ip)
if err != nil {
return nil, nil, err
}
log.Debugf("Address %s is ordinal %d", ip, ordinal)
// Check if allocated.
log.Debugf("Checking if allocated: %v", b.Allocations)
attrIdx := b.Allocations[ordinal]
if attrIdx == nil {
log.Debugf("Asked to release address that was not allocated")
unallocated = append(unallocated, ip)
continue
}
ordinals = append(ordinals, ordinal)
log.Debugf("%s is allocated, ordinals to release are now %v", ip, ordinals)
// Increment reference counting for attributes.
cnt := 1
if cur, exists := delRefCounts[*attrIdx]; exists {
cnt = cur + 1
}
delRefCounts[*attrIdx] = cnt
log.Debugf("delRefCounts: %v", delRefCounts)
// Increment count of addresses by handle if a handle
// exists.
log.Debugf("Looking up attribute with index %d", *attrIdx)
handleID := b.Attributes[*attrIdx].AttrPrimary
if handleID != nil {
log.Debugf("HandleID is %s", *handleID)
handleCount := 0
if count, ok := countByHandle[*handleID]; !ok {
handleCount = count
}
log.Debugf("Handle ref count is %d, incrementing", handleCount)
handleCount += 1
countByHandle[*handleID] = handleCount
log.Debugf("countByHandle %v", countByHandle)
}
}
// Handle cleaning up of attributes. We do this by
// reference counting. If we're deleting the last reference to
// a given attribute, then it needs to be cleaned up.
refCounts := b.attributeRefCounts()
log.Debugf("Cleaning up attributes, refCounts: %v", refCounts)
for idx, refs := range delRefCounts {
log.Debugf("Checking ref count index %d", idx)
if refCounts[idx] == refs {
attrsToDelete = append(attrsToDelete, idx)
}
}
if len(attrsToDelete) != 0 {
log.Debugf("Deleting attributes: %v", attrsToDelete)
b.deleteAttributes(attrsToDelete, ordinals)
}
// Release requested addresses.
log.Debugf("Allocations: %v", b.Allocations)
log.Debugf("Releasing ordinals: %v", ordinals)
for _, ordinal := range ordinals {
log.Debugf("Releasing ordinal %d", ordinal)
b.Allocations[ordinal] = nil
b.Unallocated = append(b.Unallocated, ordinal)
}
return unallocated, countByHandle, nil
}
func (b *allocationBlock) deleteAttributes(delIndexes, ordinals []int) {
newIndexes := make([]*int, len(b.Attributes))
newAttrs := []model.AllocationAttribute{}
y := 0 // Next free slot in the new attributes list.
for x := range b.Attributes {
if !intInSlice(x, delIndexes) {
// Attribute at x is not being deleted. Build a mapping
// of old attribute index (x) to new attribute index (y).
log.Debugf("%d in %v", x, delIndexes)
newIndex := y
newIndexes[x] = &newIndex
y += 1
newAttrs = append(newAttrs, b.Attributes[x])
}
}
b.Attributes = newAttrs
// Update attribute indexes for all allocations in this block.
for i := 0; i < b.NumAddresses(); i++ {
if b.Allocations[i] != nil {
// Get the new index that corresponds to the old index
// and update the allocation.
newIndex := newIndexes[*b.Allocations[i]]
b.Allocations[i] = newIndex
}
}
}
func (b allocationBlock) attributeRefCounts() map[int]int {
refCounts := map[int]int{}
for _, a := range b.Allocations {
if a == nil {
continue
}
if count, ok := refCounts[*a]; !ok {
// No entry for given attribute index.
refCounts[*a] = 1
} else {
refCounts[*a] = count + 1
}
}
return refCounts
}
func (b allocationBlock) attributeIndexesByHandle(handleID string) []int {
indexes := []int{}
for i, attr := range b.Attributes {
if attr.AttrPrimary != nil && *attr.AttrPrimary == handleID {
indexes = append(indexes, i)
}
}
return indexes
}
func (b *allocationBlock) releaseByHandle(handleID string) int {
attrIndexes := b.attributeIndexesByHandle(handleID)
log.Debugf("Attribute indexes to release: %v", attrIndexes)
if len(attrIndexes) == 0 {
// Nothing to release.
log.Debugf("No addresses assigned to handle '%s'", handleID)
return 0
}
// There are addresses to release.
ordinals := []int{}
var o int
for o = 0; o < b.NumAddresses(); o++ {
// Only check allocated ordinals.
if b.Allocations[o] != nil && intInSlice(*b.Allocations[o], attrIndexes) {
// Release this ordinal.
ordinals = append(ordinals, o)
}
}
// Clean and reorder attributes.
b.deleteAttributes(attrIndexes, ordinals)
// Release the addresses.
for _, o := range ordinals {
b.Allocations[o] = nil
b.Unallocated = append(b.Unallocated, o)
}
return len(ordinals)
}
func (b allocationBlock) ipsByHandle(handleID string) []cnet.IP {
ips := []cnet.IP{}
attrIndexes := b.attributeIndexesByHandle(handleID)
var o int
for o = 0; o < b.NumAddresses(); o++ {
if b.Allocations[o] != nil && intInSlice(*b.Allocations[o], attrIndexes) {
ip := b.OrdinalToIP(o)
ips = append(ips, ip)
}
}
return ips
}
func (b allocationBlock) attributesForIP(ip cnet.IP) (map[string]string, error) {
// Convert to an ordinal.
ordinal, err := b.IPToOrdinal(ip)
if err != nil {
return nil, err
}
// Check if allocated.
attrIndex := b.Allocations[ordinal]
if attrIndex == nil {
return nil, errors.New(fmt.Sprintf("IP %s is not currently assigned in block", ip))
}
return b.Attributes[*attrIndex].AttrSecondary, nil
}
func (b *allocationBlock) findOrAddAttribute(handleID *string, attrs map[string]string) int {
logCtx := log.WithField("attrs", attrs)
if handleID != nil {
logCtx = log.WithField("handle", *handleID)
}
attr := model.AllocationAttribute{handleID, attrs}
for idx, existing := range b.Attributes {
if reflect.DeepEqual(attr, existing) {
log.Debugf("Attribute '%+v' already exists", attr)
return idx
}
}
// Does not exist - add it.
logCtx.Debugf("New allocation attribute: %#v", attr)
attrIndex := len(b.Attributes)
b.Attributes = append(b.Attributes, attr)
return attrIndex
}
func getBlockCIDRForAddress(addr cnet.IP, pool *v3.IPPool) cnet.IPNet {
var mask net.IPMask
if addr.Version() == 6 {
// This is an IPv6 address.
mask = net.CIDRMask(pool.Spec.BlockSize, 128)
} else {
// This is an IPv4 address.
mask = net.CIDRMask(pool.Spec.BlockSize, 32)
}
masked := addr.Mask(mask)
return cnet.IPNet{IPNet: net.IPNet{IP: masked, Mask: mask}}
}
func getIPVersion(ip cnet.IP) int {
if ip.To4() == nil {
return 6
}
return 4
}
func largerThanOrEqualToBlock(blockCIDR cnet.IPNet, pool *v3.IPPool) bool {
ones, _ := blockCIDR.Mask.Size()
return ones <= pool.Spec.BlockSize
}
func intInSlice(searchInt int, slice []int) bool {
for _, v := range slice {
if v == searchInt {
return true
}
}
return false
}