Skip to content

Commit f31d0b8

Browse files
committed
refactor: do not read pid file when calling ovs/ovn appctl command
Signed-off-by: zhangzujian <zhangzujian.7@gmail.com>
1 parent 503af3d commit f31d0b8

File tree

4 files changed

+74
-113
lines changed

4 files changed

+74
-113
lines changed

pkg/ovn_leader_checker/ovn.go

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -219,20 +219,9 @@ func isDBLeader(address, database string) bool {
219219
}
220220

221221
func checkNorthdActive() bool {
222-
pid, err := os.ReadFile(OvnNorthdPid)
222+
output, err := ovs.Appctl("ovn-northd", "status")
223223
if err != nil {
224-
klog.Errorf("failed to read file %q: %v", OvnNorthdPid, err)
225-
return false
226-
}
227-
228-
command := []string{
229-
"-t",
230-
fmt.Sprintf("/var/run/ovn/ovn-northd.%s.ctl", strings.TrimSpace(string(pid))),
231-
"status",
232-
}
233-
output, err := exec.Command("ovn-appctl", command...).CombinedOutput() // #nosec G204
234-
if err != nil {
235-
klog.Errorf("checkNorthdActive execute err %v error msg %v", err, string(output))
224+
klog.Errorf("checkNorthdActive execute err %v error msg %v", err, output)
236225
return false
237226
}
238227

@@ -241,8 +230,8 @@ func checkNorthdActive() bool {
241230
return false
242231
}
243232

244-
klog.V(5).Infof("checkNorthdActive: output %s", string(output))
245-
result := strings.TrimSpace(string(output))
233+
klog.V(5).Infof("checkNorthdActive: output %s", output)
234+
result := strings.TrimSpace(output)
246235
return strings.Contains(result, "active")
247236
}
248237

@@ -317,21 +306,16 @@ func checkNorthdEpAlive(cfg *Configuration, namespace, service string) bool {
317306
}
318307

319308
func compactOvnDatabase(db string) {
320-
args := []string{
321-
"-t",
322-
fmt.Sprintf("/var/run/ovn/ovn%s_db.ctl", db),
323-
"ovsdb-server/compact",
324-
}
325-
output, err := exec.Command("ovn-appctl", args...).CombinedOutput() // #nosec G204
309+
output, err := ovs.OvnDatabaseControl(db, "ovsdb-server/compact")
326310
if err != nil {
327-
if !strings.Contains(string(output), "not storing a duplicate snapshot") {
328-
klog.Errorf("failed to compact ovn%s database: %s", db, string(output))
311+
if !strings.Contains(output, "not storing a duplicate snapshot") {
312+
klog.Errorf("failed to compact ovn%s database: %s", db, output)
329313
}
330314
return
331315
}
332316

333317
if len(output) != 0 {
334-
klog.V(5).Infof("compact ovn%s database: %s", db, string(output))
318+
klog.V(5).Infof("compact ovn%s database: %s", db, output)
335319
}
336320
}
337321

pkg/ovnmonitor/exporter.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,9 @@ func (e *Exporter) exportOvnClusterEnableGauge() {
261261

262262
func (e *Exporter) exportOvnClusterInfoGauge() {
263263
resetOvnClusterMetrics()
264-
dirDbMap := map[string]string{
265-
"nb": ovnnb.DatabaseName,
266-
"sb": ovnsb.DatabaseName,
267-
}
268-
for direction, database := range dirDbMap {
269-
clusterStatus, err := getClusterInfo(direction, database)
264+
dbList := []string{ovnnb.DatabaseName, ovnsb.DatabaseName}
265+
for _, database := range dbList {
266+
clusterStatus, err := getClusterInfo(database)
270267
if err != nil {
271268
klog.Errorf("Failed to get Cluster Info for database %s: %v", database, err)
272269
return

pkg/ovnmonitor/util.go

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package ovnmonitor
22

33
import (
44
"fmt"
5-
"os"
65
"os/exec"
76
"strconv"
87
"strings"
@@ -11,6 +10,7 @@ import (
1110
"github.com/kubeovn/ovsdb"
1211
"k8s.io/klog/v2"
1312

13+
"github.com/kubeovn/kube-ovn/pkg/ovs"
1414
"github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb"
1515
"github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnsb"
1616
)
@@ -26,46 +26,34 @@ func (e *Exporter) getOvnStatus() map[string]int {
2626
result := make(map[string]int)
2727

2828
// get ovn-northbound status
29-
cmd := exec.Command("ovn-appctl", "-t", "/var/run/ovn/ovnnb_db.ctl", "cluster/status", ovnnb.DatabaseName) // #nosec G204
30-
output, err := cmd.CombinedOutput()
29+
output, err := ovs.OvnDatabaseControl(ovnnb.DatabaseName, "cluster/status", ovnnb.DatabaseName)
3130
if err != nil {
3231
klog.Errorf("get ovn-northbound status failed, err %v", err)
3332
result["ovsdb-server-northbound"] = 0
3433
}
35-
result["ovsdb-server-northbound"] = parseDbStatus(string(output))
34+
result["ovsdb-server-northbound"] = parseDbStatus(output)
3635

3736
// get ovn-southbound status
38-
cmd = exec.Command("ovn-appctl", "-t", "/var/run/ovn/ovnsb_db.ctl", "cluster/status", ovnsb.DatabaseName) // #nosec G204
39-
output, err = cmd.CombinedOutput()
37+
output, err = ovs.OvnDatabaseControl(ovnsb.DatabaseName, "cluster/status", ovnsb.DatabaseName)
4038
if err != nil {
4139
klog.Errorf("get ovn-southbound status failed, err %v", err)
4240
result["ovsdb-server-southbound"] = 0
4341
}
44-
result["ovsdb-server-southbound"] = parseDbStatus(string(output))
42+
result["ovsdb-server-southbound"] = parseDbStatus(output)
4543

4644
// get ovn-northd status
47-
pid, err := os.ReadFile(e.Client.Service.Northd.File.Pid.Path)
48-
if err != nil {
49-
klog.Errorf("read ovn-northd pid failed, err %v", err)
45+
if output, err = ovs.Appctl("ovn-northd", "status"); err != nil {
46+
klog.Errorf("get ovn-northd status failed, err %v", err)
47+
result["ovn-northd"] = 0
48+
} else if len(strings.Split(output, ":")) != 2 {
5049
result["ovn-northd"] = 0
5150
} else {
52-
// #nosec G204
53-
cmd := exec.Command("ovn-appctl", "-t", fmt.Sprintf("/var/run/ovn/ovn-northd.%s.ctl", strings.Trim(string(pid), "\n")), "status")
54-
output, err := cmd.CombinedOutput()
55-
if err != nil {
56-
klog.Errorf("get ovn-northd status failed, err %v", err)
57-
result["ovn-northd"] = 0
58-
}
59-
if len(strings.Split(string(output), ":")) != 2 {
60-
result["ovn-northd"] = 0
61-
} else {
62-
status := strings.TrimSpace(strings.Split(string(output), ":")[1])
63-
switch status {
64-
case "standby":
65-
result["ovn-northd"] = 2
66-
case "active":
67-
result["ovn-northd"] = 1
68-
}
51+
status := strings.TrimSpace(strings.Split(output, ":")[1])
52+
switch status {
53+
case "standby":
54+
result["ovn-northd"] = 2
55+
case "active":
56+
result["ovn-northd"] = 1
6957
}
7058
}
7159

@@ -76,24 +64,22 @@ func (e *Exporter) getOvnStatusContent() map[string]string {
7664
result := map[string]string{"ovsdb-server-northbound": "", "ovsdb-server-southbound": ""}
7765

7866
// get ovn-northbound status
79-
cmd := exec.Command("ovn-appctl", "-t", "/var/run/ovn/ovnnb_db.ctl", "cluster/status", ovnnb.DatabaseName) // #nosec G204
80-
output, err := cmd.CombinedOutput()
67+
output, err := ovs.OvnDatabaseControl(ovnnb.DatabaseName, "cluster/status", ovnnb.DatabaseName)
8168
if err != nil {
8269
klog.Errorf("get ovn-northbound status failed, err %v", err)
8370
}
84-
if strings.Contains(string(output), "Servers:") {
85-
servers := strings.Split(string(output), "Servers:")[1]
71+
if strings.Contains(output, "Servers:") {
72+
servers := strings.Split(output, "Servers:")[1]
8673
result["ovsdb-server-northbound"] = servers
8774
}
8875

8976
// get ovn-southbound status
90-
cmd = exec.Command("ovn-appctl", "-t", "/var/run/ovn/ovnsb_db.ctl", "cluster/status", ovnsb.DatabaseName) // #nosec G204
91-
output, err = cmd.CombinedOutput()
77+
output, err = ovs.OvnDatabaseControl(ovnsb.DatabaseName, "cluster/status", ovnsb.DatabaseName)
9278
if err != nil {
9379
klog.Errorf("get ovn-southbound status failed, err %v", err)
9480
}
95-
if strings.Contains(string(output), "Servers:") {
96-
servers := strings.Split(string(output), "Servers:")[1]
81+
if strings.Contains(output, "Servers:") {
82+
servers := strings.Split(output, "Servers:")[1]
9783
result["ovsdb-server-southbound"] = servers
9884
}
9985

@@ -174,15 +160,14 @@ func (e *Exporter) setLogicalSwitchPortInfoMetric() {
174160
}
175161
}
176162

177-
func getClusterInfo(direction, dbName string) (*OVNDBClusterStatus, error) {
178-
cmd := exec.Command("ovn-appctl", "-t", fmt.Sprintf("/var/run/ovn/ovn%s_db.ctl", direction), "cluster/status", dbName) // #nosec G204
179-
output, err := cmd.CombinedOutput()
163+
func getClusterInfo(dbName string) (*OVNDBClusterStatus, error) {
164+
output, err := ovs.OvnDatabaseControl(dbName, "cluster/status", dbName)
180165
if err != nil {
181166
return nil, fmt.Errorf("failed to retrieve cluster/status info for database %s: %w", dbName, err)
182167
}
183168

184169
clusterStatus := &OVNDBClusterStatus{}
185-
for line := range strings.SplitSeq(string(output), "\n") {
170+
for line := range strings.SplitSeq(output, "\n") {
186171
idx := strings.Index(line, ":")
187172
if idx == -1 {
188173
continue
@@ -305,35 +290,21 @@ func parseDbStatus(output string) int {
305290
}
306291

307292
func getDBStatus(dbName string) (bool, error) {
308-
var cmd *exec.Cmd
309-
var result bool
310-
switch dbName {
311-
case ovnnb.DatabaseName:
312-
cmd = exec.Command("ovn-appctl", "-t", "/var/run/ovn/ovnnb_db.ctl", "ovsdb-server/get-db-storage-status", dbName) // #nosec G204
313-
case ovnsb.DatabaseName:
314-
cmd = exec.Command("ovn-appctl", "-t", "/var/run/ovn/ovnsb_db.ctl", "ovsdb-server/get-db-storage-status", dbName) // #nosec G204
315-
default:
316-
return false, fmt.Errorf("unknown db name %s", dbName)
317-
}
318-
319-
output, err := cmd.CombinedOutput()
293+
output, err := ovs.OvnDatabaseControl(dbName, "ovsdb-server/get-db-storage-status", dbName)
320294
if err != nil {
321295
klog.Errorf("get %s status failed, err %v", dbName, err)
322296
return false, err
323297
}
324-
lines := strings.SplitSeq(string(output), "\n")
325-
for line := range lines {
298+
for line := range strings.SplitSeq(output, "\n") {
326299
if strings.Contains(line, "status: ok") {
327-
result = true
328-
break
300+
return true, nil
329301
}
330302
if strings.Contains(line, "ovsdb error") {
331-
result = false
332-
break
303+
return false, nil
333304
}
334305
}
335306

336-
return result, nil
307+
return false, nil
337308
}
338309

339310
func resetLogicalSwitchMetrics() {

pkg/ovs/ovs-appctl_linux.go

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,58 @@ import (
44
"fmt"
55
"os"
66
"os/exec"
7-
"path/filepath"
87
"slices"
98
"strings"
9+
10+
"github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb"
11+
"github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnsb"
1012
)
1113

1214
const (
13-
ovsRunDir = "/var/run/openvswitch"
14-
ovnRunDir = "/var/run/ovn"
15-
1615
cmdOvsAppctl = "ovs-appctl"
16+
cmdOvnAppctl = "ovn-appctl"
17+
18+
ovnNBCtlSocket = "/var/run/ovn/ovnnb_db.ctl"
19+
ovnSBCtlSocket = "/var/run/ovn/ovnsb_db.ctl"
1720
)
1821

19-
func appctlByTarget(target string, args ...string) (string, error) {
20-
args = slices.Insert(args, 0, "-t", target)
21-
cmd := exec.Command(cmdOvsAppctl, args...)
22+
func appctlByTarget(appctlCmd, target, command string, args ...string) (string, error) {
23+
args = slices.Insert(args, 0, "-t", target, command)
24+
cmd := exec.Command(appctlCmd, args...)
2225
output, err := cmd.CombinedOutput()
2326
if err != nil {
2427
return "", fmt.Errorf("failed to run command %q: %w", cmd.String(), err)
2528
}
2629
return string(output), nil
2730
}
2831

29-
func Appctl(component string, args ...string) (string, error) {
30-
var runDir string
31-
switch {
32-
case strings.HasPrefix(component, "ovs"):
33-
runDir = ovsRunDir
34-
case strings.HasPrefix(component, "ovn"):
35-
runDir = ovnRunDir
32+
// OvnDatabaseControl sends a command to the specified OVN database control socket
33+
// and returns the output or an error if the command fails.
34+
func OvnDatabaseControl(db, command string, args ...string) (string, error) {
35+
var socket string
36+
switch db {
37+
case "nb", ovnnb.DatabaseName:
38+
socket = ovnNBCtlSocket
39+
case "sb", ovnsb.DatabaseName:
40+
socket = ovnSBCtlSocket
3641
default:
37-
return "", fmt.Errorf("unknown component %q", component)
42+
return "", fmt.Errorf("unknown db %q", db)
3843
}
44+
return Appctl(socket, command, args...)
45+
}
3946

40-
pidFile := filepath.Join(runDir, component+".pid")
41-
pidBytes, err := os.ReadFile(pidFile)
42-
if err != nil {
43-
return "", fmt.Errorf("failed to read pid file %q: %w", pidFile, err)
44-
}
45-
pidFields := strings.Fields(string(pidBytes))
46-
if len(pidFields) == 0 {
47-
return "", fmt.Errorf("pid file %q is empty or contains only whitespace", pidFile)
47+
func Appctl(target, command string, args ...string) (string, error) {
48+
var cmd string
49+
switch {
50+
case strings.IndexRune(target, os.PathSeparator) == 0:
51+
fallthrough
52+
case strings.HasPrefix(target, "ovn"):
53+
cmd = cmdOvnAppctl
54+
case strings.HasPrefix(target, "ovs"):
55+
cmd = cmdOvsAppctl
56+
default:
57+
return "", fmt.Errorf("unknown target %q", target)
4858
}
49-
target := filepath.Join(runDir, fmt.Sprintf("%s.%s.ctl", component, pidFields[0]))
5059

51-
return appctlByTarget(target, args...)
60+
return appctlByTarget(cmd, target, command, args...)
5261
}

0 commit comments

Comments
 (0)