Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid invalid ovn-nbctl daemon socket path #1799

Merged
merged 1 commit into from
Aug 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1868,14 +1871,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 @@ -1885,14 +1897,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