6 kexec "k8s.io/utils/exec"
13 ovsCommandTimeout = 15
14 ovnNbctlCommand = "ovn-nbctl"
17 // Exec runs various OVN and OVS utilities
18 type execHelper struct {
25 var runner *execHelper
27 // SetupOvnUtils does internal OVN initialization
28 var SetupOvnUtils = func() error {
29 runner.hostIP = os.Getenv("HOST_IP")
31 runner.hostPort = "6641"
32 log.Info("Host Port", "IP", runner.hostIP, "Port", runner.hostPort)
34 // Setup Distributed Router
35 err := setupDistributedRouter(ovn4nfvRouterName)
37 log.Error(err, "Failed to initialize OVN Distributed Router")
43 // SetExec validates executable paths and saves the given exec interface
44 // to be used for running various OVS and OVN utilites
45 func SetExec(exec kexec.Interface) error {
48 runner = &execHelper{exec: exec}
49 runner.nbctlPath, err = exec.LookPath(ovnNbctlCommand)
56 // Run the ovn-ctl command and retry if "Connection refused"
57 // poll waitng for service to become available
58 func runOVNretry(cmdPath string, args ...string) (*bytes.Buffer, *bytes.Buffer, error) {
62 stdout, stderr, err := run(cmdPath, args...)
64 return stdout, stderr, err
67 // Master may not be up so keep trying
68 if strings.Contains(stderr.String(), "Connection refused") {
70 return stdout, stderr, err
73 time.Sleep(2 * time.Second)
75 // Some other problem for caller to handle
76 return stdout, stderr, err
81 func run(cmdPath string, args ...string) (*bytes.Buffer, *bytes.Buffer, error) {
82 stdout := &bytes.Buffer{}
83 stderr := &bytes.Buffer{}
84 cmd := runner.exec.Command(cmdPath, args...)
87 log.Info("exec:", "cmdPath", cmdPath, "args", strings.Join(args, " "))
90 log.Error(err, "exec:", "cmdPath", cmdPath, "args", strings.Join(args, " "))
92 return stdout, stderr, err
95 // RunOVNSbctlWithTimeout runs command via ovn-nbctl with a specific timeout
96 func RunOVNNbctlWithTimeout(timeout int, args ...string) (string, string, error) {
98 if len(runner.hostIP) > 0 {
100 fmt.Sprintf("--db=tcp:%s:%s", runner.hostIP, runner.hostPort),
103 cmdArgs = append(cmdArgs, fmt.Sprintf("--timeout=%d", timeout))
104 cmdArgs = append(cmdArgs, args...)
105 stdout, stderr, err := runOVNretry(runner.nbctlPath, cmdArgs...)
106 return strings.Trim(strings.TrimSpace(stdout.String()), "\""), stderr.String(), err
109 // RunOVNNbctl runs a command via ovn-nbctl.
110 func RunOVNNbctl(args ...string) (string, string, error) {
111 return RunOVNNbctlWithTimeout(ovsCommandTimeout, args...)