@@ -16,13 +16,11 @@ package nodes
16
16
17
17
import (
18
18
"errors"
19
- "math"
20
19
"regexp"
21
20
"strconv"
22
21
"strings"
23
22
24
23
"github.com/vmware/load-balancer-and-ingress-services-for-kubernetes/internal/lib"
25
-
26
24
"github.com/vmware/load-balancer-and-ingress-services-for-kubernetes/pkg/utils"
27
25
28
26
"github.com/vmware/alb-sdk/go/models"
@@ -45,10 +43,8 @@ func (o *AviObjectGraph) BuildVRFGraph(key, vrfName, nodeName string, deleteFlag
45
43
// Each AKO should have single VRF node as it deals with single cluster only.
46
44
aviVrfNode := aviVrfNodes [0 ]
47
45
48
- routeid := 1
49
46
if len (aviVrfNode .StaticRoutes ) == 0 {
50
47
aviVrfNode .NodeStaticRoutes = make (map [string ]StaticRouteDetails )
51
- aviVrfNode .NodeIds = make (map [int ]struct {})
52
48
}
53
49
var nodeRoutes []* models.StaticRoute
54
50
// For new node addition (coming from ingestion layer), nodes static routes will be attahced at the end
@@ -60,19 +56,36 @@ func (o *AviObjectGraph) BuildVRFGraph(key, vrfName, nodeName string, deleteFlag
60
56
node , err := utils .GetInformers ().NodeInformer .Lister ().Get (nodeName )
61
57
if err != nil {
62
58
utils .AviLog .Errorf ("key: %s, Error in fetching node details: %s: %v" , key , nodeName , err )
59
+ var staticRouteCopy []* models.StaticRoute
60
+ for i := 0 ; i < len (aviVrfNode .StaticRoutes ); i ++ {
61
+ if ! strings .HasPrefix (* aviVrfNode .StaticRoutes [i ].RouteID , nodeStaticRouteDetails .RouteIDPrefix ) {
62
+ staticRouteCopy = append (staticRouteCopy , aviVrfNode .StaticRoutes [i ])
63
+ }
64
+ }
65
+ aviVrfNode .StaticRoutes = staticRouteCopy
66
+ processNodeStaticRouteAndNodeDeletion (nodeName , aviVrfNode )
63
67
return err
64
68
}
69
+ var routeIdPrefix string
65
70
if ok {
66
- routeid = nodeStaticRouteDetails .routeID
71
+ routeIdPrefix = nodeStaticRouteDetails .RouteIDPrefix
67
72
} else {
68
- //O(n) for each node. But it will re-use previous index. So used instead of always incrementing index.
69
- routeid = findFreeRouteId (aviVrfNode .NodeIds )
73
+ routeIdPrefix = lib .Uuid4 ()
70
74
}
71
- aviVrfNode . NodeIds [ routeid ] = struct {}{}
72
- nodeRoutes , err = o .addRouteForNode (node , vrfName , routeid , aviVrfNode . NodeIds )
75
+
76
+ nodeRoutes , err = o .addRouteForNode (node , vrfName , routeIdPrefix )
73
77
if err != nil {
74
78
utils .AviLog .Errorf ("key: %s, Error Adding vrf for node %s: %v" , key , nodeName , err )
75
- delete (aviVrfNode .NodeIds , routeid )
79
+
80
+ //delete all the routes of this node
81
+ var staticRouteCopy []* models.StaticRoute
82
+ for i := 0 ; i < len (aviVrfNode .StaticRoutes ); i ++ {
83
+ if ! strings .HasPrefix (* aviVrfNode .StaticRoutes [i ].RouteID , lib .GetClusterName () + "-" + routeIdPrefix ) {
84
+ staticRouteCopy = append (staticRouteCopy , aviVrfNode .StaticRoutes [i ])
85
+ }
86
+ }
87
+ aviVrfNode .StaticRoutes = staticRouteCopy
88
+ processNodeStaticRouteAndNodeDeletion (nodeName , aviVrfNode )
76
89
return err
77
90
}
78
91
if ! ok {
@@ -81,62 +94,64 @@ func (o *AviObjectGraph) BuildVRFGraph(key, vrfName, nodeName string, deleteFlag
81
94
// node is not present and no overlapping of cidr, append at last
82
95
aviVrfNode .StaticRoutes = append (aviVrfNode .StaticRoutes , nodeRoutes ... )
83
96
nodeStaticRoute := StaticRouteDetails {}
84
- // start index shows at what index of StaticRoutes, nodes routes start (index based zero)
85
- nodeStaticRoute .StartIndex = routeid - 1
86
97
nodeStaticRoute .Count = len (nodeRoutes )
87
- nodeStaticRoute .routeID = routeid
98
+ nodeStaticRoute .RouteIDPrefix = routeIdPrefix
88
99
aviVrfNode .NodeStaticRoutes [nodeName ] = nodeStaticRoute
89
100
aviVrfNode .Nodes = append (aviVrfNode .Nodes , nodeName )
90
101
} else {
91
102
if len (nodeRoutes ) == 0 {
92
- delete (aviVrfNode .NodeIds , routeid )
103
+ //delete all the routes and details of this node
104
+ var staticRouteCopy []* models.StaticRoute
105
+ for i := 0 ; i < len (aviVrfNode .StaticRoutes ); i ++ {
106
+ if ! strings .HasPrefix (* aviVrfNode .StaticRoutes [i ].RouteID , lib .GetClusterName () + "-" + routeIdPrefix ) {
107
+ staticRouteCopy = append (staticRouteCopy , aviVrfNode .StaticRoutes [i ])
108
+ }
109
+ }
110
+ aviVrfNode .StaticRoutes = staticRouteCopy
111
+ processNodeStaticRouteAndNodeDeletion (nodeName , aviVrfNode )
93
112
}
94
113
}
95
114
} else {
96
115
// update case
97
- // Assumption: updated routes (values) for given node will not overlap with other nodes.
116
+ // Assumption: updated routes (values) for given node will not overlap with other nodes
98
117
// So only updating existing routes of that node.
99
118
utils .AviLog .Debugf ("key: %s, StaticRoutes before updation/deletion: [%v]" , key , utils .Stringify (aviVrfNode .StaticRoutes ))
100
- startIndex := nodeStaticRouteDetails .StartIndex
101
119
lenNewNodeRoutes := len (nodeRoutes )
102
120
diff := lenNewNodeRoutes - nodeStaticRouteDetails .Count
103
121
104
122
var staticRouteCopy []* models.StaticRoute
105
- copyTill := int (math .Min (float64 (startIndex ), float64 (len (aviVrfNode .StaticRoutes ))))
106
- staticRouteCopy = append (staticRouteCopy , aviVrfNode .StaticRoutes [:copyTill ]... )
123
+ for i := 0 ; i < len (aviVrfNode .StaticRoutes ); i ++ {
124
+ if ! strings .HasPrefix (* aviVrfNode .StaticRoutes [i ].RouteID , lib .GetClusterName () + "-" + routeIdPrefix ) {
125
+ staticRouteCopy = append (staticRouteCopy , aviVrfNode .StaticRoutes [i ])
126
+ }
127
+ }
107
128
108
129
staticRouteCopy = append (staticRouteCopy , nodeRoutes ... )
109
-
110
- copyFrom := int (math .Min (float64 (startIndex + nodeStaticRouteDetails .Count ), float64 (len (aviVrfNode .StaticRoutes ))))
111
- staticRouteCopy = append (staticRouteCopy , aviVrfNode .StaticRoutes [copyFrom :]... )
112
-
113
130
aviVrfNode .StaticRoutes = staticRouteCopy
114
131
115
132
//if diff is 0, there is no change in number of routes previously exist and newly created.
116
133
if diff != 0 {
117
- updateNodeStaticRoutes (aviVrfNode , deleteFlag , nodeName , lenNewNodeRoutes , diff )
134
+ //update all the routes of this node
135
+ updateNodeStaticRoutes (aviVrfNode , deleteFlag , nodeName , lenNewNodeRoutes )
118
136
}
119
137
if lenNewNodeRoutes == 0 {
120
- processNodeStaticRouteAndNodeIdDeletion (nodeName , aviVrfNode )
138
+ //delete all the routes of this node
139
+ processNodeStaticRouteAndNodeDeletion (nodeName , aviVrfNode )
121
140
}
122
141
utils .AviLog .Debugf ("key: %s, StaticRoutes after updation/deletion: [%v]" , key , utils .Stringify (aviVrfNode .StaticRoutes ))
123
142
}
124
143
} else {
125
- //delete case
144
+ //delete flag is turned on and node is deleted
126
145
utils .AviLog .Debugf ("key: %s, StaticRoutes before deletion: [%v]" , key , utils .Stringify (aviVrfNode .StaticRoutes ))
127
- startIndex := nodeStaticRouteDetails .StartIndex
128
- count := nodeStaticRouteDetails .Count
129
- var staticRouteCopy []* models.StaticRoute
130
- updateNodeStaticRoutes (aviVrfNode , deleteFlag , nodeName , 0 , - count )
131
-
132
- copyTill := int (math .Min (float64 (startIndex ), float64 (len (aviVrfNode .StaticRoutes ))))
133
- staticRouteCopy = append (staticRouteCopy , aviVrfNode .StaticRoutes [:copyTill ]... )
134
-
135
- copyFrom := int (math .Min (float64 (startIndex + nodeStaticRouteDetails .Count ), float64 (len (aviVrfNode .StaticRoutes ))))
136
- staticRouteCopy = append (staticRouteCopy , aviVrfNode .StaticRoutes [copyFrom :]... )
137
146
147
+ var staticRouteCopy []* models.StaticRoute
148
+ for i := 0 ; i < len (aviVrfNode .StaticRoutes ); i ++ {
149
+ if ! ok || ! strings .HasPrefix (* aviVrfNode .StaticRoutes [i ].RouteID , lib .GetClusterName () + "-" + nodeStaticRouteDetails .RouteIDPrefix ) {
150
+ staticRouteCopy = append (staticRouteCopy , aviVrfNode .StaticRoutes [i ])
151
+ }
152
+ }
138
153
aviVrfNode .StaticRoutes = staticRouteCopy
139
- processNodeStaticRouteAndNodeIdDeletion (nodeName , aviVrfNode )
154
+ processNodeStaticRouteAndNodeDeletion (nodeName , aviVrfNode )
140
155
utils .AviLog .Debugf ("key: %s, StaticRoutes after deletion: [%v]" , key , utils .Stringify (aviVrfNode .StaticRoutes ))
141
156
}
142
157
aviVrfNode .CalculateCheckSum ()
@@ -145,29 +160,25 @@ func (o *AviObjectGraph) BuildVRFGraph(key, vrfName, nodeName string, deleteFlag
145
160
utils .AviLog .Debugf ("key: %s, vrf node: [%v]" , key , utils .Stringify (aviVrfNode ))
146
161
return nil
147
162
}
148
- func processNodeStaticRouteAndNodeIdDeletion (nodeName string , aviVrfNode * AviVrfNode ) {
163
+ func processNodeStaticRouteAndNodeDeletion (nodeName string , aviVrfNode * AviVrfNode ) {
149
164
delete (aviVrfNode .NodeStaticRoutes , nodeName )
150
- for nodeId := len (aviVrfNode .NodeIds ); nodeId > len (aviVrfNode .StaticRoutes ); nodeId -- {
151
- delete (aviVrfNode .NodeIds , nodeId )
152
- }
153
- }
154
- func findFreeRouteId (routeIdList map [int ]struct {}) int {
155
- for i := 1 ; i < math .MaxInt32 ; i ++ {
156
- if _ , ok := routeIdList [i ]; ! ok {
157
- return i
165
+ nodesCopy := []string {}
166
+ for _ , node := range aviVrfNode .Nodes {
167
+ if node != nodeName {
168
+ nodesCopy = append (nodesCopy , node )
158
169
}
159
170
}
160
- return - 1
171
+ aviVrfNode . Nodes = nodesCopy
161
172
}
162
- func updateNodeStaticRoutes (aviVrfNode * AviVrfNode , isDelete bool , nodeName string , lenNewNodeRoutes , diff int ) {
173
+
174
+ func updateNodeStaticRoutes (aviVrfNode * AviVrfNode , isDelete bool , nodeNameToUpdate string , lenNewNodeRoutes int ) {
163
175
//get index of nodename in node array
164
176
indexOfNodeUnderUpdation := - 1
165
177
166
178
for i := 0 ; i < len (aviVrfNode .Nodes ); i ++ {
167
- if aviVrfNode .Nodes [i ] == nodeName {
179
+ if aviVrfNode .Nodes [i ] == nodeNameToUpdate {
168
180
indexOfNodeUnderUpdation = i
169
181
if ! isDelete {
170
- nodeNameToUpdate := aviVrfNode .Nodes [indexOfNodeUnderUpdation ]
171
182
nodeDetails := aviVrfNode .NodeStaticRoutes [nodeNameToUpdate ]
172
183
nodeDetails .Count = lenNewNodeRoutes
173
184
aviVrfNode .NodeStaticRoutes [nodeNameToUpdate ] = nodeDetails
@@ -176,32 +187,6 @@ func updateNodeStaticRoutes(aviVrfNode *AviVrfNode, isDelete bool, nodeName stri
176
187
}
177
188
}
178
189
if indexOfNodeUnderUpdation != - 1 {
179
- clusterName := lib .GetClusterName ()
180
- //Change nodemap entries till index
181
- for nodeIndex := len (aviVrfNode .Nodes ) - 1 ; nodeIndex > indexOfNodeUnderUpdation ; nodeIndex -- {
182
- nodeNameToUpdate := aviVrfNode .Nodes [nodeIndex ]
183
- nodeDetails := aviVrfNode .NodeStaticRoutes [nodeNameToUpdate ]
184
- oldStartIndex := nodeDetails .StartIndex
185
- nodeDetails .StartIndex = nodeDetails .StartIndex + diff
186
- nodeDetails .routeID = nodeDetails .StartIndex + 1
187
- aviVrfNode .NodeStaticRoutes [nodeNameToUpdate ] = nodeDetails
188
- newRouteId := nodeDetails .routeID
189
- var tempStartIndex int
190
- if isDelete {
191
- tempStartIndex = oldStartIndex
192
- } else {
193
- tempStartIndex = nodeDetails .StartIndex
194
- }
195
- for staticRouteIndex := tempStartIndex ; staticRouteIndex < nodeDetails .Count + tempStartIndex ; staticRouteIndex ++ {
196
- newRouteName := clusterName + "-" + strconv .Itoa (newRouteId )
197
- if staticRouteIndex > (len (aviVrfNode .StaticRoutes )- 1 ) || staticRouteIndex < 0 {
198
- utils .AviLog .Warnf ("Some StaticRoutes could not be updated." )
199
- continue
200
- }
201
- aviVrfNode .StaticRoutes [staticRouteIndex ].RouteID = & newRouteName
202
- newRouteId ++
203
- }
204
- }
205
190
// lenNewNodeRoutes will be zero if Node exists without any PodCidr/BloackAffinity attached to it.
206
191
if isDelete || lenNewNodeRoutes == 0 {
207
192
// now remove nodename from Nodes list
@@ -226,7 +211,7 @@ func findRoutePrefix(nodeRoutes, aviRoutes []*models.StaticRoute, key string) bo
226
211
return false
227
212
}
228
213
229
- func (o * AviObjectGraph ) addRouteForNode (node * v1.Node , vrfName string , routeid int , routeIdList map [ int ] struct {} ) ([]* models.StaticRoute , error ) {
214
+ func (o * AviObjectGraph ) addRouteForNode (node * v1.Node , vrfName string , routeIdPrefix string ) ([]* models.StaticRoute , error ) {
230
215
var nodeIP , nodeIP6 string
231
216
var nodeRoutes []* models.StaticRoute
232
217
@@ -243,7 +228,7 @@ func (o *AviObjectGraph) addRouteForNode(node *v1.Node, vrfName string, routeid
243
228
utils .AviLog .Errorf ("Error in fetching Pod CIDR for %v: %s" , node .ObjectMeta .Name , err .Error ())
244
229
return nil , errors .New ("podcidr not found" )
245
230
}
246
- for _ , podCIDR := range podCIDRs {
231
+ for index , podCIDR := range podCIDRs {
247
232
podCIDRAndMask := strings .Split (podCIDR , "/" )
248
233
if len (podCIDRAndMask ) != 2 {
249
234
utils .AviLog .Errorf ("Error in splitting Pod CIDR for %v" , node .ObjectMeta .Name )
@@ -273,7 +258,7 @@ func (o *AviObjectGraph) addRouteForNode(node *v1.Node, vrfName string, routeid
273
258
continue
274
259
}
275
260
mask32 := int32 (mask )
276
- routeIDString := clusterName + "-" + strconv .Itoa (routeid )
261
+ routeIDString := clusterName + "-" + routeIdPrefix + "-" + strconv .Itoa (index )
277
262
nodeRoute := models.StaticRoute {
278
263
RouteID : & routeIDString ,
279
264
Prefix : & models.IPAddrPrefix {
@@ -291,8 +276,6 @@ func (o *AviObjectGraph) addRouteForNode(node *v1.Node, vrfName string, routeid
291
276
}
292
277
293
278
nodeRoutes = append (nodeRoutes , & nodeRoute )
294
- routeIdList [routeid ] = struct {}{}
295
- routeid ++
296
279
}
297
280
return nodeRoutes , nil
298
281
}
0 commit comments