-
Notifications
You must be signed in to change notification settings - Fork 33
/
virtualmachine_pool.go
595 lines (511 loc) · 21.3 KB
/
virtualmachine_pool.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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/*
Copyright 2019 The KubeMacPool Authors.
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 pool_manager
import (
"context"
"fmt"
"net"
"strings"
"time"
"github.com/go-logr/logr"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
kubevirt "kubevirt.io/api/core/v1"
"github.com/k8snetworkplumbingwg/kubemacpool/pkg/gauges"
)
func (p *PoolManager) AllocateVirtualMachineMac(virtualMachine *kubevirt.VirtualMachine, transactionTimestamp *time.Time, isNotDryRun bool, parentLogger logr.Logger) error {
p.poolMutex.Lock()
defer p.poolMutex.Unlock()
logger := parentLogger.WithName("AllocateVirtualMachineMac")
if len(virtualMachine.Spec.Template.Spec.Domain.Devices.Interfaces) == 0 {
logger.Info("no interfaces found for virtual machine, skipping mac allocation", "virtualMachine", virtualMachine)
return nil
}
// We can't allow for duplicate interfaces names, as interface.Name is macMap's key.
if isNotDryRun {
if err := checkVmForInterfaceDuplication(virtualMachine); err != nil {
return err
}
}
vmFullName := VmNamespaced(virtualMachine)
if len(getVirtualMachineNetworks(virtualMachine)) == 0 {
logger.Info("no networks found for virtual machine, skipping mac allocation",
"vmFullName", vmFullName)
return nil
}
networks := map[string]kubevirt.Network{}
for _, network := range getVirtualMachineNetworks(virtualMachine) {
networks[network.Name] = network
}
logger.V(1).Info("data before update", "macPoolMap", p.macPoolMap, "requestInterfaces", virtualMachine.Spec.Template.Spec.Domain.Devices.Interfaces)
copyVM := virtualMachine.DeepCopy()
newAllocations := map[string]string{}
for idx, iface := range copyVM.Spec.Template.Spec.Domain.Devices.Interfaces {
if iface.Masquerade == nil && iface.Slirp == nil && networks[iface.Name].Multus == nil {
logger.Info("mac address can be set only for interface of type masquerade and slirp on the pod network")
continue
}
if iface.MacAddress != "" {
if err := p.allocateRequestedVirtualMachineInterfaceMac(vmFullName, iface, isNotDryRun, logger); err != nil {
p.revertAllocationOnVm(vmFullName, newAllocations, isNotDryRun)
return err
}
newAllocations[iface.Name] = iface.MacAddress
} else {
macAddr, err := p.allocateFromPoolForVirtualMachine(vmFullName, iface, isNotDryRun, logger)
if err != nil {
p.revertAllocationOnVm(vmFullName, newAllocations, isNotDryRun)
return err
}
copyVM.Spec.Template.Spec.Domain.Devices.Interfaces[idx].MacAddress = macAddr
newAllocations[iface.Name] = macAddr
}
}
if isNotDryRun {
p.macPoolMap.updateMacTransactionTimestampForUpdatedMacs(vmFullName, transactionTimestamp, newAllocations)
}
virtualMachine.Spec.Template.Spec.Domain.Devices.Interfaces = copyVM.Spec.Template.Spec.Domain.Devices.Interfaces
logger.Info("data after allocation", "Allocations", newAllocations, "updated vm Interfaces", virtualMachine.Spec.Template.Spec.Domain.Devices.Interfaces)
return nil
}
func (p *PoolManager) ReleaseAllVirtualMachineMacs(vmFullName string, parentLogger logr.Logger) error {
logger := parentLogger.WithName("ReleaseAllVirtualMachineMacs")
p.poolMutex.Lock()
defer p.poolMutex.Unlock()
logger.V(1).Info("data", "macmap", p.macPoolMap)
vmMacMap, err := p.macPoolMap.filterInByInstanceName(vmFullName)
if err != nil {
return errors.Wrapf(err, "Failed to get VmMacMap for vm %s", vmFullName)
}
for macAddress := range *vmMacMap {
delete(p.macPoolMap, macAddress)
}
logger.Info("released macs in virtual machine", "vmMacMap", vmMacMap)
logger.V(1).Info("macmap after release", "macmap", p.macPoolMap)
return nil
}
func (p *PoolManager) UpdateMacAddressesForVirtualMachine(previousVirtualMachine, virtualMachine *kubevirt.VirtualMachine, transactionTimestamp *time.Time, isNotDryRun bool, parentLogger logr.Logger) error {
logger := parentLogger.WithName("UpdateMacAddressesForVirtualMachine")
p.poolMutex.Lock()
if previousVirtualMachine == nil {
p.poolMutex.Unlock()
return p.AllocateVirtualMachineMac(virtualMachine, transactionTimestamp, isNotDryRun, logger)
}
defer p.poolMutex.Unlock()
// We can't allow for duplicate interfaces names, as interface.Name is macMap's key.
if isNotDryRun {
if err := checkVmForInterfaceDuplication(virtualMachine); err != nil {
return err
}
}
currentInterfaces := getVirtualMachineInterfaces(previousVirtualMachine)
requestInterfaces := getVirtualMachineInterfaces(virtualMachine)
logger.V(1).Info("data before update", "macPoolMap", p.macPoolMap, "currentInterfaces", currentInterfaces, "requestInterfaces", requestInterfaces)
currentInterfacesMap := make(map[string]string)
// This map is for deltas
deltaInterfacesMap := make(map[string]string)
for _, iface := range currentInterfaces {
currentInterfacesMap[iface.Name] = iface.MacAddress
deltaInterfacesMap[iface.Name] = iface.MacAddress
}
vmFullName := VmNamespaced(virtualMachine)
copyVM := virtualMachine.DeepCopy()
newAllocations := map[string]string{}
releaseOldAllocations := map[string]string{}
for idx, requestIface := range requestInterfaces {
currentlyAllocatedMacAddress, ifaceExistsInCurrentInterfaces := currentInterfacesMap[requestIface.Name]
if ifaceExistsInCurrentInterfaces {
if requestIface.MacAddress == "" {
copyVM.Spec.Template.Spec.Domain.Devices.Interfaces[idx].MacAddress = currentlyAllocatedMacAddress
newAllocations[requestIface.Name] = currentlyAllocatedMacAddress
} else if NewMacKey(requestIface.MacAddress).String() != currentlyAllocatedMacAddress {
// Specific mac address was requested
err := p.allocateRequestedVirtualMachineInterfaceMac(vmFullName, requestIface, isNotDryRun, logger)
if err != nil {
p.revertAllocationOnVm(vmFullName, newAllocations, isNotDryRun)
return err
}
releaseOldAllocations[requestIface.Name] = currentlyAllocatedMacAddress
newAllocations[requestIface.Name] = requestIface.MacAddress
}
delete(deltaInterfacesMap, requestIface.Name)
} else {
if requestIface.MacAddress != "" {
if err := p.allocateRequestedVirtualMachineInterfaceMac(vmFullName, requestIface, isNotDryRun, logger); err != nil {
p.revertAllocationOnVm(vmFullName, newAllocations, isNotDryRun)
return err
}
newAllocations[requestIface.Name] = requestIface.MacAddress
} else {
macAddr, err := p.allocateFromPoolForVirtualMachine(vmFullName, requestIface, isNotDryRun, logger)
if err != nil {
p.revertAllocationOnVm(vmFullName, newAllocations, isNotDryRun)
return err
}
copyVM.Spec.Template.Spec.Domain.Devices.Interfaces[idx].MacAddress = macAddr
newAllocations[requestIface.Name] = macAddr
}
}
}
logger.Info("updating updated mac's transaction timestamp", "newAllocations", newAllocations, "deltaInterfacesMap", deltaInterfacesMap, "releaseOldAllocations", releaseOldAllocations)
if isNotDryRun {
p.macPoolMap.updateMacTransactionTimestampForUpdatedMacs(vmFullName, transactionTimestamp, newAllocations)
p.macPoolMap.updateMacTransactionTimestampForUpdatedMacs(vmFullName, transactionTimestamp, deltaInterfacesMap)
p.macPoolMap.updateMacTransactionTimestampForUpdatedMacs(vmFullName, transactionTimestamp, releaseOldAllocations)
}
virtualMachine.Spec.Template.Spec.Domain.Devices.Interfaces = getVirtualMachineInterfaces(copyVM)
logger.V(1).Info("data after update", "macmap", p.macPoolMap, "updated interfaces", getVirtualMachineInterfaces(virtualMachine))
return nil
}
func getVirtualMachineInterfaces(virtualMachine *kubevirt.VirtualMachine) []kubevirt.Interface {
return virtualMachine.Spec.Template.Spec.Domain.Devices.Interfaces
}
func getVirtualMachineNetworks(virtualMachine *kubevirt.VirtualMachine) []kubevirt.Network {
return virtualMachine.Spec.Template.Spec.Networks
}
func checkVmForInterfaceDuplication(virtualMachine *kubevirt.VirtualMachine) error {
vmInterfaces := getVirtualMachineInterfaces(virtualMachine)
tmpInterfaceMap := map[string]struct{}{}
for _, vmInterface := range vmInterfaces {
if _, exists := tmpInterfaceMap[vmInterface.Name]; exists {
return fmt.Errorf("Failed to mutate virtual machine: every interface must have a unique name")
}
tmpInterfaceMap[vmInterface.Name] = struct{}{}
}
return nil
}
func (p *PoolManager) allocateFromPoolForVirtualMachine(vmFullName string, iface kubevirt.Interface, isNotDryRun bool, parentLogger logr.Logger) (string, error) {
logger := parentLogger.WithName("allocateFromPoolForVirtualMachine")
macAddr, err := p.getFreeMac()
if err != nil {
return "", err
}
if isNotDryRun {
p.macPoolMap.createOrUpdateEntry(macAddr.String(), vmFullName, iface.Name)
}
logger.V(1).Info("mac from pool was allocated for virtual machine", "allocatedMac", macAddr.String())
return macAddr.String(), nil
}
func (p *PoolManager) allocateRequestedVirtualMachineInterfaceMac(vmFullName string, iface kubevirt.Interface, isNotDryRun bool, parentLogger logr.Logger) error {
logger := parentLogger.WithName("allocateRequestedVirtualMachineInterfaceMac")
requestedMac := iface.MacAddress
if _, err := net.ParseMAC(requestedMac); err != nil {
return err
}
if macEntry, exist := p.macPoolMap[NewMacKey(requestedMac)]; exist {
if !macAlreadyBelongsToVmAndInterface(vmFullName, iface.Name, macEntry) {
err := fmt.Errorf("failed to allocate requested mac address")
logger.Error(err, fmt.Sprintf("mac address %s already allocated to %s, %s, conflict with: %s, %s",
iface.MacAddress, macEntry.instanceName, macEntry.macInstanceKey, vmFullName, iface.Name))
return err
}
}
if isNotDryRun {
p.macPoolMap.createOrUpdateEntry(requestedMac, vmFullName, iface.Name)
}
logger.V(1).Info("requested mac was allocated for virtual machine", "requestedMap", requestedMac)
return nil
}
func macAlreadyBelongsToVmAndInterface(vmFullName, interfaceName string, macEntry macEntry) bool {
if macEntry.instanceName == vmFullName && macEntry.macInstanceKey == interfaceName {
return true
}
return false
}
func (p *PoolManager) initVirtualMachineMap() error {
logger := log.WithName("initVirtualMachineMap")
if !p.isKubevirt {
return nil
}
err := p.initMacMapFromCluster(logger)
if err != nil {
return errors.Wrap(err, "failed to init MacPoolMap From Cluster")
}
err = p.initMacMapFromLegacyConfigMap()
if err != nil {
return err
}
return nil
}
func (p *PoolManager) initMacMapFromCluster(parentLogger logr.Logger) error {
err := p.forEachManagedVmInterfaceInClusterRunFunction(func(vmFullName string, iface kubevirt.Interface, networks map[string]kubevirt.Network) error {
if !validateInterfaceSupported(iface, networks) {
return nil
}
if iface.MacAddress != "" {
if err := p.allocateRequestedVirtualMachineInterfaceMac(vmFullName, iface, true, parentLogger); err != nil {
if strings.Contains(err.Error(), "failed to allocate requested mac address") {
gauges.DuplicateMacGauge.Inc()
}
// Dont return an error here if we can't allocate a mac for a configured vm
parentLogger.Error(err, "Invalid/Duplicate mac address for virtual machine",
"virtualMachineFullName", vmFullName,
"virtualMachineInterfaceMac", iface.MacAddress)
}
}
return nil
})
if err != nil {
return errors.Wrap(err, "failed to iterate the cluster vm interfaces to recreate the macPoolMap")
}
return nil
}
// paginateVmsWithLimit performs a vm list request with pagination, to limit the amount of vms received at a time
// and prevent taking too much memory.
func (p *PoolManager) paginateVmsWithLimit(limit int64, vmsFunc func(pods *kubevirt.VirtualMachineList) error) error {
continueFlag := ""
for {
// Using a unstructured object.
vms := &kubevirt.VirtualMachineList{}
err := p.kubeClient.List(context.TODO(), vms, &client.ListOptions{
Namespace: metav1.NamespaceAll,
Limit: limit,
Continue: continueFlag,
})
if err != nil {
return err
}
err = vmsFunc(vms)
if err != nil {
return err
}
continueFlag = vms.GetContinue()
log.V(1).Info("limit vms list", "vms len", len(vms.Items), "remaining", vms.GetRemainingItemCount(), "continue", continueFlag)
if continueFlag == "" {
break
}
}
return nil
}
// forEachManagedVmInterfaceInClusterRunFunction gets all the macs from all the supported interfaces in all the managed cluster vms, and runs
// a function vmInterfacesFunc on it
func (p *PoolManager) forEachManagedVmInterfaceInClusterRunFunction(vmInterfacesFunc func(vmFullName string, iface kubevirt.Interface, networks map[string]kubevirt.Network) error) error {
err := p.paginateVmsWithLimit(100, func(vms *kubevirt.VirtualMachineList) error {
logger := log.WithName("forEachManagedVmInterfaceInClusterRunFunction")
for _, vm := range vms.Items {
vmNamespace := vm.GetNamespace()
isNamespaceManaged, err := p.IsVirtualMachineManaged(vmNamespace)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to check if namespace %s is managed in current opt-mode", vmNamespace))
}
if !isNamespaceManaged {
logger.V(1).Info("skipping vm in loop iteration, namespace not managed", "vmNamespace", vmNamespace)
continue
}
vmFullName := VmNamespaced(&vm)
vmInterfaces := getVirtualMachineInterfaces(&vm)
vmNetworks := getVirtualMachineNetworks(&vm)
if len(vmInterfaces) == 0 {
logger.V(1).Info("no interfaces found for virtual machine, skipping mac allocation", "virtualMachine", vm)
continue
}
if len(vmNetworks) == 0 {
logger.V(1).Info("no networks found for virtual machine, skipping mac allocation",
"virtualMachineName", vm.Name,
"virtualMachineNamespace", vm.Namespace)
continue
}
networks := map[string]kubevirt.Network{}
for _, network := range vmNetworks {
networks[network.Name] = network
}
logger.V(1).Info("virtual machine data",
"vmFullName", vmFullName,
"virtualMachineInterfaces", vmInterfaces)
for _, iface := range vmInterfaces {
err := vmInterfacesFunc(vmFullName, iface, networks)
if err != nil {
return errors.Wrapf(err, "failed vm interface loop on vm %s", vmFullName)
}
}
}
return nil
})
if err != nil {
return errors.Wrap(err, "failed iterating over all cluster vms")
}
return nil
}
func (p *PoolManager) IsKubevirtEnabled() bool {
return p.isKubevirt
}
func (p *PoolManager) isRelatedToKubevirt(pod *corev1.Pod) bool {
if pod.ObjectMeta.OwnerReferences == nil {
return false
}
for _, ref := range pod.OwnerReferences {
if ref.Kind == kubevirt.VirtualMachineInstanceGroupVersionKind.Kind {
vm := &kubevirt.VirtualMachine{}
err := p.kubeClient.Get(context.TODO(), client.ObjectKey{Namespace: pod.Namespace, Name: ref.Name}, vm)
if err != nil && apierrors.IsNotFound(err) {
log.V(1).Info("this pod is an ephemeral vmi object allocating mac as a regular pod")
return false
}
return true
}
}
return false
}
// Revert allocation if one of the requested mac addresses fails to be allocated
func (p *PoolManager) revertAllocationOnVm(vmName string, allocations map[string]string, isNotDryRun bool) {
if isNotDryRun == false {
return
}
log.V(1).Info("Revert vm allocation", "vmName", vmName, "allocations", allocations)
for _, macAddress := range allocations {
p.macPoolMap.removeMacEntry(macAddress)
}
}
// Remove all the mac addresses from the waiting configmap this mean the vm was saved in the etcd and pass validations
func (p *PoolManager) MarkVMAsReady(vm *kubevirt.VirtualMachine, latestPersistedTransactionTimeStamp *time.Time, parentLogger logr.Logger) error {
logger := parentLogger.WithName("MarkVMAsReady")
p.poolMutex.Lock()
defer p.poolMutex.Unlock()
vmFullName := VmNamespaced(vm)
vmMacMap, err := p.macPoolMap.filterInByInstanceName(vmFullName)
if err != nil {
return errors.Wrapf(err, "Failed to get VmMacMap for vm %s", vmFullName)
}
logger.Info("Macs currently set on vm", "vmMacMap", vmMacMap)
err = vmMacMap.filterMacsThatRequireCommit(latestPersistedTransactionTimeStamp, logger)
if err != nil {
return errors.Wrapf(err, "Failed to filter macs that need commit on vm %s", vmFullName)
}
if len(*vmMacMap) != 0 {
p.commitChangesToMacPoolMap(vmMacMap, vm, logger)
}
logger.V(1).Info("marked virtual machine as ready", "macPoolMap", p.macPoolMap)
return nil
}
// This function check if there are virtual machines that hits the create
// mutating webhook but we didn't get the creation event in the controller loop
func (p *PoolManager) vmWaitingCleanupLook() {
logger := log.WithName("vmWaitingCleanupLook")
c := time.Tick(3 * time.Second)
logger.Info("starting cleanup loop for waiting mac addresses")
for _ = range c {
p.healStaleMacEntries(logger)
}
}
// healStaleMacEntries looks for stale mac entries, and once find one, heals it by comparing to the real state in the
// cluster: if the vm still there, and the mac attached to it) then it allocates it, otherwise removes from the macMap
func (p *PoolManager) healStaleMacEntries(parentLogger logr.Logger) error {
p.poolMutex.Lock()
defer p.poolMutex.Unlock()
logger := parentLogger.WithName("healStaleMacEntries")
var macsToRemove []string
macsToAlign := map[string]*kubevirt.VirtualMachine{}
for macAddress, macEntry := range p.macPoolMap {
isEntryStale, err := macEntry.hasExpiredTransaction(p.waitTime)
if err == nil && isEntryStale {
logger.Info("entry is stale", "macAddress", macAddress, "vmFullName", macEntry.instanceName, "interfaceName", macEntry.macInstanceKey, "stale TS", macEntry.transactionTimestamp)
var vm *kubevirt.VirtualMachine
var err error
if macEntry.isDummyEntry() {
vm, err = p.recoverVmFromCluster(macAddress.String())
} else {
vm, err = p.getvmInstance(macEntry.instanceName)
}
if err != nil {
if apierrors.IsNotFound(err) {
logger.Info("vm no longer exists. Removing mac from pool", "macAddress", macAddress, "entry", macEntry)
macsToRemove = append(macsToRemove, macAddress.String())
continue
} else {
return err
}
}
macsToAlign[macAddress.String()] = vm
}
}
if len(macsToRemove) == 0 && len(macsToAlign) == 0 {
return nil
}
logger.Info("macMap is self healing", "macsToRemove", macsToRemove, "macsToAlign", macsToAlign)
for _, macAddress := range macsToRemove {
p.macPoolMap.removeMacEntry(macAddress)
}
for macAddress, vm := range macsToAlign {
p.macPoolMap.alignMacEntryAccordingToVmInterface(macAddress, VmNamespaced(vm), getVirtualMachineInterfaces(vm))
}
logger.V(1).Info("macMap is updated", "macPoolMap", p.macPoolMap)
return nil
}
func (p *PoolManager) recoverVmFromCluster(macAddress string) (*kubevirt.VirtualMachine, error) {
log.V(1).Info("recoverVmFromCluster", "macAddress", macAddress)
foundVmName := ""
err := p.forEachManagedVmInterfaceInClusterRunFunction(func(vmFullName string, iface kubevirt.Interface, networks map[string]kubevirt.Network) error {
if !validateInterfaceSupported(iface, networks) {
return nil
}
if iface.MacAddress != "" && NewMacKey(iface.MacAddress).String() == macAddress {
foundVmName = vmFullName
}
return nil
})
if err != nil {
return nil, errors.Wrap(err, "failed to search for vm that holds the mac address in cluster")
}
if foundVmName != "" {
return p.getvmInstance(foundVmName)
} else {
return nil, apierrors.NewNotFound(schema.GroupResource{
Group: "kubevirt.io",
Resource: "virtualmachine",
}, foundVmName)
}
}
func (p *PoolManager) getvmInstance(vmFullName string) (*kubevirt.VirtualMachine, error) {
vmFullNameSplit := strings.Split(vmFullName, "/")
vmNamespace := vmFullNameSplit[1]
vmName := vmFullNameSplit[2]
log.V(1).Info("getvmInstance", "vmNamespace", vmNamespace, "vmName", vmName)
if vmNamespace == "" || vmName == "" {
return nil, errors.New("failed to extract vm namespace and name")
}
vm := &kubevirt.VirtualMachine{}
err := p.kubeClient.Get(context.TODO(), client.ObjectKey{Namespace: vmNamespace, Name: vmName}, vm)
if err != nil {
return nil, err
}
return vm, nil
}
func validateInterfaceSupported(iface kubevirt.Interface, networks map[string]kubevirt.Network) bool {
if iface.Masquerade == nil && iface.Slirp == nil && networks[iface.Name].Multus == nil {
log.Info("mac address can be set only for interface of type masquerade and slirp on the pod network")
return false
}
return true
}
// IsVirtualMachineManaged checks if the namespace of a VirtualMachine instance is managed by kubemacpool
func (p *PoolManager) IsVirtualMachineManaged(namespaceName string) (bool, error) {
return p.IsNamespaceManaged(namespaceName, virtualMachnesWebhookName)
}
func VmNamespaced(machine *kubevirt.VirtualMachine) string {
return fmt.Sprintf("vm/%s/%s", machine.Namespace, machine.Name)
}
func VmNamespacedFromRequest(request *reconcile.Request) string {
vm := &kubevirt.VirtualMachine{ObjectMeta: metav1.ObjectMeta{
Name: request.Name,
Namespace: request.Namespace,
}}
return VmNamespaced(vm)
}
func IsVirtualMachineDeletionInProgress(vm *kubevirt.VirtualMachine) bool {
return !vm.ObjectMeta.DeletionTimestamp.IsZero()
}