-
Notifications
You must be signed in to change notification settings - Fork 434
/
ovn.go
148 lines (126 loc) · 4.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
package ovs
import (
"context"
"errors"
"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"
)
var (
ErrNoAddr = errors.New("no address")
ErrNotFound = errors.New("not found")
)
// LegacyClient is the legacy ovn client
type LegacyClient struct {
OvnNbAddress string
OvnTimeout int
OvnSbAddress string
OvnICNbAddress string
OvnICSbAddress string
ClusterRouter string
ClusterTcpLoadBalancer string
ClusterUdpLoadBalancer string
ClusterTcpSessionLoadBalancer string
ClusterUdpSessionLoadBalancer string
NodeSwitch string
NodeSwitchCIDR string
ExternalGatewayType string
Version string
}
type OvnClient struct {
ovnNbClient
}
type ovnNbClient struct {
client.Client
Timeout time.Duration
}
const (
OvnNbCtl = "ovn-nbctl"
OvnSbCtl = "ovn-sbctl"
OVNIcNbCtl = "ovn-ic-nbctl"
OVNIcSbCtl = "ovn-ic-sbctl"
OvsVsCtl = "ovs-vsctl"
MayExist = "--may-exist"
IfExists = "--if-exists"
Policy = "--policy"
PolicyDstIP = "dst-ip"
PolicySrcIP = "src-ip"
OVSDBWaitTimeout = 0
)
// NewLegacyClient init a legacy ovn client
func NewLegacyClient(ovnNbAddr string, ovnNbTimeout int, ovnSbAddr, clusterRouter, clusterTcpLoadBalancer, clusterUdpLoadBalancer, clusterTcpSessionLoadBalancer, clusterUdpSessionLoadBalancer, nodeSwitch, nodeSwitchCIDR string) *LegacyClient {
return &LegacyClient{
OvnNbAddress: ovnNbAddr,
OvnSbAddress: ovnSbAddr,
OvnTimeout: ovnNbTimeout,
ClusterRouter: clusterRouter,
ClusterTcpLoadBalancer: clusterTcpLoadBalancer,
ClusterUdpLoadBalancer: clusterUdpLoadBalancer,
ClusterTcpSessionLoadBalancer: clusterTcpSessionLoadBalancer,
ClusterUdpSessionLoadBalancer: clusterUdpSessionLoadBalancer,
NodeSwitch: nodeSwitch,
NodeSwitchCIDR: nodeSwitchCIDR,
}
}
// TODO: support sb/ic-nb client
func NewOvnClient(ovnNbAddr string, ovnNbTimeout int) (*OvnClient, error) {
nbClient, err := ovsclient.NewNbClient(ovnNbAddr)
if err != nil {
klog.Errorf("failed to create OVN NB client: %v", err)
return nil, err
}
c := &OvnClient{
ovnNbClient: ovnNbClient{
Client: nbClient,
Timeout: time.Duration(ovnNbTimeout) * time.Second,
},
}
return c, nil
}
func Transact(c client.Client, method string, operations []ovsdb.Operation, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
defer cancel()
start := time.Now()
results, err := c.Transact(ctx, operations...)
elapsed := float64((time.Since(start)) / time.Millisecond)
var dbType string
switch c.Schema().Name {
case "OVN_Northbound":
dbType = "ovn-nb"
}
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
}
func ConstructWaitForNameNotExistsOperation(name string, table string) ovsdb.Operation {
return ConstructWaitForUniqueOperation(table, "name", name)
}
func ConstructWaitForUniqueOperation(table string, 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}},
}
}