-
Notifications
You must be signed in to change notification settings - Fork 154
/
vm_instancetype.go
123 lines (103 loc) · 4.65 KB
/
vm_instancetype.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
/*
Copyright 2022 The Kubermatic Kubernetes Platform contributors.
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.
*/
package kubevirt
import (
"context"
"fmt"
"strconv"
"strings"
kubevirtv1 "kubevirt.io/api/core/v1"
kvinstancetypev1alpha1 "kubevirt.io/api/instancetype/v1alpha1"
"k8c.io/kubermatic/v2/pkg/provider"
kvmanifests "k8c.io/kubermatic/v2/pkg/provider/cloud/kubevirt/manifests"
"k8c.io/kubermatic/v2/pkg/resources/reconciling"
"k8s.io/apimachinery/pkg/api/resource"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
func instancetypeReconciler(instancetype *kvinstancetypev1alpha1.VirtualMachineInstancetype) reconciling.NamedVirtualMachineInstancetypeReconcilerFactory {
return func() (string, reconciling.VirtualMachineInstancetypeReconciler) {
return instancetype.Name, func(it *kvinstancetypev1alpha1.VirtualMachineInstancetype) (*kvinstancetypev1alpha1.VirtualMachineInstancetype, error) {
it.Labels = instancetype.Labels
it.Spec = instancetype.Spec
return it, nil
}
}
}
// reconcileInstancetypes reconciles the Kubermatic standard VirtualMachineInstancetype into the dedicated namespace.
func reconcileInstancetypes(ctx context.Context, namespace string, client ctrlruntimeclient.Client) error {
instancetypes := &kvinstancetypev1alpha1.VirtualMachineInstancetypeList{}
// add Kubermatic standards
instancetypes.Items = append(instancetypes.Items, GetKubermaticStandardInstancetypes(client, &kvmanifests.StandardInstancetypeGetter{})...)
for _, instancetype := range instancetypes.Items {
instancetypeReconcilers := []reconciling.NamedVirtualMachineInstancetypeReconcilerFactory{
instancetypeReconciler(&instancetype),
}
if err := reconciling.ReconcileVirtualMachineInstancetypes(ctx, instancetypeReconcilers, namespace, client); err != nil {
return err
}
}
return nil
}
// GetKubermaticStandardInstancetypes returns the Kubermatic standard VirtualMachineInstancetypes.
func GetKubermaticStandardInstancetypes(client ctrlruntimeclient.Client, getter kvmanifests.ManifestFSGetter) []kvinstancetypev1alpha1.VirtualMachineInstancetype {
objs := kvmanifests.RuntimeFromYaml(client, getter)
instancetypes := make([]kvinstancetypev1alpha1.VirtualMachineInstancetype, 0, len(objs))
for _, obj := range objs {
instancetypes = append(instancetypes, *obj.(*kvinstancetypev1alpha1.VirtualMachineInstancetype))
}
return instancetypes
}
// DescribeInstanceType returns the NodeCapacity from the VirtualMachine instancetype.
func DescribeInstanceType(ctx context.Context, kubeconfig string, it *kubevirtv1.InstancetypeMatcher) (*provider.NodeCapacity, error) {
client, err := NewClient(kubeconfig, ClientOptions{})
if err != nil {
return nil, err
}
switch it.Kind {
case "VirtualMachineInstancetype": // "standard" instancetype
standardInstancetypes := GetKubermaticStandardInstancetypes(client, &kvmanifests.StandardInstancetypeGetter{})
for _, instancetype := range standardInstancetypes {
if strings.EqualFold(instancetype.Name, it.Name) {
return instanceTypeToNodeCapacity(instancetype.Spec)
}
}
case "VirtualMachineClusterInstancetype": // "custom" instancetype (cluster-wide).
customInstancetypes := kvinstancetypev1alpha1.VirtualMachineClusterInstancetypeList{}
err := client.List(ctx, &customInstancetypes)
if err != nil {
return nil, err
}
for _, instancetype := range customInstancetypes.Items {
if strings.EqualFold(instancetype.Name, it.Name) {
return instanceTypeToNodeCapacity(instancetype.Spec)
}
}
}
return nil, fmt.Errorf("VMI instancetype %s of Kind %s not found", it.Name, it.Kind)
}
// instanceTypeToNodeCapacity extracts cpu and mem resource requests from the kubevirt instancetype.
func instanceTypeToNodeCapacity(it kvinstancetypev1alpha1.VirtualMachineInstancetypeSpec) (*provider.NodeCapacity, error) {
capacity := provider.NewNodeCapacity()
// CPU and Memory are mandatory fields in instancetype
if !it.Memory.Guest.IsZero() {
capacity.Memory = &it.Memory.Guest
}
cpu, err := resource.ParseQuantity(strconv.Itoa(int(it.CPU.Guest)))
if err != nil {
return nil, fmt.Errorf("error parsing instancetype CPU: %w", err)
}
if !cpu.IsZero() {
capacity.WithCPUCount(int(cpu.Value()))
}
return capacity, nil
}