-
Notifications
You must be signed in to change notification settings - Fork 56
/
client.go
564 lines (462 loc) · 16.1 KB
/
client.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package client
import (
"encoding/xml"
"fmt"
"github.com/golang/glog"
libvirt "github.com/libvirt/libvirt-go"
libvirtxml "github.com/libvirt/libvirt-go-xml"
providerconfigv1 "github.com/openshift/cluster-api-provider-libvirt/pkg/apis/libvirtproviderconfig/v1beta1"
"k8s.io/client-go/kubernetes"
)
//go:generate mockgen -source=./client.go -destination=./mock/client_generated.go -package=mock
// CreateDomainInput specifies input parameters for CreateDomain operation
type CreateDomainInput struct {
// DomainName is name of domain to be created
DomainName string
// IgnKey is name of existing volume with ignition config (DEPRECATED)
IgnKey string
// Ignition configuration to be injected during bootstrapping
Ignition *providerconfigv1.Ignition
// CloudInit configuration to be run during bootstrapping
CloudInit *providerconfigv1.CloudInit
// VolumeName of volume to be added to domain definition
VolumeName string
// CloudInitVolumeName of cloud init volume to be added to domain definition
CloudInitVolumeName string
// IgnitionVolumeName of ignition volume to be added to domain definition
IgnitionVolumeName string
// NetworkInterfaceName as name of network interface
NetworkInterfaceName string
// NetworkInterfaceAddress as address of network interface
NetworkInterfaceAddress string
// HostName as network interface hostname
HostName string
// AddressRange as IP subnet address range
ReservedLeases *Leases
// Autostart as domain autostart
Autostart bool
// DomainMemory allocated for running domain
DomainMemory int
// DomainVcpu allocated for running domain
DomainVcpu int
// KubeClient as kubernetes client
KubeClient kubernetes.Interface
// MachineNamespace with machine object
MachineNamespace string
}
// CreateVolumeInput specifies input parameters for CreateVolume operation
type CreateVolumeInput struct {
// VolumeName to be created
VolumeName string
// BaseVolumeName as name of the base volume
BaseVolumeName string
// Source as location of base volume
Source string
// VolumeFormat as volume format
VolumeFormat string
}
// LibvirtClientBuilderFuncType is function type for building aws client
type LibvirtClientBuilderFuncType func(URI string, poolName string) (Client, error)
// Client is a wrapper object for actual libvirt library to allow for easier testing.
type Client interface {
// Close closes the client's libvirt connection.
Close() error
// CreateDomain creates domain based on CreateDomainInput
CreateDomain(CreateDomainInput) error
// DeleteDomain deletes a domain
DeleteDomain(name string) error
// DomainExists checks if domain exists
DomainExists(name string) (bool, error)
// LookupDomainByName looks up a domain based on its name
LookupDomainByName(name string) (*libvirt.Domain, error)
// CreateVolume creates volume based on CreateVolumeInput
CreateVolume(CreateVolumeInput) error
// VolumeExists checks if volume exists
VolumeExists(name string) (bool, error)
// DeleteVolume deletes a domain based on its name
DeleteVolume(name string) error
// GetDHCPLeasesByNetwork get all network DHCP leases by network name
GetDHCPLeasesByNetwork(networkName string) ([]libvirt.NetworkDHCPLease, error)
// LookupDomainHostnameByDHCPLease looks up a domain hostname based on its DHCP lease
LookupDomainHostnameByDHCPLease(domIPAddress string, networkName string) (string, error)
}
type libvirtClient struct {
connection *libvirt.Connect
// storage pool that holds all volumes
pool *libvirt.StoragePool
// cache pool's name so we don't have to call failable GetName() method on pool all the time.
poolName string
}
var _ Client = &libvirtClient{}
// NewClient returns libvirt client for the specified URI
func NewClient(URI string, poolName string) (Client, error) {
connection, err := libvirt.NewConnect(URI)
if err != nil {
return nil, err
}
glog.Infof("Created libvirt connection: %p", connection)
pool, err := connection.LookupStoragePoolByName(poolName)
if err != nil {
return nil, fmt.Errorf("can't find storage pool %q: %v", poolName, err)
}
return &libvirtClient{
connection: connection,
pool: pool,
poolName: poolName,
}, nil
}
// Close closes the client's libvirt connection.
func (client *libvirtClient) Close() error {
glog.Infof("Closing libvirt connection: %p", client.connection)
_, err := client.connection.Close()
if err != nil {
glog.Infof("Error closing libvirt connection: %v", err)
}
return err
}
// CreateDomain creates domain based on CreateDomainInput
func (client *libvirtClient) CreateDomain(input CreateDomainInput) error {
if input.DomainName == "" {
return fmt.Errorf("Failed to create domain, name is empty")
}
glog.Info("Create resource libvirt_domain")
// Get default values from Host
domainDef, err := newDomainDefForConnection(client.connection)
if err != nil {
return fmt.Errorf("Failed to newDomainDefForConnection: %s", err)
}
// Get values from machineProviderConfig
if err := domainDefInit(&domainDef, input.DomainName, input.DomainMemory, input.DomainVcpu); err != nil {
return fmt.Errorf("Failed to init domain definition from machineProviderConfig: %v", err)
}
glog.Info("Create ignition configuration")
if input.Ignition != nil {
if err := setIgnition(&domainDef, client, input.Ignition, input.KubeClient, input.MachineNamespace, input.IgnitionVolumeName); err != nil {
return err
}
} else if input.IgnKey != "" {
ignVolume, err := client.getVolume(input.IgnKey)
if err != nil {
return fmt.Errorf("error getting ignition volume: %v", err)
}
ignVolumePath, err := ignVolume.GetPath()
if err != nil {
return fmt.Errorf("error getting ignition volume path: %v", err)
}
if err := setCoreOSIgnition(&domainDef, ignVolumePath); err != nil {
return err
}
} else if input.CloudInit != nil {
if err := setCloudInit(&domainDef, client, input.CloudInit, input.KubeClient, input.MachineNamespace, input.CloudInitVolumeName, input.DomainName); err != nil {
return err
}
} else {
return fmt.Errorf("machine does not has a IgnKey nor CloudInit value")
}
glog.Info("Create volume")
diskVolume, err := client.getVolume(input.VolumeName)
if err != nil {
return fmt.Errorf("can't retrieve volume %s for pool %s: %v", input.VolumeName, client.poolName, err)
}
if err := setDisks(&domainDef, diskVolume); err != nil {
return fmt.Errorf("Failed to setDisks: %s", err)
}
glog.Info("Set up network interface")
var waitForLeases []*libvirtxml.DomainInterface
hostName := input.HostName
if hostName == "" {
hostName = input.DomainName
}
// TODO: support more than 1 interface
partialNetIfaces := make(map[string]*pendingMapping, 1)
if err := setNetworkInterfaces(
&domainDef,
client.connection,
partialNetIfaces,
&waitForLeases,
hostName,
input.NetworkInterfaceName,
input.NetworkInterfaceAddress,
input.ReservedLeases,
); err != nil {
return err
}
// TODO: support setFilesystems
//if err := setFilesystems(d, &domainDef); err != nil {
// return err
//}
connectURI, err := client.connection.GetURI()
if err != nil {
return fmt.Errorf("error retrieving libvirt connection URI: %v", err)
}
glog.Infof("Creating libvirt domain at %s", connectURI)
data, err := xmlMarshallIndented(domainDef)
if err != nil {
return fmt.Errorf("error serializing libvirt domain: %v", err)
}
glog.Infof("Creating libvirt domain with XML:\n%s", data)
domain, err := client.connection.DomainDefineXML(data)
if err != nil {
return fmt.Errorf("error defining libvirt domain: %v", err)
}
if err := domain.SetAutostart(input.Autostart); err != nil {
return fmt.Errorf("error setting Autostart: %v", err)
}
err = domain.Create()
if err != nil {
return fmt.Errorf("error creating libvirt domain: %v", err)
}
defer domain.Free()
id, err := domain.GetUUIDString()
if err != nil {
return fmt.Errorf("error retrieving libvirt domain id: %v", err)
}
glog.Infof("Domain ID: %s", id)
return nil
}
// LookupDomainByName looks up a domain by name and returns a pointer to it.
// Note: The caller is responsible for freeing the returned domain.
func (client *libvirtClient) LookupDomainByName(name string) (*libvirt.Domain, error) {
glog.Infof("Lookup domain by name: %q", name)
if client.connection == nil {
return nil, ErrLibVirtConIsNil
}
domain, err := client.connection.LookupDomainByName(name)
if err != nil {
return nil, err
}
return domain, nil
}
// DomainExists checks if domain exists
func (client *libvirtClient) DomainExists(name string) (bool, error) {
glog.Infof("Check if %q domain exists", name)
if client.connection == nil {
return false, ErrLibVirtConIsNil
}
domain, err := client.connection.LookupDomainByName(name)
if err != nil {
if err.(libvirt.Error).Code == libvirt.ERR_NO_DOMAIN {
return false, nil
}
return false, err
}
defer domain.Free()
return true, nil
}
// DeleteDomain deletes a domain
func (client *libvirtClient) DeleteDomain(name string) error {
exists, err := client.DomainExists(name)
if err != nil {
return err
}
if !exists {
return ErrDomainNotFound
}
if client.connection == nil {
return ErrLibVirtConIsNil
}
glog.Infof("Deleting domain %s", name)
domain, err := client.connection.LookupDomainByName(name)
if err != nil {
return fmt.Errorf("Error retrieving libvirt domain: %s", err)
}
defer domain.Free()
state, _, err := domain.GetState()
if err != nil {
return fmt.Errorf("Couldn't get info about domain: %s", err)
}
if state == libvirt.DOMAIN_RUNNING || state == libvirt.DOMAIN_PAUSED {
if err := domain.Destroy(); err != nil {
return fmt.Errorf("Couldn't destroy libvirt domain: %s", err)
}
}
if err := domain.UndefineFlags(libvirt.DOMAIN_UNDEFINE_NVRAM); err != nil {
if e := err.(libvirt.Error); e.Code == libvirt.ERR_NO_SUPPORT || e.Code == libvirt.ERR_INVALID_ARG {
glog.Info("libvirt does not support undefine flags: will try again without flags")
if err := domain.Undefine(); err != nil {
return fmt.Errorf("couldn't undefine libvirt domain: %v", err)
}
} else {
return fmt.Errorf("couldn't undefine libvirt domain with flags: %v", err)
}
}
return nil
}
// CreateVolume creates volume based on CreateVolumeInput
func (client *libvirtClient) CreateVolume(input CreateVolumeInput) error {
var volume *libvirt.StorageVol
glog.Infof("Create a libvirt volume with name %s for pool %s from the base volume %s", input.VolumeName, client.poolName, input.BaseVolumeName)
// TODO: lock pool
//client.poolMutexKV.Lock(client.poolName)
//defer client.poolMutexKV.Unlock(client.poolName)
volume, err := client.getVolume(input.VolumeName)
if err == nil {
return fmt.Errorf("storage volume '%s' already exists", input.VolumeName)
}
volumeDef := newDefVolume(input.VolumeName)
volumeDef.Target.Format.Type = input.VolumeFormat
var img image
// an source image was given, this mean we can't choose size
if input.Source != "" {
if input.BaseVolumeName != "" {
return fmt.Errorf("'base_volume_name' can't be specified when also 'source' is given")
}
if img, err = newImage(input.Source); err != nil {
return err
}
// update the image in the description, even if the file has not changed
size, err := img.size()
if err != nil {
return err
}
glog.Infof("Image %s image is: %d bytes", img, size)
volumeDef.Capacity.Unit = "B"
volumeDef.Capacity.Value = size
} else if input.BaseVolumeName != "" {
volume = nil
baseVolume, err := client.getVolume(input.BaseVolumeName)
if err != nil {
return fmt.Errorf("Can't retrieve volume %s", input.BaseVolumeName)
}
var baseVolumeInfo *libvirt.StorageVolInfo
baseVolumeInfo, err = baseVolume.GetInfo()
if err != nil {
return fmt.Errorf("Can't retrieve volume info %s", input.BaseVolumeName)
}
if baseVolumeInfo.Capacity > uint64(defaultSize) {
volumeDef.Capacity.Value = baseVolumeInfo.Capacity
} else {
volumeDef.Capacity.Value = uint64(defaultSize)
}
backingStoreDef, err := newDefBackingStoreFromLibvirt(baseVolume)
if err != nil {
return fmt.Errorf("Could not retrieve backing store %s", input.BaseVolumeName)
}
volumeDef.BackingStore = &backingStoreDef
}
if volume == nil {
volumeDefXML, err := xml.Marshal(volumeDef)
if err != nil {
return fmt.Errorf("Error serializing libvirt volume: %s", err)
}
// create the volume
// Refresh the pool of the volume so that libvirt knows it is
// not longer in use.
waitForSuccess("error refreshing pool for volume", func() error {
return client.pool.Refresh(0)
})
if err != nil {
return fmt.Errorf("can't find storage pool '%s'", client.poolName)
}
v, err := client.pool.StorageVolCreateXML(string(volumeDefXML), 0)
if err != nil {
return fmt.Errorf("Error creating libvirt volume: %s", err)
}
volume = v
defer volume.Free()
}
// we use the key as the id
key, err := volume.GetKey()
if err != nil {
return fmt.Errorf("Error retrieving volume key: %s", err)
}
if input.Source != "" {
err = img.importImage(newCopier(client.connection, volume, volumeDef.Capacity.Value), volumeDef)
if err != nil {
return fmt.Errorf("Error while uploading source %s: %s", img.string(), err)
}
}
glog.Infof("Volume ID: %s", key)
return nil
}
// VolumeExists checks if a volume exists
func (client *libvirtClient) VolumeExists(name string) (bool, error) {
glog.Infof("Check if %q volume exists", name)
if client.connection == nil {
return false, ErrLibVirtConIsNil
}
volume, err := client.getVolume(name)
if err != nil {
return false, nil
}
volume.Free()
return true, nil
}
func (client *libvirtClient) getVolume(volumeName string) (*libvirt.StorageVol, error) {
// Check whether the storage volume exists. Its name needs to be
// unique.
volume, err := client.pool.LookupStorageVolByName(volumeName)
if err != nil {
// Let's try by ID in case of older Installer
volume, err = client.connection.LookupStorageVolByKey(volumeName)
if err != nil {
return nil, fmt.Errorf("can't retrieve volume %q: %v", volumeName, err)
}
}
return volume, nil
}
// DeleteVolume deletes a domain based on its name
func (client *libvirtClient) DeleteVolume(name string) error {
exists, err := client.VolumeExists(name)
if err != nil {
return err
}
if !exists {
glog.Infof("Volume %s does not exists", name)
return ErrVolumeNotFound
}
glog.Infof("Deleting volume %s", name)
volume, err := client.getVolume(name)
if err != nil {
return fmt.Errorf("Can't retrieve volume %s", name)
}
defer volume.Free()
// Refresh the pool of the volume so that libvirt knows it is
// not longer in use.
volPool, err := volume.LookupPoolByVolume()
if err != nil {
return fmt.Errorf("Error retrieving pool for volume: %s", err)
}
defer volPool.Free()
// TODO: add locking support
//client.poolMutexKV.Lock(client.poolName)
//defer client.poolMutexKV.Unlock(client.poolName)
waitForSuccess("Error refreshing pool for volume", func() error {
return volPool.Refresh(0)
})
// Workaround for redhat#1293804
// https://bugzilla.redhat.com/show_bug.cgi?id=1293804#c12
// Does not solve the problem but it makes it happen less often.
_, err = volume.GetXMLDesc(0)
if err != nil {
return fmt.Errorf("Can't retrieve volume %s XML desc: %s", name, err)
}
err = volume.Delete(0)
if err != nil {
return fmt.Errorf("Can't delete volume %s: %s", name, err)
}
return nil
}
// GetDHCPLeasesByNetwork returns all network DHCP leases by network name
func (client *libvirtClient) GetDHCPLeasesByNetwork(networkName string) ([]libvirt.NetworkDHCPLease, error) {
network, err := client.connection.LookupNetworkByName(networkName)
if err != nil {
glog.Errorf("Failed to fetch network %s from the libvirt", networkName)
return nil, err
}
return network.GetDHCPLeases()
}
// LookupDomainHostnameByDHCPLease looks up a domain hostname based on its DHCP lease
func (client *libvirtClient) LookupDomainHostnameByDHCPLease(domIPAddress string, networkName string) (string, error) {
dchpLeases, err := client.GetDHCPLeasesByNetwork(networkName)
if err != nil {
glog.Errorf("Failed to fetch dhcp leases for the network %s", networkName)
return "", err
}
for _, lease := range dchpLeases {
if lease.IPaddr == domIPAddress {
return lease.Hostname, nil
}
}
return "", fmt.Errorf("Failed to find hostname for the DHCP lease with IP %s", domIPAddress)
}