-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
none.go
246 lines (216 loc) · 6.94 KB
/
none.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
/*
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 none
import (
"fmt"
"os/exec"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/state"
"github.com/golang/glog"
"github.com/pkg/errors"
knet "k8s.io/apimachinery/pkg/util/net"
pkgdrivers "k8s.io/minikube/pkg/drivers"
"k8s.io/minikube/pkg/minikube/bootstrapper/bsutil/kverify"
"k8s.io/minikube/pkg/minikube/command"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/cruntime"
"k8s.io/minikube/pkg/minikube/kubeconfig"
"k8s.io/minikube/pkg/minikube/sysinit"
"k8s.io/minikube/pkg/minikube/vmpath"
)
// cleanupPaths are paths to be removed by cleanup, and are used by both kubeadm and minikube.
var cleanupPaths = []string{
vmpath.GuestEphemeralDir,
vmpath.GuestManifestsDir,
vmpath.GuestPersistentDir,
}
// Driver is a driver designed to run kubeadm w/o VM management, and assumes systemctl.
// https://minikube.sigs.k8s.io/docs/reference/drivers/none/
type Driver struct {
*drivers.BaseDriver
*pkgdrivers.CommonDriver
URL string
runtime cruntime.Manager
exec command.Runner
}
// Config is configuration for the None driver
type Config struct {
MachineName string
StorePath string
ContainerRuntime string
}
// NewDriver returns a fully configured None driver
func NewDriver(c Config) *Driver {
runner := command.NewExecRunner()
runtime, err := cruntime.New(cruntime.Config{Type: c.ContainerRuntime, Runner: runner})
// Libraries shouldn't panic, but there is no way for drivers to return error :(
if err != nil {
glog.Fatalf("unable to create container runtime: %v", err)
}
return &Driver{
BaseDriver: &drivers.BaseDriver{
MachineName: c.MachineName,
StorePath: c.StorePath,
},
runtime: runtime,
exec: runner,
}
}
// PreCreateCheck checks for correct privileges and dependencies
func (d *Driver) PreCreateCheck() error {
return d.runtime.Available()
}
// Create a host using the driver's config
func (d *Driver) Create() error {
// creation for the none driver is handled by commands.go
return nil
}
// DriverName returns the name of the driver
func (d *Driver) DriverName() string {
return "none"
}
// GetIP returns an IP or hostname that this host is available at
func (d *Driver) GetIP() (string, error) {
ip, err := knet.ChooseHostInterface()
if err != nil {
return "", err
}
return ip.String(), nil
}
// GetSSHHostname returns hostname for use with ssh
func (d *Driver) GetSSHHostname() (string, error) {
return "", fmt.Errorf("driver does not support ssh commands")
}
// GetSSHPort returns port for use with ssh
func (d *Driver) GetSSHPort() (int, error) {
return 0, fmt.Errorf("driver does not support ssh commands")
}
// GetURL returns a Docker URL inside this host
// e.g. tcp://1.2.3.4:2376
// more info https://github.com/docker/machine/blob/b170508bf44c3405e079e26d5fdffe35a64c6972/libmachine/provision/utils.go#L159_L175
func (d *Driver) GetURL() (string, error) {
ip, err := d.GetIP()
if err != nil {
return "", err
}
return fmt.Sprintf("tcp://%s:2376", ip), nil
}
// GetState returns the state that the host is in (running, stopped, etc)
func (d *Driver) GetState() (state.State, error) {
hostname, port, err := kubeconfig.Endpoint(d.BaseDriver.MachineName)
if err != nil {
glog.Warningf("unable to get port: %v", err)
port = constants.APIServerPort
}
// Confusing logic, as libmachine.Stop will loop until the state == Stopped
ast, err := kverify.APIServerStatus(d.exec, hostname, port)
if err != nil {
return ast, err
}
// If the apiserver is up, we'll claim to be up.
if ast == state.Paused || ast == state.Running {
return state.Running, nil
}
return kverify.KubeletStatus(d.exec), nil
}
// Kill stops a host forcefully, including any containers that we are managing.
func (d *Driver) Kill() error {
if err := sysinit.New(d.exec).ForceStop("kubelet"); err != nil {
glog.Warningf("couldn't force stop kubelet. will continue with kill anyways: %v", err)
}
// First try to gracefully stop containers
containers, err := d.runtime.ListContainers(cruntime.ListOptions{})
if err != nil {
return errors.Wrap(err, "containers")
}
if len(containers) == 0 {
return nil
}
// Try to be graceful before sending SIGKILL everywhere.
if err := d.runtime.StopContainers(containers); err != nil {
return errors.Wrap(err, "stop")
}
containers, err = d.runtime.ListContainers(cruntime.ListOptions{})
if err != nil {
return errors.Wrap(err, "containers")
}
if len(containers) == 0 {
return nil
}
if err := d.runtime.KillContainers(containers); err != nil {
return errors.Wrap(err, "kill")
}
return nil
}
// Remove a host, including any data which may have been written by it.
func (d *Driver) Remove() error {
if err := d.Kill(); err != nil {
return errors.Wrap(err, "kill")
}
glog.Infof("Removing: %s", cleanupPaths)
args := append([]string{"rm", "-rf"}, cleanupPaths...)
if _, err := d.exec.RunCmd(exec.Command("sudo", args...)); err != nil {
glog.Errorf("cleanup incomplete: %v", err)
}
return nil
}
// Restart a host
func (d *Driver) Restart() error {
return restartKubelet(d.exec)
}
// Start a host
func (d *Driver) Start() error {
var err error
d.IPAddress, err = d.GetIP()
if err != nil {
return err
}
d.URL, err = d.GetURL()
if err != nil {
return err
}
return nil
}
// Stop a host gracefully, including any containers that we are managing.
func (d *Driver) Stop() error {
if err := sysinit.New(d.exec).Stop("kubelet"); err != nil {
glog.Warningf("couldn't stop kubelet. will continue with stop anyways: %v", err)
if err := sysinit.New(d.exec).ForceStop("kubelet"); err != nil {
glog.Warningf("couldn't force stop kubelet. will continue with stop anyways: %v", err)
}
}
containers, err := d.runtime.ListContainers(cruntime.ListOptions{})
if err != nil {
return errors.Wrap(err, "containers")
}
if len(containers) > 0 {
if err := d.runtime.StopContainers(containers); err != nil {
return errors.Wrap(err, "stop containers")
}
}
glog.Infof("none driver is stopped!")
return nil
}
// RunSSHCommandFromDriver implements direct ssh control to the driver
func (d *Driver) RunSSHCommandFromDriver() error {
return fmt.Errorf("driver does not support ssh commands")
}
// restartKubelet restarts the kubelet
func restartKubelet(cr command.Runner) error {
glog.Infof("restarting kubelet.service ...")
c := exec.Command("sudo", "systemctl", "restart", "kubelet.service")
if _, err := cr.RunCmd(c); err != nil {
return err
}
return nil
}