-
Notifications
You must be signed in to change notification settings - Fork 434
/
ovn.go
204 lines (172 loc) · 5.04 KB
/
ovn.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
package ovs
import (
"context"
"fmt"
"reflect"
"time"
"github.com/ovn-org/libovsdb/client"
"github.com/ovn-org/libovsdb/ovsdb"
"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/ovsdb/ovnsb"
)
// LegacyClient is the legacy ovn client
type LegacyClient struct {
OvnTimeout int
OvnICNbAddress string
OvnICSbAddress string
}
type OVNNbClient struct {
ovsDbClient
ClusterRouter string
}
type OVNSbClient struct {
ovsDbClient
}
type ovsDbClient struct {
client.Client
Timeout time.Duration
}
const (
OVNIcNbCtl = "ovn-ic-nbctl"
OVNIcSbCtl = "ovn-ic-sbctl"
OvsVsCtl = "ovs-vsctl"
MayExist = "--may-exist"
IfExists = "--if-exists"
OVSDBWaitTimeout = 0
)
// NewLegacyClient init a legacy ovn client
func NewLegacyClient(timeout int) *LegacyClient {
return &LegacyClient{
OvnTimeout: timeout,
}
}
func NewOvnNbClient(ovnNbAddr string, ovnNbTimeout int) (*OVNNbClient, error) {
dbModel, err := ovnnb.FullDatabaseModel()
if err != nil {
klog.Error(err)
return nil, err
}
monitors := []client.MonitorOption{
client.WithTable(&ovnnb.ACL{}),
client.WithTable(&ovnnb.AddressSet{}),
client.WithTable(&ovnnb.BFD{}),
client.WithTable(&ovnnb.DHCPOptions{}),
client.WithTable(&ovnnb.GatewayChassis{}),
client.WithTable(&ovnnb.LoadBalancer{}),
client.WithTable(&ovnnb.LoadBalancerHealthCheck{}),
client.WithTable(&ovnnb.LogicalRouterPolicy{}),
client.WithTable(&ovnnb.LogicalRouterPort{}),
client.WithTable(&ovnnb.LogicalRouterStaticRoute{}),
client.WithTable(&ovnnb.LogicalRouter{}),
client.WithTable(&ovnnb.LogicalSwitchPort{}),
client.WithTable(&ovnnb.LogicalSwitch{}),
client.WithTable(&ovnnb.NAT{}),
client.WithTable(&ovnnb.NBGlobal{}),
client.WithTable(&ovnnb.PortGroup{}),
}
nbClient, err := ovsclient.NewOvsDbClient(ovsclient.NBDB, ovnNbAddr, dbModel, monitors)
if err != nil {
klog.Errorf("failed to create OVN NB client: %v", err)
return nil, err
}
c := &OVNNbClient{
ovsDbClient: ovsDbClient{
Client: nbClient,
Timeout: time.Duration(ovnNbTimeout) * time.Second,
},
}
return c, nil
}
func NewOvnSbClient(ovnSbAddr string, ovnSbTimeout int) (*OVNSbClient, error) {
dbModel, err := ovnsb.FullDatabaseModel()
if err != nil {
klog.Error(err)
return nil, err
}
monitors := []client.MonitorOption{
client.WithTable(&ovnsb.Chassis{}),
// TODO:// monitor other necessary tables in ovsdb/ovnsb/model.go
}
sbClient, err := ovsclient.NewOvsDbClient(ovsclient.SBDB, ovnSbAddr, dbModel, monitors)
if err != nil {
klog.Errorf("failed to create OVN SB client: %v", err)
return nil, err
}
c := &OVNSbClient{
ovsDbClient: ovsDbClient{
Client: sbClient,
Timeout: time.Duration(ovnSbTimeout) * time.Second,
},
}
return c, nil
}
// TODO: support ic-nb ic-sb client
func ConstructWaitForNameNotExistsOperation(name, table string) ovsdb.Operation {
return ConstructWaitForUniqueOperation(table, "name", name)
}
func ConstructWaitForUniqueOperation(table, column string, value interface{}) ovsdb.Operation {
timeout := OVSDBWaitTimeout
return ovsdb.Operation{
Op: ovsdb.OperationWait,
Table: table,
Timeout: &timeout,
Where: []ovsdb.Condition{{Column: column, Function: ovsdb.ConditionEqual, Value: value}},
Columns: []string{column},
Until: "!=",
Rows: []ovsdb.Row{{column: value}},
}
}
func (c *ovsDbClient) Transact(method string, operations []ovsdb.Operation) error {
if len(operations) == 0 {
klog.V(6).Info("operations should not be empty")
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()
start := time.Now()
results, err := c.Client.Transact(ctx, operations...)
elapsed := float64((time.Since(start)) / time.Millisecond)
var dbType string
switch c.Schema().Name {
case "OVN_Northbound":
dbType = "ovn-nb"
case "OVN_Southbound":
dbType = "ovn-sb"
}
code := "0"
defer func() {
ovsClientRequestLatency.WithLabelValues(dbType, method, code).Observe(elapsed)
}()
if err != nil {
code = "1"
klog.Errorf("error occurred in transact with %s operations: %+v in %vms", dbType, operations, elapsed)
return err
}
if elapsed > 500 {
klog.Warningf("%s operations took too long: %+v in %vms", dbType, operations, elapsed)
}
errors, err := ovsdb.CheckOperationResults(results, operations)
if err != nil {
klog.Errorf("error occurred in transact with operations %+v with operation errors %+v: %v", operations, errors, err)
return err
}
return nil
}
// GetEntityInfo get entity info by column which is the index,
// reference to ovn-nb.ovsschema(ovsdb-client get-schema unix:/var/run/ovn/ovnnb_db.sock OVN_Northbound) for more information,
// UUID is index
func (c *ovsDbClient) GetEntityInfo(entity interface{}) error {
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()
entityPtr := reflect.ValueOf(entity)
if entityPtr.Kind() != reflect.Pointer {
return fmt.Errorf("entity must be pointer")
}
err := c.Get(ctx, entity)
if err != nil {
return err
}
return nil
}