forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vms-generator.go
161 lines (136 loc) · 5.24 KB
/
vms-generator.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
/*
* 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 2018 Red Hat, Inc.
*
*/
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"kubevirt.io/kubevirt/tools/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sfield "k8s.io/apimachinery/pkg/util/validation/field"
v1 "kubevirt.io/kubevirt/pkg/api/v1"
validating_webhook "kubevirt.io/kubevirt/pkg/virt-api/webhooks/validating-webhook"
"kubevirt.io/kubevirt/tools/vms-generator/utils"
)
func main() {
flag.StringVar(&utils.DockerPrefix, "container-prefix", utils.DockerPrefix, "")
flag.StringVar(&utils.DockerTag, "container-tag", utils.DockerTag, "")
genDir := flag.String("generated-vms-dir", "", "")
flag.Parse()
// Required to validate DataVolume usage
os.Setenv("FEATURE_GATES", "DataVolumes,LiveMigration,SRIOV")
var vms = map[string]*v1.VirtualMachine{
utils.VmCirros: utils.GetVMCirros(),
utils.VmAlpineMultiPvc: utils.GetVMMultiPvc(),
utils.VmAlpineDataVolume: utils.GetVMDataVolume(),
}
var vmis = map[string]*v1.VirtualMachineInstance{
utils.VmiEphemeral: utils.GetVMIEphemeral(),
utils.VmiMigratable: utils.GetVMIMigratable(),
utils.VmiFlavorSmall: utils.GetVMIFlavorSmall(),
utils.VmiSata: utils.GetVMISata(),
utils.VmiFedora: utils.GetVMIEphemeralFedora(),
utils.VmiAlpineEFI: utils.GetVMIAlpineEFI(),
utils.VmiNoCloud: utils.GetVMINoCloud(),
utils.VmiPVC: utils.GetVMIPvc(),
utils.VmiBlockPVC: utils.GetVMIBlockPvc(),
utils.VmiWindows: utils.GetVMIWindows(),
utils.VmiSlirp: utils.GetVMISlirp(),
utils.VmiSRIOV: utils.GetVMISRIOV(),
utils.VmiWithHookSidecar: utils.GetVMIWithHookSidecar(),
utils.VmiMultusPtp: utils.GetVMIMultusPtp(),
utils.VmiMultusMultipleNet: utils.GetVMIMultusMultipleNet(),
utils.VmiGeniePtp: utils.GetVMIGeniePtp(),
utils.VmiGenieMultipleNet: utils.GetVMIGenieMultipleNet(),
utils.VmiMasquerade: utils.GetVMIMasquerade(),
utils.VmiHostDisk: utils.GetVMIHostDisk(),
}
var vmireplicasets = map[string]*v1.VirtualMachineInstanceReplicaSet{
utils.VmiReplicaSetCirros: utils.GetVMIReplicaSetCirros(),
}
var vmipresets = map[string]*v1.VirtualMachineInstancePreset{
utils.VmiPresetSmall: utils.GetVMIPresetSmall(),
}
var migrations = map[string]*v1.VirtualMachineInstanceMigration{
utils.VmiMigration: utils.GetVMIMigration(),
}
var templates = map[string]*utils.Template{
utils.VmTemplateFedora: utils.GetTemplateFedora(),
utils.VmTemplateRHEL7: utils.GetTemplateRHEL7(),
utils.VmTemplateWindows: utils.GetTemplateWindows(),
}
handleError := func(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
panic(err)
}
}
handleCauses := func(causes []metav1.StatusCause, name string, objType string) {
if len(causes) > 0 {
for _, cause := range causes {
fmt.Fprintf(
os.Stderr,
"Failed to validate %s spec: failed to admit yaml for %s: %s at %s: %s\n",
objType, name, cause.Type, cause.Field, cause.Message)
}
panic(fmt.Errorf("Failed to admit %s of type %s", name, objType))
}
}
dumpObject := func(name string, obj interface{}) error {
filename := filepath.Join(*genDir, fmt.Sprintf("%s.yaml", name))
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("Failed to open file %v, %v", filename, err)
}
defer file.Close()
util.MarshallObject(obj, file)
return nil
}
// Having no generics is lots of fun
for name, obj := range vms {
causes := validating_webhook.ValidateVirtualMachineSpec(k8sfield.NewPath("spec"), &obj.Spec)
handleCauses(causes, name, "vm")
handleError(dumpObject(name, *obj))
}
for name, obj := range vmis {
causes := validating_webhook.ValidateVirtualMachineInstanceSpec(k8sfield.NewPath("spec"), &obj.Spec)
handleCauses(causes, name, "vmi")
handleError(dumpObject(name, *obj))
}
for name, obj := range vmireplicasets {
causes := validating_webhook.ValidateVMIRSSpec(k8sfield.NewPath("spec"), &obj.Spec)
handleCauses(causes, name, "vmi replica set")
handleError(dumpObject(name, *obj))
}
for name, obj := range vmipresets {
causes := validating_webhook.ValidateVMIPresetSpec(k8sfield.NewPath("spec"), &obj.Spec)
handleCauses(causes, name, "vmi preset")
handleError(dumpObject(name, *obj))
}
for name, obj := range migrations {
causes := validating_webhook.ValidateVirtualMachineInstanceMigrationSpec(k8sfield.NewPath("spec"), &obj.Spec)
handleCauses(causes, name, "vmi preset")
handleError(dumpObject(name, *obj))
}
// TODO:(ihar) how to validate templates?
for name, obj := range templates {
handleError(dumpObject(name, *obj))
}
}