forked from kubeovn/kube-ovn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathovn-nb-logical_router_route.go
354 lines (304 loc) · 11.2 KB
/
ovn-nb-logical_router_route.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package ovs
import (
"context"
"errors"
"fmt"
"github.com/ovn-org/libovsdb/client"
"github.com/ovn-org/libovsdb/model"
"github.com/ovn-org/libovsdb/ovsdb"
"github.com/scylladb/go-set/strset"
"k8s.io/klog/v2"
ovsclient "github.com/kubeovn/kube-ovn/pkg/ovsdb/client"
"github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb"
"github.com/kubeovn/kube-ovn/pkg/util"
)
func (c *ovnClient) ListLogicalRouterStaticRoutesByOption(lrName, routeTable, key, value string) ([]*ovnnb.LogicalRouterStaticRoute, error) {
fnFilter := func(route *ovnnb.LogicalRouterStaticRoute) bool {
return len(route.Options) != 0 && route.Options[key] == value
}
return c.listLogicalRouterStaticRoutesByFilter(lrName, fnFilter)
}
// CreateLogicalRouterStaticRoutes create several logical router static route once
func (c *ovnClient) CreateLogicalRouterStaticRoutes(lrName string, routes ...*ovnnb.LogicalRouterStaticRoute) error {
if len(routes) == 0 {
return nil
}
models := make([]model.Model, 0, len(routes))
routeUUIDs := make([]string, 0, len(routes))
for _, route := range routes {
if route != nil {
models = append(models, model.Model(route))
routeUUIDs = append(routeUUIDs, route.UUID)
}
}
createRoutesOp, err := c.ovnNbClient.Create(models...)
if err != nil {
return fmt.Errorf("generate operations for creating static routes: %v", err)
}
routeAddOp, err := c.LogicalRouterUpdateStaticRouteOp(lrName, routeUUIDs, ovsdb.MutateOperationInsert)
if err != nil {
return fmt.Errorf("generate operations for adding static routes to logical router %s: %v", lrName, err)
}
ops := make([]ovsdb.Operation, 0, len(createRoutesOp)+len(routeAddOp))
ops = append(ops, createRoutesOp...)
ops = append(ops, routeAddOp...)
if err = c.Transact("lr-routes-add", ops); err != nil {
return fmt.Errorf("add static routes to %s: %v", lrName, err)
}
return nil
}
// AddLogicalRouterStaticRoute add a logical router static route
func (c *ovnClient) AddLogicalRouterStaticRoute(lrName, routeTable, policy, ipPrefix string, bfdId *string, nexthops ...string) error {
if len(policy) == 0 {
policy = ovnnb.LogicalRouterStaticRoutePolicyDstIP
}
routes, err := c.ListLogicalRouterStaticRoutes(lrName, &routeTable, &policy, ipPrefix, nil)
if err != nil {
klog.Error(err)
return err
}
existing := strset.New()
var toDel []string
for _, route := range routes {
if util.ContainsString(nexthops, route.Nexthop) {
existing.Add(route.Nexthop)
} else {
if route.BFD != nil && bfdId != nil && *route.BFD != *bfdId {
continue
}
toDel = append(toDel, route.UUID)
}
}
var toAdd []*ovnnb.LogicalRouterStaticRoute
for _, nexthop := range nexthops {
if !existing.Has(nexthop) {
route, err := c.newLogicalRouterStaticRoute(lrName, routeTable, policy, ipPrefix, nexthop, bfdId)
if err != nil {
klog.Error(err)
return err
}
toAdd = append(toAdd, route)
}
}
klog.Infof("logical router %s del static routes: %v", lrName, toDel)
ops, err := c.LogicalRouterUpdateStaticRouteOp(lrName, toDel, ovsdb.MutateOperationDelete)
if err != nil {
klog.Error(err)
return fmt.Errorf("generate operations for removing static routes from logical router %s: %v", lrName, err)
}
if err = c.Transact("lr-route-del", ops); err != nil {
klog.Error(err)
return fmt.Errorf("failed to delete static routes from logical router %s: %v", lrName, err)
}
if err = c.CreateLogicalRouterStaticRoutes(lrName, toAdd...); err != nil {
return fmt.Errorf("failed to add static routes to logical router %s: %v", lrName, err)
}
return nil
}
// UpdateLogicalRouterStaticRoute update logical router static route
func (c *ovnClient) UpdateLogicalRouterStaticRoute(route *ovnnb.LogicalRouterStaticRoute, fields ...interface{}) error {
if route == nil {
return fmt.Errorf("route is nil")
}
op, err := c.ovnNbClient.Where(route).Update(route, fields...)
if err != nil {
klog.Error(err)
return fmt.Errorf("generate operations for updating logical router static route 'policy %s ip_prefix %s': %v", *route.Policy, route.IPPrefix, err)
}
if err = c.Transact("net-update", op); err != nil {
return fmt.Errorf("update logical router static route 'policy %s ip_prefix %s': %v", *route.Policy, route.IPPrefix, err)
}
return nil
}
// DeleteLogicalRouterStaticRoute add a logical router static route
func (c *ovnClient) DeleteLogicalRouterStaticRoute(lrName string, routeTable, policy *string, ipPrefix, nexthop string) error {
if policy == nil || len(*policy) == 0 {
policy = &ovnnb.LogicalRouterStaticRoutePolicyDstIP
}
routes, err := c.ListLogicalRouterStaticRoutes(lrName, routeTable, policy, ipPrefix, nil)
if err != nil {
klog.Error(err)
return err
}
// not found, skip
if len(routes) == 0 {
return nil
}
uuids := make([]string, 0, len(routes))
for _, route := range routes {
if nexthop == "" || route.Nexthop == nexthop {
uuids = append(uuids, route.UUID)
}
}
// remove static route from logical router
ops, err := c.LogicalRouterUpdateStaticRouteOp(lrName, uuids, ovsdb.MutateOperationDelete)
if err != nil {
klog.Error(err)
return fmt.Errorf("generate operations for removing static routes %v from logical router %s: %v", uuids, lrName, err)
}
if err = c.Transact("lr-route-del", ops); err != nil {
klog.Error(err)
return fmt.Errorf("delete static routes %v from logical router %s: %v", uuids, lrName, err)
}
return nil
}
// ClearLogicalRouterStaticRoute clear static route from logical router once
func (c *ovnClient) ClearLogicalRouterStaticRoute(lrName string) error {
lr, err := c.GetLogicalRouter(lrName, false)
if err != nil {
klog.Error(err)
return fmt.Errorf("get logical router %s: %v", lrName, err)
}
// clear static route
lr.StaticRoutes = nil
ops, err := c.UpdateLogicalRouterOp(lr, &lr.StaticRoutes)
if err != nil {
klog.Error(err)
return fmt.Errorf("generate operations for clear logical router %s static route: %v", lrName, err)
}
if err = c.Transact("lr-route-clear", ops); err != nil {
klog.Error(err)
return fmt.Errorf("clear logical router %s static routes: %v", lrName, err)
}
return nil
}
// GetLogicalRouterStaticRouteByUUID get logical router static route by UUID
func (c *ovnClient) GetLogicalRouterStaticRouteByUUID(uuid string) (*ovnnb.LogicalRouterStaticRoute, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()
route := &ovnnb.LogicalRouterStaticRoute{UUID: uuid}
if err := c.Get(ctx, route); err != nil {
klog.Error(err)
return nil, err
}
return route, nil
}
// GetLogicalRouterStaticRoute get logical router static route by some attribute,
// a static route is uniquely identified by router(lrName), policy and ipPrefix when route is not ecmp
// a static route is uniquely identified by router(lrName), policy, ipPrefix and nexthop when route is ecmp
func (c *ovnClient) GetLogicalRouterStaticRoute(lrName, routeTable, policy, ipPrefix, nexthop string, ignoreNotFound bool) (*ovnnb.LogicalRouterStaticRoute, error) {
// this is necessary because may exist same static route in different logical router
if len(lrName) == 0 {
return nil, fmt.Errorf("the logical router name is required")
}
fnFilter := func(route *ovnnb.LogicalRouterStaticRoute) bool {
return route.RouteTable == routeTable && route.Policy != nil && *route.Policy == policy && route.IPPrefix == ipPrefix && route.Nexthop == nexthop
}
routeList, err := c.listLogicalRouterStaticRoutesByFilter(lrName, fnFilter)
if err != nil {
klog.Error(err)
return nil, fmt.Errorf("get logical router %s static route 'policy %s ip_prefix %s nexthop %s': %v", lrName, policy, ipPrefix, nexthop, err)
}
// not found
if len(routeList) == 0 {
if ignoreNotFound {
return nil, nil
}
return nil, fmt.Errorf("not found logical router %s static route 'policy %s ip_prefix %s nexthop %s'", lrName, policy, ipPrefix, nexthop)
}
if len(routeList) > 1 {
return nil, fmt.Errorf("more than one static route 'policy %s ip_prefix %s nexthop %s' in logical router %s", policy, ipPrefix, nexthop, lrName)
}
return routeList[0], nil
}
// ListLogicalRouterStaticRoutes list route which match the given externalIDs
func (c *ovnClient) ListLogicalRouterStaticRoutes(lrName string, routeTable, policy *string, ipPrefix string, externalIDs map[string]string) ([]*ovnnb.LogicalRouterStaticRoute, error) {
fnFilter := func(route *ovnnb.LogicalRouterStaticRoute) bool {
if len(route.ExternalIDs) < len(externalIDs) {
return false
}
if len(route.ExternalIDs) != 0 {
for k, v := range externalIDs {
// if only key exist but not value in externalIDs, we should include this route,
// it's equal to shell command `ovn-nbctl --columns=xx find logical_router_static_route external_ids:key!=\"\"`
if len(v) == 0 {
if len(route.ExternalIDs[k]) == 0 {
return false
}
} else {
if route.ExternalIDs[k] != v {
return false
}
}
}
}
if routeTable != nil && route.RouteTable != *routeTable {
return false
}
if policy != nil {
if route.Policy != nil {
if *route.Policy != *policy {
return false
}
} else if *policy != ovnnb.LogicalRouterStaticRoutePolicyDstIP {
return false
}
}
if ipPrefix != "" && route.IPPrefix != ipPrefix {
return false
}
return true
}
return c.listLogicalRouterStaticRoutesByFilter(lrName, fnFilter)
}
func (c *ovnClient) LogicalRouterStaticRouteExists(lrName, routeTable, policy, ipPrefix, nexthop string) (bool, error) {
route, err := c.GetLogicalRouterStaticRoute(lrName, routeTable, policy, ipPrefix, nexthop, true)
return route != nil, err
}
// newLogicalRouterStaticRoute return logical router static route with basic information
func (c *ovnClient) newLogicalRouterStaticRoute(lrName, routeTable, policy, ipPrefix, nexthop string, bfdId *string, options ...func(route *ovnnb.LogicalRouterStaticRoute)) (*ovnnb.LogicalRouterStaticRoute, error) {
if len(lrName) == 0 {
return nil, fmt.Errorf("the logical router name is required")
}
if len(policy) == 0 {
policy = ovnnb.LogicalRouterStaticRoutePolicyDstIP
}
exists, err := c.LogicalRouterStaticRouteExists(lrName, routeTable, policy, ipPrefix, nexthop)
if err != nil {
klog.Error(err)
return nil, fmt.Errorf("get logical router %s route: %v", lrName, err)
}
// found, ignore
if exists {
return nil, nil
}
route := &ovnnb.LogicalRouterStaticRoute{
UUID: ovsclient.NamedUUID(),
Policy: &policy,
IPPrefix: ipPrefix,
Nexthop: nexthop,
}
for _, option := range options {
option(route)
}
if bfdId != nil {
route.BFD = bfdId
if route.Options == nil {
route.Options = make(map[string]string)
}
route.Options[util.StaticRouteBfdEcmp] = "true"
}
return route, nil
}
func (c *ovnClient) listLogicalRouterStaticRoutesByFilter(lrName string, filter func(route *ovnnb.LogicalRouterStaticRoute) bool) ([]*ovnnb.LogicalRouterStaticRoute, error) {
lr, err := c.GetLogicalRouter(lrName, false)
if err != nil {
klog.Error(err)
return nil, err
}
routeList := make([]*ovnnb.LogicalRouterStaticRoute, 0, len(lr.StaticRoutes))
for _, uuid := range lr.StaticRoutes {
route, err := c.GetLogicalRouterStaticRouteByUUID(uuid)
if err != nil {
if errors.Is(err, client.ErrNotFound) {
continue
}
klog.Error(err)
return nil, err
}
if filter == nil || filter(route) {
routeList = append(routeList, route)
}
}
return routeList, nil
}