@@ -246,9 +246,14 @@ func (na *NodeAllocator) syncNodeNetworkAnnotations(node *corev1.Node) error {
246246 networkName := na .netInfo .GetNetworkName ()
247247
248248 networkID , err := util .ParseNetworkIDAnnotation (node , networkName )
249- if err != nil && ! util .IsAnnotationNotSetError (err ) {
250- // Log the error and try to allocate new subnets
251- 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
252257 }
253258
254259 updatedSubnetsMap := map [string ][]* net.IPNet {}
@@ -319,14 +324,14 @@ func (na *NodeAllocator) syncNodeNetworkAnnotations(node *corev1.Node) error {
319324 updatedSubnetsMap [networkName ] = validExistingSubnets
320325 }
321326 }
322- newTunnelID := util . NoID
327+ newTunnelID := types . NoTunnelID
323328 if util .IsNetworkSegmentationSupportEnabled () && na .netInfo .IsPrimaryNetwork () && util .DoesNetworkRequireTunnelIDs (na .netInfo ) {
324329 existingTunnelID , err := util .ParseUDNLayer2NodeGRLRPTunnelIDs (node , networkName )
325330 if err != nil && ! util .IsAnnotationNotSetError (err ) {
326331 return fmt .Errorf ("failed to fetch tunnelID annotation from the node %s for network %s, err: %v" ,
327332 node .Name , networkName , err )
328333 }
329- if existingTunnelID == util .InvalidID {
334+ if existingTunnelID == types .InvalidID {
330335 if newTunnelID , err = na .idAllocator .AllocateID (networkName + "_" + node .Name ); err != nil {
331336 return fmt .Errorf ("failed to assign node %s tunnel id for network %s: %w" , node .Name , networkName , err )
332337 }
@@ -340,22 +345,24 @@ func (na *NodeAllocator) syncNodeNetworkAnnotations(node *corev1.Node) error {
340345 }
341346 }
342347
343- // only update node annotation with ID if it had it before and does not match
344- // NoID means it will not update
345- // InvalidID means node did not have the annotation
346- annoUpdateID := util .NoID
347- if networkID != util .InvalidID && networkID != na .networkID {
348- annoUpdateID = na .networkID
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
349356 }
350357
351358 // Also update the node annotation if the networkID doesn't match
352- if len (updatedSubnetsMap ) > 0 || annoUpdateID != util . NoID || len (allocatedJoinSubnets ) > 0 || newTunnelID != util . NoID {
353- err = na .updateNodeNetworkAnnotationsWithRetry (node .Name , updatedSubnetsMap , annoUpdateID , 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 )
354361 if err != nil {
355362 if errR := na .clusterSubnetAllocator .ReleaseNetworks (node .Name , allocatedSubnets ... ); errR != nil {
356363 klog .Warningf ("Error releasing node %s subnets: %v" , node .Name , errR )
357364 }
358- if newTunnelID != util . NoID {
365+ if newTunnelID != types . NoTunnelID {
359366 na .idAllocator .ReleaseID (networkName + "_" + node .Name )
360367 klog .Infof ("Releasing node %s tunnelID for network %s since annotation update failed" , node .Name , networkName )
361368 }
@@ -453,14 +460,14 @@ func (na *NodeAllocator) updateNodeNetworkAnnotationsWithRetry(nodeName string,
453460 return fmt .Errorf ("failed to update node %q annotation LRPAddrAnnotation %s: %w" ,
454461 node .Name , util .JoinIPNets (joinAddr , "," ), err )
455462 }
456- if networkId != util . NoID {
463+ if networkId != types . NoNetworkID {
457464 cnode .Annotations , err = util .UpdateNetworkIDAnnotation (cnode .Annotations , networkName , networkId )
458465 if err != nil {
459466 return fmt .Errorf ("failed to update node %q network id annotation %d for network %s: %w" ,
460467 node .Name , networkId , networkName , err )
461468 }
462469 }
463- if tunnelID != util . NoID {
470+ if tunnelID != types . NoTunnelID {
464471 cnode .Annotations , err = util .UpdateUDNLayer2NodeGRLRPTunnelIDs (cnode .Annotations , networkName , tunnelID )
465472 if err != nil {
466473 return fmt .Errorf ("failed to update node %q tunnel id annotation %d for network %s: %w" ,
@@ -497,7 +504,7 @@ func (na *NodeAllocator) Cleanup() error {
497504
498505 hostSubnetsMap := map [string ][]* net.IPNet {networkName : nil }
499506 // passing util.InvalidID deletes the network/tunnel id annotation for the network.
500- err = na .updateNodeNetworkAnnotationsWithRetry (node .Name , hostSubnetsMap , util .InvalidID , util .InvalidID , nil )
507+ err = na .updateNodeNetworkAnnotationsWithRetry (node .Name , hostSubnetsMap , types .InvalidID , types .InvalidID , nil )
501508 if err != nil {
502509 return fmt .Errorf ("failed to clear node %q subnet annotation for network %s" ,
503510 node .Name , networkName )
0 commit comments