-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
storage.go
239 lines (210 loc) · 6.13 KB
/
storage.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
/*
* This file is part of the KubeVirt project
*
* 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.
*
* Copyright 2020 Red Hat, Inc.
*
*/
package libvmi
import (
"fmt"
k8sv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
v1 "kubevirt.io/api/core/v1"
)
// WithContainerImage specifies the name of the container image to be used.
func WithContainerImage(name string) Option {
return func(vmi *v1.VirtualMachineInstance) {
diskName := "disk0"
addDisk(vmi, newDisk(diskName, v1.DiskBusVirtio))
addVolume(vmi, newContainerVolume(diskName, name))
}
}
// WithPersistentVolumeClaim specifies the name of the PersistentVolumeClaim to be used.
func WithPersistentVolumeClaim(diskName, pvcName string) Option {
return func(vmi *v1.VirtualMachineInstance) {
addDisk(vmi, newDisk(diskName, v1.DiskBusVirtio))
addVolume(vmi, newPersistentVolumeClaimVolume(diskName, pvcName))
}
}
// WithDataVolume specifies the name of the DataVolume to be used.
func WithDataVolume(diskName, pvcName string) Option {
return func(vmi *v1.VirtualMachineInstance) {
addDisk(vmi, newDisk(diskName, v1.DiskBusVirtio))
addVolume(vmi, newDataVolume(diskName, pvcName))
}
}
// WithEmptyDisk specifies the name of the EmptyDisk to be used.
func WithEmptyDisk(diskName string, bus v1.DiskBus, capacity resource.Quantity) Option {
return func(vmi *v1.VirtualMachineInstance) {
addDisk(vmi, newDisk(diskName, bus))
addVolume(vmi, newEmptyDisk(diskName, capacity))
}
}
// WithCDRom specifies a CDRom drive backed by a PVC to be used.
func WithCDRom(cdRomName string, bus v1.DiskBus, claimName string) Option {
return func(vmi *v1.VirtualMachineInstance) {
addDisk(vmi, newCDRom(cdRomName, bus))
addVolume(vmi, newPersistentVolumeClaimVolume(cdRomName, claimName))
}
}
// WithFilesystemPVC specifies a filesystem backed by a PVC to be used.
func WithFilesystemPVC(claimName string) Option {
return func(vmi *v1.VirtualMachineInstance) {
addFilesystem(vmi, newVirtiofsFilesystem(claimName))
addVolume(vmi, newPersistentVolumeClaimVolume(claimName, claimName))
}
}
// WithFilesystemDV specifies a filesystem backed by a DV to be used.
func WithFilesystemDV(dataVolumeName string) Option {
return func(vmi *v1.VirtualMachineInstance) {
addFilesystem(vmi, newVirtiofsFilesystem(dataVolumeName))
addVolume(vmi, newDataVolume(dataVolumeName, dataVolumeName))
}
}
func WithPersistentVolumeClaimLun(diskName, pvcName string, reservation bool) Option {
return func(vmi *v1.VirtualMachineInstance) {
addDisk(vmi, newLun(diskName, reservation))
addVolume(vmi, newPersistentVolumeClaimVolume(diskName, pvcName))
}
}
func addDisk(vmi *v1.VirtualMachineInstance, disk v1.Disk) {
if !diskExists(vmi, disk) {
vmi.Spec.Domain.Devices.Disks = append(vmi.Spec.Domain.Devices.Disks, disk)
}
}
func addVolume(vmi *v1.VirtualMachineInstance, volume v1.Volume) {
if !volumeExists(vmi, volume) {
vmi.Spec.Volumes = append(vmi.Spec.Volumes, volume)
}
}
func addFilesystem(vmi *v1.VirtualMachineInstance, filesystem v1.Filesystem) {
if filesystemExists(vmi, filesystem) {
panic(fmt.Errorf("filesystem %s already exists", filesystem.Name))
}
vmi.Spec.Domain.Devices.Filesystems = append(vmi.Spec.Domain.Devices.Filesystems, filesystem)
}
func getVolume(vmi *v1.VirtualMachineInstance, name string) *v1.Volume {
for i := range vmi.Spec.Volumes {
if vmi.Spec.Volumes[i].Name == name {
return &vmi.Spec.Volumes[i]
}
}
return nil
}
func diskExists(vmi *v1.VirtualMachineInstance, disk v1.Disk) bool {
for _, d := range vmi.Spec.Domain.Devices.Disks {
if d.Name == disk.Name {
return true
}
}
return false
}
func volumeExists(vmi *v1.VirtualMachineInstance, volume v1.Volume) bool {
for _, v := range vmi.Spec.Volumes {
if v.Name == volume.Name {
return true
}
}
return false
}
func filesystemExists(vmi *v1.VirtualMachineInstance, filesystem v1.Filesystem) bool {
for _, f := range vmi.Spec.Domain.Devices.Filesystems {
if f.Name == filesystem.Name {
return true
}
}
return false
}
func newDisk(name string, bus v1.DiskBus) v1.Disk {
return v1.Disk{
Name: name,
DiskDevice: v1.DiskDevice{
Disk: &v1.DiskTarget{
Bus: bus,
},
},
}
}
func newCDRom(name string, bus v1.DiskBus) v1.Disk {
return v1.Disk{
Name: name,
DiskDevice: v1.DiskDevice{
CDRom: &v1.CDRomTarget{
Bus: bus,
},
},
}
}
func newVirtiofsFilesystem(name string) v1.Filesystem {
return v1.Filesystem{
Name: name,
Virtiofs: &v1.FilesystemVirtiofs{},
}
}
func newLun(name string, reservation bool) v1.Disk {
return v1.Disk{
Name: name,
DiskDevice: v1.DiskDevice{
LUN: &v1.LunTarget{
Bus: v1.DiskBusSCSI,
Reservation: reservation,
},
},
}
}
func newVolume(name string) v1.Volume {
return v1.Volume{Name: name}
}
func newContainerVolume(name, image string) v1.Volume {
return v1.Volume{
Name: name,
VolumeSource: v1.VolumeSource{
ContainerDisk: &v1.ContainerDiskSource{
Image: image,
},
},
}
}
func newPersistentVolumeClaimVolume(name, claimName string) v1.Volume {
return v1.Volume{
Name: name,
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
PersistentVolumeClaimVolumeSource: k8sv1.PersistentVolumeClaimVolumeSource{
ClaimName: claimName,
}},
},
}
}
func newDataVolume(name, dataVolumeName string) v1.Volume {
return v1.Volume{
Name: name,
VolumeSource: v1.VolumeSource{
DataVolume: &v1.DataVolumeSource{
Name: dataVolumeName,
},
},
}
}
func newEmptyDisk(name string, capacity resource.Quantity) v1.Volume {
return v1.Volume{
Name: name,
VolumeSource: v1.VolumeSource{
EmptyDisk: &v1.EmptyDiskSource{
Capacity: capacity,
},
},
}
}