/
example_kubevirt.go
131 lines (114 loc) · 4.05 KB
/
example_kubevirt.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
package fixtures
import (
"fmt"
"strings"
hyperv1 "github.com/openshift/hypershift/api/hypershift/v1beta1"
corev1 "k8s.io/api/core/v1"
apiresource "k8s.io/apimachinery/pkg/api/resource"
)
type ExampleKubevirtOptions struct {
ServicePublishingStrategy string
APIServerAddress string
Memory string
Cores uint32
Image string
RootVolumeSize uint32
RootVolumeStorageClass string
RootVolumeAccessModes string
RootVolumeVolumeMode string
BaseDomainPassthrough bool
InfraKubeConfig []byte
InfraNamespace string
CacheStrategyType string
InfraStorageClassMappings []string
InfraVolumeSnapshotClassMappings []string
NetworkInterfaceMultiQueue *hyperv1.MultiQueueSetting
QoSClass *hyperv1.QoSClass
AdditionalNetworks []hyperv1.KubevirtNetwork
AttachDefaultNetwork *bool
VmNodeSelector map[string]string
}
func ExampleKubeVirtTemplate(o *ExampleKubevirtOptions) *hyperv1.KubevirtNodePoolPlatform {
var storageClassName *string
var accessModesStr []string
var accessModes []hyperv1.PersistentVolumeAccessMode
volumeSize := apiresource.MustParse(fmt.Sprintf("%vGi", o.RootVolumeSize))
if o.RootVolumeStorageClass != "" {
storageClassName = &o.RootVolumeStorageClass
}
if o.RootVolumeAccessModes != "" {
accessModesStr = strings.Split(o.RootVolumeAccessModes, ",")
for _, ams := range accessModesStr {
var am hyperv1.PersistentVolumeAccessMode
am = hyperv1.PersistentVolumeAccessMode(ams)
accessModes = append(accessModes, am)
}
}
exampleTemplate := &hyperv1.KubevirtNodePoolPlatform{
RootVolume: &hyperv1.KubevirtRootVolume{
KubevirtVolume: hyperv1.KubevirtVolume{
Type: hyperv1.KubevirtVolumeTypePersistent,
Persistent: &hyperv1.KubevirtPersistentVolume{
Size: &volumeSize,
StorageClass: storageClassName,
AccessModes: accessModes,
},
},
},
Compute: &hyperv1.KubevirtCompute{},
AdditionalNetworks: o.AdditionalNetworks,
AttachDefaultNetwork: o.AttachDefaultNetwork,
}
if o.RootVolumeVolumeMode != "" {
vm := corev1.PersistentVolumeMode(o.RootVolumeVolumeMode)
exampleTemplate.RootVolume.KubevirtVolume.Persistent.VolumeMode = &vm
}
if o.Memory != "" {
memory := apiresource.MustParse(o.Memory)
exampleTemplate.Compute.Memory = &memory
}
if o.Cores != 0 {
exampleTemplate.Compute.Cores = &o.Cores
}
if o.QoSClass != nil && *o.QoSClass == hyperv1.QoSClassGuaranteed {
exampleTemplate.Compute.QosClass = o.QoSClass
}
if o.Image != "" {
exampleTemplate.RootVolume.Image = &hyperv1.KubevirtDiskImage{
ContainerDiskImage: &o.Image,
}
}
strategyType := hyperv1.KubevirtCachingStrategyType(o.CacheStrategyType)
if strategyType == hyperv1.KubevirtCachingStrategyNone || strategyType == hyperv1.KubevirtCachingStrategyPVC {
exampleTemplate.RootVolume.CacheStrategy = &hyperv1.KubevirtCachingStrategy{
Type: strategyType,
}
}
if o.NetworkInterfaceMultiQueue != nil && *o.NetworkInterfaceMultiQueue == hyperv1.MultiQueueEnable {
exampleTemplate.NetworkInterfaceMultiQueue = o.NetworkInterfaceMultiQueue
}
if o.VmNodeSelector != nil && len(o.VmNodeSelector) > 0 {
exampleTemplate.NodeSelector = o.VmNodeSelector
}
return exampleTemplate
}
func parseTenantClassString(optionString string) (string, string) {
guestName := optionString
optionsSplit := strings.Split(optionString, ",")
if len(optionsSplit) > 1 {
guestName = optionsSplit[0]
for i := 1; i < len(optionsSplit); i++ {
optionSplit := strings.Split(optionsSplit[i], "=")
if len(optionSplit) != 2 {
panic(fmt.Sprintf("invalid KubeVirt infra storage class mapping option [%s]", optionsSplit[i]))
}
if isValidGroupOption(optionSplit[0]) {
return guestName, optionSplit[1]
}
}
}
return guestName, ""
}
func isValidGroupOption(input string) bool {
return strings.TrimSpace(strings.ToLower(input)) == "group"
}