From 41cb044c9c15de6bc3919f03e6a358eda822c085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=A5=96=E5=BB=BA?= Date: Mon, 13 May 2024 09:37:01 +0800 Subject: [PATCH] simplify file reading (#4010) Signed-off-by: zhangzujian --- pkg/ovn_leader_checker/ovn.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/pkg/ovn_leader_checker/ovn.go b/pkg/ovn_leader_checker/ovn.go index 505e69d7878..ec7e3204cfd 100755 --- a/pkg/ovn_leader_checker/ovn.go +++ b/pkg/ovn_leader_checker/ovn.go @@ -4,7 +4,6 @@ import ( "context" "flag" "fmt" - "io" "net" "os" "os/exec" @@ -183,22 +182,15 @@ func isDBLeader(dbName string, port int) bool { } func checkNorthdActive() bool { - var command []string - file, err := os.OpenFile(OvnNorthdPid, os.O_RDWR, 0o600) + pid, err := os.ReadFile(OvnNorthdPid) if err != nil { - klog.Errorf("failed to open %s err = %v", OvnNorthdPid, err) - return false - } - defer file.Close() - fileByte, err := io.ReadAll(file) - if err != nil { - klog.Errorf("failed to read %s err = %v", OvnNorthdPid, err) + klog.Errorf("failed to read file %q: %v", OvnNorthdPid, err) return false } - command = []string{ + command := []string{ "-t", - fmt.Sprintf("/var/run/ovn/ovn-northd.%s.ctl", strings.TrimSpace(string(fileByte))), + fmt.Sprintf("/var/run/ovn/ovn-northd.%s.ctl", strings.TrimSpace(string(pid))), "status", } output, err := exec.Command("ovs-appctl", command...).CombinedOutput()