Skip to content

Commit 562f752

Browse files
committed
Refactor ID nomenclature
InvalidID was being used for both networkID and tunnelID. noID was previously used for just tunnelID and I overloaded it to be used for networkID as well. This was not a great choice as it causes even more confusion because noID (value 0) has the same value as DefaultNetworkID. This commit refactors the variables and moves them into our global constants file. It changes noID to be noTunnelID and declares DefaultNetworkID there in a single place. It also creates a noNetworkID with a value that doesn't collide with DefaultNetworkID. Now logically the code should be much easier to read. Also removes a function and unit test that are no longer needed. Signed-off-by: Tim Rozet <[email protected]>
1 parent 029f4f6 commit 562f752

File tree

11 files changed

+59
-138
lines changed

11 files changed

+59
-138
lines changed

go-controller/pkg/clustermanager/clustermanager.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ import (
2525
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/util"
2626
)
2727

28-
const (
29-
// ID of the default network.
30-
defaultNetworkID = 0
31-
)
32-
3328
// ClusterManager structure is the object which manages the cluster nodes.
3429
// It creates a default network controller for the default network and a
3530
// secondary network cluster controller manager to manage the multi networks.

go-controller/pkg/clustermanager/network_cluster_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func newDefaultNetworkClusterController(netInfo util.NetInfo, ovnClient *util.OV
120120
// defaiult network
121121
networkIDAllocator := id.NewIDAllocator(types.DefaultNetworkName, 1)
122122
// Reserve the id 0 for the default network.
123-
err := networkIDAllocator.ReserveID(types.DefaultNetworkName, defaultNetworkID)
123+
err := networkIDAllocator.ReserveID(types.DefaultNetworkName, types.DefaultNetworkID)
124124
if err != nil {
125125
panic(fmt.Errorf("could not reserve default network ID: %w", err))
126126
}
@@ -175,7 +175,7 @@ func (ncc *networkClusterController) init() error {
175175
if util.DoesNetworkRequireTunnelIDs(ncc.GetNetInfo()) {
176176
ncc.tunnelIDAllocator = id.NewIDAllocator(ncc.GetNetworkName(), types.MaxLogicalPortTunnelKey)
177177
// Reserve the id 0. We don't want to assign this id to any of the pods or nodes.
178-
if err = ncc.tunnelIDAllocator.ReserveID("zero", util.NoID); err != nil {
178+
if err = ncc.tunnelIDAllocator.ReserveID("zero", types.NoTunnelID); err != nil {
179179
return err
180180
}
181181
if util.IsNetworkSegmentationSupportEnabled() && ncc.IsPrimaryNetwork() {
@@ -196,7 +196,7 @@ func (ncc *networkClusterController) init() error {
196196
node.Name, ncc.GetNetworkName(), err)
197197
}
198198
}
199-
if tunnelID != util.InvalidID {
199+
if tunnelID != types.InvalidID {
200200
if err := ncc.tunnelIDAllocator.ReserveID(ncc.GetNetworkName()+"_"+node.Name, tunnelID); err != nil {
201201
return fmt.Errorf("unable to reserve id for network %s, node %s: %w", ncc.GetNetworkName(), node.Name, err)
202202
}

go-controller/pkg/clustermanager/node/node_allocator.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

go-controller/pkg/networkmanager/api.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import (
1313
var ErrNetworkControllerTopologyNotManaged = errors.New("no cluster network controller to manage topology")
1414

1515
const (
16-
// DefaultNetworkID is the default network.
17-
DefaultNetworkID = 0
18-
1916
// MaxNetworks is the maximum number of networks allowed.
2017
MaxNetworks = 4096
2118
)

go-controller/pkg/networkmanager/nad_controller.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func newController(
104104
if zone == "" && node == "" {
105105
c.networkIDAllocator = id.NewIDAllocator("NetworkIDs", MaxNetworks)
106106
// Reserve the ID of the default network
107-
err := c.networkIDAllocator.ReserveID(types.DefaultNetworkName, DefaultNetworkID)
107+
err := c.networkIDAllocator.ReserveID(types.DefaultNetworkName, types.DefaultNetworkID)
108108
if err != nil {
109109
return nil, fmt.Errorf("failed to allocate default network ID: %w", err)
110110
}
@@ -359,7 +359,7 @@ func (c *nadController) syncNAD(key string, nad *nettypes.NetworkAttachmentDefin
359359
// if network ID has not been set and this is not the well known default
360360
// network, need to wait until cluster nad controller allocates an ID for
361361
// the network
362-
if ensureNetwork.GetNetworkID() == util.InvalidID {
362+
if ensureNetwork.GetNetworkID() == types.InvalidID {
363363
klog.V(4).Infof("%s: will wait for cluster manager to allocate an ID before ensuring network %s", c.name, nadNetworkName)
364364
return nil
365365
}
@@ -553,7 +553,7 @@ func (c *nadController) handleNetworkID(old util.NetInfo, new util.MutableNetInf
553553
}
554554

555555
var err error
556-
id := util.InvalidID
556+
id := types.InvalidID
557557

558558
// check what ID is currently annotated
559559
if nad != nil && nad.Annotations[types.OvnNetworkIDAnnotation] != "" {
@@ -585,16 +585,16 @@ func (c *nadController) handleNetworkID(old util.NetInfo, new util.MutableNetInf
585585
name := new.GetNetworkName()
586586

587587
// an ID was annotated, check if it is free to use or stale
588-
if id != util.InvalidID {
588+
if id != types.InvalidID {
589589
err = c.networkIDAllocator.ReserveID(name, id)
590590
if err != nil {
591591
// already reserved for a different network, allocate a new id
592-
id = util.InvalidID
592+
id = types.InvalidID
593593
}
594594
}
595595

596596
// we don't have an ID, allocate a new one
597-
if id == util.InvalidID {
597+
if id == types.InvalidID {
598598
id, err = c.networkIDAllocator.AllocateID(name)
599599
if err != nil {
600600
return fmt.Errorf("failed to allocate network ID: %w", err)

go-controller/pkg/networkmanager/nad_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ func TestNADController(t *testing.T) {
492492
nadClient: fakeClient.NetworkAttchDefClient,
493493
namespaceLister: &fakeNamespaceLister{},
494494
}
495-
err = nadController.networkIDAllocator.ReserveID(types.DefaultNetworkName, DefaultNetworkID)
495+
err = nadController.networkIDAllocator.ReserveID(types.DefaultNetworkName, types.DefaultNetworkID)
496496
g.Expect(err).ToNot(gomega.HaveOccurred())
497497
netController := nadController.networkController
498498

@@ -749,7 +749,7 @@ func TestSyncAll(t *testing.T) {
749749
for name, network := range expectedNetworks {
750750
g.Expect(actualNetworks).To(gomega.HaveKey(name))
751751
g.Expect(util.AreNetworksCompatible(actualNetworks[name], network)).To(gomega.BeTrue())
752-
if network.GetNetworkID() != util.InvalidID {
752+
if network.GetNetworkID() != types.InvalidID {
753753
g.Expect(actualNetworks[name].GetNetworkID()).To(gomega.Equal(network.GetNetworkID()))
754754
}
755755
}

go-controller/pkg/networkmanager/network_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ func nodeNeedsUpdate(oldNode, newNode *corev1.Node) bool {
532532
}
533533

534534
func (c *networkController) getRunningNetwork(id int) string {
535-
if id == DefaultNetworkID {
535+
if id == types.DefaultNetworkID {
536536
return types.DefaultNetworkName
537537
}
538538
for _, state := range c.getAllNetworkStates() {

go-controller/pkg/types/const.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,17 @@ const (
264264
// entry for the gateway routers. After this time, the entry is removed and
265265
// may be refreshed with a new ARP request.
266266
GRMACBindingAgeThreshold = "300"
267+
268+
// InvalidID signifies an invalid ID. Currently used for network and tunnel IDs.
269+
InvalidID = -1
270+
271+
// NoTunnelID signifies an empty/unset ID. Currently used for tunnel ID (reserved as un-usable when the allocator is created)
272+
NoTunnelID = 0
273+
274+
// DefaultNetworkID is reserved for the default network only
275+
DefaultNetworkID = 0
276+
277+
// NoNetworkID is used to signal internally that an ID is empty and should, updates
278+
// with this value should be ignored
279+
NoNetworkID = -2
267280
)

go-controller/pkg/util/multi_network.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ func newLayer3NetConfInfo(netconf *ovncnitypes.NetConf) (MutableNetInfo, error)
892892
joinSubnets: joinSubnets,
893893
mtu: netconf.MTU,
894894
mutableNetInfo: mutableNetInfo{
895-
id: InvalidID,
895+
id: types.InvalidID,
896896
nads: sets.Set[string]{},
897897
},
898898
}
@@ -919,7 +919,7 @@ func newLayer2NetConfInfo(netconf *ovncnitypes.NetConf) (MutableNetInfo, error)
919919
mtu: netconf.MTU,
920920
allowPersistentIPs: netconf.AllowPersistentIPs,
921921
mutableNetInfo: mutableNetInfo{
922-
id: InvalidID,
922+
id: types.InvalidID,
923923
nads: sets.Set[string]{},
924924
},
925925
}
@@ -943,7 +943,7 @@ func newLocalnetNetConfInfo(netconf *ovncnitypes.NetConf) (MutableNetInfo, error
943943
allowPersistentIPs: netconf.AllowPersistentIPs,
944944
physicalNetworkName: netconf.PhysicalNetworkName,
945945
mutableNetInfo: mutableNetInfo{
946-
id: InvalidID,
946+
id: types.InvalidID,
947947
nads: sets.Set[string]{},
948948
},
949949
}
@@ -1123,13 +1123,13 @@ func ParseNADInfo(nad *nettypes.NetworkAttachmentDefinition) (NetInfo, error) {
11231123
return nil, err
11241124
}
11251125

1126-
id := InvalidID
1126+
id := types.InvalidID
11271127
n, err := newNetInfo(netconf)
11281128
if err != nil {
11291129
return nil, err
11301130
}
11311131
if n.GetNetworkName() == types.DefaultNetworkName {
1132-
id = NoID
1132+
id = types.DefaultNetworkID
11331133
}
11341134
if nad.Annotations[types.OvnNetworkIDAnnotation] != "" {
11351135
annotated := nad.Annotations[types.OvnNetworkIDAnnotation]

go-controller/pkg/util/node_annotations.go

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,6 @@ const (
151151
// "l2-network-b":"10"}
152152
// }",
153153
ovnUDNLayer2NodeGRLRPTunnelIDs = "k8s.ovn.org/udn-layer2-node-gateway-router-lrp-tunnel-ids"
154-
155-
// InvalidID signifies an invalid ID. Currently used for network and tunnel IDs.
156-
InvalidID = -1
157-
158-
// NoID signifies an empty/unset ID. Currently used for tunnel ID (reserved as un-usable when the allocator is created)
159-
// and network ID (reserved as default network ID).
160-
NoID = 0
161154
)
162155

163156
type L3GatewayConfig struct {
@@ -485,12 +478,12 @@ func HasUDNLayer2NodeGRLRPTunnelID(node *corev1.Node, netName string) bool {
485478
func ParseUDNLayer2NodeGRLRPTunnelIDs(node *corev1.Node, netName string) (int, error) {
486479
tunnelIDsMap, err := parseNetworkMapAnnotation(node.Annotations, ovnUDNLayer2NodeGRLRPTunnelIDs)
487480
if err != nil {
488-
return InvalidID, err
481+
return types.InvalidID, err
489482
}
490483

491484
tunnelID, ok := tunnelIDsMap[netName]
492485
if !ok {
493-
return InvalidID, newAnnotationNotSetError("node %q has no %q annotation for network %s", node.Name, ovnUDNLayer2NodeGRLRPTunnelIDs, netName)
486+
return types.InvalidID, newAnnotationNotSetError("node %q has no %q annotation for network %s", node.Name, ovnUDNLayer2NodeGRLRPTunnelIDs, netName)
494487
}
495488

496489
return strconv.Atoi(tunnelID)
@@ -1363,12 +1356,12 @@ func parseNetworkMapAnnotation(nodeAnnotations map[string]string, annotationName
13631356
func ParseNetworkIDAnnotation(node *corev1.Node, netName string) (int, error) {
13641357
networkIDsMap, err := parseNetworkMapAnnotation(node.Annotations, ovnNetworkIDs)
13651358
if err != nil {
1366-
return InvalidID, err
1359+
return types.InvalidID, err
13671360
}
13681361

13691362
networkID, ok := networkIDsMap[netName]
13701363
if !ok {
1371-
return InvalidID, newAnnotationNotSetError("node %q has no %q annotation for network %s", node.Name, ovnNetworkIDs, netName)
1364+
return types.InvalidID, newAnnotationNotSetError("node %q has no %q annotation for network %s", node.Name, ovnNetworkIDs, netName)
13721365
}
13731366

13741367
return strconv.Atoi(networkID)
@@ -1393,7 +1386,7 @@ func updateNetworkAnnotation(annotations map[string]string, netName string, id i
13931386
}
13941387

13951388
// add or delete network id of the specified network
1396-
if id == InvalidID {
1389+
if id == types.InvalidID {
13971390
delete(idsMap, netName)
13981391
} else {
13991392
idsMap[netName] = strconv.Itoa(id)
@@ -1481,23 +1474,3 @@ func filterIPVersion(cidrs []netip.Prefix, v6 bool) []netip.Prefix {
14811474
}
14821475
return validCIDRs
14831476
}
1484-
1485-
// GetNetworkID will retrieve the network id for the specified network from the
1486-
// first node that contains that network at the network id annotations, it will
1487-
// return at the first ocurrence, rest of nodes will not be parsed.
1488-
func GetNetworkID(nodes []*corev1.Node, nInfo NetInfo) (int, error) {
1489-
for _, node := range nodes {
1490-
var err error
1491-
networkID, err := ParseNetworkIDAnnotation(node, nInfo.GetNetworkName())
1492-
if err != nil {
1493-
if IsAnnotationNotSetError(err) {
1494-
continue
1495-
}
1496-
return InvalidID, err
1497-
}
1498-
if networkID != InvalidID {
1499-
return networkID, nil
1500-
}
1501-
}
1502-
return InvalidID, fmt.Errorf("missing network id for network '%s'", nInfo.GetNetworkName())
1503-
}

0 commit comments

Comments
 (0)