@@ -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
0 commit comments