Skip to content

Commit

Permalink
avoid invalid ovn-nbctl daemon socket path (#1799)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed Aug 15, 2022
1 parent 4680e63 commit 5bf8de0
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions pkg/ovs/ovn-nbctl-legacy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ovs

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand All @@ -25,6 +26,8 @@ const (
SgAclEgressDirection AclDirection = "from-lport"
)

var nbctlDaemonSocketRegexp = regexp.MustCompile(`^/var/run/ovn/ovn-nbctl\.[0-9]+\.ctl$`)

func (c LegacyClient) ovnNbCommand(cmdArgs ...string) (string, error) {
start := time.Now()
cmdArgs = append([]string{fmt.Sprintf("--timeout=%d", c.OvnTimeout), "--no-wait"}, cmdArgs...)
Expand Down Expand Up @@ -1749,14 +1752,23 @@ func StartOvnNbctlDaemon(ovnNbAddr string) error {
"--overwrite-pidfile",
}
}
_ = os.Unsetenv("OVN_NB_DAEMON")
output, err = exec.Command("ovn-nbctl", command...).CombinedOutput()
if err != nil {
klog.Errorf("start ovn-nbctl daemon failed, %q", output)

var stdout, stderr bytes.Buffer
cmd := exec.Command("ovn-nbctl", command...)
cmd.Stdout, cmd.Stderr = &stdout, &stderr
if err = cmd.Run(); err != nil {
klog.Errorf("failed to start ovn-nbctl daemon: %v, %s, %s", err, stdout.String(), stderr.String())
return err
}

daemonSocket := strings.TrimSpace(stdout.String())
if !nbctlDaemonSocketRegexp.MatchString(daemonSocket) {
err = fmt.Errorf("invalid nbctl daemon socket: %q", daemonSocket)
klog.Error(err)
return err
}

daemonSocket := strings.TrimSpace(string(output))
_ = os.Unsetenv("OVN_NB_DAEMON")
if err := os.Setenv("OVN_NB_DAEMON", daemonSocket); err != nil {
klog.Errorf("failed to set env OVN_NB_DAEMON, %v", err)
return err
Expand All @@ -1766,14 +1778,12 @@ func StartOvnNbctlDaemon(ovnNbAddr string) error {

// CheckAlive check if kube-ovn-controller can access ovn-nb from nbctl-daemon
func CheckAlive() error {
output, err := exec.Command(
"ovn-nbctl",
"--timeout=60",
"show",
).CombinedOutput()
var stderr bytes.Buffer
cmd := exec.Command("ovn-nbctl", "--timeout=60", "show")
cmd.Stderr = &stderr

if err != nil {
klog.Errorf("failed to access ovn-nb from daemon, %q", output)
if err := cmd.Run(); err != nil {
klog.Errorf("failed to access ovn-nb from daemon: %v, %s", err, stderr.String())
return err
}
return nil
Expand Down

0 comments on commit 5bf8de0

Please sign in to comment.