-
Notifications
You must be signed in to change notification settings - Fork 21
/
docs.go
207 lines (180 loc) · 4.22 KB
/
docs.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
package docs
import (
_ "embed"
"strconv"
"strings"
"text/template"
"golang.org/x/text/cases"
"golang.org/x/text/language"
k8sv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
v1 "kubevirt.io/api/core/v1"
)
type TemplateData struct {
Name string
Description string
Example string
}
type UserData struct {
Username string
AuthorizedKeys []string
}
type Option func(vm *v1.VirtualMachine)
//go:embed data/cloudinit.tpl
var cloudinitTemplate string
//go:embed data/ignition.tpl
var ignitionTemplate string
//go:embed data/description.tpl
var descriptionTemplate string
func NewVM(name, image string, opts ...Option) *v1.VirtualMachine {
vm := BasicVM(name, image)
for _, opt := range opts {
opt(vm)
}
return vm
}
func BasicVM(name, image string) *v1.VirtualMachine {
const terminationGracePeriod int64 = 180
return &v1.VirtualMachine{
TypeMeta: metav1.TypeMeta{
Kind: "VirtualMachine",
APIVersion: "kubevirt.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1.VirtualMachineSpec{
RunStrategy: ptr.To(v1.RunStrategyAlways),
Template: &v1.VirtualMachineInstanceTemplateSpec{
Spec: v1.VirtualMachineInstanceSpec{
TerminationGracePeriodSeconds: ptr.To(terminationGracePeriod),
Domain: v1.DomainSpec{
Resources: v1.ResourceRequirements{
Requests: map[k8sv1.ResourceName]resource.Quantity{
k8sv1.ResourceMemory: resource.MustParse("1Gi"),
},
},
Devices: v1.Devices{
Disks: []v1.Disk{
{
Name: "containerdisk",
DiskDevice: v1.DiskDevice{
Disk: &v1.DiskTarget{
Bus: "virtio",
},
},
},
},
},
},
Volumes: []v1.Volume{
{
Name: "containerdisk",
VolumeSource: v1.VolumeSource{
ContainerDisk: &v1.ContainerDiskSource{
Image: image,
},
},
},
},
},
},
},
}
}
func WithRng() Option {
return func(vm *v1.VirtualMachine) {
vm.Spec.Template.Spec.Domain.Devices.Rng = &v1.Rng{}
}
}
func withCloudInit(volumeSource v1.VolumeSource) Option {
return func(vm *v1.VirtualMachine) {
vm.Spec.Template.Spec.Domain.Devices.Disks = append(
vm.Spec.Template.Spec.Domain.Devices.Disks,
v1.Disk{
Name: "cloudinit",
DiskDevice: v1.DiskDevice{
Disk: &v1.DiskTarget{
Bus: "virtio",
},
},
},
)
vm.Spec.Template.Spec.Volumes = append(
vm.Spec.Template.Spec.Volumes,
v1.Volume{
Name: "cloudinit",
VolumeSource: volumeSource,
},
)
}
}
func WithCloudInitNoCloud(userData string) Option {
return withCloudInit(v1.VolumeSource{
CloudInitNoCloud: &v1.CloudInitNoCloudSource{
UserData: userData,
},
})
}
func WithCloudInitConfigDrive(userData string) Option {
return withCloudInit(v1.VolumeSource{
CloudInitConfigDrive: &v1.CloudInitConfigDriveSource{
UserData: userData,
},
})
}
func WithSecureBoot() Option {
return func(vm *v1.VirtualMachine) {
vm.Spec.Template.Spec.Domain.Features = &v1.Features{
SMM: &v1.FeatureState{
Enabled: ptr.To(true),
},
}
vm.Spec.Template.Spec.Domain.Firmware = &v1.Firmware{
Bootloader: &v1.Bootloader{
EFI: &v1.EFI{
SecureBoot: ptr.To(true),
},
},
}
}
}
func Template() *template.Template {
caser := cases.Title(language.English)
funcMap := template.FuncMap{
"ToTitle": caser.String,
}
return template.Must(
template.New("description").Funcs(funcMap).Parse(descriptionTemplate),
)
}
func CloudInit(data *UserData) string {
tpl := template.Must(
template.New("cloudinit").Parse(cloudinitTemplate),
)
return mustExecute(tpl, data)
}
func Ignition(data *UserData) string {
funcMap := template.FuncMap{
"Quote": func(items []string) []string {
for i, item := range items {
items[i] = strconv.Quote(item)
}
return items
},
"Join": strings.Join,
}
tpl := template.Must(
template.New("ignition").Funcs(funcMap).Parse(ignitionTemplate),
)
return mustExecute(tpl, data)
}
func mustExecute(tpl *template.Template, data interface{}) string {
res := strings.Builder{}
if err := tpl.Execute(&res, data); err != nil {
panic(err)
}
return res.String()
}