-
Notifications
You must be signed in to change notification settings - Fork 434
/
ovn-sbctl.go
90 lines (83 loc) · 2.78 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
package ovs
import (
"fmt"
"os"
"os/exec"
"strings"
"time"
"k8s.io/klog/v2"
)
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) DeleteChassis(node string) error {
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)
}
for _, chassis := range strings.Split(output, "\n") {
chassis = strings.TrimSpace(chassis)
if len(chassis) > 0 {
if _, err := c.ovnSbCommand("chassis-del", strings.TrimSpace(chassis), "--", "destroy", "chassis_private", strings.TrimSpace(chassis)); 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("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) GetAllChassisHostname() ([]string, error) {
output, err := c.ovnSbCommand("--format=csv", "--no-heading", "--data=bare", "--columns=hostname", "find", "chassis")
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
}