/
reconciler.go
602 lines (502 loc) · 25.4 KB
/
reconciler.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
// 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 maintenance
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/Masterminds/semver"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
"k8s.io/utils/clock"
"k8s.io/utils/pointer"
"k8s.io/utils/strings/slices"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
v1beta1helper "github.com/gardener/gardener/pkg/apis/core/v1beta1/helper"
"github.com/gardener/gardener/pkg/controllermanager/apis/config"
"github.com/gardener/gardener/pkg/controllerutils"
gardenerutils "github.com/gardener/gardener/pkg/utils/gardener"
kubernetesutils "github.com/gardener/gardener/pkg/utils/kubernetes"
)
// Reconciler reconciles Shoots and maintains them by updating versions or triggering operations.
type Reconciler struct {
Client client.Client
Config config.ShootMaintenanceControllerConfiguration
Clock clock.Clock
Recorder record.EventRecorder
}
// Reconcile reconciles Shoots and maintains them by updating versions or triggering operations.
func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
log := logf.FromContext(ctx)
shoot := &gardencorev1beta1.Shoot{}
if err := r.Client.Get(ctx, request.NamespacedName, shoot); err != nil {
if apierrors.IsNotFound(err) {
log.V(1).Info("Object is gone, stop reconciling")
return reconcile.Result{}, nil
}
return reconcile.Result{}, fmt.Errorf("error retrieving object from store: %w", err)
}
if shoot.DeletionTimestamp != nil {
log.V(1).Info("Skipping Shoot because it is marked for deletion")
return reconcile.Result{}, nil
}
requeueAfter, nextMaintenance := requeueAfterDuration(shoot)
if !mustMaintainNow(shoot, r.Clock) {
log.V(1).Info("Skipping Shoot because it doesn't need to be maintained now")
log.V(1).Info("Scheduled next maintenance for Shoot", "duration", requeueAfter.Round(time.Minute), "nextMaintenance", nextMaintenance.Round(time.Minute))
return reconcile.Result{RequeueAfter: requeueAfter}, nil
}
if err := r.reconcile(ctx, log, shoot); err != nil {
return reconcile.Result{}, err
}
log.V(1).Info("Scheduled next maintenance for Shoot", "duration", requeueAfter.Round(time.Minute), "nextMaintenance", nextMaintenance.Round(time.Minute))
return reconcile.Result{RequeueAfter: requeueAfter}, nil
}
func requeueAfterDuration(shoot *gardencorev1beta1.Shoot) (time.Duration, time.Time) {
var (
now = time.Now()
window = gardenerutils.EffectiveShootMaintenanceTimeWindow(shoot)
duration = window.RandomDurationUntilNext(now, false)
nextMaintenance = time.Now().UTC().Add(duration)
)
return duration, nextMaintenance
}
func (r *Reconciler) reconcile(ctx context.Context, log logr.Logger, shoot *gardencorev1beta1.Shoot) error {
log.Info("Maintaining Shoot")
var (
maintainedShoot = shoot.DeepCopy()
operations []string
)
cloudProfile := &gardencorev1beta1.CloudProfile{}
if err := r.Client.Get(ctx, kubernetesutils.Key(shoot.Spec.CloudProfileName), cloudProfile); err != nil {
return err
}
reasonForImageUpdatePerPool, err := maintainMachineImages(log, maintainedShoot, cloudProfile)
if err != nil {
// continue execution to allow the kubernetes version update
log.Error(err, "Failed to maintain Shoot machine images")
}
operations = append(operations, reasonForImageUpdatePerPool...)
reasonForKubernetesUpdate, err := maintainKubernetesVersion(log, maintainedShoot.Spec.Kubernetes.Version, maintainedShoot.Spec.Maintenance.AutoUpdate.KubernetesVersion, cloudProfile, func(v string) error {
maintainedShoot.Spec.Kubernetes.Version = v
return nil
}, "Control Plane")
if err != nil {
// continue execution to allow the machine image version update
log.Error(err, "Failed to maintain Shoot kubernetes version")
}
shootKubernetesVersion, err := semver.NewVersion(maintainedShoot.Spec.Kubernetes.Version)
if err != nil {
return err
}
// Now it's time to update worker pool kubernetes version if specified
for i, pool := range maintainedShoot.Spec.Provider.Workers {
if pool.Kubernetes == nil || pool.Kubernetes.Version == nil {
continue
}
workerLog := log.WithValues("worker", pool.Name)
name := "Worker Pool " + pool.Name
reasonForWorkerPoolKubernetesUpdate, err := maintainKubernetesVersion(workerLog, *pool.Kubernetes.Version, maintainedShoot.Spec.Maintenance.AutoUpdate.KubernetesVersion, cloudProfile, func(v string) error {
workerPoolSemver, err := semver.NewVersion(v)
if err != nil {
return err
}
// If during autoupdate a worker pool kubernetes gets forcefully updated to the next minor which might be higher than the same minor of the shoot, take this
if workerPoolSemver.GreaterThan(shootKubernetesVersion) {
workerPoolSemver = shootKubernetesVersion
}
v = workerPoolSemver.String()
maintainedShoot.Spec.Provider.Workers[i].Kubernetes.Version = &v
return nil
}, name)
if err != nil {
// continue execution to allow the machine image version update
workerLog.Error(err, "Could not maintain kubernetes version for worker pool")
}
reasonForKubernetesUpdate = append(reasonForKubernetesUpdate, reasonForWorkerPoolKubernetesUpdate...)
}
operations = append(operations, reasonForKubernetesUpdate...)
operation := maintainOperation(maintainedShoot)
if operation != "" {
operations = append(operations, fmt.Sprintf("Added %q operation annotation", operation))
}
if len(operations) > 0 {
patch := client.MergeFrom(shoot.DeepCopy())
shoot.Status.LastMaintenance = &gardencorev1beta1.LastMaintenance{
Description: strings.Join(operations, ", "),
TriggeredTime: metav1.Time{Time: r.Clock.Now()},
State: gardencorev1beta1.LastOperationStateProcessing,
}
// First dry run the update call to check if it can be executed successfully.
// If not shoot maintenance is marked as failed and is retried only in
// next maintenance window.
if err := r.Client.Update(ctx, maintainedShoot, &client.UpdateOptions{
DryRun: []string{metav1.DryRunAll},
}); err != nil {
// If shoot maintenance is triggered by `gardener.cloud/operation=maintain` annotation and if it fails in dry run,
// `maintain` operation annotation needs to be removed so that if reason for failure is fixed and maintenance is triggered
// again via `maintain` operation annotation then it should not fail with the reason that annotation is already present.
// Removal of annotation during shoot status patch is possible cause only spec is kept in original form during status update
// https://github.com/gardener/gardener/blob/a2f7de0badaae6170d7b9b84c163b8cab43a84d2/pkg/registry/core/shoot/strategy.go#L258-L267
if hasMaintainNowAnnotation(shoot) {
delete(shoot.Annotations, v1beta1constants.GardenerOperation)
}
shoot.Status.LastMaintenance.State = gardencorev1beta1.LastOperationStateFailed
shoot.Status.LastMaintenance.FailureReason = pointer.String(fmt.Sprintf("Maintenance failed: %s", err.Error()))
if err := r.Client.Status().Patch(ctx, shoot, patch); err != nil {
return err
}
log.Info("Shoot maintenance failed", "reason", err)
return nil
}
if err := r.Client.Status().Patch(ctx, shoot, patch); err != nil {
return err
}
}
// update shoot spec changes in maintenance call
shoot.Spec = *maintainedShoot.Spec.DeepCopy()
_ = maintainOperation(shoot)
maintainTasks(shoot, r.Config)
// try to maintain shoot, but don't retry on conflict, because a conflict means that we potentially operated on stale
// data (e.g. when calculating the updated k8s version), so rather return error and backoff
if err := r.Client.Update(ctx, shoot); err != nil {
r.Recorder.Event(shoot, corev1.EventTypeWarning, gardencorev1beta1.ShootMaintenanceFailed, err.Error())
return err
}
if shoot.Status.LastMaintenance != nil && shoot.Status.LastMaintenance.State == gardencorev1beta1.LastOperationStateProcessing {
patch := client.MergeFrom(shoot.DeepCopy())
shoot.Status.LastMaintenance.State = gardencorev1beta1.LastOperationStateSucceeded
if err := r.Client.Status().Patch(ctx, shoot, patch); err != nil {
return err
}
}
for _, reason := range reasonForImageUpdatePerPool {
r.Recorder.Eventf(shoot, corev1.EventTypeNormal, gardencorev1beta1.ShootEventImageVersionMaintenance, "%s",
fmt.Sprintf("Updated %s.", reason))
}
for _, reason := range reasonForKubernetesUpdate {
r.Recorder.Eventf(shoot, corev1.EventTypeNormal, gardencorev1beta1.ShootEventK8sVersionMaintenance, "%s", reason)
}
log.Info("Shoot maintenance completed")
return nil
}
func maintainOperation(shoot *gardencorev1beta1.Shoot) string {
var operation string
if hasMaintainNowAnnotation(shoot) {
delete(shoot.Annotations, v1beta1constants.GardenerOperation)
}
if shoot.Status.LastOperation == nil {
return ""
}
switch {
case shoot.Status.LastOperation.State == gardencorev1beta1.LastOperationStateFailed:
if needsRetry(shoot) {
metav1.SetMetaDataAnnotation(&shoot.ObjectMeta, v1beta1constants.GardenerOperation, v1beta1constants.ShootOperationRetry)
delete(shoot.Annotations, v1beta1constants.FailedShootNeedsRetryOperation)
}
default:
operation = getOperation(shoot)
metav1.SetMetaDataAnnotation(&shoot.ObjectMeta, v1beta1constants.GardenerOperation, operation)
delete(shoot.Annotations, v1beta1constants.GardenerMaintenanceOperation)
}
if operation == v1beta1constants.GardenerOperationReconcile {
return ""
}
return operation
}
func maintainTasks(shoot *gardencorev1beta1.Shoot, config config.ShootMaintenanceControllerConfiguration) {
controllerutils.AddTasks(shoot.Annotations,
v1beta1constants.ShootTaskDeployInfrastructure,
v1beta1constants.ShootTaskDeployDNSRecordInternal,
v1beta1constants.ShootTaskDeployDNSRecordExternal,
v1beta1constants.ShootTaskDeployDNSRecordIngress,
)
if pointer.BoolDeref(config.EnableShootControlPlaneRestarter, false) {
controllerutils.AddTasks(shoot.Annotations, v1beta1constants.ShootTaskRestartControlPlanePods)
}
if pointer.BoolDeref(config.EnableShootCoreAddonRestarter, false) {
controllerutils.AddTasks(shoot.Annotations, v1beta1constants.ShootTaskRestartCoreAddons)
}
}
// maintainMachineImages updates the machine images of a Shoot's worker pools if necessary
func maintainMachineImages(log logr.Logger, shoot *gardencorev1beta1.Shoot, cloudProfile *gardencorev1beta1.CloudProfile) ([]string, error) {
var reasonsForUpdate []string
controlPlaneVersion, err := semver.NewVersion(shoot.Spec.Kubernetes.Version)
if err != nil {
return nil, err
}
for i, worker := range shoot.Spec.Provider.Workers {
workerImage := worker.Machine.Image
workerLog := log.WithValues("worker", worker.Name, "image", workerImage.Name, "version", workerImage.Version)
machineImageFromCloudProfile, err := determineMachineImage(cloudProfile, workerImage)
if err != nil {
return nil, err
}
kubeletVersion, err := v1beta1helper.CalculateEffectiveKubernetesVersion(controlPlaneVersion, worker.Kubernetes)
if err != nil {
return nil, err
}
filteredMachineImageVersionsFromCloudProfile := filterForArchitecture(&machineImageFromCloudProfile, worker.Machine.Architecture)
filteredMachineImageVersionsFromCloudProfile = filterForCRI(filteredMachineImageVersionsFromCloudProfile, worker.CRI)
filteredMachineImageVersionsFromCloudProfile = filterForKubeleteVersionConstraint(filteredMachineImageVersionsFromCloudProfile, kubeletVersion)
shouldBeUpdated, reason, updatedMachineImage, err := shouldMachineImageBeUpdated(workerLog, shoot.Spec.Maintenance.AutoUpdate.MachineImageVersion, filteredMachineImageVersionsFromCloudProfile, workerImage)
if err != nil {
return nil, err
}
if !shouldBeUpdated {
continue
}
shoot.Spec.Provider.Workers[i].Machine.Image = updatedMachineImage
workerLog.Info("MachineImage will be updated", "newVersion", *updatedMachineImage.Version, "reason", reason)
reasonsForUpdate = append(reasonsForUpdate, fmt.Sprintf("Machine image of worker-pool %q upgraded from %q version %q to version %q. Reason: %s", worker.Name, workerImage.Name, *workerImage.Version, *updatedMachineImage.Version, reason))
}
return reasonsForUpdate, nil
}
// maintainKubernetesVersion updates a Shoot's Kubernetes version if necessary and returns the reason why an update was done
func maintainKubernetesVersion(log logr.Logger, kubernetesVersion string, autoUpdate bool, profile *gardencorev1beta1.CloudProfile, updateFunc func(string) error, name string) ([]string, error) {
var reasonsForUpdate []string
shouldBeUpdated, reason, isExpired, err := shouldKubernetesVersionBeUpdated(kubernetesVersion, autoUpdate, profile)
if err != nil {
return reasonsForUpdate, err
}
if !shouldBeUpdated {
return reasonsForUpdate, nil
}
updatedKubernetesVersion, err := determineKubernetesVersion(kubernetesVersion, profile, isExpired)
if err != nil {
return reasonsForUpdate, err
}
if updatedKubernetesVersion == "" {
return reasonsForUpdate, nil
}
err = updateFunc(updatedKubernetesVersion)
if err != nil {
return reasonsForUpdate, err
}
reason = fmt.Sprintf("For %q: Kubernetes version upgraded %q to version %q. Reason: %s", name, kubernetesVersion, updatedKubernetesVersion, reason)
reasonsForUpdate = append(reasonsForUpdate, reason)
log.Info("Kubernetes version will be updated", "version", kubernetesVersion, "newVersion", updatedKubernetesVersion, "reason", reason)
return reasonsForUpdate, err
}
func determineKubernetesVersion(kubernetesVersion string, profile *gardencorev1beta1.CloudProfile, isExpired bool) (string, error) {
// get latest version that qualifies for a patch update
newerPatchVersionFound, latestPatchVersion, err := v1beta1helper.GetKubernetesVersionForPatchUpdate(profile, kubernetesVersion)
if err != nil {
return "", fmt.Errorf("failure while determining the latest Kubernetes patch version in the CloudProfile: %s", err.Error())
}
if newerPatchVersionFound {
return latestPatchVersion, nil
}
// no newer patch version found & is expired -> forcefully update to latest patch of next minor version
if isExpired {
// get latest version that qualifies for a minor update
newMinorAvailable, latestPatchVersionNewMinor, err := v1beta1helper.GetKubernetesVersionForMinorUpdate(profile, kubernetesVersion)
if err != nil {
return "", fmt.Errorf("failure while determining newer Kubernetes minor version in the CloudProfile: %s", err.Error())
}
// cannot update as there is no consecutive minor version available (e.g shoot is on 1.20.X, but there is only 1.22.X, available and not 1.21.X)
if !newMinorAvailable {
return "", fmt.Errorf("cannot perform minor Kubernetes version update for expired Kubernetes version %q. No suitable version found in CloudProfile - this is most likely a misconfiguration of the CloudProfile", kubernetesVersion)
}
return latestPatchVersionNewMinor, nil
}
return "", nil
}
func shouldKubernetesVersionBeUpdated(kubernetesVersion string, autoUpdate bool, profile *gardencorev1beta1.CloudProfile) (shouldBeUpdated bool, reason string, isExpired bool, error error) {
versionExistsInCloudProfile, version, err := v1beta1helper.KubernetesVersionExistsInCloudProfile(profile, kubernetesVersion)
if err != nil {
return false, "", false, err
}
var updateReason string
if !versionExistsInCloudProfile {
updateReason = "Version does not exist in CloudProfile"
return true, updateReason, true, nil
}
if ExpirationDateExpired(version.ExpirationDate) {
updateReason = "Kubernetes version expired - force update required"
return true, updateReason, true, nil
}
if autoUpdate {
updateReason = "AutoUpdate of Kubernetes version configured"
return true, updateReason, false, nil
}
return false, "", false, nil
}
func mustMaintainNow(shoot *gardencorev1beta1.Shoot, clock clock.Clock) bool {
return hasMaintainNowAnnotation(shoot) || gardenerutils.IsNowInEffectiveShootMaintenanceTimeWindow(shoot, clock)
}
func hasMaintainNowAnnotation(shoot *gardencorev1beta1.Shoot) bool {
operation, ok := shoot.Annotations[v1beta1constants.GardenerOperation]
return ok && operation == v1beta1constants.ShootOperationMaintain
}
func needsRetry(shoot *gardencorev1beta1.Shoot) bool {
needsRetryOperation := false
if val, ok := shoot.Annotations[v1beta1constants.FailedShootNeedsRetryOperation]; ok {
needsRetryOperation, _ = strconv.ParseBool(val)
}
return needsRetryOperation
}
func getOperation(shoot *gardencorev1beta1.Shoot) string {
var (
operation = v1beta1constants.GardenerOperationReconcile
maintenanceOperation = shoot.Annotations[v1beta1constants.GardenerMaintenanceOperation]
)
if maintenanceOperation != "" {
operation = maintenanceOperation
}
return operation
}
func filterForArchitecture(machineImageFromCloudProfile *gardencorev1beta1.MachineImage, arch *string) *gardencorev1beta1.MachineImage {
filteredMachineImages := gardencorev1beta1.MachineImage{Name: machineImageFromCloudProfile.Name,
Versions: []gardencorev1beta1.MachineImageVersion{}}
for _, cloudProfileVersion := range machineImageFromCloudProfile.Versions {
if slices.Contains(cloudProfileVersion.Architectures, *arch) {
filteredMachineImages.Versions = append(filteredMachineImages.Versions, cloudProfileVersion)
}
}
return &filteredMachineImages
}
func filterForCRI(machineImageFromCloudProfile *gardencorev1beta1.MachineImage, workerCRI *gardencorev1beta1.CRI) *gardencorev1beta1.MachineImage {
if workerCRI == nil {
return filterForCRI(machineImageFromCloudProfile, &gardencorev1beta1.CRI{Name: gardencorev1beta1.CRINameDocker})
}
filteredMachineImages := gardencorev1beta1.MachineImage{Name: machineImageFromCloudProfile.Name,
Versions: []gardencorev1beta1.MachineImageVersion{}}
for _, cloudProfileVersion := range machineImageFromCloudProfile.Versions {
criFromCloudProfileVersion, found := findCRIByName(workerCRI.Name, cloudProfileVersion.CRI)
if !found {
continue
}
if !areAllWorkerCRsPartOfCloudProfileVersion(workerCRI.ContainerRuntimes, criFromCloudProfileVersion.ContainerRuntimes) {
continue
}
filteredMachineImages.Versions = append(filteredMachineImages.Versions, cloudProfileVersion)
}
return &filteredMachineImages
}
func filterForKubeleteVersionConstraint(machineImageFromCloudProfile *gardencorev1beta1.MachineImage, kubeletVersion *semver.Version) *gardencorev1beta1.MachineImage {
filteredMachineImages := gardencorev1beta1.MachineImage{Name: machineImageFromCloudProfile.Name,
Versions: []gardencorev1beta1.MachineImageVersion{}}
for _, cloudProfileVersion := range machineImageFromCloudProfile.Versions {
if cloudProfileVersion.KubeletVersionConstraint != nil {
// CloudProfile cannot contain an invalid kubeletVersionConstraint
constraint, _ := semver.NewConstraint(*cloudProfileVersion.KubeletVersionConstraint)
if !constraint.Check(kubeletVersion) {
continue
}
}
filteredMachineImages.Versions = append(filteredMachineImages.Versions, cloudProfileVersion)
}
return &filteredMachineImages
}
func findCRIByName(wanted gardencorev1beta1.CRIName, cris []gardencorev1beta1.CRI) (gardencorev1beta1.CRI, bool) {
for _, cri := range cris {
if cri.Name == wanted {
return cri, true
}
}
return gardencorev1beta1.CRI{}, false
}
func areAllWorkerCRsPartOfCloudProfileVersion(workerCRs []gardencorev1beta1.ContainerRuntime, crsFromCloudProfileVersion []gardencorev1beta1.ContainerRuntime) bool {
if workerCRs == nil {
return true
}
for _, workerCr := range workerCRs {
if !isWorkerCRPartOfCloudProfileVersionCRs(workerCr, crsFromCloudProfileVersion) {
return false
}
}
return true
}
func isWorkerCRPartOfCloudProfileVersionCRs(wanted gardencorev1beta1.ContainerRuntime, cloudProfileVersionCRs []gardencorev1beta1.ContainerRuntime) bool {
for _, cr := range cloudProfileVersionCRs {
if wanted.Type == cr.Type {
return true
}
}
return false
}
func determineMachineImage(cloudProfile *gardencorev1beta1.CloudProfile, shootMachineImage *gardencorev1beta1.ShootMachineImage) (gardencorev1beta1.MachineImage, error) {
machineImagesFound, machineImageFromCloudProfile := v1beta1helper.DetermineMachineImageForName(cloudProfile, shootMachineImage.Name)
if !machineImagesFound {
return gardencorev1beta1.MachineImage{}, fmt.Errorf("failure while determining the default machine image in the CloudProfile: no machineImage with name %q (specified in shoot) could be found in the cloudProfile %q", shootMachineImage.Name, cloudProfile.Name)
}
return machineImageFromCloudProfile, nil
}
// shouldMachineImageBeUpdated determines if a machine image should be updated based on whether it exists in the CloudProfile, auto update applies or a force update is required.
func shouldMachineImageBeUpdated(log logr.Logger, autoUpdateMachineImageVersion bool, machineImage *gardencorev1beta1.MachineImage, shootMachineImage *gardencorev1beta1.ShootMachineImage) (shouldBeUpdated bool, reason string, updatedMachineImage *gardencorev1beta1.ShootMachineImage, error error) {
versionExistsInCloudProfile, versionIndex := v1beta1helper.ShootMachineImageVersionExists(*machineImage, *shootMachineImage)
var reasonForUpdate string
forceUpdateRequired := ForceMachineImageUpdateRequired(shootMachineImage, *machineImage)
if !versionExistsInCloudProfile || autoUpdateMachineImageVersion || forceUpdateRequired {
// safe operation, as Shoot machine image version can only be a valid semantic version
shootSemanticVersion := *semver.MustParse(*shootMachineImage.Version)
// get latest version qualifying for Shoot machine image update
qualifyingVersionFound, latestShootMachineImage, err := v1beta1helper.GetLatestQualifyingShootMachineImage(*machineImage, v1beta1helper.FilterLowerVersion(shootSemanticVersion))
if err != nil {
return false, "", nil, fmt.Errorf("an error occured while determining the latest Shoot Machine Image for machine image %q: %w", machineImage.Name, err)
}
// this is a special case when a Shoot is using a preview version. Preview versions should not be updated-to and are therefore not part of the qualifying versions.
// if no qualifying version can be found and the Shoot is already using a preview version, then do nothing.
if !qualifyingVersionFound && versionExistsInCloudProfile && machineImage.Versions[versionIndex].Classification != nil && *machineImage.Versions[versionIndex].Classification == gardencorev1beta1.ClassificationPreview {
log.V(1).Info("MachineImage update not required, shoot worker is already using preview version")
return false, "", nil, nil
}
// otherwise, there should always be a qualifying version (at least the Shoot's machine image version itself).
if !qualifyingVersionFound {
return false, "", nil, fmt.Errorf("no latest qualifying Shoot machine image could be determined for machine image %q. Either the machine image is reaching end of life and migration to another machine image is required or there is a misconfiguration in the CloudProfile. If it is the latter, make sure the machine image in the CloudProfile has at least one version that is not expired, not in preview and greater or equal to the current Shoot image version %q", machineImage.Name, *shootMachineImage.Version)
}
if *latestShootMachineImage.Version == *shootMachineImage.Version {
log.V(1).Info("MachineImage update not required, shoot worker is already up to date")
return false, "", nil, nil
}
if !versionExistsInCloudProfile {
// deletion a machine image that is still in use by a Shoot is blocked in the apiserver. However it is still required,
// because old installations might still have shoot's that have no corresponding version in the CloudProfile.
reasonForUpdate = "Version does not exist in CloudProfile"
} else if autoUpdateMachineImageVersion {
reasonForUpdate = "AutoUpdate of MachineImage configured"
} else if forceUpdateRequired {
reasonForUpdate = "MachineImage expired - force update required"
}
return true, reasonForUpdate, latestShootMachineImage, nil
}
return false, "", nil, nil
}
// ForceMachineImageUpdateRequired checks if the shoots current machine image has to be forcefully updated
func ForceMachineImageUpdateRequired(shootCurrentImage *gardencorev1beta1.ShootMachineImage, imageCloudProvider gardencorev1beta1.MachineImage) bool {
for _, image := range imageCloudProvider.Versions {
if shootCurrentImage.Version != nil && *shootCurrentImage.Version != image.Version {
continue
}
return ExpirationDateExpired(image.ExpirationDate)
}
return false
}
// ExpirationDateExpired returns if now is equal or after the given expirationDate
func ExpirationDateExpired(timestamp *metav1.Time) bool {
if timestamp == nil {
return false
}
return time.Now().UTC().After(timestamp.Time) || time.Now().UTC().Equal(timestamp.Time)
}