@@ -175,6 +175,43 @@ func (na *NodeAllocator) releaseHybridOverlayNodeSubnet(nodeName string) {
175175 klog .Infof ("Deleted hybrid overlay HostSubnets for node %s" , nodeName )
176176}
177177
178+ // NeedsNodeAllocation determines if the annotations that are assigned by NodeAllocator are missing on a node
179+ func (na * NodeAllocator ) NeedsNodeAllocation (node * corev1.Node ) bool {
180+ // hybrid overlay check
181+ if util .NoHostSubnet (node ) {
182+ if na .hasHybridOverlayAllocation () {
183+ if _ , ok := node .Annotations [hotypes .HybridOverlayNodeSubnet ]; ! ok {
184+ return true
185+ }
186+ }
187+ return false
188+ }
189+
190+ // ovn node check
191+ // allocation is all or nothing, so if one field was allocated from:
192+ // nodeSubnets, joinSubnet, layer 2 tunnel id, then all of them were
193+ if na .hasNodeSubnetAllocation () {
194+ if util .HasNodeHostSubnetAnnotation (node , na .netInfo .GetNetworkName ()) {
195+ return false
196+ }
197+ }
198+
199+ if na .hasJoinSubnetAllocation () {
200+ if util .HasNodeGatewayRouterJoinNetwork (node , na .netInfo .GetNetworkName ()) {
201+ return false
202+ }
203+ }
204+
205+ if util .IsNetworkSegmentationSupportEnabled () && na .netInfo .IsPrimaryNetwork () && util .DoesNetworkRequireTunnelIDs (na .netInfo ) {
206+ if util .HasUDNLayer2NodeGRLRPTunnelID (node , na .netInfo .GetNetworkName ()) {
207+ return false
208+ }
209+ }
210+
211+ return true
212+
213+ }
214+
178215// HandleAddUpdateNodeEvent handles the add or update node event
179216func (na * NodeAllocator ) HandleAddUpdateNodeEvent (node * corev1.Node ) error {
180217 defer na .recordSubnetUsage ()
@@ -202,14 +239,21 @@ func (na *NodeAllocator) HandleAddUpdateNodeEvent(node *corev1.Node) error {
202239
203240// syncNodeNetworkAnnotations does 2 things
204241// - syncs the node's allocated subnets in the node subnet annotation
205- // - syncs the network id in the node network id annotation
242+ // - syncs the join subnet annotation
243+ // - syncs the layer 2 tunnel id annotation
244+ // - syncs the network id in the node network id annotation (legacy)
206245func (na * NodeAllocator ) syncNodeNetworkAnnotations (node * corev1.Node ) error {
207246 networkName := na .netInfo .GetNetworkName ()
208247
209248 networkID , err := util .ParseNetworkIDAnnotation (node , networkName )
210- if err != nil && ! util .IsAnnotationNotSetError (err ) {
211- // Log the error and try to allocate new subnets
212- klog .Warningf ("Failed to get node %s network id annotations for network %s : %v" , node .Name , networkName , err )
249+ if err != nil {
250+ if ! util .IsAnnotationNotSetError (err ) {
251+ // Log the error and try to allocate new subnets
252+ klog .Warningf ("Failed to get node %s network id annotations for network %s : %v" , node .Name , networkName , err )
253+ }
254+ // if there is an error we are going to set networkID here to NoNetworkID to prevent
255+ // the annotation being updated
256+ networkID = types .NoNetworkID
213257 }
214258
215259 updatedSubnetsMap := map [string ][]* net.IPNet {}
@@ -280,14 +324,14 @@ func (na *NodeAllocator) syncNodeNetworkAnnotations(node *corev1.Node) error {
280324 updatedSubnetsMap [networkName ] = validExistingSubnets
281325 }
282326 }
283- newTunnelID := util . NoID
327+ newTunnelID := types . NoTunnelID
284328 if util .IsNetworkSegmentationSupportEnabled () && na .netInfo .IsPrimaryNetwork () && util .DoesNetworkRequireTunnelIDs (na .netInfo ) {
285329 existingTunnelID , err := util .ParseUDNLayer2NodeGRLRPTunnelIDs (node , networkName )
286330 if err != nil && ! util .IsAnnotationNotSetError (err ) {
287331 return fmt .Errorf ("failed to fetch tunnelID annotation from the node %s for network %s, err: %v" ,
288332 node .Name , networkName , err )
289333 }
290- if existingTunnelID == util .InvalidID {
334+ if existingTunnelID == types .InvalidID {
291335 if newTunnelID , err = na .idAllocator .AllocateID (networkName + "_" + node .Name ); err != nil {
292336 return fmt .Errorf ("failed to assign node %s tunnel id for network %s: %w" , node .Name , networkName , err )
293337 }
@@ -301,14 +345,24 @@ func (na *NodeAllocator) syncNodeNetworkAnnotations(node *corev1.Node) error {
301345 }
302346 }
303347
348+ // only update node annotation with ID if it had it before (networkID is not NoNetworkID)
349+ // and does not match.
350+ // NoNetworkID means do not update the annotation
351+ if networkID != types .NoNetworkID && networkID != na .networkID {
352+ networkID = na .networkID
353+ } else if networkID == na .networkID {
354+ // don't need to update if there was no change to the ID
355+ networkID = types .NoNetworkID
356+ }
357+
304358 // Also update the node annotation if the networkID doesn't match
305- if len (updatedSubnetsMap ) > 0 || na . networkID != networkID || len (allocatedJoinSubnets ) > 0 || newTunnelID != util . NoID {
306- err = na .updateNodeNetworkAnnotationsWithRetry (node .Name , updatedSubnetsMap , na . networkID , newTunnelID , allocatedJoinSubnets )
359+ if len (updatedSubnetsMap ) > 0 || networkID != types . NoNetworkID || len (allocatedJoinSubnets ) > 0 || newTunnelID != types . NoTunnelID {
360+ err = na .updateNodeNetworkAnnotationsWithRetry (node .Name , updatedSubnetsMap , networkID , newTunnelID , allocatedJoinSubnets )
307361 if err != nil {
308362 if errR := na .clusterSubnetAllocator .ReleaseNetworks (node .Name , allocatedSubnets ... ); errR != nil {
309363 klog .Warningf ("Error releasing node %s subnets: %v" , node .Name , errR )
310364 }
311- if util . IsNetworkSegmentationSupportEnabled () && na . netInfo . IsPrimaryNetwork () && util . DoesNetworkRequireTunnelIDs ( na . netInfo ) {
365+ if newTunnelID != types . NoTunnelID {
312366 na .idAllocator .ReleaseID (networkName + "_" + node .Name )
313367 klog .Infof ("Releasing node %s tunnelID for network %s since annotation update failed" , node .Name , networkName )
314368 }
@@ -406,12 +460,14 @@ func (na *NodeAllocator) updateNodeNetworkAnnotationsWithRetry(nodeName string,
406460 return fmt .Errorf ("failed to update node %q annotation LRPAddrAnnotation %s: %w" ,
407461 node .Name , util .JoinIPNets (joinAddr , "," ), err )
408462 }
409- cnode .Annotations , err = util .UpdateNetworkIDAnnotation (cnode .Annotations , networkName , networkId )
410- if err != nil {
411- return fmt .Errorf ("failed to update node %q network id annotation %d for network %s: %w" ,
412- node .Name , networkId , networkName , err )
463+ if networkId != types .NoNetworkID {
464+ cnode .Annotations , err = util .UpdateNetworkIDAnnotation (cnode .Annotations , networkName , networkId )
465+ if err != nil {
466+ return fmt .Errorf ("failed to update node %q network id annotation %d for network %s: %w" ,
467+ node .Name , networkId , networkName , err )
468+ }
413469 }
414- if tunnelID != util . NoID {
470+ if tunnelID != types . NoTunnelID {
415471 cnode .Annotations , err = util .UpdateUDNLayer2NodeGRLRPTunnelIDs (cnode .Annotations , networkName , tunnelID )
416472 if err != nil {
417473 return fmt .Errorf ("failed to update node %q tunnel id annotation %d for network %s: %w" ,
@@ -448,7 +504,7 @@ func (na *NodeAllocator) Cleanup() error {
448504
449505 hostSubnetsMap := map [string ][]* net.IPNet {networkName : nil }
450506 // passing util.InvalidID deletes the network/tunnel id annotation for the network.
451- err = na .updateNodeNetworkAnnotationsWithRetry (node .Name , hostSubnetsMap , util .InvalidID , util .InvalidID , nil )
507+ err = na .updateNodeNetworkAnnotationsWithRetry (node .Name , hostSubnetsMap , types .InvalidID , types .InvalidID , nil )
452508 if err != nil {
453509 return fmt .Errorf ("failed to clear node %q subnet annotation for network %s" ,
454510 node .Name , networkName )
@@ -472,8 +528,6 @@ func (na *NodeAllocator) allocateNodeSubnets(allocator SubnetAllocator, nodeName
472528 expectedHostSubnets = 2
473529 }
474530
475- klog .Infof ("Expected %d subnets on node %s, found %d: %v" , expectedHostSubnets , nodeName , len (existingSubnets ), existingSubnets )
476-
477531 // If any existing subnets the node has are valid, mark them as reserved.
478532 // The node might have invalid or already-reserved subnets, or it might
479533 // have more subnets than configured in OVN (like for dual-stack to/from
@@ -486,7 +540,6 @@ func (na *NodeAllocator) allocateNodeSubnets(allocator SubnetAllocator, nodeName
486540 for _ , subnet := range existingSubnets {
487541 if (ipv4Mode && utilnet .IsIPv4CIDR (subnet ) && ! foundIPv4 ) || (ipv6Mode && utilnet .IsIPv6CIDR (subnet ) && ! foundIPv6 ) {
488542 if err := allocator .MarkAllocatedNetworks (nodeName , subnet ); err == nil {
489- klog .Infof ("Valid subnet %v allocated on node %s" , subnet , nodeName )
490543 existingSubnets [n ] = subnet
491544 n ++
492545 if utilnet .IsIPv4CIDR (subnet ) {
@@ -498,17 +551,18 @@ func (na *NodeAllocator) allocateNodeSubnets(allocator SubnetAllocator, nodeName
498551 }
499552 }
500553 // this subnet is no longer needed; release it
501- klog .Infof ("Releasing unused or invalid subnet %v on node %s" , subnet , nodeName )
554+ klog .Infof ("Releasing unused or invalid subnet %v on node %s, network %s" ,
555+ subnet , na .netInfo .GetNetworkName (), nodeName )
502556 if err := allocator .ReleaseNetworks (nodeName , subnet ); err != nil {
503- klog .Warningf ("Failed to release subnet %v on node %s: %v" , subnet , nodeName , err )
557+ klog .Warningf ("Failed to release subnet %v on node %s, network %s: %v" ,
558+ subnet , nodeName , na .netInfo .GetNetworkName (), err )
504559 }
505560 }
506561 // recreate existingSubnets with the valid subnets
507562 existingSubnets = existingSubnets [:n ]
508563
509564 // Node has enough valid subnets already allocated
510565 if len (existingSubnets ) == expectedHostSubnets {
511- klog .Infof ("Allowed existing subnets %v on node %s" , existingSubnets , nodeName )
512566 return existingSubnets , allocatedSubnets , nil
513567 }
514568
@@ -517,9 +571,10 @@ func (na *NodeAllocator) allocateNodeSubnets(allocator SubnetAllocator, nodeName
517571 defer func () {
518572 if releaseAllocatedSubnets {
519573 for _ , subnet := range allocatedSubnets {
520- klog .Warningf ("Releasing subnet %v on node %s" , subnet , nodeName )
574+ klog .Warningf ("Releasing subnet %v on node %s, network: %s " , subnet , nodeName , na . netInfo . GetNetworkName () )
521575 if errR := allocator .ReleaseNetworks (nodeName , subnet ); errR != nil {
522- klog .Warningf ("Error releasing subnet %v on node %s: %v" , subnet , nodeName , errR )
576+ klog .Warningf ("Error releasing subnet %v on node %s, network %s: %v" ,
577+ subnet , na .netInfo .GetNetworkName (), nodeName , errR )
523578 }
524579 }
525580 }
@@ -528,12 +583,14 @@ func (na *NodeAllocator) allocateNodeSubnets(allocator SubnetAllocator, nodeName
528583 // allocateOneSubnet is a helper to process the result of a subnet allocation
529584 allocateOneSubnet := func (allocatedHostSubnet * net.IPNet , allocErr error ) error {
530585 if allocErr != nil {
531- return fmt .Errorf ("error allocating network for node %s: %v" , nodeName , allocErr )
586+ return fmt .Errorf ("error allocating network for node %s, network name %s: %v" ,
587+ nodeName , na .netInfo .GetNetworkName (), allocErr )
532588 }
533589 // the allocator returns nil if it can't provide a subnet
534590 // we should filter them out or they will be appended to the slice
535591 if allocatedHostSubnet != nil {
536- klog .V (5 ).Infof ("Allocating subnet %v on node %s" , allocatedHostSubnet , nodeName )
592+ klog .V (5 ).Infof ("Allocating subnet %v on node %s for network: %s" ,
593+ allocatedHostSubnet , nodeName , na .netInfo .GetNetworkName ())
537594 allocatedSubnets = append (allocatedSubnets , allocatedHostSubnet )
538595 }
539596 return nil
@@ -556,12 +613,12 @@ func (na *NodeAllocator) allocateNodeSubnets(allocator SubnetAllocator, nodeName
556613 // so it will require a reconfiguration and restart.
557614 wantedSubnets := expectedHostSubnets - len (existingSubnets )
558615 if wantedSubnets > 0 && len (allocatedSubnets ) != wantedSubnets {
559- return nil , nil , fmt .Errorf ("error allocating networks for node %s: %d subnets expected only new %d subnets allocated" ,
560- nodeName , expectedHostSubnets , len (allocatedSubnets ))
616+ return nil , nil , fmt .Errorf ("error allocating networks for network: %s, node %s: %d subnets expected only new %d subnets allocated" ,
617+ na . netInfo . GetNetworkName (), nodeName , expectedHostSubnets , len (allocatedSubnets ))
561618 }
562619
563620 hostSubnets := append (existingSubnets , allocatedSubnets ... )
564- klog .Infof ("Allocated Subnets %v on Node %s" , hostSubnets , nodeName )
621+ klog .Infof ("Allocated Subnets %v on Node %s for Network: %s " , hostSubnets , nodeName , na . netInfo . GetNetworkName () )
565622
566623 // Success; prevent the release-on-error from triggering and return all node subnets
567624 releaseAllocatedSubnets = false
0 commit comments