/
structure_virtual_machine_spec.go
296 lines (257 loc) · 7.39 KB
/
structure_virtual_machine_spec.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
package kubevirt
import (
"github.com/hashicorp/terraform/helper/schema"
)
// CloudInitDiskName is name of disk/volume which is used for cloud init created by terraform kubevirt provider
const CloudInitDiskName = "clinitterraform"
// DatavolumePrefix is prefix of volume created by terraform for datavolumes:
const DatavolumePrefix = "dvvolume"
func expandVirtualMachineSpec(d *schema.ResourceData) (*map[string]interface{}, error) {
labels := d.Get("labels").(map[string]interface{})
annotations := d.Get("annotations").(map[string]interface{})
cloudInit := d.Get("cloud_init").(string)
name := d.Get("name").(string)
image := d.Get("image").(interface{})
memory := d.Get("memory").([]interface{})
cpu := d.Get("cpu").([]interface{})
interfaces := d.Get("interfaces").([]interface{})
obj := map[string]interface{}{
"running": d.Get("running").(bool),
"dataVolumeTemplates": expandImage(image, name),
"template": map[string]interface{}{
"metadata": map[string]interface{}{
"labels": labels,
"annotations": annotations,
},
"spec": map[string]interface{}{
"volumes": expandVolumes(name, image, cloudInit),
"domain": map[string]interface{}{
"devices": expandDevices(name, image, cloudInit, interfaces),
"cpu": expandCPU(cpu),
"resources": expandResources(memory, cpu),
},
"networks": expandNetworks(interfaces),
},
},
}
return &obj, nil
}
func expandVolumes(vmName string, image interface{}, cloudInit string) []map[string]interface{} {
volumes := make([]map[string]interface{}, 0)
// Append qcow image volume:
if len(image.(map[string]interface{})) > 0 {
volumes = append(
volumes,
map[string]interface{}{
"dataVolume": map[string]interface{}{
"name": DatavolumePrefix + vmName,
},
"name": DatavolumePrefix + vmName,
},
)
}
// Append cloud-init volume:
if cloudInit != "" {
volumes = append(
volumes,
map[string]interface{}{
"cloudInitNoCloud": map[string]string{
"userData": cloudInit,
},
"name": CloudInitDiskName,
},
)
}
return volumes
}
func expandDevices(vmName string, image interface{}, cloudInit string, interfaces []interface{}) map[string]interface{} {
devices := make(map[string]interface{})
disks := make([]map[string]interface{}, 0)
// Append qcow image disk:
if len(image.(map[string]interface{})) > 0 {
disks = append(
disks,
map[string]interface{}{
"disk": map[string]interface{}{
"bus": "virtio",
},
"name": DatavolumePrefix + vmName,
},
)
}
// Append cloud-init disk:
if cloudInit != "" {
disks = append(
disks,
map[string]interface{}{
"name": CloudInitDiskName,
"disk": map[string]string{
"bus": "virtio",
},
},
)
}
if len(disks) > 0 {
devices["disks"] = disks
}
if len(interfaces) > 0 {
devices["interfaces"] = expandInterfaces(interfaces)
}
return devices
}
func expandInterfaces(interfacesIn []interface{}) []map[string]interface{} {
result := make([]map[string]interface{}, len(interfacesIn))
for i, nic := range interfacesIn {
nic := nic.(map[string]interface{})
result[i] = make(map[string]interface{})
nicType := nic["type"].(string)
result[i][nicType] = map[string]string{}
result[i]["name"] = nic["name"]
result[i]["ports"] = nic["ports"]
result[i]["pciAddress"] = nic["pci_address"]
result[i]["model"] = nic["model"]
result[i]["macAddress"] = nic["mac_address"]
if nic["boot_order"] != 0 {
result[i]["bootOrder"] = nic["boot_order"]
}
}
return result
}
func expandNetworks(interfacesIn []interface{}) []map[string]interface{} {
if len(interfacesIn) == 0 {
return nil
}
result := make([]map[string]interface{}, len(interfacesIn))
for i, nic := range interfacesIn {
nic := nic.(map[string]interface{})
result[i] = make(map[string]interface{})
nicNetwork := nic["network"].(string)
result[i]["name"] = nic["name"]
result[i][nicNetwork] = map[string]string{}
}
return result
}
func expandCPU(cpuIn []interface{}) map[string]interface{} {
if len(cpuIn) == 0 {
return nil
}
cpu := make(map[string]interface{})
if len(cpuIn) > 0 {
cpu = cpuIn[0].(map[string]interface{})
}
result := make(map[string]interface{})
result["sockets"] = cpu["sockets"]
result["model"] = cpu["model"]
result["dedicatedCpuPlacement"] = cpu["dedicated"]
result["threads"] = cpu["threads"]
result["cores"] = cpu["cores"]
return result
}
func expandResources(memoryIn []interface{}, cpuIn []interface{}) map[string]map[string]string {
if len(memoryIn) == 0 && len(cpuIn) == 0 {
return nil
}
memory := make(map[string]interface{})
if len(memoryIn) > 0 {
memory = memoryIn[0].(map[string]interface{})
}
cpu := make(map[string]interface{})
if len(cpuIn) > 0 {
cpu = cpuIn[0].(map[string]interface{})
}
resources := map[string]map[string]string{
"requests": map[string]string{},
"limits": map[string]string{},
}
// Process memory limit:
mLimit := memory["limits"]
if mLimit != "" {
resources["limits"]["memory"] = mLimit.(string)
}
// Process requests memory:
mReq := memory["requests"]
if mReq != "" {
resources["requests"]["memory"] = mReq.(string)
}
// Process requests cpu:
cpuReq := cpu["requests"]
if cpuReq != "" {
resources["requests"]["cpu"] = cpuReq.(string)
}
// Process cpu limit:
cpuLimit := cpu["limits"]
if cpuLimit != "" {
resources["limits"]["cpu"] = cpuLimit.(string)
}
return resources
}
func expandImage(img interface{}, name string) []map[string]interface{} {
if img == nil {
return nil
}
image := img.(map[string]interface{})
accessModes, ok := image["accessmodes"].([]interface{})
if !ok {
accessModes = []interface{}{"ReadWriteOnce"}
}
storage, ok := image["storage"].(string)
if !ok {
storage = "1Gi"
}
return []map[string]interface{}{
map[string]interface{}{
"metadata": map[string]interface{}{
"name": DatavolumePrefix + name,
},
"spec": map[string]interface{}{
"pvc": map[string]interface{}{
"accessModes": accessModes,
"resources": map[string]interface{}{
"requests": map[string]interface{}{
"storage": storage,
},
},
},
"source": map[string]interface{}{
"http": map[string]interface{}{
"url": image["url"].(string),
},
},
},
},
}
}
func flattenCloudInitSpec(volumes []interface{}) string {
for _, v := range volumes {
diskName := v.(map[string]interface{})["name"].(string)
if diskName == CloudInitDiskName {
cloudInitNoCloud := v.(map[string]interface{})["cloudInitNoCloud"].(map[string]interface{})
return cloudInitNoCloud["userData"].(string)
}
}
return ""
}
func flattenVMInterfacesSpec(interfaces, networks []interface{}) []map[string]interface{} {
flatten := make([]map[string]interface{}, len(interfaces))
for index, iface := range interfaces {
iface := iface.(map[string]interface{})
flatten[index] = make(map[string]interface{})
flatten[index]["name"] = iface["name"]
flatten[index]["model"] = iface["model"]
flatten[index]["mac_address"] = iface["macAddress"]
flatten[index]["pci_address"] = iface["pciAddress"]
flatten[index]["type"] = getKeyFromMap(iface, "bridge", "slirp", "sriov", "masquerade")
flatten[index]["network"] = getKeyFromMap(networks[index].(map[string]interface{}), "pod", "multus", "genie")
}
return flatten
}
func getKeyFromMap(themap map[string]interface{}, keys ...string) string {
for k := range themap {
for _, v := range keys {
if k == v {
return k
}
}
}
return ""
}