Skip to content

Commit 8f6e3ee

Browse files
committed
Add GetActiveNetworkForNamespaceFast to network manager
GetActiveNetworkForNamespaceFast returns the primary network for the namespace if any or the default network otherwise. It is faster than GetActiveNetworkForNamespace because it does not copy the network and it does not verify against UDNs. To be used by controllers capable of reconciling primary network changes. Signed-off-by: Jaime Caamaño Ruiz <[email protected]>
1 parent d66a807 commit 8f6e3ee

File tree

3 files changed

+70
-27
lines changed

3 files changed

+70
-27
lines changed

go-controller/pkg/networkmanager/api.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,22 @@ const (
2020
// Interface is the main package entrypoint and provides network related
2121
// information to the rest of the project.
2222
type Interface interface {
23+
// GetActiveNetworkForNamespace returns a copy of the primary network for
24+
// the namespace if any or the default network otherwise. If there is a
25+
// primary UDN defined but the NAD has not been processed yet, returns
26+
// ErrNetworkControllerTopologyNotManaged. Used for controllers that are not
27+
// capable of reconciling primary network changes. If unsure, use this one
28+
// and not GetActiveNetworkForNamespaceFast.
2329
GetActiveNetworkForNamespace(namespace string) (util.NetInfo, error)
2430

31+
// GetActiveNetworkForNamespaceFast returns the primary network for the
32+
// namespace if any or the default network otherwise. It is faster than
33+
// GetActiveNetworkForNamespace because it does not copy the network and it
34+
// does not verify against UDNs. However, it is recommended to be used only
35+
// by controllers capable of reconciling primary network changes. If unsure,
36+
// use GetActiveNetworkForNamespace.
37+
GetActiveNetworkForNamespaceFast(namespace string) util.NetInfo
38+
2539
// GetNetwork returns the network of the given name or nil if unknown
2640
GetNetwork(name string) util.NetInfo
2741

@@ -171,6 +185,10 @@ func (nm defaultNetworkManager) GetActiveNetworkForNamespace(string) (util.NetIn
171185
return &util.DefaultNetInfo{}, nil
172186
}
173187

188+
func (nm defaultNetworkManager) GetActiveNetworkForNamespaceFast(string) util.NetInfo {
189+
return &util.DefaultNetInfo{}
190+
}
191+
174192
func (nm defaultNetworkManager) GetNetwork(name string) util.NetInfo {
175193
if name != types.DefaultNetworkName {
176194
return nil

go-controller/pkg/networkmanager/nad_controller.go

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
485506
func (c *nadController) GetNetwork(name string) util.NetInfo {
486507
network := c.networkController.getNetwork(name)
487508
if network == nil && name == types.DefaultNetworkName {

go-controller/pkg/testing/networkmanager/fake.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,28 @@ func (fnm *FakeNetworkManager) Start() error { return nil }
5454
func (fnm *FakeNetworkManager) Stop() {}
5555

5656
func (fnm *FakeNetworkManager) GetActiveNetworkForNamespace(namespace string) (util.NetInfo, error) {
57+
return fnm.GetActiveNetworkForNamespaceFast(namespace), nil
58+
}
59+
60+
func (fnm *FakeNetworkManager) GetActiveNetworkForNamespaceFast(namespace string) util.NetInfo {
5761
if primaryNetworks, ok := fnm.PrimaryNetworks[namespace]; ok && primaryNetworks != nil {
58-
return primaryNetworks, nil
62+
return primaryNetworks
5963
}
60-
return &util.DefaultNetInfo{}, nil
64+
return &util.DefaultNetInfo{}
6165
}
6266

63-
func (nc *FakeNetworkManager) GetNetwork(networkName string) util.NetInfo {
64-
for _, ni := range nc.PrimaryNetworks {
67+
func (fnm *FakeNetworkManager) GetNetwork(networkName string) util.NetInfo {
68+
for _, ni := range fnm.PrimaryNetworks {
6569
if ni.GetNetworkName() == networkName {
6670
return ni
6771
}
6872
}
6973
return &util.DefaultNetInfo{}
7074
}
7175

72-
func (nc *FakeNetworkManager) GetActiveNetworkNamespaces(networkName string) ([]string, error) {
76+
func (fnm *FakeNetworkManager) GetActiveNetworkNamespaces(networkName string) ([]string, error) {
7377
namespaces := make([]string, 0)
74-
for namespaceName, primaryNAD := range nc.PrimaryNetworks {
78+
for namespaceName, primaryNAD := range fnm.PrimaryNetworks {
7579
nadNetworkName := primaryNAD.GetNADs()[0]
7680
if nadNetworkName != networkName {
7781
continue
@@ -81,9 +85,9 @@ func (nc *FakeNetworkManager) GetActiveNetworkNamespaces(networkName string) ([]
8185
return namespaces, nil
8286
}
8387

84-
func (nc *FakeNetworkManager) DoWithLock(f func(network util.NetInfo) error) error {
88+
func (fnm *FakeNetworkManager) DoWithLock(f func(network util.NetInfo) error) error {
8589
var errs []error
86-
for _, ni := range nc.PrimaryNetworks {
90+
for _, ni := range fnm.PrimaryNetworks {
8791
if err := f(ni); err != nil {
8892
errs = append(errs, err)
8993
}

0 commit comments

Comments
 (0)