@@ -311,70 +311,40 @@ func (ca *cloudCIDRAllocator) updateCIDRAllocation(nodeName string) error {
311
311
312
312
cidrStrings := make ([]string , 0 )
313
313
314
- // if there are no interfaces or there is 1 interface
315
- // but does not have IP alias or IPv6 ranges no CIDR
316
- // can be allocated
317
- if len (instance .NetworkInterfaces ) == 0 ||
318
- (len (instance .NetworkInterfaces ) == 1 &&
319
- len (instance .NetworkInterfaces [0 ].AliasIpRanges ) == 0 &&
320
- ca .cloud .GetIPV6Address (instance .NetworkInterfaces [0 ]) == nil ) {
314
+ if len (instance .NetworkInterfaces ) == 0 || (len (instance .NetworkInterfaces ) == 1 && len (instance .NetworkInterfaces [0 ].AliasIpRanges ) == 0 ) {
321
315
nodeutil .RecordNodeStatusChange (ca .recorder , node , "CIDRNotAvailable" )
322
316
return fmt .Errorf ("failed to allocate cidr: Node %v has no ranges from which CIDRs can be allocated" , node .Name )
323
317
}
324
318
325
319
// sets the v1.NodeNetworkUnavailable condition to False
326
320
ca .setNetworkCondition (node )
327
321
328
- // nodes in clusters WITHOUT multi-networking are expected to have
329
- // only 1 network-interface and 1 alias IPv4 range or/and 1 IPv6 address
330
- // multi-network cluster may have 1 interface with multiple alias
331
- if len (instance .NetworkInterfaces ) == 1 &&
332
- (len (instance .NetworkInterfaces [0 ].AliasIpRanges ) == 1 ||
333
- ca .cloud .GetIPV6Address (instance .NetworkInterfaces [0 ]) != nil ) {
334
- // with 1 alias IPv4 range on single IPv4 or dual stack clusters
335
- if len (instance .NetworkInterfaces [0 ].AliasIpRanges ) == 1 {
336
- cidrStrings = append (cidrStrings , instance .NetworkInterfaces [0 ].AliasIpRanges [0 ].IpCidrRange )
337
- }
338
- // with 1 IPv6 range on single IPv6 or dual stack cluster
322
+ // nodes in clusters WITHOUT multi-networking are expected to have only 1 network-interface with 1 alias IP range.
323
+ if len (instance .NetworkInterfaces ) == 1 && len (instance .NetworkInterfaces [0 ].AliasIpRanges ) == 1 {
324
+ cidrStrings = append (cidrStrings , instance .NetworkInterfaces [0 ].AliasIpRanges [0 ].IpCidrRange )
339
325
ipv6Addr := ca .cloud .GetIPV6Address (instance .NetworkInterfaces [0 ])
340
326
if ipv6Addr != nil {
341
327
cidrStrings = append (cidrStrings , ipv6Addr .String ())
342
328
}
343
329
} else {
344
330
// multi-networking enabled clusters
345
- cidrStrings , err = ca .performMultiNetworkCIDRAllocation (node , instance .NetworkInterfaces )
331
+ hasNodeLabels , defaultSubnet , defaultPodRange := getNodeDefaultLabels (node )
332
+ // if there's no node label get the cidrStrings with the old way by comparing the default Network and GNP
333
+ cidrStrings , err = ca .performMultiNetworkCIDRAllocation (node , instance .NetworkInterfaces , hasNodeLabels )
346
334
if err != nil {
347
- nodeutil .RecordNodeStatusChange (ca .recorder , node , "CIDRNotAvailable " )
348
- return fmt .Errorf ("failed to get cidr(s) from provider : %v" , err )
335
+ nodeutil .RecordNodeStatusChange (ca .recorder , node , "AnnotationsNotAvailable " )
336
+ return fmt .Errorf ("failed to perform node annotations for multi-networking : %v" , err )
349
337
}
350
- }
351
- if len (cidrStrings ) == 0 {
352
- nodeutil .RecordNodeStatusChange (ca .recorder , node , "CIDRNotAvailable" )
353
- return fmt .Errorf ("failed to allocate cidr: Node %v has no CIDRs" , node .Name )
354
- }
355
- // Can have at most 2 ips (one for v4 and one for v6)
356
- if len (cidrStrings ) > 2 {
357
- klog .InfoS ("Got more than 2 ips, truncating to 2" , "cidrStrings" , cidrStrings )
358
- cidrStrings = cidrStrings [:2 ]
359
- }
360
-
361
- cidrs , err := netutils .ParseCIDRs (cidrStrings )
362
- if err != nil {
363
- return fmt .Errorf ("failed to parse strings %v as CIDRs: %v" , cidrStrings , err )
364
- }
365
- if len (cidrs ) > 1 {
366
- if dualStack , _ := netutils .IsDualStackCIDRs (cidrs ); ! dualStack {
367
- return fmt .Errorf ("err: IPs are not dual stack, CIDRS: %v" , cidrStrings )
338
+ if hasNodeLabels {
339
+ cidrStrings = ca .extractDefaultNwCIDRs (instance .NetworkInterfaces , defaultSubnet , defaultPodRange )
368
340
}
369
341
}
370
342
371
- node .Spec .PodCIDR = cidrStrings [0 ]
372
- node .Spec .PodCIDRs = cidrStrings
373
-
374
- err = ca .updateNodeCIDR (node , oldNode )
375
- if err != nil {
343
+ // update Node.Spec.PodCIDR(s)
344
+ if err = ca .updateNodePodCIDRWithCidrStrings (oldNode , node , cidrStrings ); err != nil {
376
345
return err
377
346
}
347
+
378
348
if ! reflect .DeepEqual (node .Annotations , oldNode .Annotations ) {
379
349
// retain old north interfaces annotation
380
350
var oldNorthInterfacesAnnotation networkv1.NorthInterfacesAnnotation
@@ -409,6 +379,34 @@ func (ca *cloudCIDRAllocator) updateCIDRAllocation(nodeName string) error {
409
379
return err
410
380
}
411
381
382
+ // updateNodePodCIDRWithCidrStrings update the Node object with Spec.PodCIDR(s),
383
+ // returns error if cidrStrings is not valid or fails to update the Node object
384
+ func (ca * cloudCIDRAllocator ) updateNodePodCIDRWithCidrStrings (oldNode * v1.Node , node * v1.Node , cidrStrings []string ) error {
385
+ if len (cidrStrings ) == 0 {
386
+ nodeutil .RecordNodeStatusChange (ca .recorder , node , "CIDRNotAvailable" )
387
+ return fmt .Errorf ("failed to allocate cidr: Node %v has no CIDRs" , node .Name )
388
+ }
389
+ // Can have at most 2 ips (one for v4 and one for v6)
390
+ if len (cidrStrings ) > 2 {
391
+ klog .InfoS ("Got more than 2 ips, truncating to 2" , "cidrStrings" , cidrStrings )
392
+ cidrStrings = cidrStrings [:2 ]
393
+ }
394
+
395
+ cidrs , err := netutils .ParseCIDRs (cidrStrings )
396
+ if err != nil {
397
+ return fmt .Errorf ("failed to parse strings %v as CIDRs: %v" , cidrStrings , err )
398
+ }
399
+ if len (cidrs ) > 1 {
400
+ if dualStack , _ := netutils .IsDualStackCIDRs (cidrs ); ! dualStack {
401
+ return fmt .Errorf ("err: IPs are not dual stack, CIDRS: %v" , cidrStrings )
402
+ }
403
+ }
404
+ node .Spec .PodCIDR = cidrStrings [0 ]
405
+ node .Spec .PodCIDRs = cidrStrings
406
+
407
+ return ca .updateNodeCIDR (node , oldNode )
408
+ }
409
+
412
410
func (ca * cloudCIDRAllocator ) setNetworkCondition (node * v1.Node ) {
413
411
cond := v1.NodeCondition {
414
412
Type : v1 .NodeNetworkUnavailable ,
0 commit comments