-
Notifications
You must be signed in to change notification settings - Fork 438
/
ovn-nb-logical_switch.go
416 lines (337 loc) · 11.1 KB
/
ovn-nb-logical_switch.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package ovs
import (
"context"
"fmt"
"strings"
"github.com/ovn-org/libovsdb/model"
"github.com/ovn-org/libovsdb/ovsdb"
"k8s.io/klog/v2"
"github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb"
"github.com/kubeovn/kube-ovn/pkg/util"
)
// CreateLogicalSwitch create logical switch
func (c *OVNNbClient) CreateLogicalSwitch(lsName, lrName, cidrBlock, gateway string, needRouter, randomAllocateGW bool) error {
lspName := fmt.Sprintf("%s-%s", lsName, lrName)
lrpName := fmt.Sprintf("%s-%s", lrName, lsName)
networks := util.GetIPAddrWithMask(gateway, cidrBlock)
exist, err := c.LogicalSwitchExists(lsName)
if err != nil {
return err
}
// only update logical router port networks when logical switch exist
if exist {
if randomAllocateGW {
return nil
}
lrp := &ovnnb.LogicalRouterPort{
Name: lrpName,
Networks: strings.Split(networks, ","),
}
if err := c.UpdateLogicalRouterPort(lrp, &lrp.Networks); err != nil {
return fmt.Errorf("update logical router port %s", lrpName)
}
} else {
if err := c.CreateBareLogicalSwitch(lsName); err != nil {
return fmt.Errorf("create logical switch %s", lsName)
}
}
if needRouter {
if err := c.CreateLogicalPatchPort(lsName, lrName, lspName, lrpName, networks, util.GenerateMac()); err != nil {
return err
}
} else {
if randomAllocateGW {
return nil
}
if err := c.RemoveLogicalPatchPort(lspName, lrpName); err != nil {
return fmt.Errorf("remove router type port %s and %s: %v", lspName, lrpName, err)
}
}
return nil
}
// CreateBareLogicalSwitch create logical switch with basic configuration
func (c *OVNNbClient) CreateBareLogicalSwitch(lsName string) error {
exist, err := c.LogicalSwitchExists(lsName)
if err != nil {
klog.Error(err)
return err
}
// ingnore
if exist {
return nil
}
ls := &ovnnb.LogicalSwitch{
Name: lsName,
ExternalIDs: map[string]string{"vendor": util.CniTypeName},
}
op, err := c.ovsDbClient.Create(ls)
if err != nil {
klog.Error(err)
return fmt.Errorf("generate operations for creating logical switch %s: %v", lsName, err)
}
if err := c.Transact("ls-add", op); err != nil {
return fmt.Errorf("create logical switch %s: %v", lsName, err)
}
return nil
}
// LogicalSwitchAddPort add port to logical switch
func (c *OVNNbClient) LogicalSwitchAddPort(lsName, lspName string) error {
lsp, err := c.GetLogicalSwitchPort(lspName, false)
if err != nil {
klog.Error(err)
return fmt.Errorf("get logical switch port %s when logical switch add port: %v", lspName, err)
}
ops, err := c.LogicalSwitchUpdatePortOp(lsName, lsp.UUID, ovsdb.MutateOperationInsert)
if err != nil {
klog.Error(err)
return fmt.Errorf("generate operations for logical switch %s add port %s: %v", lsName, lspName, err)
}
if err := c.Transact("lsp-add", ops); err != nil {
klog.Error(err)
return fmt.Errorf("add port %s to logical switch %s: %v", lspName, lsName, err)
}
return nil
}
// LogicalSwitchDelPort del port from logical switch
func (c *OVNNbClient) LogicalSwitchDelPort(lsName, lspName string) error {
lsp, err := c.GetLogicalSwitchPort(lspName, false)
if err != nil {
klog.Error(err)
return fmt.Errorf("get logical switch port %s when logical switch del port: %v", lspName, err)
}
ops, err := c.LogicalSwitchUpdatePortOp(lsName, lsp.UUID, ovsdb.MutateOperationDelete)
if err != nil {
klog.Error(err)
return fmt.Errorf("generate operations for logical switch %s del port %s: %v", lsName, lspName, err)
}
if err := c.Transact("lsp-del", ops); err != nil {
return fmt.Errorf("del port %s from logical switch %s: %v", lspName, lsName, err)
}
return nil
}
// LogicalSwitchUpdateLoadBalancers add several lb to or from logical switch once
func (c *OVNNbClient) LogicalSwitchUpdateLoadBalancers(lsName string, op ovsdb.Mutator, lbNames ...string) error {
if len(lbNames) == 0 {
return nil
}
lbUUIDs := make([]string, 0, len(lbNames))
for _, lbName := range lbNames {
lb, err := c.GetLoadBalancer(lbName, true)
if err != nil {
klog.Error(err)
return err
}
// ignore non-existent object
if lb != nil {
lbUUIDs = append(lbUUIDs, lb.UUID)
}
}
ops, err := c.LogicalSwitchUpdateLoadBalancerOp(lsName, lbUUIDs, op)
if err != nil {
klog.Error(err)
return fmt.Errorf("generate operations for logical switch %s update lbs %v: %v", lsName, lbNames, err)
}
if err := c.Transact("ls-lb-update", ops); err != nil {
return fmt.Errorf("logical switch %s update lbs %v: %v", lsName, lbNames, err)
}
return nil
}
// LogicalSwitchUpdateOtherConfig add other config to or from logical switch once
func (c *OVNNbClient) LogicalSwitchUpdateOtherConfig(lsName string, op ovsdb.Mutator, otherConfig map[string]string) error {
if len(otherConfig) == 0 {
return nil
}
ops, err := c.LogicalSwitchUpdateOtherConfigOp(lsName, otherConfig, op)
if err != nil {
klog.Error(err)
return fmt.Errorf("generate operations for logical switch %s update other config %v: %v", lsName, otherConfig, err)
}
if err := c.Transact("ls-other-config-update", ops); err != nil {
return fmt.Errorf("logical switch %s update other config %v: %v", lsName, otherConfig, err)
}
return nil
}
// DeleteLogicalSwitch delete logical switch
func (c *OVNNbClient) DeleteLogicalSwitch(lsName string) error {
op, err := c.DeleteLogicalSwitchOp(lsName)
if err != nil {
klog.Error(err)
return err
}
if err := c.Transact("ls-del", op); err != nil {
return fmt.Errorf("delete logical switch %s: %v", lsName, err)
}
return nil
}
// GetLogicalSwitch get logical switch by name,
// it is because of lack name index that does't use OVNNbClient.Get
func (c *OVNNbClient) GetLogicalSwitch(lsName string, ignoreNotFound bool) (*ovnnb.LogicalSwitch, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()
lsList := make([]ovnnb.LogicalSwitch, 0)
if err := c.ovsDbClient.WhereCache(func(ls *ovnnb.LogicalSwitch) bool {
return ls.Name == lsName
}).List(ctx, &lsList); err != nil {
return nil, fmt.Errorf("list switch switch %q: %v", lsName, err)
}
// not found
if len(lsList) == 0 {
if ignoreNotFound {
return nil, nil
}
return nil, fmt.Errorf("not found logical switch %q", lsName)
}
if len(lsList) > 1 {
return nil, fmt.Errorf("more than one logical switch with same name %q", lsName)
}
// #nosec G602
return &lsList[0], nil
}
func (c *OVNNbClient) LogicalSwitchExists(lsName string) (bool, error) {
ls, err := c.GetLogicalSwitch(lsName, true)
return ls != nil, err
}
// ListLogicalSwitch list logical switch
func (c *OVNNbClient) ListLogicalSwitch(needVendorFilter bool, filter func(ls *ovnnb.LogicalSwitch) bool) ([]ovnnb.LogicalSwitch, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()
lsList := make([]ovnnb.LogicalSwitch, 0)
if err := c.ovsDbClient.WhereCache(func(ls *ovnnb.LogicalSwitch) bool {
if needVendorFilter && (len(ls.ExternalIDs) == 0 || ls.ExternalIDs["vendor"] != util.CniTypeName) {
return false
}
if filter != nil {
return filter(ls)
}
return true
}).List(ctx, &lsList); err != nil {
return nil, fmt.Errorf("list logical switch: %v", err)
}
return lsList, nil
}
// LogicalSwitchUpdatePortOp create operations add port to or delete port from logical switch
func (c *OVNNbClient) LogicalSwitchUpdatePortOp(lsName, lspUUID string, op ovsdb.Mutator) ([]ovsdb.Operation, error) {
if len(lspUUID) == 0 {
return nil, nil
}
if lsName == "" && op == ovsdb.MutateOperationDelete {
lsList, err := c.ListLogicalSwitch(false, func(ls *ovnnb.LogicalSwitch) bool {
return util.ContainsString(ls.Ports, lspUUID)
})
if err != nil {
klog.Error(err)
return nil, fmt.Errorf("failed to list LS by LSP UUID %s: %v", lspUUID, err)
}
if len(lsList) == 0 {
err = fmt.Errorf("no LS found for LSP %s", lspUUID)
klog.Error(err)
return nil, err
}
if len(lsList) != 1 {
lsNames := make([]string, len(lsList))
for i := range lsList {
lsNames[i] = lsList[i].Name
}
err = fmt.Errorf("multiple LS found for LSP %s: %s", lspUUID, strings.Join(lsNames, ", "))
klog.Error(err)
return nil, err
}
lsName = lsList[0].Name
}
mutation := func(ls *ovnnb.LogicalSwitch) *model.Mutation {
mutation := &model.Mutation{
Field: &ls.Ports,
Value: []string{lspUUID},
Mutator: op,
}
return mutation
}
return c.LogicalSwitchOp(lsName, mutation)
}
// LogicalSwitchUpdateOtherConfigOp create operations add otherConfig to or delete otherConfig from logical switch
func (c *OVNNbClient) LogicalSwitchUpdateOtherConfigOp(lsName string, otherConfig map[string]string, op ovsdb.Mutator) ([]ovsdb.Operation, error) {
if len(otherConfig) == 0 {
return nil, nil
}
mutation := func(ls *ovnnb.LogicalSwitch) *model.Mutation {
mutation := &model.Mutation{
Field: &ls.OtherConfig,
Value: otherConfig,
Mutator: op,
}
return mutation
}
return c.LogicalSwitchOp(lsName, mutation)
}
// LogicalSwitchUpdateLoadBalancerOp create operations add lb to or delete lb from logical switch
func (c *OVNNbClient) LogicalSwitchUpdateLoadBalancerOp(lsName string, lbUUIDs []string, op ovsdb.Mutator) ([]ovsdb.Operation, error) {
if len(lbUUIDs) == 0 {
return nil, nil
}
mutation := func(ls *ovnnb.LogicalSwitch) *model.Mutation {
mutation := &model.Mutation{
Field: &ls.LoadBalancer,
Value: lbUUIDs,
Mutator: op,
}
return mutation
}
return c.LogicalSwitchOp(lsName, mutation)
}
// logicalSwitchUpdateACLOp create operations add acl to or delete acl from logical switch
func (c *OVNNbClient) logicalSwitchUpdateACLOp(lsName string, aclUUIDs []string, op ovsdb.Mutator) ([]ovsdb.Operation, error) {
if len(aclUUIDs) == 0 {
return nil, nil
}
mutation := func(ls *ovnnb.LogicalSwitch) *model.Mutation {
mutation := &model.Mutation{
Field: &ls.ACLs,
Value: aclUUIDs,
Mutator: op,
}
return mutation
}
return c.LogicalSwitchOp(lsName, mutation)
}
// LogicalSwitchOp create operations about logical switch
func (c *OVNNbClient) LogicalSwitchOp(lsName string, mutationsFunc ...func(ls *ovnnb.LogicalSwitch) *model.Mutation) ([]ovsdb.Operation, error) {
ls, err := c.GetLogicalSwitch(lsName, false)
if err != nil {
klog.Error(err)
return nil, fmt.Errorf("get logical switch %s when generate mutate operations: %v", lsName, err)
}
if len(mutationsFunc) == 0 {
return nil, nil
}
mutations := make([]model.Mutation, 0, len(mutationsFunc))
for _, f := range mutationsFunc {
mutation := f(ls)
if mutation != nil {
mutations = append(mutations, *mutation)
}
}
ops, err := c.ovsDbClient.Where(ls).Mutate(ls, mutations...)
if err != nil {
klog.Error(err)
return nil, fmt.Errorf("generate operations for mutating logical switch %s: %v", lsName, err)
}
return ops, nil
}
// DeleteLogicalSwitchOp create operations that delete logical switch
func (c *OVNNbClient) DeleteLogicalSwitchOp(lsName string) ([]ovsdb.Operation, error) {
ls, err := c.GetLogicalSwitch(lsName, true)
if err != nil {
klog.Error(err)
return nil, fmt.Errorf("get logical switch %s: %v", lsName, err)
}
// not found, skip
if ls == nil {
return nil, nil
}
op, err := c.Where(ls).Delete()
if err != nil {
klog.Error(err)
return nil, fmt.Errorf("generate operations for deleting logical switch %s: %v", lsName, err)
}
return op, nil
}