forked from projectcalico/libcalico-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipam_handle.go
65 lines (56 loc) · 1.85 KB
/
ipam_handle.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
// Copyright (c) 2016 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 client
import (
"errors"
"fmt"
"github.com/projectcalico/libcalico-go/lib/backend/model"
"github.com/projectcalico/libcalico-go/lib/net"
)
type allocationHandle struct {
*model.IPAMHandle
}
func (h allocationHandle) incrementBlock(blockCidr net.IPNet, num int) int {
blockId := blockCidr.String()
newNum := num
if val, ok := h.Block[blockId]; ok {
// An entry exists for this block, increment the number
// of allocations.
newNum = val + num
}
h.Block[blockId] = newNum
return newNum
}
func (h allocationHandle) decrementBlock(blockCidr net.IPNet, num int) (*int, error) {
blockId := blockCidr.String()
if current, ok := h.Block[blockId]; !ok {
// This entry doesn't exist.
errStr := fmt.Sprintf("Tried to decrement block %s by %s but it isn't linked to handle %s", blockId, num, h.HandleID)
return nil, errors.New(errStr)
} else {
newNum := current - num
if newNum < 0 {
errStr := fmt.Sprintf("Tried to decrement block %s by %s but it only has %s addresses on handle %s", blockId, num, current, h.HandleID)
return nil, errors.New(errStr)
}
if newNum == 0 {
delete(h.Block, blockId)
} else {
h.Block[blockId] = newNum
}
return &newNum, nil
}
}
func (h allocationHandle) empty() bool {
return len(h.Block) == 0
}