-
Notifications
You must be signed in to change notification settings - Fork 450
/
helpers.go
299 lines (259 loc) · 10.7 KB
/
helpers.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 helper
import (
"fmt"
"github.com/gardener/gardener/pkg/apis/core"
"github.com/Masterminds/semver"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
)
// GetConditionIndex returns the index of the condition with the given <conditionType> out of the list of <conditions>.
// In case the required type could not be found, it returns -1.
func GetConditionIndex(conditions []core.Condition, conditionType core.ConditionType) int {
for index, condition := range conditions {
if condition.Type == conditionType {
return index
}
}
return -1
}
// GetCondition returns the condition with the given <conditionType> out of the list of <conditions>.
// In case the required type could not be found, it returns nil.
func GetCondition(conditions []core.Condition, conditionType core.ConditionType) *core.Condition {
if index := GetConditionIndex(conditions, conditionType); index != -1 {
return &conditions[index]
}
return nil
}
// QuotaScope returns the scope of a quota scope reference.
func QuotaScope(scopeRef corev1.ObjectReference) (string, error) {
if gvk := schema.FromAPIVersionAndKind(scopeRef.APIVersion, scopeRef.Kind); gvk.Group == "core.gardener.cloud" && gvk.Kind == "Project" {
return "project", nil
}
if scopeRef.APIVersion == "v1" && scopeRef.Kind == "Secret" {
return "secret", nil
}
return "", fmt.Errorf("unknown quota scope")
}
// DetermineLatestMachineImageVersions determines the latest versions (semVer) of the given machine images from a slice of machine images
func DetermineLatestMachineImageVersions(images []core.MachineImage) (map[string]core.MachineImageVersion, error) {
resultMapVersions := make(map[string]core.MachineImageVersion)
for _, image := range images {
latestMachineImageVersion, err := DetermineLatestMachineImageVersion(image.Versions, false)
if err != nil {
return nil, fmt.Errorf("failed to determine latest machine image version for image '%s': %v", image.Name, err)
}
resultMapVersions[image.Name] = latestMachineImageVersion
}
return resultMapVersions, nil
}
// DetermineLatestMachineImageVersion determines the latest MachineImageVersion from a slice of MachineImageVersion
// when filterPreviewVersions is set, versions with classification preview are not considered
func DetermineLatestMachineImageVersion(versions []core.MachineImageVersion, filterPreviewVersions bool) (core.MachineImageVersion, error) {
latestVersion, err := DetermineLatestExpirableVersion(ToExpirableVersions(versions), filterPreviewVersions)
if err != nil {
return core.MachineImageVersion{}, err
}
for _, version := range versions {
if version.Version == latestVersion.Version {
return version, nil
}
}
return core.MachineImageVersion{}, fmt.Errorf("the latest machine version has been removed")
}
// DetermineLatestExpirableVersion determines the latest ExpirableVersion from a slice of ExpirableVersions
// when filterPreviewVersions is set, versions with classification preview are not considered
func DetermineLatestExpirableVersion(versions []core.ExpirableVersion, filterPreviewVersions bool) (core.ExpirableVersion, error) {
var (
latestSemVerVersion *semver.Version
latestExpirableVersion core.ExpirableVersion
)
for _, version := range versions {
v, err := semver.NewVersion(version.Version)
if err != nil {
return core.ExpirableVersion{}, fmt.Errorf("error while parsing expirable version '%s': %s", version.Version, err.Error())
}
if filterPreviewVersions && version.Classification != nil && *version.Classification == core.ClassificationPreview {
continue
}
if latestSemVerVersion == nil || v.GreaterThan(latestSemVerVersion) {
latestSemVerVersion = v
latestExpirableVersion = version
}
}
if latestSemVerVersion == nil {
return core.ExpirableVersion{}, fmt.Errorf("unable to determine latest expirable version")
}
return latestExpirableVersion, nil
}
// ToExpirableVersions converts MachineImageVersion to ExpirableVersion
func ToExpirableVersions(versions []core.MachineImageVersion) []core.ExpirableVersion {
expirableVersions := []core.ExpirableVersion{}
for _, version := range versions {
expirableVersions = append(expirableVersions, version.ExpirableVersion)
}
return expirableVersions
}
// ShootWantsBasicAuthentication returns true if basic authentication is not configured or
// if it is set explicitly to 'true'.
func ShootWantsBasicAuthentication(kubeAPIServerConfig *core.KubeAPIServerConfig) bool {
if kubeAPIServerConfig == nil {
return true
}
if kubeAPIServerConfig.EnableBasicAuthentication == nil {
return true
}
return *kubeAPIServerConfig.EnableBasicAuthentication
}
// TaintsHave returns true if the given key is part of the taints list.
func TaintsHave(taints []core.SeedTaint, key string) bool {
for _, taint := range taints {
if taint.Key == key {
return true
}
}
return false
}
// TaintsAreTolerated returns true when all the given taints are tolerated by the given tolerations.
func TaintsAreTolerated(taints []core.SeedTaint, tolerations []core.Toleration) bool {
if len(taints) == 0 {
return true
}
if len(taints) > len(tolerations) {
return false
}
tolerationKeyValues := make(map[string]string, len(tolerations))
for _, toleration := range tolerations {
v := ""
if toleration.Value != nil {
v = *toleration.Value
}
tolerationKeyValues[toleration.Key] = v
}
for _, taint := range taints {
tolerationValue, ok := tolerationKeyValues[taint.Key]
if !ok {
return false
}
if taint.Value != nil && *taint.Value != tolerationValue {
return false
}
}
return true
}
// SeedSettingExcessCapacityReservationEnabled returns true if the 'excess capacity reservation' setting is enabled.
func SeedSettingExcessCapacityReservationEnabled(settings *core.SeedSettings) bool {
return settings == nil || settings.ExcessCapacityReservation == nil || settings.ExcessCapacityReservation.Enabled
}
// SeedSettingSchedulingVisible returns true if the 'scheduling' setting is set to 'visible'.
func SeedSettingSchedulingVisible(settings *core.SeedSettings) bool {
return settings == nil || settings.Scheduling == nil || settings.Scheduling.Visible
}
// SeedSettingShootDNSEnabled returns true if the 'shoot dns' setting is enabled.
func SeedSettingShootDNSEnabled(settings *core.SeedSettings) bool {
return settings == nil || settings.ShootDNS == nil || settings.ShootDNS.Enabled
}
// ShootUsesUnmanagedDNS returns true if the shoot's DNS section is marked as 'unmanaged'.
func ShootUsesUnmanagedDNS(shoot *core.Shoot) bool {
if shoot.Spec.DNS == nil {
return false
}
primary := FindPrimaryDNSProvider(shoot.Spec.DNS.Providers)
if primary != nil {
return *primary.Primary && primary.Type != nil && *primary.Type == core.DNSUnmanaged
}
return len(shoot.Spec.DNS.Providers) > 0 && shoot.Spec.DNS.Providers[0].Type != nil && *shoot.Spec.DNS.Providers[0].Type == core.DNSUnmanaged
}
// FindPrimaryDNSProvider finds the primary provider among the given `providers`.
// It returns the first provider if multiple candidates are found.
func FindPrimaryDNSProvider(providers []core.DNSProvider) *core.DNSProvider {
for _, provider := range providers {
if provider.Primary != nil && *provider.Primary {
primaryProvider := provider
return &primaryProvider
}
}
return nil
}
// FindWorkerByName tries to find the worker with the given name. If it cannot be found it returns nil.
func FindWorkerByName(workers []core.Worker, name string) *core.Worker {
for _, w := range workers {
if w.Name == name {
return &w
}
}
return nil
}
// GetRemovedVersions finds versions that have been removed in the old compared to the new version slice.
// returns a map associating the version with its index in the in the old version slice.
func GetRemovedVersions(old, new []core.ExpirableVersion) map[string]int {
return getVersionDiff(old, new)
}
// GetAddedVersions finds versions that have been added in the new compared to the new version slice.
// returns a map associating the version with its index in the in the old version slice.
func GetAddedVersions(old, new []core.ExpirableVersion) map[string]int {
return getVersionDiff(new, old)
}
// getVersionDiff gets versions that are in v1 but not in v2.
// Returns versions mapped to their index in v1.
func getVersionDiff(v1, v2 []core.ExpirableVersion) map[string]int {
v2Versions := sets.String{}
for _, x := range v2 {
v2Versions.Insert(x.Version)
}
diff := map[string]int{}
for index, x := range v1 {
if !v2Versions.Has(x.Version) {
diff[x.Version] = index
}
}
return diff
}
// FilterVersionsWithClassification filters versions for a classification
func FilterVersionsWithClassification(versions []core.ExpirableVersion, classification core.VersionClassification) []core.ExpirableVersion {
var result []core.ExpirableVersion
for _, version := range versions {
if version.Classification == nil || *version.Classification != classification {
continue
}
result = append(result, version)
}
return result
}
// FindVersionsWithSameMajorMinor filters the given versions slice for versions other the given one, having the same major and minor version as the given version
func FindVersionsWithSameMajorMinor(versions []core.ExpirableVersion, version semver.Version) ([]core.ExpirableVersion, error) {
var result []core.ExpirableVersion
for _, v := range versions {
// semantic version already checked by validator
semVer, err := semver.NewVersion(v.Version)
if err != nil {
return nil, err
}
if semVer.Equal(&version) || semVer.Minor() != version.Minor() || semVer.Major() != version.Major() {
continue
}
result = append(result, v)
}
return result, nil
}
// HibernationIsEnabled checks if the given shoot's desired state is hibernated.
func HibernationIsEnabled(shoot *core.Shoot) bool {
return shoot.Spec.Hibernation != nil && shoot.Spec.Hibernation.Enabled != nil && *shoot.Spec.Hibernation.Enabled
}
// SystemComponentsAllowed checks if the given worker allows system components to be scheduled onto it
func SystemComponentsAllowed(worker *core.Worker) bool {
return worker.SystemComponents == nil || worker.SystemComponents.Allow
}