-
Notifications
You must be signed in to change notification settings - Fork 434
/
ovn-sbctl.go
130 lines (117 loc) · 4.06 KB
/
ovn-sbctl.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
package ovs
import (
"fmt"
"os"
"os/exec"
"strings"
"time"
"k8s.io/klog/v2"
"github.com/kubeovn/kube-ovn/pkg/util"
)
func (c LegacyClient) ovnSbCommand(cmdArgs ...string) (string, error) {
start := time.Now()
if os.Getenv("ENABLE_SSL") == "true" {
cmdArgs = append([]string{
fmt.Sprintf("--timeout=%d", c.OvnTimeout),
fmt.Sprintf("--db=%s", c.OvnSbAddress),
"-p", "/var/run/tls/key",
"-c", "/var/run/tls/cert",
"-C", "/var/run/tls/cacert"}, cmdArgs...)
} else {
cmdArgs = append([]string{
fmt.Sprintf("--timeout=%d", c.OvnTimeout),
fmt.Sprintf("--db=%s", c.OvnSbAddress)}, cmdArgs...)
}
raw, err := exec.Command(OvnSbCtl, cmdArgs...).CombinedOutput()
elapsed := float64((time.Since(start)) / time.Millisecond)
klog.V(4).Infof("command %s %s in %vms", OvnSbCtl, strings.Join(cmdArgs, " "), elapsed)
method := ""
for _, arg := range cmdArgs {
if !strings.HasPrefix(arg, "--") {
method = arg
break
}
}
code := "0"
defer func() {
ovsClientRequestLatency.WithLabelValues("ovn-sb", method, code).Observe(elapsed)
}()
if err != nil {
code = "1"
klog.Warningf("ovn-sbctl command error: %s %s in %vms", OvnSbCtl, strings.Join(cmdArgs, " "), elapsed)
return "", fmt.Errorf("%s, %q", raw, err)
} else if elapsed > 500 {
klog.Warningf("ovn-sbctl command took too long: %s %s in %vms", OvnSbCtl, strings.Join(cmdArgs, " "), elapsed)
}
return trimCommandOutput(raw), nil
}
func (c LegacyClient) DeleteChassisByNode(node string) error {
chassis, err := c.GetChassis(node)
if err != nil {
return fmt.Errorf("failed to get node chassis %s, %v", node, err)
}
if chassis == "" {
return nil
}
return c.DeleteChassisByName(chassis)
}
func (c LegacyClient) DeleteChassisByName(chassisName string) error {
ovnVersion, err := c.GetVersion()
if err != nil {
return fmt.Errorf("failed to get ovn version, %v", err)
}
cmdArg := []string{"chassis-del", strings.TrimSpace(chassisName)}
if util.CompareVersion(ovnVersion, "20.09") >= 0 {
cmdArg = append(cmdArg, "--", "destroy", "chassis_private", strings.TrimSpace(chassisName))
}
if _, err := c.ovnSbCommand(cmdArg...); err != nil {
return err
}
return nil
}
func (c LegacyClient) GetChassis(node string) (string, error) {
output, err := c.ovnSbCommand("--format=csv", "--no-heading", "--data=bare", "--columns=name", "find", "chassis", fmt.Sprintf("external_ids:node=%s", node))
if err != nil {
return "", fmt.Errorf("failed to find node chassis %s, %v", node, err)
}
if len(output) == 0 {
output, err = c.ovnSbCommand("--format=csv", "--no-heading", "--data=bare", "--columns=name", "find", "chassis", fmt.Sprintf("hostname=%s", node))
if err != nil {
return "", fmt.Errorf("failed to find node chassis %s, %v", node, err)
}
}
return strings.TrimSpace(output), nil
}
func (c LegacyClient) ChassisExist(chassisName string) (bool, error) {
output, err := c.ovnSbCommand("--format=csv", "--no-heading", "--data=bare", "--columns=name", "find", "chassis", fmt.Sprintf("name=%s", chassisName))
if err != nil {
return false, fmt.Errorf("failed to find node chassis %s, %v", chassisName, err)
}
if len(output) == 0 {
return false, nil
}
return true, nil
}
func (c LegacyClient) InitChassisNodeTag(chassisName string, nodeName string) error {
_, err := c.ovnSbCommand("set", "chassis", chassisName, fmt.Sprintf("external_ids:vendor=%s", util.CniTypeName), fmt.Sprintf("external_ids:node=%s", nodeName))
if err != nil {
return fmt.Errorf("failed to set chassis external_ids, %v", err)
}
return nil
}
// GetAllChassis get all chassis init by kube-ovn
func (c LegacyClient) GetAllChassis() ([]string, error) {
output, err := c.ovnSbCommand("--format=csv", "--no-heading", "--data=bare", "--columns=name", "find", "chassis", fmt.Sprintf("external_ids:vendor=%s", util.CniTypeName))
if err != nil {
return nil, fmt.Errorf("failed to find node chassis, %v", err)
}
lines := strings.Split(output, "\n")
result := make([]string, 0, len(lines))
for _, l := range lines {
if len(strings.TrimSpace(l)) == 0 {
continue
}
result = append(result, strings.TrimSpace(l))
}
return result, nil
}