Skip to content

Commit

Permalink
Wait for ovs-vswitchd PID before calling ovs-appctl (#2711)
Browse files Browse the repository at this point in the history
Otherwise the call may fail and crash the process.

Besides, it cleans up the OVS run files on exit to avoid stale PID
from being used.

Signed-off-by: Quan Tian <qtian@vmware.com>
  • Loading branch information
tnqn authored Sep 3, 2021
1 parent f97a102 commit 1ab926e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions build/images/scripts/start_ovs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ function quit {
stop_ovs
# kill background sleep process
if [ "$SLEEP_PID" != "" ]; then kill $SLEEP_PID > /dev/null 2>&1 || true; fi
cleanup_ovs_run_files
exit 0
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/ovs/ovsctl/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package ovsctl

import (
"fmt"
"net"
"os/exec"
)
Expand Down Expand Up @@ -85,3 +86,7 @@ func (e *ExecError) GetErrorOutput() string {
}
return e.errorOutput
}

func (e *ExecError) Error() string {
return fmt.Sprintf("ExecError: %v, output: %s", e.error, e.errorOutput)
}
22 changes: 19 additions & 3 deletions pkg/ovs/ovsctl/ovsctl_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"fmt"
"os"
"os/exec"
"time"

"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
)

Expand All @@ -47,12 +49,26 @@ func ovsVSwitchdUDS() string {
// are possible. Besides, this value is only used when invoking ovs-appctl (as a new
// process) at the moment, so the overhead of reading the PID from file should not be a
// concern.
pid, err := readOVSVSwitchdPID()
if err != nil {
klog.ErrorS(err, "Failed to read ovs-vswitchd PID")
var pid int
var readErr error
startTime := time.Now()
hasFailure := false
pollErr := wait.PollImmediate(50*time.Millisecond, 5*time.Second, func() (bool, error) {
pid, readErr = readOVSVSwitchdPID()
if readErr != nil {
hasFailure = true
return false, nil
}
return true, nil
})
if pollErr != nil {
klog.ErrorS(readErr, "Failed to read ovs-vswitchd PID")
// that seems like a reasonable value to return if we cannot read the PID
return "/var/run/openvswitch/ovs-vswitchd.*.ctl"
}
if hasFailure {
klog.V(2).InfoS("Waited for ovs-vswitchd PID to be ready", "duration", time.Since(startTime))
}
return fmt.Sprintf("/var/run/openvswitch/ovs-vswitchd.%d.ctl", pid)
}

Expand Down

0 comments on commit 1ab926e

Please sign in to comment.