This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
guestconversion.go
189 lines (171 loc) · 5.75 KB
/
guestconversion.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
package guestconversion
import (
"fmt"
"os"
cdiv1 "kubevirt.io/containerized-data-importer/pkg/apis/core/v1alpha1"
"kubevirt.io/containerized-data-importer/pkg/common"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
v1 "kubevirt.io/client-go/api/v1"
libvirtxml "libvirt.org/libvirt-go-xml"
)
const configMapVolumeName = "libvirt-domain-xml"
var (
virtV2vImage = os.Getenv("VIRTV2V_IMAGE")
imagePullPolicy = corev1.PullPolicy(os.Getenv("IMAGE_PULL_POLICY"))
)
// MakeGuestConversionPodSpec creates a pod spec for a virt-v2v pod,
// containing a volume and a mount for each volume on the VM, as well
// as a volume and mount for the config map containing the libvirt domain XML.
func MakeGuestConversionPodSpec(vmSpec *v1.VirtualMachine, dataVolumes map[string]cdiv1.DataVolume, libvirtConfigMap *corev1.ConfigMap) *corev1.Pod {
// this is the fsGroup that the CDI importer pod uses
fsGroup := common.QemuSubGid
volumes, volumeMounts, volumeDevices := makePodVolumeMounts(vmSpec, dataVolumes, libvirtConfigMap)
return &corev1.Pod{
Spec: corev1.PodSpec{
SecurityContext: &corev1.PodSecurityContext{
FSGroup: &fsGroup,
},
RestartPolicy: corev1.RestartPolicyNever,
Containers: []corev1.Container{
{
Name: "virt-v2v",
Image: virtV2vImage,
VolumeMounts: volumeMounts,
VolumeDevices: volumeDevices,
ImagePullPolicy: imagePullPolicy,
// Request access to /dev/kvm via Kubevirt's Device Manager
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"devices.kubevirt.io/kvm": resource.MustParse("1"),
},
},
},
},
Volumes: volumes,
// Ensure that the pod is deployed on a node where /dev/kvm is present.
NodeSelector: map[string]string{
"kubevirt.io/schedulable": "true",
},
},
}
}
func makePodVolumeMounts(vmSpec *v1.VirtualMachine, dataVolumes map[string]cdiv1.DataVolume, libvirtConfigMap *corev1.ConfigMap) ([]corev1.Volume, []corev1.VolumeMount, []corev1.VolumeDevice) {
volumes := make([]corev1.Volume, 0)
volumeMounts := make([]corev1.VolumeMount, 0)
volumeDevices := make([]corev1.VolumeDevice, 0)
// add volumes and mounts for each of the VM's disks.
// the virt-v2v pod expects to see the disks mounted at /mnt/disks/diskX
for i, v := range vmSpec.Spec.Template.Spec.Volumes {
var volumeMode corev1.PersistentVolumeMode
dv, ok := dataVolumes[v.DataVolume.Name]
if ok && dv.Spec.PVC != nil && dv.Spec.PVC.VolumeMode != nil {
volumeMode = *dv.Spec.PVC.VolumeMode
} else {
// default to Filesystem if a volume mode is not specified
volumeMode = corev1.PersistentVolumeFilesystem
}
vol := corev1.Volume{
Name: dv.Name,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: dv.Name,
ReadOnly: false,
},
},
}
volumes = append(volumes, vol)
if volumeMode == corev1.PersistentVolumeBlock {
volDevice := corev1.VolumeDevice{
Name: v.DataVolume.Name,
DevicePath: fmt.Sprintf("/dev/block%v", i),
}
volumeDevices = append(volumeDevices, volDevice)
} else {
volMount := corev1.VolumeMount{
Name: v.DataVolume.Name,
MountPath: fmt.Sprintf("/mnt/disks/disk%v", i),
}
volumeMounts = append(volumeMounts, volMount)
}
}
// add volume and mount for the libvirt domain xml config map.
// the virt-v2v pod expects to see the libvirt xml at /mnt/v2v/input.xml
volumes = append(volumes, corev1.Volume{
Name: configMapVolumeName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: libvirtConfigMap.Name,
},
},
},
})
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: configMapVolumeName,
MountPath: "/mnt/v2v",
})
return volumes, volumeMounts, volumeDevices
}
// MakeLibvirtDomain makes a minimal libvirt domain for a VM to be used by the guest conversion pod
func MakeLibvirtDomain(vmSpec *v1.VirtualMachine, dataVolumes map[string]cdiv1.DataVolume) *libvirtxml.Domain {
// virt-v2v needs a very minimal libvirt domain XML file to be provided
// with the locations of each of the disks on the VM that is to be converted.
libvirtDisks := make([]libvirtxml.DomainDisk, 0)
for i, vol := range vmSpec.Spec.Template.Spec.Volumes {
diskSource := libvirtxml.DomainDiskSource{}
dv := dataVolumes[vol.DataVolume.Name]
if *dv.Spec.PVC.VolumeMode == corev1.PersistentVolumeBlock {
diskSource.Block = &libvirtxml.DomainDiskSourceBlock{
Dev: fmt.Sprintf("/dev/block%v", i),
}
} else {
diskSource.File = &libvirtxml.DomainDiskSourceFile{
// the location where the disk images will be found on
// the virt-v2v pod. See also makePodVolumeMounts.
File: fmt.Sprintf("/mnt/disks/disk%v/disk.img", i),
}
}
libvirtDisk := libvirtxml.DomainDisk{
Device: "disk",
Driver: &libvirtxml.DomainDiskDriver{
Name: "qemu",
Type: "raw",
},
Source: &diskSource,
Target: &libvirtxml.DomainDiskTarget{
Dev: "hd" + string(rune('a'+i)),
Bus: "virtio",
},
}
libvirtDisks = append(libvirtDisks, libvirtDisk)
}
// generate libvirt domain xml
domain := vmSpec.Spec.Template.Spec.Domain
return &libvirtxml.Domain{
Type: "kvm",
Name: vmSpec.Name,
Memory: &libvirtxml.DomainMemory{
Value: uint(domain.Resources.Requests.Memory().Value()),
},
CPU: &libvirtxml.DomainCPU{
Topology: &libvirtxml.DomainCPUTopology{
Sockets: int(domain.CPU.Sockets),
Cores: int(domain.CPU.Cores),
},
},
OS: &libvirtxml.DomainOS{
Type: &libvirtxml.DomainOSType{
Type: "hvm",
},
BootDevices: []libvirtxml.DomainBootDevice{
{
Dev: "hd",
},
},
},
Devices: &libvirtxml.DomainDeviceList{
Disks: libvirtDisks,
},
}
}