-
Notifications
You must be signed in to change notification settings - Fork 20
/
pool.go
222 lines (196 loc) · 6.69 KB
/
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
/*
Copyright 2023 The Kubernetes Authors.
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 poolutil implements utility functions to manage a pool of IP addresses.
package poolutil
import (
"context"
"errors"
"math"
"math/big"
"net/netip"
"strings"
"go4.org/netipx"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
ipamv1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/cluster-api-ipam-provider-in-cluster/api/v1alpha2"
"sigs.k8s.io/cluster-api-ipam-provider-in-cluster/internal/index"
)
// AddressesOutOfRangeIPSet returns an IPSet of the inUseAddresses IPs that are
// not in the poolIPSet.
func AddressesOutOfRangeIPSet(inUseAddresses []ipamv1.IPAddress, poolIPSet *netipx.IPSet) (*netipx.IPSet, error) {
outOfRangeBuilder := &netipx.IPSetBuilder{}
for _, address := range inUseAddresses {
ip, err := netip.ParseAddr(address.Spec.Address)
if err != nil {
// if an address we fetch for the pool is unparsable then it isn't in the pool ranges
continue
}
outOfRangeBuilder.Add(ip)
}
outOfRangeBuilder.RemoveSet(poolIPSet)
return outOfRangeBuilder.IPSet()
}
// ListAddressesInUse fetches all IPAddresses belonging to the specified pool.
// Note: requires `index.ipAddressByCombinedPoolRef` to be set up.
func ListAddressesInUse(ctx context.Context, c client.Reader, namespace string, poolRef corev1.TypedLocalObjectReference) ([]ipamv1.IPAddress, error) {
addresses := &ipamv1.IPAddressList{}
err := c.List(ctx, addresses,
client.MatchingFields{
index.IPAddressPoolRefCombinedField: index.IPPoolRefValue(poolRef),
},
client.InNamespace(namespace),
)
addr := []ipamv1.IPAddress{}
for _, a := range addresses.Items {
gv, _ := schema.ParseGroupVersion(a.APIVersion)
if gv.Group != "ipam.cluster.x-k8s.io" {
continue
}
addr = append(addr, a)
}
return addr, err
}
// AddressByNamespacedName finds a specific ip address by namespace and name in a slice of addresses.
func AddressByNamespacedName(addresses []ipamv1.IPAddress, namespace, name string) *ipamv1.IPAddress {
for _, a := range addresses {
if a.Namespace == namespace && a.Name == name {
return &a
}
}
return nil
}
// FindFreeAddress returns the next free IP Address in a range based on a set of existing addresses.
func FindFreeAddress(poolIPSet *netipx.IPSet, inUseIPSet *netipx.IPSet) (netip.Addr, error) {
for _, iprange := range poolIPSet.Ranges() {
ip := iprange.From()
for {
if !inUseIPSet.Contains(ip) {
return ip, nil
}
if ip == iprange.To() {
break
}
ip = ip.Next()
}
}
return netip.Addr{}, errors.New("no address available")
}
// PoolSpecToIPSet converts a pool spec to an IPSet. Reserved addresses will be
// omitted from the set depending on whether the
// `spec.AllocateReservedIPAddresses` flag is set.
func PoolSpecToIPSet(poolSpec *v1alpha2.InClusterIPPoolSpec) (*netipx.IPSet, error) {
addressesIPSet, err := AddressesToIPSet(poolSpec.Addresses)
if err != nil {
return nil, err // should not happen, webhook validates pools for correctness.
}
builder := &netipx.IPSetBuilder{}
builder.AddSet(addressesIPSet)
if len(poolSpec.ExcludedAddresses) > 0 {
excludedAddressesIPSet, err := AddressesToIPSet(poolSpec.ExcludedAddresses)
if err != nil {
return nil, err
}
builder.RemoveSet(excludedAddressesIPSet)
}
if !poolSpec.AllocateReservedIPAddresses {
subnet := netip.PrefixFrom(addressesIPSet.Ranges()[0].From(), poolSpec.Prefix) // safe because of webhook validation
subnetRange := netipx.RangeOfPrefix(subnet)
builder.Remove(subnetRange.From()) // network addr in IPv4, anycast addr in IPv6
if subnet.Addr().Is4() {
builder.Remove(subnetRange.To()) // broadcast addr
}
}
if poolSpec.Gateway != "" {
gateway, err := netip.ParseAddr(poolSpec.Gateway)
if err != nil {
return nil, err // should not happen, webhook validates pools for correctness.
}
builder.Remove(gateway)
}
return builder.IPSet()
}
// AddressesToIPSet converts an array of addresses to an AddressesToIPSet
// addresses may be specified as individual IPs, CIDR ranges, or hyphenated IP
// ranges.
func AddressesToIPSet(addresses []string) (*netipx.IPSet, error) {
builder := &netipx.IPSetBuilder{}
for _, addressStr := range addresses {
ipSet, err := AddressToIPSet(addressStr)
if err != nil {
return nil, err
}
builder.AddSet(ipSet)
}
return builder.IPSet()
}
// AddressToIPSet converts an addresses to an AddressesToIPSet addresses may be
// specified as individual IPs, CIDR ranges, or hyphenated IP ranges.
func AddressToIPSet(addressStr string) (*netipx.IPSet, error) {
builder := &netipx.IPSetBuilder{}
if strings.Contains(addressStr, "-") {
addrRange, err := netipx.ParseIPRange(addressStr)
if err != nil {
return nil, err
}
builder.AddRange(addrRange)
} else if strings.Contains(addressStr, "/") {
prefix, err := netip.ParsePrefix(addressStr)
if err != nil {
return nil, err
}
builder.AddPrefix(prefix)
} else {
addr, err := netip.ParseAddr(addressStr)
if err != nil {
return nil, err
}
builder.Add(addr)
}
return builder.IPSet()
}
// IPSetCount returns the number of IPs contained in the given IPSet.
// This function returns type int, which is much smaller than
// the possible size of a range, which could be 2^128 IPs.
// When an IPSet's count is would be larger than an int, math.MaxInt
// is returned instead.
func IPSetCount(ipSet *netipx.IPSet) int {
if ipSet == nil {
return 0
}
total := big.NewInt(0)
for _, iprange := range ipSet.Ranges() {
total.Add(
total,
big.NewInt(0).Sub(
big.NewInt(0).SetBytes(iprange.To().AsSlice()),
big.NewInt(0).SetBytes(iprange.From().AsSlice()),
),
)
// Subtracting To and From misses that one of those is a valid IP
total.Add(total, big.NewInt(1))
}
// If total is greater than Uint64, Uint64() will return 0
// We want to display MaxInt if the value overflows what int can contain
if total.IsInt64() && total.Uint64() <= uint64(math.MaxInt) {
return int(total.Uint64())
}
return math.MaxInt
}
// AddressStrParses checks to see that the addresss string is one of
// a valid single IP address, a hyphonated IP range, or a Prefix.
func AddressStrParses(addressStr string) bool {
_, err := AddressToIPSet(addressStr)
return err == nil
}