Skip to content

Commit 4d858ea

Browse files
author
Michael Patsula
committed
Merge branch 'fix-duplicates-in-podCIDR-allocation-logic' into 'main'
fix: duplicates in podCIDR allocation logic See merge request cloudnative/go/cidr-allocator!28
2 parents 70e069c + 974713a commit 4d858ea

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

internal/controller/nodecidrallocation_controller.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,12 @@ func (r *NodeCIDRAllocationReconciler) Reconcile(ctx context.Context, req ctrl.R
209209
// Begin allocation process
210210
//
211211

212-
freeSubnets := make(map[uint8][]string)
212+
// The subnets that were used as node podCIRD's in this reconcile
213+
var allocatedSubnetInReconcile []string
213214
for _, node := range matchingNodes.Items {
215+
// The subnets that could have been used for the node's podCIDR in this iteration of the loop (exluding the one used for the node)
216+
var remainingFreeSubnets []string
217+
214218
if node.Spec.PodCIDR != "" {
215219
rl.V(1).Info("node already contains CIDR allocation. skipping",
216220
"name", node.GetName(),
@@ -244,7 +248,8 @@ func (r *NodeCIDRAllocationReconciler) Reconcile(ctx context.Context, req ctrl.R
244248
}
245249

246250
for _, subnet := range subnets {
247-
networkAllocated, err := statcan_net.NetworkAllocated(subnet, &allClusterNodes, nodeCIDRAllocation.Spec.StaticAllocations)
251+
// check that that the subnet IP space isn't already allocated by another node and doesn't overlap with allocatedSubnetInReconcile & staticAllocations
252+
networkAllocated, err := statcan_net.NetworkAllocated(subnet, &allClusterNodes, append(allocatedSubnetInReconcile, nodeCIDRAllocation.Spec.StaticAllocations...))
248253
if err != nil {
249254
rl.Error(
250255
err,
@@ -254,13 +259,20 @@ func (r *NodeCIDRAllocationReconciler) Reconcile(ctx context.Context, req ctrl.R
254259
return ctrl.Result{}, r.finalizeReconcile(ctx, &nodeCIDRAllocation, &matchingNodes, err)
255260
}
256261

257-
if !networkAllocated && !helper.StringInSlice(subnet, freeSubnets[requiredCIDRMask]) {
258-
freeSubnets[requiredCIDRMask] = append(freeSubnets[requiredCIDRMask], subnet)
262+
if !networkAllocated {
263+
if node.Spec.PodCIDR != "" {
264+
node.Spec.PodCIDR = subnet
265+
allocatedSubnetInReconcile = append(allocatedSubnetInReconcile, subnet)
266+
267+
continue
268+
}
269+
270+
remainingFreeSubnets = append(remainingFreeSubnets, subnet)
259271
}
260272
}
261273
}
262274

263-
if len(freeSubnets[requiredCIDRMask]) == 0 {
275+
if node.Spec.PodCIDR == "" {
264276
rl.Info("unable to allocate podCIDR for node. no sufficient address space capacity for Node",
265277
"name", node.GetName(),
266278
"requiredSubnetCIDR", requiredCIDRMask,
@@ -277,8 +289,6 @@ func (r *NodeCIDRAllocationReconciler) Reconcile(ctx context.Context, req ctrl.R
277289
return ctrl.Result{}, r.finalizeReconcile(ctx, &nodeCIDRAllocation, &matchingNodes, nil)
278290
}
279291

280-
// Assign the first free subnet of the requested size PodCIDR to the Node
281-
node.Spec.PodCIDR, freeSubnets[requiredCIDRMask] = freeSubnets[requiredCIDRMask][0], freeSubnets[requiredCIDRMask][1:]
282292
if err := r.Update(ctx, &node); err != nil {
283293
if apierrors.IsNotFound(err) {
284294
// Node no longer found. It may have been deleted after reconcilliation request - return and do not requeue
@@ -287,7 +297,7 @@ func (r *NodeCIDRAllocationReconciler) Reconcile(ctx context.Context, req ctrl.R
287297
rl.Error(err, "unable to set pod CIDR for Node resource",
288298
"name", node.GetName(),
289299
"podCIDR", node.Spec.PodCIDR,
290-
"freeAvailable", freeSubnets,
300+
"remainingFreeSubnets", remainingFreeSubnets,
291301
)
292302

293303
return ctrl.Result{}, r.finalizeReconcile(ctx, &nodeCIDRAllocation, &matchingNodes, err)
@@ -297,7 +307,7 @@ func (r *NodeCIDRAllocationReconciler) Reconcile(ctx context.Context, req ctrl.R
297307
"assigned PodCIDR to Node resource",
298308
"name", node.GetName(),
299309
"podCIDR", node.Spec.PodCIDR,
300-
"remainingFreeSubnets", len(freeSubnets[requiredCIDRMask]),
310+
"remainingFreeSubnets", len(remainingFreeSubnets),
301311
)
302312
}
303313

internal/networking/networking.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func NetworksOverlap(a, b string) (bool, error) {
9898
// NetworkAllocated uses a variety of conditions to ensure that there is no
9999
// conflicting allocation that would present problems for subnet.
100100
// returns (true,nil) when the subnet provided is not allocated by or overlapping with any nodes.
101-
func NetworkAllocated(subnet string, nodes *corev1.NodeList, staticAllocations []string) (bool, error) {
101+
func NetworkAllocated(subnet string, nodes *corev1.NodeList, reservedSubnets []string) (bool, error) {
102102
for _, n := range nodes.Items {
103103
if n.Spec.PodCIDR == "" {
104104
continue
@@ -114,8 +114,8 @@ func NetworkAllocated(subnet string, nodes *corev1.NodeList, staticAllocations [
114114
}
115115
}
116116

117-
for _, sa := range staticAllocations {
118-
networksOverlap, err := NetworksOverlap(subnet, sa)
117+
for _, s := range reservedSubnets {
118+
networksOverlap, err := NetworksOverlap(subnet, s)
119119
if err != nil {
120120
return false, err
121121
}

0 commit comments

Comments
 (0)