/
operation.go
731 lines (632 loc) · 27.3 KB
/
operation.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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
// 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 operation
import (
"context"
"fmt"
"strings"
"github.com/gardener/gardener/pkg/gardenlet/apis/config/helper"
gardencorev1alpha1 "github.com/gardener/gardener/pkg/apis/core/v1alpha1"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
gardencorev1beta1helper "github.com/gardener/gardener/pkg/apis/core/v1beta1/helper"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/client/kubernetes/clientmap"
"github.com/gardener/gardener/pkg/client/kubernetes/clientmap/keys"
"github.com/gardener/gardener/pkg/gardenlet/apis/config"
"github.com/gardener/gardener/pkg/operation/common"
"github.com/gardener/gardener/pkg/operation/garden"
"github.com/gardener/gardener/pkg/operation/seed"
shootpkg "github.com/gardener/gardener/pkg/operation/shoot"
"github.com/gardener/gardener/pkg/utils/chart"
"github.com/gardener/gardener/pkg/utils/flow"
gutil "github.com/gardener/gardener/pkg/utils/gardener"
"github.com/gardener/gardener/pkg/utils/imagevector"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
"github.com/gardener/gardener/pkg/utils/version"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// NewBuilder returns a new Builder.
func NewBuilder() *Builder {
return &Builder{
configFunc: func() (*config.GardenletConfiguration, error) {
return nil, fmt.Errorf("config is required but not set")
},
gardenFunc: func(context.Context, map[string]*corev1.Secret) (*garden.Garden, error) {
return nil, fmt.Errorf("garden object is required but not set")
},
gardenerInfoFunc: func() (*gardencorev1beta1.Gardener, error) {
return nil, fmt.Errorf("gardener info is required but not set")
},
gardenClusterIdentityFunc: func() (string, error) {
return "", fmt.Errorf("garden cluster identity is required but not set")
},
imageVectorFunc: func() (imagevector.ImageVector, error) {
return nil, fmt.Errorf("image vector is required but not set")
},
loggerFunc: func() (logr.Logger, error) {
return logr.Discard(), fmt.Errorf("logger is required but not set")
},
secretsFunc: func() (map[string]*corev1.Secret, error) {
return nil, fmt.Errorf("secrets map is required but not set")
},
seedFunc: func(context.Context) (*seed.Seed, error) {
return nil, fmt.Errorf("seed object is required but not set")
},
shootFunc: func(context.Context, client.Reader, *garden.Garden, *seed.Seed) (*shootpkg.Shoot, error) {
return nil, fmt.Errorf("shoot object is required but not set")
},
exposureClassFunc: func(string) (*config.ExposureClassHandler, error) {
return nil, nil
},
}
}
// WithConfig sets the configFunc attribute at the Builder.
func (b *Builder) WithConfig(cfg *config.GardenletConfiguration) *Builder {
b.configFunc = func() (*config.GardenletConfiguration, error) { return cfg, nil }
return b
}
// WithGarden sets the gardenFunc attribute at the Builder.
func (b *Builder) WithGarden(g *garden.Garden) *Builder {
b.gardenFunc = func(context.Context, map[string]*corev1.Secret) (*garden.Garden, error) { return g, nil }
return b
}
// WithGardenFrom sets the gardenFunc attribute at the Builder which will build a new Garden object.
func (b *Builder) WithGardenFrom(reader client.Reader, namespace string) *Builder {
b.gardenFunc = func(ctx context.Context, secrets map[string]*corev1.Secret) (*garden.Garden, error) {
return garden.
NewBuilder().
WithProjectFrom(reader, namespace).
WithInternalDomainFromSecrets(secrets).
WithDefaultDomainsFromSecrets(secrets).
Build(ctx)
}
return b
}
// WithGardenerInfo sets the gardenerInfoFunc attribute at the Builder.
func (b *Builder) WithGardenerInfo(gardenerInfo *gardencorev1beta1.Gardener) *Builder {
b.gardenerInfoFunc = func() (*gardencorev1beta1.Gardener, error) { return gardenerInfo, nil }
return b
}
// WithGardenClusterIdentity sets the identity of the Garden cluster as attribute at the Builder.
func (b *Builder) WithGardenClusterIdentity(gardenClusterIdentity string) *Builder {
b.gardenClusterIdentityFunc = func() (string, error) { return gardenClusterIdentity, nil }
return b
}
// WithImageVector sets the imageVectorFunc attribute at the Builder.
func (b *Builder) WithImageVector(imageVector imagevector.ImageVector) *Builder {
b.imageVectorFunc = func() (imagevector.ImageVector, error) { return imageVector, nil }
return b
}
// WithLogger sets the loggerFunc attribute at the Builder.
func (b *Builder) WithLogger(log logr.Logger) *Builder {
b.loggerFunc = func() (logr.Logger, error) { return log, nil }
return b
}
// WithSecrets sets the secretsFunc attribute at the Builder.
func (b *Builder) WithSecrets(secrets map[string]*corev1.Secret) *Builder {
b.secretsFunc = func() (map[string]*corev1.Secret, error) { return secrets, nil }
return b
}
// WithSeed sets the seedFunc attribute at the Builder.
func (b *Builder) WithSeed(s *seed.Seed) *Builder {
b.seedFunc = func(_ context.Context) (*seed.Seed, error) { return s, nil }
return b
}
// WithSeedFrom sets the seedFunc attribute at the Builder which will build a new Seed object.
func (b *Builder) WithSeedFrom(gardenClient client.Reader, seedName string) *Builder {
b.seedFunc = func(ctx context.Context) (*seed.Seed, error) {
return seed.
NewBuilder().
WithSeedObjectFrom(gardenClient, seedName).
Build(ctx)
}
return b
}
// WithShoot sets the shootFunc attribute at the Builder.
func (b *Builder) WithShoot(s *shootpkg.Shoot) *Builder {
b.shootFunc = func(_ context.Context, _ client.Reader, _ *garden.Garden, _ *seed.Seed) (*shootpkg.Shoot, error) {
return s, nil
}
return b
}
// WithShootFromCluster sets the shootFunc attribute at the Builder which will build a new Shoot object constructed from the cluster resource.
// The shoot status is still taken from the passed `shoot`, though.
func (b *Builder) WithShootFromCluster(gardenClient, seedClient kubernetes.Interface, s *gardencorev1beta1.Shoot) *Builder {
b.shootFunc = func(ctx context.Context, c client.Reader, gardenObj *garden.Garden, seedObj *seed.Seed) (*shootpkg.Shoot, error) {
shootNamespace := shootpkg.ComputeTechnicalID(gardenObj.Project.Name, s)
shoot, err := shootpkg.
NewBuilder().
WithShootObjectFromCluster(seedClient, shootNamespace).
WithCloudProfileObjectFromCluster(seedClient, shootNamespace).
WithShootSecretFrom(gardenClient.Client()).
WithProjectName(gardenObj.Project.Name).
WithExposureClassFrom(gardenClient.Client()).
WithDisableDNS(!seedObj.GetInfo().Spec.Settings.ShootDNS.Enabled).
WithInternalDomain(gardenObj.InternalDomain).
WithDefaultDomains(gardenObj.DefaultDomains).
Build(ctx, c)
if err != nil {
return nil, err
}
// It's OK to modify the value returned by GetInfo() here because at this point there
// can be no concurrent reads or writes
shoot.GetInfo().Status = s.Status
return shoot, nil
}
return b
}
// WithExposureClassHandlerFromConfig sets the exposureClassFunc attribute at the Builder which will find the
// the required exposure class handler in the passed Gardenlet config.
func (b *Builder) WithExposureClassHandlerFromConfig(cfg *config.GardenletConfiguration) *Builder {
b.exposureClassFunc = func(handlerName string) (*config.ExposureClassHandler, error) {
for _, handler := range cfg.ExposureClassHandlers {
if handler.Name == handlerName {
return &handler, nil
}
}
return nil, fmt.Errorf("no exposure class handler with name %q found", handlerName)
}
return b
}
// Build initializes a new Operation object.
func (b *Builder) Build(ctx context.Context, clientMap clientmap.ClientMap) (*Operation, error) {
operation := &Operation{
ClientMap: clientMap,
}
gardenClient, err := clientMap.GetClient(ctx, keys.ForGarden())
if err != nil {
return nil, fmt.Errorf("failed to get garden client: %w", err)
}
operation.K8sGardenClient = gardenClient
config, err := b.configFunc()
if err != nil {
return nil, err
}
operation.Config = config
secretsMap, err := b.secretsFunc()
if err != nil {
return nil, err
}
secrets := make(map[string]*corev1.Secret)
for k, v := range secretsMap {
secrets[k] = v
}
operation.secrets = secrets
garden, err := b.gardenFunc(ctx, secrets)
if err != nil {
return nil, err
}
operation.Garden = garden
gardenerInfo, err := b.gardenerInfoFunc()
if err != nil {
return nil, err
}
operation.GardenerInfo = gardenerInfo
gardenClusterIdentity, err := b.gardenClusterIdentityFunc()
if err != nil {
return nil, err
}
operation.GardenClusterIdentity = gardenClusterIdentity
imageVector, err := b.imageVectorFunc()
if err != nil {
return nil, err
}
operation.ImageVector = imageVector
logger, err := b.loggerFunc()
if err != nil {
return nil, err
}
operation.Logger = logger
seed, err := b.seedFunc(ctx)
if err != nil {
return nil, err
}
operation.Seed = seed
shoot, err := b.shootFunc(ctx, gardenClient.Client(), garden, seed)
if err != nil {
return nil, err
}
operation.Shoot = shoot
if shoot.ExposureClass != nil {
exposureClassHandler, err := b.exposureClassFunc(shoot.ExposureClass.Handler)
if err != nil {
return nil, err
}
operation.ExposureClassHandler = exposureClassHandler
}
// Get the ManagedSeed object for this shoot, if it exists.
// Also read the managed seed API server settings from the managed-seed-api-server annotation.
operation.ManagedSeed, err = kutil.GetManagedSeedWithReader(ctx, gardenClient.Cache(), shoot.GetInfo().Namespace, shoot.GetInfo().Name)
if err != nil {
return nil, fmt.Errorf("could not get managed seed for shoot %s/%s: %w", shoot.GetInfo().Namespace, shoot.GetInfo().Name, err)
}
operation.ManagedSeedAPIServer, err = gardencorev1beta1helper.ReadManagedSeedAPIServer(shoot.GetInfo())
if err != nil {
return nil, fmt.Errorf("could not read managed seed API server settings of shoot %s/%s: %+v", shoot.GetInfo().Namespace, shoot.GetInfo().Name, err)
}
return operation, nil
}
// InitializeSeedClients will use the Garden Kubernetes client to read the Seed Secret in the Garden
// cluster which contains a Kubeconfig that can be used to authenticate against the Seed cluster. With it,
// a Kubernetes client as well as a Chart renderer for the Seed cluster will be initialized and attached to
// the already existing Operation object.
func (o *Operation) InitializeSeedClients(ctx context.Context) error {
if o.K8sSeedClient != nil {
return nil
}
seedClient, err := o.ClientMap.GetClient(ctx, keys.ForSeed(o.Seed.GetInfo()))
if err != nil {
return fmt.Errorf("failed to get seed client: %w", err)
}
o.K8sSeedClient = seedClient
return nil
}
// InitializeShootClients will use the Seed Kubernetes client to read the gardener Secret in the Seed
// cluster which contains a Kubeconfig that can be used to authenticate against the Shoot cluster. With it,
// a Kubernetes client as well as a Chart renderer for the Shoot cluster will be initialized and attached to
// the already existing Operation object.
func (o *Operation) InitializeShootClients(ctx context.Context) error {
return o.initShootClients(ctx, false)
}
// InitializeDesiredShootClients will use the Seed Kubernetes client to read the gardener Secret in the Seed
// cluster which contains a Kubeconfig that can be used to authenticate against the Shoot cluster. With it,
// a Kubernetes client as well as a Chart renderer for the Shoot cluster will be initialized and attached to
// the already existing Operation object.
// In contrast to InitializeShootClients, InitializeDesiredShootClients returns an error if the discovered version
// via the client does not match the desired Kubernetes version from the shoot spec.
// This is especially useful, if the client is initialized after a rolling update of the Kube-Apiserver
// and you want to ensure that the discovered version matches the expected version.
func (o *Operation) InitializeDesiredShootClients(ctx context.Context) error {
return o.initShootClients(ctx, true)
}
func (o *Operation) initShootClients(ctx context.Context, versionMatchRequired bool) error {
if o.K8sShootClient != nil {
return nil
}
if o.Shoot.HibernationEnabled {
// Don't initialize clients for Shoots, that are currently hibernated and their API server is not running
apiServerRunning, err := o.IsAPIServerRunning(ctx)
if err != nil {
return err
}
if !apiServerRunning {
return nil
}
}
shootClient, err := o.ClientMap.GetClient(ctx, keys.ForShoot(o.Shoot.GetInfo()))
if err != nil {
return err
}
if versionMatchRequired {
var (
shootClientVersion = shootClient.Version()
kubeVersion = o.Shoot.GetInfo().Spec.Kubernetes.Version
)
ok, err := version.CompareVersions(shootClientVersion, "=", kubeVersion)
if err != nil {
return err
}
if !ok {
return fmt.Errorf("shoot client version %q does not match desired version %q", shootClientVersion, kubeVersion)
}
}
o.K8sShootClient = shootClient
return nil
}
// IsAPIServerRunning checks if the API server of the Shoot currently running (not scaled-down/deleted).
func (o *Operation) IsAPIServerRunning(ctx context.Context) (bool, error) {
deployment := &appsv1.Deployment{}
// use API reader here to make sure, we're not reading from a stale cache, when checking if we should initialize a shoot client (e.g. from within the care controller)
if err := o.K8sSeedClient.APIReader().Get(ctx, kutil.Key(o.Shoot.SeedNamespace, v1beta1constants.DeploymentNameKubeAPIServer), deployment); err != nil {
if apierrors.IsNotFound(err) {
return false, nil
}
return false, err
}
if deployment.GetDeletionTimestamp() != nil {
return false, nil
}
if deployment.Spec.Replicas == nil {
return false, nil
}
return *deployment.Spec.Replicas > 0, nil
}
// GetSecretKeysOfRole returns a list of keys which are present in the Garden Secrets map and which
// are prefixed with <kind>.
func (o *Operation) GetSecretKeysOfRole(kind string) []string {
return common.FilterEntriesByPrefix(kind, o.AllSecretKeys())
}
func makeDescription(stats *flow.Stats) string {
if stats.ProgressPercent() == 0 {
return "Starting " + stats.FlowName
}
if stats.ProgressPercent() == 100 {
return stats.FlowName + " finished"
}
return strings.Join(stats.Running.StringList(), ", ")
}
// ReportShootProgress will update the last operation object in the Shoot manifest `status` section
// by the current progress of the Flow execution.
func (o *Operation) ReportShootProgress(ctx context.Context, stats *flow.Stats) {
var (
description = makeDescription(stats)
progress = stats.ProgressPercent()
lastUpdateTime = metav1.Now()
)
if err := o.Shoot.UpdateInfoStatus(ctx, o.K8sGardenClient.Client(), true, func(shoot *gardencorev1beta1.Shoot) error {
if shoot.Status.LastOperation == nil {
return fmt.Errorf("last operation of Shoot %s/%s is unset", shoot.Namespace, shoot.Name)
}
if shoot.Status.LastOperation.LastUpdateTime.After(lastUpdateTime.Time) {
return fmt.Errorf("last operation of Shoot %s/%s was updated mid-air", shoot.Namespace, shoot.Name)
}
if description != "" {
shoot.Status.LastOperation.Description = description
}
shoot.Status.LastOperation.Progress = progress
shoot.Status.LastOperation.LastUpdateTime = lastUpdateTime
return nil
}); err != nil {
o.Logger.Error(err, "Could not report shoot progress")
}
}
// CleanShootTaskError removes the error with taskID from the Shoot's status.LastErrors array.
// If the status.LastErrors array is empty then status.LastErrors is also removed.
func (o *Operation) CleanShootTaskError(ctx context.Context, taskID string) {
if err := o.Shoot.UpdateInfoStatus(ctx, o.K8sGardenClient.Client(), false, func(shoot *gardencorev1beta1.Shoot) error {
shoot.Status.LastErrors = gardencorev1beta1helper.DeleteLastErrorByTaskID(shoot.Status.LastErrors, taskID)
return nil
}); err != nil {
o.Logger.Error(err, "Could not update last errors of shoot", "shoot", client.ObjectKeyFromObject(o.Shoot.GetInfo()))
}
}
// SeedVersion is a shorthand for the kubernetes version of the K8sSeedClient.
func (o *Operation) SeedVersion() string {
return o.K8sSeedClient.Version()
}
// ShootVersion is a shorthand for the desired kubernetes version of the operation's shoot.
func (o *Operation) ShootVersion() string {
return o.Shoot.GetInfo().Spec.Kubernetes.Version
}
// InjectSeedSeedImages injects images that shall run on the Seed and target the Seed's Kubernetes version.
func (o *Operation) InjectSeedSeedImages(values map[string]interface{}, names ...string) (map[string]interface{}, error) {
return chart.InjectImages(values, o.ImageVector, names, imagevector.RuntimeVersion(o.SeedVersion()), imagevector.TargetVersion(o.SeedVersion()))
}
// InjectSeedShootImages injects images that shall run on the Seed but target the Shoot's Kubernetes version.
func (o *Operation) InjectSeedShootImages(values map[string]interface{}, names ...string) (map[string]interface{}, error) {
return chart.InjectImages(values, o.ImageVector, names, imagevector.RuntimeVersion(o.SeedVersion()), imagevector.TargetVersion(o.ShootVersion()))
}
// InjectShootShootImages injects images that shall run on the Shoot and target the Shoot's Kubernetes version.
func (o *Operation) InjectShootShootImages(values map[string]interface{}, names ...string) (map[string]interface{}, error) {
return chart.InjectImages(values, o.ImageVector, names, imagevector.RuntimeVersion(o.ShootVersion()), imagevector.TargetVersion(o.ShootVersion()))
}
// EnsureShootStateExists creates the ShootState resource for the corresponding shoot and updates the operations object
func (o *Operation) EnsureShootStateExists(ctx context.Context) error {
var (
err error
shootState = &gardencorev1alpha1.ShootState{
ObjectMeta: metav1.ObjectMeta{
Name: o.Shoot.GetInfo().Name,
Namespace: o.Shoot.GetInfo().Namespace,
},
}
)
if err = o.K8sGardenClient.Client().Create(ctx, shootState); client.IgnoreAlreadyExists(err) != nil {
return err
}
if err = o.K8sGardenClient.Client().Get(ctx, client.ObjectKeyFromObject(shootState), shootState); err != nil {
return err
}
o.SetShootState(shootState)
return nil
}
// DeleteShootState deletes the ShootState resource for the corresponding shoot.
func (o *Operation) DeleteShootState(ctx context.Context) error {
shootState := &gardencorev1alpha1.ShootState{
ObjectMeta: metav1.ObjectMeta{
Name: o.Shoot.GetInfo().Name,
Namespace: o.Shoot.GetInfo().Namespace,
},
}
if err := gutil.ConfirmDeletion(ctx, o.K8sGardenClient.Client(), shootState); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
return client.IgnoreNotFound(o.K8sGardenClient.Client().Delete(ctx, shootState))
}
// GetShootState returns the shootstate resource of this Shoot in a concurrency safe way.
// This method should be used only for reading the data of the returned shootstate resource. The returned shootstate
// resource MUST NOT BE MODIFIED (except in test code) since this might interfere with other concurrent reads and writes.
// To properly update the shootstate resource of this Shoot use SaveGardenerResourceDataInShootState.
func (o *Operation) GetShootState() *gardencorev1alpha1.ShootState {
shootState, ok := o.shootState.Load().(*gardencorev1alpha1.ShootState)
if !ok {
return nil
}
return shootState
}
// SetShootState sets the shootstate resource of this Shoot in a concurrency safe way.
// This method is not protected by a mutex and does not update the shootstate resource in the cluster and so
// should be used only in exceptional situations, or as a convenience in test code. The shootstate passed as a parameter
// MUST NOT BE MODIFIED after the call to SetShootState (except in test code) since this might interfere with other concurrent reads and writes.
// To properly update the shootstate resource of this Shoot use SaveGardenerResourceDataInShootState.
func (o *Operation) SetShootState(shootState *gardencorev1alpha1.ShootState) {
o.shootState.Store(shootState)
}
// SaveGardenerResourceDataInShootState updates the shootstate resource of this Shoot in a concurrency safe way,
// using the given context and mutate function.
// The mutate function should modify the passed GardenerResourceData so that changes are persisted.
// This method is protected by a mutex, so only a single SaveGardenerResourceDataInShootState operation can be
// executed at any point in time.
func (o *Operation) SaveGardenerResourceDataInShootState(ctx context.Context, f func(*[]gardencorev1alpha1.GardenerResourceData) error) error {
o.shootStateMutex.Lock()
defer o.shootStateMutex.Unlock()
shootState := o.GetShootState().DeepCopy()
original := shootState.DeepCopy()
patch := client.StrategicMergeFrom(original)
if err := f(&shootState.Spec.Gardener); err != nil {
return err
}
if equality.Semantic.DeepEqual(original.Spec.Gardener, shootState.Spec.Gardener) {
return nil
}
if err := o.K8sGardenClient.Client().Patch(ctx, shootState, patch); err != nil {
return err
}
o.SetShootState(shootState)
return nil
}
// DeleteClusterResourceFromSeed deletes the `Cluster` extension resource for the shoot in the seed cluster.
func (o *Operation) DeleteClusterResourceFromSeed(ctx context.Context) error {
if err := o.InitializeSeedClients(ctx); err != nil {
return fmt.Errorf("could not initialize a new Kubernetes client for the seed cluster: %w", err)
}
return client.IgnoreNotFound(o.K8sSeedClient.Client().Delete(ctx, &extensionsv1alpha1.Cluster{ObjectMeta: metav1.ObjectMeta{Name: o.Shoot.SeedNamespace}}))
}
// ComputeGrafanaHosts computes the host for both grafanas.
func (o *Operation) ComputeGrafanaHosts() []string {
return []string{
o.ComputeGrafanaOperatorsHost(),
o.ComputeGrafanaUsersHost(),
}
}
// ComputePrometheusHosts computes the hosts for prometheus.
func (o *Operation) ComputePrometheusHosts() []string {
return []string{
o.ComputePrometheusHost(),
}
}
// ComputeAlertManagerHosts computes the host for alert manager.
func (o *Operation) ComputeAlertManagerHosts() []string {
return []string{
o.ComputeAlertManagerHost(),
}
}
// ComputeGrafanaOperatorsHost computes the host for users Grafana.
func (o *Operation) ComputeGrafanaOperatorsHost() string {
return o.ComputeIngressHost(common.GrafanaOperatorsPrefix)
}
// ComputeLokiHosts computes the host for loki.
func (o *Operation) ComputeLokiHosts() []string {
return []string{
o.ComputeLokiHost(),
}
}
// IsShootMonitoringEnabled returns true if shoot monitoring is enabled and shoot is not of purpose testing.
func (o *Operation) IsShootMonitoringEnabled() bool {
return helper.IsMonitoringEnabled(o.Config) && o.Shoot.Purpose != gardencorev1beta1.ShootPurposeTesting
}
// WantsGrafana returns true if shoot is not of purpose testing and either shoot monitoring or loki is enabled.
func (o *Operation) WantsGrafana() bool {
return o.Shoot.Purpose != gardencorev1beta1.ShootPurposeTesting && (helper.IsMonitoringEnabled(o.Config) || helper.IsLokiEnabled(o.Config))
}
// ComputeGrafanaUsersHost computes the host for operators Grafana.
func (o *Operation) ComputeGrafanaUsersHost() string {
return o.ComputeIngressHost(common.GrafanaUsersPrefix)
}
// ComputeAlertManagerHost computes the host for alert manager.
func (o *Operation) ComputeAlertManagerHost() string {
return o.ComputeIngressHost(common.AlertManagerPrefix)
}
// ComputePrometheusHost computes the host for prometheus.
func (o *Operation) ComputePrometheusHost() string {
return o.ComputeIngressHost(common.PrometheusPrefix)
}
// ComputeLokiHost computes the host for loki.
func (o *Operation) ComputeLokiHost() string {
return o.ComputeIngressHost(common.LokiPrefix)
}
// ComputeIngressHost computes the host for a given prefix.
func (o *Operation) ComputeIngressHost(prefix string) string {
shortID := strings.Replace(o.Shoot.GetInfo().Status.TechnicalID, shootpkg.TechnicalIDPrefix, "", 1)
return fmt.Sprintf("%s-%s.%s", prefix, shortID, o.Seed.IngressDomain())
}
// ToAdvertisedAddresses returns list of advertised addresses on a Shoot cluster.
func (o *Operation) ToAdvertisedAddresses() []gardencorev1beta1.ShootAdvertisedAddress {
var addresses []gardencorev1beta1.ShootAdvertisedAddress
if o.Shoot == nil {
return addresses
}
if o.Shoot.ExternalClusterDomain != nil && len(*o.Shoot.ExternalClusterDomain) > 0 {
addresses = append(addresses, gardencorev1beta1.ShootAdvertisedAddress{
Name: "external",
URL: "https://" + gutil.GetAPIServerDomain(*o.Shoot.ExternalClusterDomain),
})
}
if len(o.Shoot.InternalClusterDomain) > 0 {
addresses = append(addresses, gardencorev1beta1.ShootAdvertisedAddress{
Name: "internal",
URL: "https://" + gutil.GetAPIServerDomain(o.Shoot.InternalClusterDomain),
})
}
if len(o.APIServerAddress) > 0 && len(addresses) == 0 {
addresses = append(addresses, gardencorev1beta1.ShootAdvertisedAddress{
Name: "unmanaged",
URL: "https://" + o.APIServerAddress,
})
}
return addresses
}
// UpdateAdvertisedAddresses updates the shoot.status.advertisedAddresses with the list of
// addresses on which the API server of the shoot is accessible.
func (o *Operation) UpdateAdvertisedAddresses(ctx context.Context) error {
return o.Shoot.UpdateInfoStatus(ctx, o.K8sGardenClient.Client(), false, func(shoot *gardencorev1beta1.Shoot) error {
shoot.Status.AdvertisedAddresses = o.ToAdvertisedAddresses()
return nil
})
}
// StoreSecret stores the passed secret under the given key from the operation. Calling this function is thread-safe.
func (o *Operation) StoreSecret(key string, secret *corev1.Secret) {
o.secretsMutex.Lock()
defer o.secretsMutex.Unlock()
if o.secrets == nil {
o.secrets = make(map[string]*corev1.Secret)
}
o.secrets[key] = secret
}
// AllSecretKeys returns all stored secret keys from the operation. Calling this function is thread-safe.
func (o *Operation) AllSecretKeys() []string {
o.secretsMutex.RLock()
defer o.secretsMutex.RUnlock()
var keys []string
for key := range o.secrets {
keys = append(keys, key)
}
return keys
}
// LoadSecret loads the secret under the given key from the operation. Calling this function is thread-safe.
// Be aware that the returned pointer and the underlying secret map refer to the same secret object.
// If you need to modify the returned secret, copy it first and store the changes via `StoreSecret`.
func (o *Operation) LoadSecret(key string) *corev1.Secret {
o.secretsMutex.RLock()
defer o.secretsMutex.RUnlock()
val := o.secrets[key]
return val
}
// DeleteSecret deleted the secret under the given key from the operation. Calling this function is thread-safe.
func (o *Operation) DeleteSecret(key string) {
o.secretsMutex.Lock()
defer o.secretsMutex.Unlock()
delete(o.secrets, key)
}