-
Notifications
You must be signed in to change notification settings - Fork 522
refactor: do not read pid file when calling ovs/ovn appctl command #6230
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,49 +4,58 @@ import ( | |
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb" | ||
| "github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnsb" | ||
| ) | ||
|
|
||
| const ( | ||
| ovsRunDir = "/var/run/openvswitch" | ||
| ovnRunDir = "/var/run/ovn" | ||
|
|
||
| cmdOvsAppctl = "ovs-appctl" | ||
| cmdOvnAppctl = "ovn-appctl" | ||
|
|
||
| ovnNBCtlSocket = "/var/run/ovn/ovnnb_db.ctl" | ||
| ovnSBCtlSocket = "/var/run/ovn/ovnsb_db.ctl" | ||
| ) | ||
|
|
||
| func appctlByTarget(target string, args ...string) (string, error) { | ||
| args = slices.Insert(args, 0, "-t", target) | ||
| cmd := exec.Command(cmdOvsAppctl, args...) | ||
| func appctlByTarget(appctlCmd, target, command string, args ...string) (string, error) { | ||
| args = slices.Insert(args, 0, "-t", target, command) | ||
| cmd := exec.Command(appctlCmd, args...) | ||
| output, err := cmd.CombinedOutput() | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to run command %q: %w", cmd.String(), err) | ||
| } | ||
| return string(output), nil | ||
| } | ||
|
|
||
| func Appctl(component string, args ...string) (string, error) { | ||
| var runDir string | ||
| switch { | ||
| case strings.HasPrefix(component, "ovs"): | ||
| runDir = ovsRunDir | ||
| case strings.HasPrefix(component, "ovn"): | ||
| runDir = ovnRunDir | ||
| // OvnDatabaseControl sends a command to the specified OVN database control socket | ||
| // and returns the output or an error if the command fails. | ||
| func OvnDatabaseControl(db, command string, args ...string) (string, error) { | ||
| var socket string | ||
| switch db { | ||
| case "nb", ovnnb.DatabaseName: | ||
| socket = ovnNBCtlSocket | ||
| case "sb", ovnsb.DatabaseName: | ||
| socket = ovnSBCtlSocket | ||
| default: | ||
| return "", fmt.Errorf("unknown component %q", component) | ||
| return "", fmt.Errorf("unknown db %q", db) | ||
| } | ||
| return Appctl(socket, command, args...) | ||
| } | ||
|
|
||
| pidFile := filepath.Join(runDir, component+".pid") | ||
| pidBytes, err := os.ReadFile(pidFile) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to read pid file %q: %w", pidFile, err) | ||
| } | ||
| pidFields := strings.Fields(string(pidBytes)) | ||
| if len(pidFields) == 0 { | ||
| return "", fmt.Errorf("pid file %q is empty or contains only whitespace", pidFile) | ||
| func Appctl(target, command string, args ...string) (string, error) { | ||
| var cmd string | ||
| switch { | ||
| case strings.IndexRune(target, os.PathSeparator) == 0: | ||
| fallthrough | ||
| case strings.HasPrefix(target, "ovn"): | ||
| cmd = cmdOvnAppctl | ||
| case strings.HasPrefix(target, "ovs"): | ||
| cmd = cmdOvsAppctl | ||
| default: | ||
| return "", fmt.Errorf("unknown target %q", target) | ||
| } | ||
|
Comment on lines
+49
to
58
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of
Comment on lines
+49
to
58
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic to determine whether to use switch {
case strings.HasPrefix(target, "ovs"):
cmd = cmdOvsAppctl
case strings.HasPrefix(target, "ovn"):
cmd = cmdOvnAppctl
case strings.IndexRune(target, os.PathSeparator) == 0:
if strings.Contains(target, "openvswitch") {
cmd = cmdOvsAppctl
} else {
cmd = cmdOvnAppctl
}
default:
return "", fmt.Errorf("unknown target %q", target)
} |
||
| target := filepath.Join(runDir, fmt.Sprintf("%s.%s.ctl", component, pidFields[0])) | ||
|
|
||
| return appctlByTarget(target, args...) | ||
| return appctlByTarget(cmd, target, command, args...) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.