@@ -70,7 +70,7 @@ type nadController struct {
7070 // nads to network mapping
7171 nads map [string ]string
7272
73- // primaryNADs holds a mapping of namespace to primary NAD names
73+ // primaryNADs holds a mapping of namespace to NAD of primary UDNs
7474 primaryNADs map [string ]string
7575
7676 networkIDAllocator id.Allocator
@@ -425,26 +425,15 @@ func (c *nadController) GetActiveNetworkForNamespace(namespace string) (util.Net
425425 return & util.DefaultNetInfo {}, nil
426426 }
427427
428- c .RLock ()
429- defer c .RUnlock ()
430- primaryNAD := c .primaryNADs [namespace ]
431- if primaryNAD != "" {
432- // we have a primary NAD, no need to check for NS UDN annotation because NAD would not have existed otherwise
433- // get the network
434- netName := c .nads [primaryNAD ]
435- if netName == "" {
436- // this should never happen where we have a nad keyed in the primaryNADs
437- // map, but it doesn't exist in the nads map
438- panic ("NAD Controller broken consistency between primary NADs and cached NADs" )
439- }
440- network := c .networkController .getNetwork (netName )
441- n := util .NewMutableNetInfo (network )
442- // update the returned netInfo copy to only have the primary NAD for this namespace
443- n .SetNADs (primaryNAD )
444- return n , nil
428+ network , nad := c .getActiveNetworkForNamespace (namespace )
429+ if network != nil && network .IsPrimaryNetwork () {
430+ // primary UDN found
431+ copy := util .NewMutableNetInfo (network )
432+ copy .SetNADs (nad )
433+ return copy , nil
445434 }
446435
447- // no primary network found, make sure we just haven't processed it yet and no UDN / CUDN exists
436+ // no primary UDN found, make sure we just haven't processed it yet and no UDN / CUDN exists
448437 udns , err := c .udnLister .UserDefinedNetworks (namespace ).List (labels .Everything ())
449438 if err != nil {
450439 return nil , fmt .Errorf ("error getting user defined networks: %w" , err )
@@ -482,6 +471,38 @@ func (c *nadController) GetActiveNetworkForNamespace(namespace string) (util.Net
482471 return nil , util .NewInvalidPrimaryNetworkError (namespace )
483472}
484473
474+ func (c * nadController ) GetActiveNetworkForNamespaceFast (namespace string ) util.NetInfo {
475+ network , _ := c .getActiveNetworkForNamespace (namespace )
476+ return network
477+ }
478+
479+ func (c * nadController ) getActiveNetworkForNamespace (namespace string ) (util.NetInfo , string ) {
480+ c .RLock ()
481+ defer c .RUnlock ()
482+
483+ var network util.NetInfo
484+ primaryNAD := c .primaryNADs [namespace ]
485+ switch primaryNAD {
486+ case "" :
487+ // default network
488+ network = c .networkController .getNetwork (types .DefaultNetworkName )
489+ if network == nil {
490+ network = & util.DefaultNetInfo {}
491+ }
492+ default :
493+ // we have a primary network
494+ netName := c .nads [primaryNAD ]
495+ if netName == "" {
496+ // this should never happen where we have a nad keyed in the primaryNADs
497+ // map, but it doesn't exist in the nads map
498+ panic ("NAD Controller broken consistency between primary NADs and cached NADs" )
499+ }
500+ network = c .networkController .getNetwork (netName )
501+ }
502+
503+ return network , primaryNAD
504+ }
505+
485506func (c * nadController ) GetNetwork (name string ) util.NetInfo {
486507 network := c .networkController .getNetwork (name )
487508 if network == nil && name == types .DefaultNetworkName {
0 commit comments