-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathguest.go
458 lines (396 loc) · 10.7 KB
/
guest.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package lochness
import (
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"math/rand"
"net"
"path/filepath"
log "github.com/Sirupsen/logrus"
"github.com/mistifyio/lochness/pkg/kv"
"github.com/pborman/uuid"
)
var (
// GuestPath is the path in the config store
GuestPath = "lochness/guests/"
)
type (
// Guest is a virtual machine
Guest struct {
context *Context
modifiedIndex uint64
ID string `json:"id"`
Metadata map[string]string `json:"metadata"`
Type string `json:"type"` // type of guest. currently just kvm
FlavorID string `json:"flavor"` // resource flavor
HypervisorID string `json:"hypervisor"` // hypervisor. may be blank if not assigned yet
NetworkID string `json:"network"`
SubnetID string `json:"subnet"`
FWGroupID string `json:"fwgroup"`
VLANGroupID string `json:"vlangroup"`
MAC net.HardwareAddr `json:"mac"`
IP net.IP `json:"ip"`
Bridge string `json:"bridge"`
}
// Guests is an alias to a slice of *Guest
Guests []*Guest
// guestJSON is used to ease json marshal/unmarshal
guestJSON struct {
ID string `json:"id"`
Metadata map[string]string `json:"metadata"`
Type string `json:"type"` // type of guest. currently just kvm
FlavorID string `json:"flavor"` // resource flavor
HypervisorID string `json:"hypervisor"` // hypervisor. may be blank if not assigned yet
NetworkID string `json:"network"`
SubnetID string `json:"subnet"`
FWGroupID string `json:"fwgroup"`
VLANGroupID string `json:"vlangroup"`
MAC string `json:"mac"`
IP net.IP `json:"ip"`
Bridge string `json:"bridge"`
}
// CandidateFunction is used to select hypervisors that can run the given guest.
CandidateFunction func(*Guest, Hypervisors) (Hypervisors, error)
)
// MarshalJSON is a helper for marshalling a Guest
func (g *Guest) MarshalJSON() ([]byte, error) {
data := guestJSON{
ID: g.ID,
Metadata: g.Metadata,
Type: g.Type,
FlavorID: g.FlavorID,
NetworkID: g.NetworkID,
SubnetID: g.SubnetID,
FWGroupID: g.FWGroupID,
VLANGroupID: g.VLANGroupID,
HypervisorID: g.HypervisorID,
IP: g.IP,
MAC: g.MAC.String(),
Bridge: g.Bridge,
}
return json.Marshal(data)
}
// UnmarshalJSON is a helper for unmarshalling a Guest
func (g *Guest) UnmarshalJSON(input []byte) error {
data := guestJSON{}
if err := json.Unmarshal(input, &data); err != nil {
return err
}
if data.ID != "" {
g.ID = data.ID
}
if data.Metadata != nil {
g.Metadata = data.Metadata
}
if data.Type != "" {
g.Type = data.Type
}
if data.FlavorID != "" {
g.FlavorID = data.FlavorID
}
if data.NetworkID != "" {
g.NetworkID = data.NetworkID
}
if data.SubnetID != "" {
g.SubnetID = data.SubnetID
}
if data.FWGroupID != "" {
g.FWGroupID = data.FWGroupID
}
if data.VLANGroupID != "" {
g.VLANGroupID = data.VLANGroupID
}
if data.HypervisorID != "" {
g.HypervisorID = data.HypervisorID
}
if data.IP != nil {
g.IP = data.IP
}
if data.Bridge != "" {
g.Bridge = data.Bridge
}
if data.MAC != "" {
a, err := net.ParseMAC(data.MAC)
if err != nil {
return err
}
g.MAC = a
}
return nil
}
// NewGuest create a new blank Guest
func (c *Context) NewGuest() *Guest {
g := &Guest{
context: c,
ID: uuid.New(),
Metadata: make(map[string]string),
}
// Generate a MAC based on the ID. May be overwritten later.
md5ID := md5.Sum([]byte(g.ID))
mac := fmt.Sprintf("02:%02x:%02x:%02x:%02x:%02x",
md5ID[0],
md5ID[1],
md5ID[2],
md5ID[3],
md5ID[4],
)
g.MAC, _ = net.ParseMAC(mac)
return g
}
// Guest fetches a Guest from the config store
func (c *Context) Guest(id string) (*Guest, error) {
var err error
id, err = canonicalizeUUID(id)
if err != nil {
return nil, err
}
g := &Guest{
context: c,
ID: id,
}
err = g.Refresh()
if err != nil {
return nil, err
}
return g, nil
}
// key is a helper to generate the config store key
func (g *Guest) key() string {
return filepath.Join(GuestPath, g.ID, "metadata")
}
// fromResponse is a helper to unmarshal a Guest
func (g *Guest) fromResponse(value kv.Value) error {
g.modifiedIndex = value.Index
return json.Unmarshal(value.Data, &g)
}
// Refresh reloads from the data store
func (g *Guest) Refresh() error {
resp, err := g.context.kv.Get(g.key())
if err != nil {
return err
}
return g.fromResponse(resp)
}
// Validate ensures a Guest has reasonable data.
func (g *Guest) Validate() error {
if _, err := canonicalizeUUID(g.ID); err != nil {
return errors.New("missing or invalid id")
}
if _, err := canonicalizeUUID(g.FlavorID); err != nil {
return errors.New("missing or invalid flavor")
}
if _, err := canonicalizeUUID(g.NetworkID); err != nil {
return errors.New("missing or invalid network")
}
if g.MAC == nil {
return errors.New("missing MAC")
}
return nil
}
// Save persists the Guest to the data store.
func (g *Guest) Save() error {
if err := g.Validate(); err != nil {
return err
}
v, err := json.Marshal(g)
if err != nil {
return err
}
index, err := g.context.kv.Update(g.key(), kv.Value{Data: v, Index: g.modifiedIndex})
if err != nil {
return err
}
g.modifiedIndex = index
return nil
}
// Destroy removes a guest
func (g *Guest) Destroy() error {
if g.modifiedIndex == 0 {
// it has not been saved?
return errors.New("not persisted")
}
if g.HypervisorID != "" {
hypervisor, err := g.context.Hypervisor(g.HypervisorID)
if err != nil {
return err
}
if err := hypervisor.RemoveGuest(g); err != nil {
return err
}
}
if err := g.context.kv.Remove(g.key(), g.modifiedIndex); err != nil {
return err
}
return g.context.kv.Delete(filepath.Join(GuestPath, g.ID), true)
}
// Candidates returns a list of Hypervisors that may run this Guest.
func (g *Guest) Candidates(f ...CandidateFunction) (Hypervisors, error) {
// this is not terribly efficient, but is fairly easy to understand
var hypervisors Hypervisors
_ = g.context.ForEachHypervisor(func(h *Hypervisor) error {
hypervisors = append(hypervisors, h)
return nil
})
if len(hypervisors) == 0 {
return nil, errors.New("no hypervisors")
}
for _, fn := range f {
hs, err := fn(g, hypervisors)
if err != nil {
return nil, err
}
hypervisors = hs
if len(hypervisors) == 0 {
return nil, errors.New("no suitable hypervisors")
}
}
return hypervisors, nil
}
// CandidateIsAlive returns Hypervisors that are "alive" based on heartbeat
func CandidateIsAlive(g *Guest, hs Hypervisors) (Hypervisors, error) {
logFields := log.Fields{
"guestID": g.ID,
"func": "CandidateIsAlive",
}
var hypervisors Hypervisors
for _, h := range hs {
if h.IsAlive() {
hypervisors = append(hypervisors, h)
} else {
log.WithFields(logFields).WithFields(log.Fields{
"hypervisorID": h.ID,
}).Debug("hypervisor candidate failed")
}
}
log.WithFields(logFields).WithFields(log.Fields{
"in": len(hs),
"out": len(hypervisors),
"removed": len(hs) - len(hypervisors),
}).Info("hypervisor candidates filtered")
return hypervisors, nil
}
// CandidateHasResources returns Hypervisors that have available resources
// based on the request Flavor of the Guest.
func CandidateHasResources(g *Guest, hs Hypervisors) (Hypervisors, error) {
logFields := log.Fields{
"guestID": g.ID,
"func": "CandidateHasResources",
}
f, err := g.context.Flavor(g.FlavorID)
if err != nil {
return nil, err
}
var hypervisors Hypervisors
for _, h := range hs {
avail := h.AvailableResources
if avail.Disk < f.Disk {
log.WithFields(logFields).WithFields(log.Fields{
"hypervisorID": h.ID,
"resource": "disk",
}).Debug("hypervisor candidate failed")
} else if avail.Memory < f.Memory {
log.WithFields(logFields).WithFields(log.Fields{
"hypervisorID": h.ID,
"resource": "memory",
}).Debug("hypervisor candidate failed")
} else if avail.CPU < f.CPU {
log.WithFields(logFields).WithFields(log.Fields{
"hypervisorID": h.ID,
"resource": "cpu",
}).Debug("hypervisor candidate failed")
} else {
hypervisors = append(hypervisors, h)
}
}
log.WithFields(logFields).WithFields(log.Fields{
"in": len(hs),
"out": len(hypervisors),
"removed": len(hs) - len(hypervisors),
}).Info("hypervisor candidates filtered")
return hypervisors, nil
}
// CandidateHasSubnet returns Hypervisors that have subnets with available addresses
// in the request Network of the Guest.
func CandidateHasSubnet(g *Guest, hs Hypervisors) (Hypervisors, error) {
logFields := log.Fields{
"guestID": g.ID,
"func": "CandidateHasSubnet",
}
n, err := g.context.Network(g.NetworkID)
if err != nil {
return nil, err
}
s := n.Subnets()
subnets := make(map[string]bool, len(s))
var hypervisors Hypervisors
for _, k := range s {
subnet, err := g.context.Subnet(k)
if err != nil {
return nil, err
}
// only include subnets that have available addresses
avail := subnet.AvailableAddresses()
if len(avail) > 0 {
subnets[k] = true
}
}
for _, h := range hs {
hasSubnet := false
for k := range h.Subnets() {
if _, ok := subnets[k]; ok {
hasSubnet = true
break
}
}
if hasSubnet {
hypervisors = append(hypervisors, h)
} else {
log.WithFields(logFields).WithFields(log.Fields{
"hypervisorID": h.ID,
}).Debug("hypervisor candidate failed")
}
}
log.WithFields(logFields).WithFields(log.Fields{
"in": len(hs),
"out": len(hypervisors),
"removed": len(hs) - len(hypervisors),
}).Info("hypervisor candidates filtered")
return hypervisors, nil
}
// CandidateRandomize shuffles the list of Hypervisors.
func CandidateRandomize(g *Guest, hs Hypervisors) (Hypervisors, error) {
return randomizeHypervisors(hs), nil
}
// based on code found on stackoverflow(?)
func randomizeHypervisors(s Hypervisors) Hypervisors {
for i := range s {
j := rand.Intn(i + 1)
s[i], s[j] = s[j], s[i]
}
return s
}
// DefaultCandidateFunctions is a default list of CandidateFunctions for general use
var DefaultCandidateFunctions = []CandidateFunction{
CandidateIsAlive,
CandidateHasSubnet,
CandidateHasResources,
CandidateRandomize,
}
// ForEachGuest will run f on each Guest. It will stop iteration if f returns an error.
func (c *Context) ForEachGuest(f func(*Guest) error) error {
keys, err := c.kv.Keys(GuestPath)
if err != nil {
return err
}
for _, k := range keys {
g, err := c.Guest(filepath.Base(k))
if err != nil {
return err
}
if err := f(g); err != nil {
return err
}
}
return nil
}