-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
363 lines (297 loc) · 7.13 KB
/
node.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
package kuttilib
import (
"encoding/json"
"fmt"
"time"
"github.com/kuttiproject/kuttilog"
"github.com/kuttiproject/drivercore"
)
// NodeStatus represents the status of a Node.
type NodeStatus drivercore.MachineStatus
// The NodeStatus* constants define valid node statuses.
const (
NodeStatusError = NodeStatus(drivercore.MachineStatusError)
NodeStatusUnknown = NodeStatus(drivercore.MachineStatusUnknown)
NodeStatusStopped = NodeStatus(drivercore.MachineStatusStopped)
NodeStatusRunning = NodeStatus(drivercore.MachineStatusRunning)
)
// nodedata is a data-only representation of the Node type,
// used for serialization and output.
type nodedata struct {
ClusterName string
Name string
CreatedAt time.Time
Type string
Ports map[int]int
}
// Node represents a node in a Kubernetes cluster.
//
// The associated Cluster's Driver ensures that an appropriate
// host is created and manipulated per node.
type Node struct {
cluster *Cluster
clusterName string
name string
createdAt time.Time
nodetype string
host drivercore.Machine
//status string
ports map[int]int
}
// Name returns the name of the node.
func (n *Node) Name() string {
return n.name
}
// Type returns the type of this node.
func (n *Node) Type() string {
return n.nodetype
}
// CreatedAt returns the time this node was created.
func (n *Node) CreatedAt() time.Time {
return n.createdAt
}
// Ports returns the node port to host port
// mappings of this node.
func (n *Node) Ports() map[int]int {
return n.ports
}
// IPAddress returns the IP address of the node if it is running,
// or an empty string if not.
func (n *Node) IPAddress() string {
err := n.ensurehost()
if err != nil {
return ""
}
return n.host.IPAddress()
}
// SSHAddress returns the address to SSH into the node.
// The return value is in "HOST:PORT" format if the node
// is running, or an empty string if not. If the Driver
// uses NAT networking, the host is "localhost".
func (n *Node) SSHAddress() string {
if n.Cluster().Driver().UsesNATNetworking() {
port, ok := n.Ports()[22]
if !ok {
return ""
}
return fmt.Sprintf("localhost:%v", port)
}
err := n.ensurehost()
if err != nil {
return ""
}
return n.host.SSHAddress()
}
// Cluster returns the Cluster this node belongs to.
func (n *Node) Cluster() *Cluster {
if n.cluster == nil {
n.cluster = config.Clusters[n.clusterName]
n.cluster.ensuredriver()
}
return n.cluster
}
// Status returns the current status of this node.
func (n *Node) Status() NodeStatus {
err := n.ensurehost()
if err != nil {
return NodeStatusError
}
return NodeStatus(n.host.Status())
}
// Start starts this node.
func (n *Node) Start() error {
err := n.ensurehost()
if err != nil {
return err
}
if n.Status() == NodeStatusStopped {
err = n.host.Start()
if err != nil {
return err
}
n.host.WaitForStateChange(25)
return nil
}
return errNodeCannotStart
}
// ForceStart tries to forcibly start this node.
// It does not check the current status before doing so.
func (n *Node) ForceStart() error {
err := n.ensurehost()
if err != nil {
return err
}
err = n.host.Start()
if err != nil {
return err
}
// TODO: Consider moving this wait, or standardize the duration
kuttilog.Print(kuttilog.Info, "Waiting for node to start...")
n.host.WaitForStateChange(25)
kuttilog.Println(kuttilog.Info, "Done.")
return nil
}
// Stop stops this node gracefully.
func (n *Node) Stop() error {
err := n.ensurehost()
if err != nil {
return err
}
if n.Status() == NodeStatusRunning {
err = n.host.Stop()
if err != nil {
return err
}
n.host.WaitForStateChange(25)
return nil
}
return errNodeCannotStop
}
// ForceStop tries to forcibly stop this node.
// It does not check the current status before doing so.
func (n *Node) ForceStop() error {
err := n.ensurehost()
if err != nil {
return err
}
err = n.host.ForceStop()
if err != nil {
return err
}
// TODO: Consider moving this wait, or standardize the duration
kuttilog.Print(kuttilog.Info, "Waiting for node to stop...")
n.host.WaitForStateChange(25)
kuttilog.Println(kuttilog.Info, "Done.")
return nil
}
// ForwardSSHPort forwards the node's SSH port to the specified host
// port.
func (n *Node) ForwardSSHPort(hostport int) error {
return n.ForwardPort(hostport, 22)
}
// ForwardPort forwards a port of the node to the specified host port.
func (n *Node) ForwardPort(hostport int, nodeport int) error {
err := n.Cluster().ensuredriver()
if err != nil {
return err
}
if !n.Cluster().driver.UsesNATNetworking() {
return errPortForwardNotSupported
}
if !ValidPort(nodeport) {
return errPortNodePortInvalid
}
if !ValidPort(hostport) {
return errPortHostPortInvalid
}
err = n.ensurehost()
if err != nil {
return err
}
err = n.CheckHostPort(hostport)
if err != nil {
return err
}
_, ok := n.ports[nodeport]
if ok {
return errPortNodePortInUse
}
err = n.host.ForwardPort(hostport, nodeport)
if err != nil {
return err
}
n.ports[nodeport] = hostport
return clusterconfigmanager.Save()
}
// UnforwardPort removes any mapping of the specified node port.
func (n *Node) UnforwardPort(nodeport int) error {
cluster := n.Cluster()
err := cluster.ensuredriver()
if err != nil {
return err
}
if !cluster.driver.UsesNATNetworking() {
return errPortForwardNotSupported
}
if !ValidPort(nodeport) {
return errPortNodePortInvalid
}
if nodeport == 22 {
return errPortCannotUnmap
}
_, ok := n.ports[nodeport]
if !ok {
return errPortNotForwarded
}
err = n.ensurehost()
if err != nil {
return err
}
err = n.host.UnforwardPort(nodeport)
if err != nil {
return err
}
delete(n.ports, nodeport)
return clusterconfigmanager.Save()
}
// CheckHostPort checks if a host port is occupied in the current cluster.
func (n *Node) CheckHostPort(hostport int) error {
c := n.Cluster()
return c.CheckHostPort(hostport)
}
// MarshalJSON returns the JSON encoding of the node.
func (n *Node) MarshalJSON() ([]byte, error) {
utcloc, _ := time.LoadLocation("UTC")
savedata := nodedata{
ClusterName: n.clusterName,
Name: n.name,
CreatedAt: n.createdAt.In(utcloc),
Type: n.nodetype,
Ports: n.ports,
}
return json.Marshal(savedata)
}
// UnmarshalJSON parses and restores a JSON-encoded
// node.
func (n *Node) UnmarshalJSON(b []byte) error {
var loaddata nodedata
err := json.Unmarshal(b, &loaddata)
if err != nil {
return err
}
localloc, _ := time.LoadLocation("Local")
n.clusterName = loaddata.ClusterName
n.name = loaddata.Name
n.createdAt = loaddata.CreatedAt.In(localloc)
n.nodetype = loaddata.Type
n.ports = loaddata.Ports
return nil
}
func (n *Node) createhost() error {
c := n.Cluster()
driverimage, err := c.driver.GetImage(c.k8sVersion)
if err != nil {
return err
}
if driverimage.Deprecated() {
return errVersionDeprecated
}
host, err := c.driver.NewMachine(n.name, c.name, c.k8sVersion)
if err != nil {
n.host = nil
return err
}
n.host = host
return nil
}
func (n *Node) ensurehost() error {
if n.host == nil {
c := n.Cluster()
host, err := c.driver.GetMachine(n.name, c.name)
if err != nil {
return err
}
n.host = host
}
return nil
}