forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflavor.go
136 lines (113 loc) · 3.93 KB
/
flavor.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
package flavor
import (
"fmt"
"strings"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/cache"
k8sfield "k8s.io/apimachinery/pkg/util/validation/field"
virtv1 "kubevirt.io/api/core/v1"
flavorv1alpha1 "kubevirt.io/api/flavor/v1alpha1"
)
type Methods interface {
FindProfile(vm *virtv1.VirtualMachine) (*flavorv1alpha1.VirtualMachineFlavorProfile, error)
ApplyToVmi(field *k8sfield.Path, profile *flavorv1alpha1.VirtualMachineFlavorProfile, vmiSpec *virtv1.VirtualMachineInstanceSpec) Conflicts
}
type Conflicts []*k8sfield.Path
func (c Conflicts) String() string {
pathStrings := make([]string, 0, len(c))
for _, path := range c {
pathStrings = append(pathStrings, path.String())
}
return strings.Join(pathStrings, ", ")
}
type methods struct {
flavorStore cache.Store
clusterFlavorStore cache.Store
}
var _ Methods = &methods{}
func NewMethods(flavorStore, clusterFlavorStore cache.Store) Methods {
return &methods{
flavorStore: flavorStore,
clusterFlavorStore: clusterFlavorStore,
}
}
func (m *methods) FindProfile(vm *virtv1.VirtualMachine) (*flavorv1alpha1.VirtualMachineFlavorProfile, error) {
if vm.Spec.Flavor == nil {
return nil, nil
}
profiles, err := getProfiles(vm.Spec.Flavor.Name, vm.Namespace, vm.Spec.Flavor.Kind, m.flavorStore, m.clusterFlavorStore)
if err != nil {
return nil, err
}
if vm.Spec.Flavor.Profile == "" {
profile := findFirstProfile(profiles, func(profile *flavorv1alpha1.VirtualMachineFlavorProfile) bool {
return profile.Default
})
if profile == nil {
return nil, fmt.Errorf("flavor does not specify a default profile")
}
return profile, nil
} else {
profile := findFirstProfile(profiles, func(profile *flavorv1alpha1.VirtualMachineFlavorProfile) bool {
return profile.Name == vm.Spec.Flavor.Profile
})
if profile == nil {
return nil, fmt.Errorf("flavor does not have a profile with name: %v", vm.Spec.Flavor.Profile)
}
return profile, nil
}
}
func (m *methods) ApplyToVmi(field *k8sfield.Path, profile *flavorv1alpha1.VirtualMachineFlavorProfile, vmiSpec *virtv1.VirtualMachineInstanceSpec) Conflicts {
var conflicts Conflicts
conflicts = append(conflicts, applyCpu(field, profile, vmiSpec)...)
return conflicts
}
func findFirstProfile(profiles []flavorv1alpha1.VirtualMachineFlavorProfile, predicate func(profile *flavorv1alpha1.VirtualMachineFlavorProfile) bool) *flavorv1alpha1.VirtualMachineFlavorProfile {
for i := range profiles {
profile := &profiles[i]
if predicate(profile) {
return profile
}
}
return nil
}
func getKey(namespace string, name string) string {
return namespace + "/" + name
}
func getProfiles(name string, namespace string, kind string, flavorStore, clusterFlavorStore cache.Store) ([]flavorv1alpha1.VirtualMachineFlavorProfile, error) {
switch strings.ToLower(kind) {
case "virtualmachineflavors", "virtualmachineflavor":
key := getKey(namespace, name)
obj, exists, err := flavorStore.GetByKey(key)
if err != nil {
return nil, err
}
if !exists {
return nil, errors.NewNotFound(flavorv1alpha1.Resource("virtualmachineflavor"), key)
}
flavor := obj.(*flavorv1alpha1.VirtualMachineFlavor)
return flavor.Profiles, nil
case "", "virtualmachineclusterflavors", "virtualmachineclusterflavor":
obj, exists, err := clusterFlavorStore.GetByKey(name)
if err != nil {
return nil, err
}
if !exists {
return nil, errors.NewNotFound(flavorv1alpha1.Resource("virtualmachineclusterflavor"), name)
}
flavor := obj.(*flavorv1alpha1.VirtualMachineClusterFlavor)
return flavor.Profiles, nil
default:
return nil, fmt.Errorf("got unexpected kind in FlavorMatcher: %s", kind)
}
}
func applyCpu(field *k8sfield.Path, profile *flavorv1alpha1.VirtualMachineFlavorProfile, vmiSpec *virtv1.VirtualMachineInstanceSpec) Conflicts {
if profile.CPU == nil {
return nil
}
if vmiSpec.Domain.CPU != nil {
return Conflicts{field.Child("domain", "cpu")}
}
vmiSpec.Domain.CPU = profile.CPU.DeepCopy()
return nil
}