-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.go
420 lines (373 loc) · 12.5 KB
/
cluster.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cluster
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"html/template"
"net"
"os"
"os/exec"
"regexp"
"time"
"github.com/docker/machine/drivers/virtualbox"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/mcnerror"
"github.com/docker/machine/libmachine/ssh"
"github.com/docker/machine/libmachine/state"
"github.com/golang/glog"
"github.com/pkg/errors"
cfg "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/util"
)
//This init function is used to set the logtostderr variable to false so that INFO level log info does not clutter the CLI
//INFO lvl logging is displayed due to the kubernetes api calling flag.Set("logtostderr", "true") in its init()
//see: https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/util/logs/logs.go#L32-L34
func init() {
flag.Set("logtostderr", "false")
// Setting the default client to native gives much better performance.
ssh.SetDefaultClient(ssh.Native)
}
// StartHost starts a host VM.
func StartHost(api libmachine.API, config MachineConfig) (*host.Host, error) {
exists, err := api.Exists(cfg.GetMachineName())
if err != nil {
return nil, errors.Wrapf(err, "Error checking if host exists: %s", cfg.GetMachineName())
}
if !exists {
glog.Infoln("Machine does not exist... provisioning new machine")
glog.Infof("Provisioning machine with config: %+v", config)
return createHost(api, config)
} else {
glog.Infoln("Skipping create...Using existing machine configuration")
}
h, err := api.Load(cfg.GetMachineName())
if err != nil {
return nil, errors.Wrap(err, "Error loading existing host. Please try running [minikube delete], then run [minikube start] again.")
}
s, err := h.Driver.GetState()
glog.Infoln("Machine state: ", s)
if err != nil {
return nil, errors.Wrap(err, "Error getting state for host")
}
if s != state.Running {
if err := h.Driver.Start(); err != nil {
return nil, errors.Wrap(err, "Error starting stopped host")
}
if err := api.Save(h); err != nil {
return nil, errors.Wrap(err, "Error saving started host")
}
}
if h.Driver.DriverName() != "none" {
if err := h.ConfigureAuth(); err != nil {
return nil, &util.RetriableError{Err: errors.Wrap(err, "Error configuring auth on host")}
}
}
return h, nil
}
// StopHost stops the host VM.
func StopHost(api libmachine.API) error {
host, err := api.Load(cfg.GetMachineName())
if err != nil {
return errors.Wrapf(err, "Error loading host: %s", cfg.GetMachineName())
}
if err := host.Stop(); err != nil {
alreadyInStateError, ok := err.(mcnerror.ErrHostAlreadyInState)
if ok && alreadyInStateError.State == state.Stopped {
return nil
}
return errors.Wrapf(err, "Error stopping host: %s", cfg.GetMachineName())
}
return nil
}
// DeleteHost deletes the host VM.
func DeleteHost(api libmachine.API) error {
host, err := api.Load(cfg.GetMachineName())
if err != nil {
return errors.Wrapf(err, "Error deleting host: %s", cfg.GetMachineName())
}
m := util.MultiError{}
m.Collect(host.Driver.Remove())
m.Collect(api.Remove(cfg.GetMachineName()))
return m.ToError()
}
// GetHostStatus gets the status of the host VM.
func GetHostStatus(api libmachine.API) (string, error) {
exists, err := api.Exists(cfg.GetMachineName())
if err != nil {
return "", errors.Wrapf(err, "Error checking that api exists for: %s", cfg.GetMachineName())
}
if !exists {
return state.None.String(), nil
}
host, err := api.Load(cfg.GetMachineName())
if err != nil {
return "", errors.Wrapf(err, "Error loading api for: %s", cfg.GetMachineName())
}
s, err := host.Driver.GetState()
if err != nil {
return "", errors.Wrap(err, "Error getting host state")
}
return s.String(), nil
}
// GetHostDriverIP gets the ip address of the current minikube cluster
func GetHostDriverIP(api libmachine.API) (net.IP, error) {
host, err := CheckIfApiExistsAndLoad(api)
if err != nil {
return nil, err
}
ipStr, err := host.Driver.GetIP()
if err != nil {
return nil, errors.Wrap(err, "Error getting IP")
}
ip := net.ParseIP(ipStr)
if ip == nil {
return nil, errors.Wrap(err, "Error parsing IP")
}
return ip, nil
}
func engineOptions(config MachineConfig) *engine.Options {
o := engine.Options{
Env: config.DockerEnv,
InsecureRegistry: config.InsecureRegistry,
RegistryMirror: config.RegistryMirror,
ArbitraryFlags: config.DockerOpt,
}
return &o
}
func createVirtualboxHost(config MachineConfig) drivers.Driver {
d := virtualbox.NewDriver(cfg.GetMachineName(), constants.GetMinipath())
d.Boot2DockerURL = config.Downloader.GetISOFileURI(config.MinikubeISO)
d.Memory = config.Memory
d.CPU = config.CPUs
d.DiskSize = int(config.DiskSize)
d.HostOnlyCIDR = config.HostOnlyCIDR
d.NoShare = config.DisableDriverMounts
return d
}
func createHost(api libmachine.API, config MachineConfig) (*host.Host, error) {
var driver interface{}
if config.VMDriver != "none" {
if err := config.Downloader.CacheMinikubeISOFromURL(config.MinikubeISO); err != nil {
return nil, errors.Wrap(err, "Error attempting to cache minikube ISO from URL")
}
}
switch config.VMDriver {
case "virtualbox":
driver = createVirtualboxHost(config)
case "vmwarefusion":
driver = createVMwareFusionHost(config)
case "kvm", "kvm2":
driver = createKVMHost(config)
case "xhyve":
driver = createXhyveHost(config)
case "hyperv":
driver = createHypervHost(config)
case "none":
driver = createNoneHost(config)
case "hyperkit":
driver = createHyperkitHost(config)
default:
glog.Exitf("Unsupported driver: %s\n", config.VMDriver)
}
data, err := json.Marshal(driver)
if err != nil {
return nil, errors.Wrap(err, "Error marshalling json")
}
h, err := api.NewHost(config.VMDriver, data)
if err != nil {
return nil, errors.Wrap(err, "Error creating new host")
}
h.HostOptions.AuthOptions.CertDir = constants.GetMinipath()
h.HostOptions.AuthOptions.StorePath = constants.GetMinipath()
h.HostOptions.EngineOptions = engineOptions(config)
if err := api.Create(h); err != nil {
// Wait for all the logs to reach the client
time.Sleep(2 * time.Second)
return nil, errors.Wrap(err, "Error creating host")
}
if err := api.Save(h); err != nil {
return nil, errors.Wrap(err, "Error attempting to save")
}
return h, nil
}
// GetHostDockerEnv gets the necessary docker env variables to allow the use of docker through minikube's vm
func GetHostDockerEnv(api libmachine.API) (map[string]string, error) {
host, err := CheckIfApiExistsAndLoad(api)
if err != nil {
return nil, errors.Wrap(err, "Error checking that api exists and loading it")
}
ip, err := host.Driver.GetIP()
if err != nil {
return nil, errors.Wrap(err, "Error getting ip from host")
}
tcpPrefix := "tcp://"
port := "2376"
envMap := map[string]string{
"DOCKER_TLS_VERIFY": "1",
"DOCKER_HOST": tcpPrefix + net.JoinHostPort(ip, port),
"DOCKER_CERT_PATH": constants.MakeMiniPath("certs"),
}
return envMap, nil
}
// MountHost runs the mount command from the 9p client on the VM to the 9p server on the host
func MountHost(api libmachine.API, ip net.IP, path, port, mountVersion string, uid, gid, msize int) error {
host, err := CheckIfApiExistsAndLoad(api)
if err != nil {
return errors.Wrap(err, "Error checking that api exists and loading it")
}
if ip == nil {
ip, err = GetVMHostIP(host)
if err != nil {
return errors.Wrap(err, "Error getting the host IP address to use from within the VM")
}
}
host.RunSSHCommand(GetMountCleanupCommand(path))
mountCmd, err := GetMountCommand(ip, path, port, mountVersion, uid, gid, msize)
if err != nil {
return errors.Wrap(err, "Error getting mount command")
}
_, err = host.RunSSHCommand(mountCmd)
if err != nil {
return errors.Wrap(err, "running mount host command")
}
return nil
}
// GetVMHostIP gets the ip address to be used for mapping host -> VM and VM -> host
func GetVMHostIP(host *host.Host) (net.IP, error) {
switch host.DriverName {
case "kvm":
return net.ParseIP("192.168.42.1"), nil
case "hyperv":
re := regexp.MustCompile(`"VSwitch": "(.*?)",`)
// TODO(aprindle) Change this to deserialize the driver instead
hypervVirtualSwitch := re.FindStringSubmatch(string(host.RawDriver))[1]
ip, err := getIPForInterface(fmt.Sprintf("vEthernet (%s)", hypervVirtualSwitch))
if err != nil {
return []byte{}, errors.Wrap(err, "Error getting VM/Host IP address")
}
return ip, nil
case "virtualbox":
out, err := exec.Command(detectVBoxManageCmd(), "showvminfo", "minikube", "--machinereadable").Output()
if err != nil {
return []byte{}, errors.Wrap(err, "Error running vboxmanage command")
}
re := regexp.MustCompile(`hostonlyadapter2="(.*?)"`)
iface := re.FindStringSubmatch(string(out))[1]
ip, err := getIPForInterface(iface)
if err != nil {
return []byte{}, errors.Wrap(err, "Error getting VM/Host IP address")
}
return ip, nil
case "xhyve":
return net.ParseIP("192.168.64.1"), nil
default:
return []byte{}, errors.New("Error, attempted to get host ip address for unsupported driver")
}
}
// Based on code from http://stackoverflow.com/questions/23529663/how-to-get-all-addresses-and-masks-from-local-interfaces-in-go
func getIPForInterface(name string) (net.IP, error) {
i, _ := net.InterfaceByName(name)
addrs, _ := i.Addrs()
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok {
if ip := ipnet.IP.To4(); ip != nil {
return ip, nil
}
}
}
return nil, errors.Errorf("Error finding IPV4 address for %s", name)
}
func CheckIfApiExistsAndLoad(api libmachine.API) (*host.Host, error) {
exists, err := api.Exists(cfg.GetMachineName())
if err != nil {
return nil, errors.Wrapf(err, "Error checking that api exists for: %s", cfg.GetMachineName())
}
if !exists {
return nil, errors.Errorf("Machine does not exist for api.Exists(%s)", cfg.GetMachineName())
}
host, err := api.Load(cfg.GetMachineName())
if err != nil {
return nil, errors.Wrapf(err, "Error loading api for: %s", cfg.GetMachineName())
}
return host, nil
}
func CreateSSHShell(api libmachine.API, args []string) error {
host, err := CheckIfApiExistsAndLoad(api)
if err != nil {
return errors.Wrap(err, "Error checking if api exist and loading it")
}
currentState, err := host.Driver.GetState()
if err != nil {
return errors.Wrap(err, "Error getting state of host")
}
if currentState != state.Running {
return errors.Errorf("Error: Cannot run ssh command: Host %q is not running", cfg.GetMachineName())
}
client, err := host.CreateSSHClient()
if err != nil {
return errors.Wrap(err, "Error creating ssh client")
}
return client.Shell(args...)
}
// EnsureMinikubeRunningOrExit checks that minikube has a status available and that
// that the status is `Running`, otherwise it will exit
func EnsureMinikubeRunningOrExit(api libmachine.API, exitStatus int) {
s, err := GetHostStatus(api)
if err != nil {
glog.Errorln("Error getting machine status:", err)
os.Exit(1)
}
if s != state.Running.String() {
fmt.Fprintln(os.Stderr, "minikube is not currently running so the service cannot be accessed")
os.Exit(exitStatus)
}
}
func GetMountCleanupCommand(path string) string {
return fmt.Sprintf("sudo umount %s;", path)
}
var mountTemplate = `
sudo mkdir -p {{.Path}} || true;
sudo mount -t 9p -o trans=tcp,port={{.Port}},dfltuid={{.UID}},dfltgid={{.GID}},version={{.Version}},msize={{.Msize}} {{.IP}} {{.Path}};
sudo chmod 775 {{.Path}};`
func GetMountCommand(ip net.IP, path, port, mountVersion string, uid, gid, msize int) (string, error) {
t := template.Must(template.New("mountCommand").Parse(mountTemplate))
buf := bytes.Buffer{}
data := struct {
IP string
Path string
Port string
Version string
UID int
GID int
Msize int
}{
IP: ip.String(),
Path: path,
Port: port,
Version: mountVersion,
UID: uid,
GID: gid,
Msize: msize,
}
if err := t.Execute(&buf, data); err != nil {
return "", err
}
return buf.String(), nil
}