/
ovs.go
65 lines (56 loc) · 1.82 KB
/
ovs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package cni
import (
"fmt"
"os/exec"
"strconv"
"strings"
)
func ovsExec(args ...string) (string, error) {
args = append([]string{"--timeout=30"}, args...)
output, err := exec.Command("ovs-vsctl", args...).CombinedOutput()
if err != nil {
return "", fmt.Errorf("failed to run 'ovs-vsctl %s': %v\n %q", strings.Join(args, " "), err, string(output))
}
outStr := string(output)
trimmed := strings.TrimSpace(outStr)
// If output is a single line, strip the trailing newline
if strings.Count(trimmed, "\n") == 0 {
outStr = trimmed
}
return outStr, nil
}
func ovsCreate(table string, values ...string) (string, error) {
args := append([]string{"create", table}, values...)
return ovsExec(args...)
}
func ovsDestroy(table, record string) error {
_, err := ovsExec("--if-exists", "destroy", table, record)
return err
}
func ovsSet(table, record string, values ...string) error {
args := append([]string{"set", table, record}, values...)
_, err := ovsExec(args...)
return err
}
// Returns the given column of records that match the condition
func ovsFind(table, column, condition string) ([]string, error) {
output, err := ovsExec("--no-heading", "--columns="+column, "find", table, condition)
if err != nil {
return nil, err
}
values := strings.Split(output, "\n\n")
// We want "bare" values for strings, but we can't pass --bare to ovs-vsctl because
// it breaks more complicated types. So try passing each value through Unquote();
// if it fails, that means the value wasn't a quoted string, so use it as-is.
for i, val := range values {
if unquoted, err := strconv.Unquote(val); err == nil {
values[i] = unquoted
}
}
return values, nil
}
func ovsClear(table, record string, columns ...string) error {
args := append([]string{"--if-exists", "clear", table, record}, columns...)
_, err := ovsExec(args...)
return err
}