forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloudprofile.go
More file actions
117 lines (99 loc) · 4.19 KB
/
cloudprofile.go
File metadata and controls
117 lines (99 loc) · 4.19 KB
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
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package worker
import (
"fmt"
"slices"
v1beta1helper "github.com/gardener/gardener/pkg/api/core/v1beta1/helper"
"github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/gardener/gardener/pkg/apis/utils"
)
// CapabilitiesAccessor defines an interface for retrieving Capabilities.
type CapabilitiesAccessor interface {
GetCapabilities() v1beta1.Capabilities
}
// FindBestImageFlavor finds the most appropriate image version flavor based on the requested machine capabilities.
// The provided capability definitions are used for applying defaults.
func FindBestImageFlavor[T CapabilitiesAccessor](
providerImageFlavors []T,
machineCapabilities v1beta1.Capabilities,
capabilityDefinitions []v1beta1.CapabilityDefinition,
) (T, error) {
var zeroValue T
compatibleFlavors := filterCompatibleImageFlavors(providerImageFlavors, machineCapabilities, capabilityDefinitions)
if len(compatibleFlavors) == 0 {
return zeroValue, fmt.Errorf("no compatible flavor found")
}
bestFlavor, err := selectBestImageFlavor(compatibleFlavors, capabilityDefinitions)
if err != nil {
return zeroValue, err
}
return bestFlavor, nil
}
// filterCompatibleImageFlavors returns all image capabilityFlavors that are compatible with the given machine capabilities.
func filterCompatibleImageFlavors[T CapabilitiesAccessor](
imageFlavors []T,
machineCapabilities v1beta1.Capabilities,
capabilityDefinitions []v1beta1.CapabilityDefinition,
) []T {
return slices.Collect(utils.FilterElements(imageFlavors, func(imageFlavor T) bool {
return v1beta1helper.AreCapabilitiesCompatible(imageFlavor.GetCapabilities(), machineCapabilities, capabilityDefinitions)
}))
}
// selectBestImageFlavor selects the most appropriate image flavor based on the priority
// of its capabilities and their values as defined in capabilityDefinitions.
//
// Selection follows a priority-based approach:
// 1. Capabilities are ordered by priority in the definitions list (highest priority first)
// 2. Within each capability, values are ordered by preference (most preferred first)
// 3. Selection is determined by the first capability value difference found
func selectBestImageFlavor[T CapabilitiesAccessor](
compatibleSets []T,
capabilityDefinitions []v1beta1.CapabilityDefinition,
) (T, error) {
if len(compatibleSets) == 1 {
return compatibleSets[0], nil
}
type capabilitiesWithProviderType struct {
providerEntry T
capabilities v1beta1.Capabilities
}
capabilitiesWithProviderTypes := make([]capabilitiesWithProviderType, 0, len(compatibleSets))
for _, set := range compatibleSets {
capabilitiesWithProviderTypes = append(capabilitiesWithProviderTypes, capabilitiesWithProviderType{
providerEntry: set,
// Normalize capabilities copy by applying defaults
capabilities: v1beta1.GetCapabilitiesWithAppliedDefaults(set.GetCapabilities(), capabilityDefinitions),
})
}
// Evaluate flavor capabilities based on capability definitions priority
remainingSets := capabilitiesWithProviderTypes
// For each capability (in priority order)
for _, capabilityDef := range capabilityDefinitions {
// For each preferred value (in preference order)
for _, capabilityValue := range capabilityDef.Values {
var setsWithPreferredValue []capabilitiesWithProviderType
// Find sets that support this capability value
for _, set := range remainingSets {
if slices.Contains(set.capabilities[capabilityDef.Name], capabilityValue) {
setsWithPreferredValue = append(setsWithPreferredValue, set)
}
}
// If we found sets with this value, narrow down our selection
if len(setsWithPreferredValue) > 0 {
remainingSets = setsWithPreferredValue
// If only one set remains, we've found our match
if len(remainingSets) == 1 {
return remainingSets[0].providerEntry, nil
}
}
}
}
// If we couldn't determine a single best match, this indicates a problem with the cloud profile
if len(remainingSets) != 1 {
var zeroValue T
return zeroValue, fmt.Errorf("could not determine a unique capability flavor; this is usually attributed to an invalid CloudProfile")
}
return remainingSets[0].providerEntry, nil
}