-
Notifications
You must be signed in to change notification settings - Fork 79
/
helper.go
195 lines (168 loc) · 7.63 KB
/
helper.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package helper
import (
"fmt"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
"k8s.io/utils/ptr"
api "github.com/gardener/gardener-extension-provider-openstack/pkg/apis/openstack"
"github.com/gardener/gardener-extension-provider-openstack/pkg/utils"
)
// FindSubnetByPurpose takes a list of subnets and tries to find the first entry
// whose purpose matches with the given purpose. If no such entry is found then an error will be
// returned.
func FindSubnetByPurpose(subnets []api.Subnet, purpose api.Purpose) (*api.Subnet, error) {
for _, subnet := range subnets {
if subnet.Purpose == purpose {
return &subnet, nil
}
}
return nil, fmt.Errorf("cannot find subnet with purpose %q", purpose)
}
// FindSecurityGroupByPurpose takes a list of security groups and tries to find the first entry
// whose purpose matches with the given purpose. If no such entry is found then an error will be
// returned.
func FindSecurityGroupByPurpose(securityGroups []api.SecurityGroup, purpose api.Purpose) (*api.SecurityGroup, error) {
for _, securityGroup := range securityGroups {
if securityGroup.Purpose == purpose {
return &securityGroup, nil
}
}
return nil, fmt.Errorf("cannot find security group with purpose %q", purpose)
}
// FindMachineImage takes a list of machine images and tries to find the first entry
// whose name, version, and zone matches with the given name, version, and cloud profile. If no such
// entry is found then an error will be returned.
func FindMachineImage(machineImages []api.MachineImage, name, version, architecture string) (*api.MachineImage, error) {
for _, machineImage := range machineImages {
// If the architecture field is not present, ignore it for backwards-compatibility.
if machineImage.Name == name && machineImage.Version == version &&
(machineImage.Architecture == nil || *machineImage.Architecture == architecture) {
return &machineImage, nil
}
}
return nil, fmt.Errorf("no machine image with name %q, version %q found", name, version)
}
// FindImageFromCloudProfile takes a list of machine images, and the desired image name and version. It tries
// to find the image with the given name and version in the desired cloud profile. If it cannot be found then an error
// is returned.
func FindImageFromCloudProfile(cloudProfileConfig *api.CloudProfileConfig, imageName, imageVersion, regionName, architecture string) (*api.MachineImage, error) {
if cloudProfileConfig != nil {
for _, machineImage := range cloudProfileConfig.MachineImages {
if machineImage.Name != imageName {
continue
}
for _, version := range machineImage.Versions {
if imageVersion != version.Version {
continue
}
for _, region := range version.Regions {
if regionName == region.Name && architecture == ptr.Deref(region.Architecture, v1beta1constants.ArchitectureAMD64) {
return &api.MachineImage{
Name: imageName,
Version: imageVersion,
Architecture: &architecture,
ID: region.ID,
}, nil
}
}
// if we haven't found a region mapping, fallback to the image name
if version.Image != "" && architecture == v1beta1constants.ArchitectureAMD64 {
// The fallback image name doesn't specify an architecture, but we assume it is amd64 as arm was not supported
// previously.
// Referencing images by name is error-prone and is highly discouraged anyways.
// If people want to use arm images in their CloudProfile, they need to specify a region mapping and can't
// use the fallback MachineImage by name.
return &api.MachineImage{
Name: imageName,
Version: imageVersion,
Architecture: ptr.To(v1beta1constants.ArchitectureAMD64),
Image: version.Image,
}, nil
}
}
}
}
return nil, fmt.Errorf("could not find an image for name %q in version %q for region %q", imageName, imageVersion, regionName)
}
// FindKeyStoneURL takes a list of keystone URLs and tries to find the first entry
// whose region matches with the given region. If no such entry is found then it tries to use the non-regional
// keystone URL. If this is not specified then an error will be returned.
func FindKeyStoneURL(keyStoneURLs []api.KeyStoneURL, keystoneURL, region string) (string, error) {
for _, keyStoneURL := range keyStoneURLs {
if keyStoneURL.Region == region {
return keyStoneURL.URL, nil
}
}
if len(keystoneURL) > 0 {
return keystoneURL, nil
}
return "", fmt.Errorf("cannot find keystone URL for region %q", region)
}
// FindKeyStoneCACert takes a list of keystone URLs and tries to find the first entry
// whose region matches with the given region and returns the CA cert for this region. If no such entry is found then it
// tries to use the non-regional value.
func FindKeyStoneCACert(keyStoneURLs []api.KeyStoneURL, keystoneCABundle *string, region string) *string {
for _, keyStoneURL := range keyStoneURLs {
if keyStoneURL.Region == region && keyStoneURL.CACert != nil && len(*keyStoneURL.CACert) > 0 {
return keyStoneURL.CACert
}
}
return keystoneCABundle
}
// FindFloatingPool receives a list of floating pools and tries to find the best
// match for a given `floatingPoolNamePattern` considering constraints like
// `region` and `domain`. If no matching floating pool was found then an error will be returned.
func FindFloatingPool(floatingPools []api.FloatingPool, floatingPoolNamePattern, region string, domain *string) (*api.FloatingPool, error) {
var (
floatingPoolCandidate *api.FloatingPool
maxCandidateScore int
nonConstrainingFloatingPools []api.FloatingPool
)
for _, f := range floatingPools {
var fip = f
// Check non constraining floating pools with second priority
// which means only when no other floating pool is matching.
if fip.NonConstraining != nil && *fip.NonConstraining {
nonConstrainingFloatingPools = append(nonConstrainingFloatingPools, fip)
continue
}
if candidate, score := checkFloatingPoolCandidate(&fip, floatingPoolNamePattern, region, domain); candidate != nil && score > maxCandidateScore {
floatingPoolCandidate = candidate
maxCandidateScore = score
}
}
if floatingPoolCandidate != nil {
return floatingPoolCandidate, nil
}
// So far no floating pool was matching to the `floatingPoolNamePattern`
// therefore try now if there is a non contraining floating pool matching.
for _, f := range nonConstrainingFloatingPools {
var fip = f
if candidate, score := checkFloatingPoolCandidate(&fip, floatingPoolNamePattern, region, domain); candidate != nil && score > maxCandidateScore {
floatingPoolCandidate = candidate
maxCandidateScore = score
}
}
if floatingPoolCandidate != nil {
return floatingPoolCandidate, nil
}
return nil, fmt.Errorf("cannot find a matching floating pool for pattern %q", floatingPoolNamePattern)
}
func checkFloatingPoolCandidate(floatingPool *api.FloatingPool, floatingPoolNamePattern, region string, domain *string) (*api.FloatingPool, int) {
// If the domain should be considered then only floating pools
// in the same domain will be considered.
if domain != nil && !utils.IsStringPtrValueEqual(floatingPool.Domain, *domain) {
return nil, 0
}
// Require floating pools are in the same region.
if !utils.IsStringPtrValueEqual(floatingPool.Region, region) {
return nil, 0
}
// Check that the name of the current floatingPool is matching to the `floatingPoolNamePattern`.
if isMatching, score := utils.SimpleMatch(floatingPool.Name, floatingPoolNamePattern); isMatching {
return floatingPool, score
}
return nil, 0
}