-
Notifications
You must be signed in to change notification settings - Fork 0
/
ovs.go
265 lines (237 loc) · 8.01 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package util
import (
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"runtime"
"strings"
"time"
"unicode"
"github.com/sirupsen/logrus"
kexec "k8s.io/utils/exec"
"github.com/openvswitch/ovn-kubernetes/go-controller/pkg/config"
)
const (
// On Windows we need an increased timeout on OVS commands, because
// adding internal ports on a non Hyper-V enabled host will call
// external Powershell commandlets.
// TODO: Decrease the timeout once port adding is improved on Windows
ovsCommandTimeout = 15
ovsVsctlCommand = "ovs-vsctl"
ovsOfctlCommand = "ovs-ofctl"
ovnNbctlCommand = "ovn-nbctl"
ipCommand = "ip"
powershellCommand = "powershell"
netshCommand = "netsh"
routeCommand = "route"
osRelease = "/etc/os-release"
rhel = "RHEL"
ubuntu = "Ubuntu"
windowsOS = "windows"
)
func runningPlatform() (string, error) {
if runtime.GOOS == windowsOS {
return windowsOS, nil
}
fileContents, err := ioutil.ReadFile(osRelease)
if err != nil {
return "", fmt.Errorf("failed to parse file %s (%v)", osRelease, err)
}
var platform string
ss := strings.Split(string(fileContents), "\n")
for _, pair := range ss {
keyValue := strings.Split(pair, "=")
if len(keyValue) == 2 {
if keyValue[0] == "Name" || keyValue[0] == "NAME" {
platform = keyValue[1]
break
}
}
}
if platform == "" {
return "", fmt.Errorf("failed to find the platform name")
}
if strings.Contains(platform, "Fedora") ||
strings.Contains(platform, "Red Hat") || strings.Contains(platform, "CentOS") {
return rhel, nil
} else if strings.Contains(platform, "Debian") ||
strings.Contains(platform, ubuntu) {
return ubuntu, nil
} else if strings.Contains(platform, "VMware") {
return "Photon", nil
}
return "", fmt.Errorf("Unknown platform")
}
// Exec runs various OVN and OVS utilities
type execHelper struct {
exec kexec.Interface
ofctlPath string
vsctlPath string
nbctlPath string
ipPath string
powershellPath string
netshPath string
routePath string
}
var runner *execHelper
// SetExec validates executable paths and saves the given exec interface
// to be used for running various OVS and OVN utilites
func SetExec(exec kexec.Interface) error {
var err error
runner = &execHelper{exec: exec}
runner.ofctlPath, err = exec.LookPath(ovsOfctlCommand)
if err != nil {
return err
}
runner.vsctlPath, err = exec.LookPath(ovsVsctlCommand)
if err != nil {
return err
}
runner.nbctlPath, err = exec.LookPath(ovnNbctlCommand)
if err != nil {
return err
}
if runtime.GOOS == windowsOS {
runner.powershellPath, err = exec.LookPath(powershellCommand)
if err != nil {
return err
}
runner.netshPath, err = exec.LookPath(netshCommand)
if err != nil {
return err
}
runner.routePath, err = exec.LookPath(routeCommand)
if err != nil {
return err
}
} else {
runner.ipPath, err = exec.LookPath(ipCommand)
if err != nil {
return err
}
}
return nil
}
// GetExec returns the exec interface which can be used for running commands directly.
// Only use for passing an exec interface into pkg/config which cannot call this
// function directly because this module imports pkg/config already.
func GetExec() kexec.Interface {
return runner.exec
}
func run(cmdPath string, args ...string) (*bytes.Buffer, *bytes.Buffer, error) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd := runner.exec.Command(cmdPath, args...)
cmd.SetStdout(stdout)
cmd.SetStderr(stderr)
logrus.Debugf("exec: %s %s", cmdPath, strings.Join(args, " "))
err := cmd.Run()
if err != nil {
logrus.Debugf("exec: %s %s => %v", cmdPath, strings.Join(args, " "), err)
}
return stdout, stderr, err
}
// RunOVSOfctl runs a command via ovs-ofctl.
func RunOVSOfctl(args ...string) (string, string, error) {
stdout, stderr, err := run(runner.ofctlPath, args...)
return strings.Trim(stdout.String(), "\" \n"), stderr.String(), err
}
// RunOVSVsctl runs a command via ovs-vsctl.
func RunOVSVsctl(args ...string) (string, string, error) {
cmdArgs := []string{fmt.Sprintf("--timeout=%d", ovsCommandTimeout)}
cmdArgs = append(cmdArgs, args...)
stdout, stderr, err := run(runner.vsctlPath, cmdArgs...)
return strings.Trim(strings.TrimSpace(stdout.String()), "\""), stderr.String(), err
}
// Run the ovn-ctl command and retry if "Connection refused"
// poll waitng for service to become available
func runOVNretry(cmdPath string, args ...string) (*bytes.Buffer, *bytes.Buffer, error) {
retriesLeft := 200
for {
stdout, stderr, err := run(cmdPath, args...)
if err == nil {
return stdout, stderr, err
}
// Connection refused
// Master may not be up so keep trying
if strings.Contains(stderr.String(), "Connection refused") {
if retriesLeft == 0 {
return stdout, stderr, err
}
retriesLeft--
time.Sleep(2 * time.Second)
} else {
// Some other problem for caller to handle
return stdout, stderr, fmt.Errorf("OVN command '%s %s' failed: %s", cmdPath, strings.Join(args, " "), err)
}
}
}
// RunOVNNbctlUnix runs command via ovn-nbctl, with ovn-nbctl using the unix
// domain sockets to connect to the ovsdb-server backing the OVN NB database.
func RunOVNNbctlUnix(args ...string) (string, string, error) {
cmdArgs := []string{fmt.Sprintf("--timeout=%d", ovsCommandTimeout)}
cmdArgs = append(cmdArgs, args...)
stdout, stderr, err := runOVNretry(runner.nbctlPath, cmdArgs...)
return strings.Trim(strings.TrimFunc(stdout.String(), unicode.IsSpace), "\""),
stderr.String(), err
}
// RunOVNNbctlWithTimeout runs command via ovn-nbctl with a specific timeout
func RunOVNNbctlWithTimeout(timeout int, args ...string) (string, string,
error) {
var cmdArgs []string
if config.OvnNorth.ClientAuth.Scheme == config.OvnDBSchemeSSL {
cmdArgs = []string{
fmt.Sprintf("--private-key=%s", config.OvnNorth.ClientAuth.PrivKey),
fmt.Sprintf("--certificate=%s", config.OvnNorth.ClientAuth.Cert),
fmt.Sprintf("--bootstrap-ca-cert=%s", config.OvnNorth.ClientAuth.CACert),
fmt.Sprintf("--db=%s", config.OvnNorth.ClientAuth.GetURL()),
}
} else if config.OvnNorth.ClientAuth.Scheme == config.OvnDBSchemeTCP {
cmdArgs = []string{
fmt.Sprintf("--db=%s", config.OvnNorth.ClientAuth.GetURL()),
}
}
cmdArgs = append(cmdArgs, fmt.Sprintf("--timeout=%d", timeout))
cmdArgs = append(cmdArgs, args...)
stdout, stderr, err := runOVNretry(runner.nbctlPath, cmdArgs...)
return strings.Trim(strings.TrimSpace(stdout.String()), "\""), stderr.String(), err
}
// RunOVNNbctl runs a command via ovn-nbctl.
func RunOVNNbctl(args ...string) (string, string, error) {
return RunOVNNbctlWithTimeout(ovsCommandTimeout, args...)
}
// RunIP runs a command via the iproute2 "ip" utility
func RunIP(args ...string) (string, string, error) {
stdout, stderr, err := run(runner.ipPath, args...)
return strings.TrimSpace(stdout.String()), stderr.String(), err
}
// RunPowershell runs a command via the Windows powershell utility
func RunPowershell(args ...string) (string, string, error) {
stdout, stderr, err := run(runner.powershellPath, args...)
return strings.TrimSpace(stdout.String()), stderr.String(), err
}
// RunNetsh runs a command via the Windows netsh utility
func RunNetsh(args ...string) (string, string, error) {
stdout, stderr, err := run(runner.netshPath, args...)
return strings.TrimSpace(stdout.String()), stderr.String(), err
}
// RunRoute runs a command via the Windows route utility
func RunRoute(args ...string) (string, string, error) {
stdout, stderr, err := run(runner.routePath, args...)
return strings.TrimSpace(stdout.String()), stderr.String(), err
}
// RawExec runs the given command via the exec interface. Should only be used
// for early calls before configuration is read.
func RawExec(cmdPath string, args ...string) (string, string, error) {
// If the command is not a path to a binary, try finding it
if filepath.Base(cmdPath) == cmdPath {
var err error
cmdPath, err = runner.exec.LookPath(cmdPath)
if err != nil {
return "", "", err
}
}
stdout, stderr, err := run(cmdPath, args...)
return strings.TrimSpace(stdout.String()), stderr.String(), err
}