forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
health_test.go
877 lines (773 loc) · 32.9 KB
/
health_test.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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
// Copyright 2023 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 care_test
import (
"context"
"strings"
"time"
druidv1alpha1 "github.com/gardener/etcd-druid/api/v1alpha1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/types"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
testclock "k8s.io/utils/clock/testing"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
operatorv1alpha1 "github.com/gardener/gardener/pkg/apis/operator/v1alpha1"
resourcesv1alpha1 "github.com/gardener/gardener/pkg/apis/resources/v1alpha1"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/component/etcd"
"github.com/gardener/gardener/pkg/component/gardeneraccess"
"github.com/gardener/gardener/pkg/component/gardeneradmissioncontroller"
"github.com/gardener/gardener/pkg/component/gardenerapiserver"
"github.com/gardener/gardener/pkg/component/gardenercontrollermanager"
"github.com/gardener/gardener/pkg/component/gardenermetricsexporter"
"github.com/gardener/gardener/pkg/component/gardenerscheduler"
runtimegardensystem "github.com/gardener/gardener/pkg/component/gardensystem/runtime"
virtualgardensystem "github.com/gardener/gardener/pkg/component/gardensystem/virtual"
"github.com/gardener/gardener/pkg/component/hvpa"
"github.com/gardener/gardener/pkg/component/kubecontrollermanager"
"github.com/gardener/gardener/pkg/component/kubestatemetrics"
"github.com/gardener/gardener/pkg/component/logging/fluentoperator"
"github.com/gardener/gardener/pkg/component/logging/vali"
"github.com/gardener/gardener/pkg/component/plutono"
"github.com/gardener/gardener/pkg/component/resourcemanager"
"github.com/gardener/gardener/pkg/component/vpa"
"github.com/gardener/gardener/pkg/features"
operatorclient "github.com/gardener/gardener/pkg/operator/client"
. "github.com/gardener/gardener/pkg/operator/controller/garden/care"
"github.com/gardener/gardener/pkg/utils/test"
. "github.com/gardener/gardener/pkg/utils/test/matchers"
)
var (
gardenManagedResources = []string{
etcd.Druid,
runtimegardensystem.ManagedResourceName,
"istio-system",
"virtual-garden-istio",
}
virtualGardenManagedResources = []string{
resourcemanager.ManagedResourceName,
gardeneraccess.ManagedResourceName,
kubecontrollermanager.ManagedResourceName,
gardenerapiserver.ManagedResourceNameRuntime,
gardenerapiserver.ManagedResourceNameVirtual,
gardeneradmissioncontroller.ManagedResourceNameRuntime,
gardeneradmissioncontroller.ManagedResourceNameVirtual,
gardenercontrollermanager.ManagedResourceNameRuntime,
gardenercontrollermanager.ManagedResourceNameVirtual,
gardenerscheduler.ManagedResourceNameRuntime,
gardenerscheduler.ManagedResourceNameVirtual,
virtualgardensystem.ManagedResourceName,
}
observabilityManagedResources = []string{
hvpa.ManagedResourceName,
kubestatemetrics.ManagedResourceName,
fluentoperator.OperatorManagedResourceName,
fluentoperator.CustomResourcesManagedResourceName + "-garden",
fluentoperator.FluentBitManagedResourceName,
vali.ManagedResourceNameRuntime,
plutono.ManagedResourceName,
gardenermetricsexporter.ManagedResourceNameRuntime,
gardenermetricsexporter.ManagedResourceNameVirtual,
}
virtualGardenDeployments = []string{
"virtual-garden-gardener-resource-manager",
"virtual-garden-kube-apiserver",
"virtual-garden-kube-controller-manager",
}
virtualGardenETCDs = []string{
"virtual-garden-etcd-events",
"virtual-garden-etcd-main",
}
)
var _ = Describe("Garden health", func() {
var (
ctx context.Context
runtimeClient client.Client
gardenClientSet kubernetes.Interface
fakeClock *testclock.FakeClock
garden *operatorv1alpha1.Garden
gardenNamespace string
gardenConditions GardenConditions
allManagedResources []string
apiserverAvailabilityCondition gardencorev1beta1.Condition
runtimeComponentsHealthyCondition gardencorev1beta1.Condition
virtualComponentsHealthyCondition gardencorev1beta1.Condition
observabilityComponentsHealthyCondition gardencorev1beta1.Condition
)
BeforeEach(func() {
DeferCleanup(test.WithFeatureGate(features.DefaultFeatureGate, features.HVPA, true))
ctx = context.Background()
runtimeClient = fakeclient.NewClientBuilder().WithScheme(operatorclient.RuntimeScheme).Build()
garden = &operatorv1alpha1.Garden{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
}
gardenNamespace = "garden"
fakeClock = testclock.NewFakeClock(time.Now())
allManagedResources = sets.New[string]().
Insert(gardenManagedResources...).
Insert(virtualGardenManagedResources...).
Insert(observabilityManagedResources...).
UnsortedList()
apiserverAvailabilityCondition = gardencorev1beta1.Condition{
Type: operatorv1alpha1.VirtualGardenAPIServerAvailable,
LastTransitionTime: metav1.Time{Time: fakeClock.Now()},
}
runtimeComponentsHealthyCondition = gardencorev1beta1.Condition{
Type: operatorv1alpha1.RuntimeComponentsHealthy,
LastTransitionTime: metav1.Time{Time: fakeClock.Now()},
}
virtualComponentsHealthyCondition = gardencorev1beta1.Condition{
Type: operatorv1alpha1.VirtualComponentsHealthy,
LastTransitionTime: metav1.Time{Time: fakeClock.Now()},
}
observabilityComponentsHealthyCondition = gardencorev1beta1.Condition{
Type: operatorv1alpha1.ObservabilityComponentsHealthy,
LastTransitionTime: metav1.Time{Time: fakeClock.Now()},
}
})
JustBeforeEach(func() {
garden.Status = operatorv1alpha1.GardenStatus{
Conditions: []gardencorev1beta1.Condition{
apiserverAvailabilityCondition,
runtimeComponentsHealthyCondition,
virtualComponentsHealthyCondition,
observabilityComponentsHealthyCondition,
},
}
gardenConditions = NewGardenConditions(fakeClock, garden.Status)
})
Describe("#Check", func() {
Context("when all managed resources, deployments and ETCDs are deployed successfully", func() {
JustBeforeEach(func() {
for _, name := range allManagedResources {
Expect(runtimeClient.Create(ctx, healthyManagedResource(name))).To(Succeed())
}
for _, name := range virtualGardenDeployments {
Expect(runtimeClient.Create(ctx, newDeployment(gardenNamespace, name, "controlplane", true))).To(Succeed())
}
for _, name := range virtualGardenETCDs {
Expect(runtimeClient.Create(ctx, newEtcd(gardenNamespace, name, "controlplane", true, nil))).To(Succeed())
}
})
It("should set RuntimeComponentsHealthy and VirtualComponentsHealthy conditions to true", func() {
updatedConditions := NewHealth(
garden,
runtimeClient,
gardenClientSet,
fakeClock,
nil,
gardenNamespace,
).Check(ctx, gardenConditions)
Expect(len(updatedConditions)).ToNot(BeZero())
Expect(updatedConditions).To(ContainElements(
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionTrue, "RuntimeComponentsRunning", "All runtime components are healthy."),
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionTrue, "VirtualComponentsRunning", "All virtual garden components are healthy."),
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionTrue, "ObservabilityComponentsRunning", "All observability components are healthy."),
))
})
})
Context("when optional managed resources are turned off, and required resources are deployed successfully", func() {
JustBeforeEach(func() {
DeferCleanup(test.WithFeatureGate(features.DefaultFeatureGate, features.HVPA, false))
garden.Spec.RuntimeCluster.Settings = &operatorv1alpha1.Settings{
VerticalPodAutoscaler: &operatorv1alpha1.SettingVerticalPodAutoscaler{
Enabled: pointer.Bool(false),
},
}
for _, name := range allManagedResources {
Expect(runtimeClient.Create(ctx, healthyManagedResource(name))).To(Succeed())
}
for _, name := range virtualGardenDeployments {
Expect(runtimeClient.Create(ctx, newDeployment(gardenNamespace, name, "controlplane", true))).To(Succeed())
}
for _, name := range virtualGardenETCDs {
Expect(runtimeClient.Create(ctx, newEtcd(gardenNamespace, name, "controlplane", true, nil))).To(Succeed())
}
})
It("should set RuntimeComponentsHealthy and VirtualComponentsHealthy conditions to true", func() {
updatedConditions := NewHealth(
garden,
runtimeClient,
gardenClientSet,
fakeClock,
nil,
gardenNamespace,
).Check(ctx, gardenConditions)
Expect(len(updatedConditions)).ToNot(BeZero())
Expect(updatedConditions).To(ContainElements(
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionTrue, "RuntimeComponentsRunning", "All runtime components are healthy."),
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionTrue, "VirtualComponentsRunning", "All virtual garden components are healthy."),
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionTrue, "ObservabilityComponentsRunning", "All observability components are healthy."),
))
})
})
Context("when optional managed resources are turned on, and all resources are deployed successfully", func() {
JustBeforeEach(func() {
garden.Spec.RuntimeCluster.Settings = &operatorv1alpha1.Settings{
VerticalPodAutoscaler: &operatorv1alpha1.SettingVerticalPodAutoscaler{
Enabled: pointer.Bool(true),
},
}
for _, name := range append(allManagedResources, vpa.ManagedResourceControlName) {
Expect(runtimeClient.Create(ctx, healthyManagedResource(name))).To(Succeed())
}
for _, name := range virtualGardenDeployments {
Expect(runtimeClient.Create(ctx, newDeployment(gardenNamespace, name, "controlplane", true))).To(Succeed())
}
for _, name := range virtualGardenETCDs {
Expect(runtimeClient.Create(ctx, newEtcd(gardenNamespace, name, "controlplane", true, nil))).To(Succeed())
}
})
It("should set RuntimeComponentsHealthy and VirtualComponentsHealthy conditions to true", func() {
updatedConditions := NewHealth(
garden,
runtimeClient,
gardenClientSet,
fakeClock,
nil,
gardenNamespace,
).Check(ctx, gardenConditions)
Expect(len(updatedConditions)).ToNot(BeZero())
Expect(updatedConditions).To(ContainElements(
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionTrue, "RuntimeComponentsRunning", "All runtime components are healthy."),
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionTrue, "VirtualComponentsRunning", "All virtual garden components are healthy."),
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionTrue, "ObservabilityComponentsRunning", "All observability components are healthy."),
))
})
})
Context("when there are issues with runtime and virtual managed resources", func() {
var (
tests = func(reason, message string) {
It("should set RuntimeComponentsHealthy and VirtualComponentsHealthy conditions to False if there is no Progressing threshold duration mapping", func() {
updatedConditions := NewHealth(
garden,
runtimeClient,
gardenClientSet,
fakeClock,
nil,
gardenNamespace,
).Check(ctx, gardenConditions)
Expect(len(updatedConditions)).ToNot(BeZero())
Expect(updatedConditions).To(ContainElements(
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionFalse, reason, message),
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionFalse, reason, message),
))
})
Context("condition is currently False", func() {
BeforeEach(func() {
runtimeComponentsHealthyCondition.Status = gardencorev1beta1.ConditionFalse
virtualComponentsHealthyCondition.Status = gardencorev1beta1.ConditionFalse
})
It("should set RuntimeComponentsHealthy and VirtualComponentsHealthy conditions to Progressing if time is within threshold duration", func() {
fakeClock.Step(30 * time.Second)
updatedConditions := NewHealth(
garden,
runtimeClient,
gardenClientSet,
fakeClock,
map[gardencorev1beta1.ConditionType]time.Duration{
operatorv1alpha1.RuntimeComponentsHealthy: time.Minute,
operatorv1alpha1.VirtualComponentsHealthy: time.Minute,
},
gardenNamespace,
).Check(ctx, gardenConditions)
Expect(len(updatedConditions)).ToNot(BeZero())
Expect(updatedConditions).To(ContainElements(
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionProgressing, reason, message),
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionProgressing, reason, message),
))
})
})
Context("condition is currently True", func() {
BeforeEach(func() {
runtimeComponentsHealthyCondition.Status = gardencorev1beta1.ConditionTrue
virtualComponentsHealthyCondition.Status = gardencorev1beta1.ConditionTrue
})
It("should set RuntimeComponentsHealthy and VirtualComponentsHealthy conditions to Progressing if time is within threshold duration", func() {
fakeClock.Step(30 * time.Second)
updatedConditions := NewHealth(
garden,
runtimeClient,
gardenClientSet,
fakeClock,
map[gardencorev1beta1.ConditionType]time.Duration{
operatorv1alpha1.RuntimeComponentsHealthy: time.Minute,
operatorv1alpha1.VirtualComponentsHealthy: time.Minute,
},
gardenNamespace,
).Check(ctx, gardenConditions)
Expect(len(updatedConditions)).ToNot(BeZero())
Expect(updatedConditions).To(ContainElements(
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionProgressing, reason, message),
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionProgressing, reason, message),
))
})
})
Context("condition is currently Progressing", func() {
BeforeEach(func() {
runtimeComponentsHealthyCondition.Status = gardencorev1beta1.ConditionProgressing
virtualComponentsHealthyCondition.Status = gardencorev1beta1.ConditionProgressing
})
It("should not set RuntimeComponentsHealthy and VirtualComponentsHealthy conditions to Progressing if Progressing threshold duration has not expired", func() {
fakeClock.Step(30 * time.Second)
updatedConditions := NewHealth(
garden,
runtimeClient,
gardenClientSet,
fakeClock,
map[gardencorev1beta1.ConditionType]time.Duration{
operatorv1alpha1.RuntimeComponentsHealthy: time.Minute,
operatorv1alpha1.VirtualComponentsHealthy: time.Minute,
},
gardenNamespace,
).Check(ctx, gardenConditions)
Expect(len(updatedConditions)).ToNot(BeZero())
Expect(updatedConditions).To(ContainElements(
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionProgressing, reason, message),
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionProgressing, reason, message),
))
})
It("should set RuntimeComponentsHealthy and VirtualComponentsHealthy conditions to false if Progressing threshold duration has expired", func() {
fakeClock.Step(90 * time.Second)
updatedConditions := NewHealth(
garden,
runtimeClient,
gardenClientSet,
fakeClock,
map[gardencorev1beta1.ConditionType]time.Duration{
operatorv1alpha1.RuntimeComponentsHealthy: time.Minute,
operatorv1alpha1.VirtualComponentsHealthy: time.Minute,
},
gardenNamespace,
).Check(ctx, gardenConditions)
Expect(len(updatedConditions)).ToNot(BeZero())
Expect(updatedConditions).To(ContainElements(
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionFalse, reason, message),
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionFalse, reason, message),
))
})
})
}
)
Context("when managed resources are not deployed", func() {
JustBeforeEach(func() {
for _, name := range virtualGardenDeployments {
Expect(runtimeClient.Create(ctx, newDeployment(gardenNamespace, name, "controlplane", true))).To(Succeed())
}
for _, name := range virtualGardenETCDs {
Expect(runtimeClient.Create(ctx, newEtcd(gardenNamespace, name, "controlplane", true, nil))).To(Succeed())
}
})
tests("ResourceNotFound", "not found")
})
Context("when all managed resources are deployed, but not healthy", func() {
JustBeforeEach(func() {
for _, name := range append(gardenManagedResources, virtualGardenManagedResources...) {
Expect(runtimeClient.Create(ctx, notHealthyManagedResource(name))).To(Succeed())
}
for _, name := range virtualGardenDeployments {
Expect(runtimeClient.Create(ctx, newDeployment(gardenNamespace, name, "controlplane", true))).To(Succeed())
}
for _, name := range virtualGardenETCDs {
Expect(runtimeClient.Create(ctx, newEtcd(gardenNamespace, name, "controlplane", true, nil))).To(Succeed())
}
})
tests("NotHealthy", "Resources are not healthy")
})
Context("when all managed resources are deployed but their resources are not applied", func() {
JustBeforeEach(func() {
for _, name := range append(gardenManagedResources, virtualGardenManagedResources...) {
Expect(runtimeClient.Create(ctx, notAppliedManagedResource(name))).To(Succeed())
}
for _, name := range virtualGardenDeployments {
Expect(runtimeClient.Create(ctx, newDeployment(gardenNamespace, name, "controlplane", true))).To(Succeed())
}
for _, name := range virtualGardenETCDs {
Expect(runtimeClient.Create(ctx, newEtcd(gardenNamespace, name, "controlplane", true, nil))).To(Succeed())
}
})
tests("NotApplied", "Resources are not applied")
})
Context("when all managed resources are deployed but their resources are still progressing", func() {
JustBeforeEach(func() {
for _, name := range append(gardenManagedResources, virtualGardenManagedResources...) {
Expect(runtimeClient.Create(ctx, progressingManagedResource(name))).To(Succeed())
}
for _, name := range virtualGardenDeployments {
Expect(runtimeClient.Create(ctx, newDeployment(gardenNamespace, name, "controlplane", true))).To(Succeed())
}
for _, name := range virtualGardenETCDs {
Expect(runtimeClient.Create(ctx, newEtcd(gardenNamespace, name, "controlplane", true, nil))).To(Succeed())
}
})
tests("ResourcesProgressing", "Resources are progressing")
})
Context("when all managed resources are deployed but not all required conditions are present", func() {
JustBeforeEach(func() {
for _, name := range append(gardenManagedResources, virtualGardenManagedResources...) {
Expect(runtimeClient.Create(ctx, managedResource(name, []gardencorev1beta1.Condition{{
Type: resourcesv1alpha1.ResourcesApplied,
Status: gardencorev1beta1.ConditionTrue}},
))).To(Succeed())
}
for _, name := range virtualGardenDeployments {
Expect(runtimeClient.Create(ctx, newDeployment(gardenNamespace, name, "controlplane", true))).To(Succeed())
}
for _, name := range virtualGardenETCDs {
Expect(runtimeClient.Create(ctx, newEtcd(gardenNamespace, name, "controlplane", true, nil))).To(Succeed())
}
})
tests("MissingManagedResourceCondition", "is missing the following condition(s)")
})
})
Context("when there are issues with deployments for virtual garden", func() {
It("should set VirtualComponentsHealthy conditions to false when the deployments are missing", func() {
updatedConditions := NewHealth(
garden,
runtimeClient,
gardenClientSet,
fakeClock,
nil,
gardenNamespace,
).Check(ctx, gardenConditions)
Expect(len(updatedConditions)).ToNot(BeZero())
Expect(updatedConditions).To(ContainElements(
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionFalse, "DeploymentMissing", "Missing required deployments: [virtual-garden-gardener-resource-manager virtual-garden-kube-apiserver virtual-garden-kube-controller-manager]"),
))
})
It("should set VirtualComponentsHealthy conditions to false when the deployments are existing but unhealthy", func() {
for _, name := range virtualGardenDeployments {
Expect(runtimeClient.Create(ctx, newDeployment(gardenNamespace, name, "controlplane", false))).To(Succeed())
}
updatedConditions := NewHealth(
garden,
runtimeClient,
gardenClientSet,
fakeClock,
nil,
gardenNamespace,
).Check(ctx, gardenConditions)
Expect(len(updatedConditions)).ToNot(BeZero())
Expect(updatedConditions).To(ContainElements(
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionFalse, "DeploymentUnhealthy", "is unhealthy: condition \"Available\" is missing"),
))
})
})
Context("when there are issues with ETCDs for virtual garden", func() {
JustBeforeEach(func() {
for _, name := range virtualGardenDeployments {
Expect(runtimeClient.Create(ctx, newDeployment(gardenNamespace, name, "controlplane", true))).To(Succeed())
}
})
It("should set VirtualComponentsHealthy conditions to false when the ETCDs are missing", func() {
updatedConditions := NewHealth(
garden,
runtimeClient,
gardenClientSet,
fakeClock,
nil,
gardenNamespace,
).Check(ctx, gardenConditions)
Expect(len(updatedConditions)).ToNot(BeZero())
Expect(updatedConditions).To(ContainElements(
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionFalse, "EtcdMissing", "Missing required etcds: [virtual-garden-etcd-events virtual-garden-etcd-main]"),
))
})
It("should set VirtualComponentsHealthy conditions to false when the ETCDs are existing but unhealthy", func() {
for _, name := range virtualGardenETCDs {
Expect(runtimeClient.Create(ctx, newEtcd(gardenNamespace, name, "controlplane", false, nil))).To(Succeed())
}
updatedConditions := NewHealth(
garden,
runtimeClient,
gardenClientSet,
fakeClock,
nil,
gardenNamespace,
).Check(ctx, gardenConditions)
Expect(len(updatedConditions)).ToNot(BeZero())
Expect(updatedConditions).To(ContainElements(
beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionFalse, "EtcdUnhealthy", "Etcd extension resource \"virtual-garden-etcd-events\" is unhealthy: etcd \"virtual-garden-etcd-events\" is not ready yet"),
))
})
})
Context("when there are issues with observability components", func() {
It("should set ObservabilityComponentsHealthy conditions to false when no ManagedResources are deployed", func() {
updatedConditions := NewHealth(
garden,
runtimeClient,
gardenClientSet,
fakeClock,
nil,
gardenNamespace,
).Check(ctx, gardenConditions)
Expect(len(updatedConditions)).ToNot(BeZero())
Expect(updatedConditions).To(ContainCondition(OfType(operatorv1alpha1.ObservabilityComponentsHealthy), WithReason("ResourceNotFound")))
})
It("should set ObservabilityComponentsHealthy conditions to false when ManagedResources are unhealthy", func() {
for _, name := range observabilityManagedResources {
Expect(runtimeClient.Create(ctx, notHealthyManagedResource(name))).To(Succeed())
}
updatedConditions := NewHealth(
garden,
runtimeClient,
gardenClientSet,
fakeClock,
nil,
gardenNamespace,
).Check(ctx, gardenConditions)
Expect(len(updatedConditions)).ToNot(BeZero())
Expect(updatedConditions).To(ContainCondition(OfType(operatorv1alpha1.ObservabilityComponentsHealthy), WithReason("NotHealthy")))
})
It("should set ObservabilityComponentsHealthy conditions to false when ManagedResources are not applied", func() {
for _, name := range observabilityManagedResources {
Expect(runtimeClient.Create(ctx, notAppliedManagedResource(name))).To(Succeed())
}
updatedConditions := NewHealth(
garden,
runtimeClient,
gardenClientSet,
fakeClock,
nil,
gardenNamespace,
).Check(ctx, gardenConditions)
Expect(len(updatedConditions)).ToNot(BeZero())
Expect(updatedConditions).To(ContainCondition(OfType(operatorv1alpha1.ObservabilityComponentsHealthy), WithReason("NotApplied")))
})
It("should set ObservabilityComponentsHealthy conditions to false when ManagedResources are progressing", func() {
for _, name := range observabilityManagedResources {
Expect(runtimeClient.Create(ctx, progressingManagedResource(name))).To(Succeed())
}
updatedConditions := NewHealth(
garden,
runtimeClient,
gardenClientSet,
fakeClock,
nil,
gardenNamespace,
).Check(ctx, gardenConditions)
Expect(len(updatedConditions)).ToNot(BeZero())
Expect(updatedConditions).To(ContainCondition(OfType(operatorv1alpha1.ObservabilityComponentsHealthy), WithReason("ResourcesProgressing")))
})
It("should set ObservabilityComponentsHealthy conditions to false when some ManagedResources conditions are missing", func() {
for _, name := range observabilityManagedResources {
Expect(runtimeClient.Create(ctx, managedResource(name, []gardencorev1beta1.Condition{{
Type: resourcesv1alpha1.ResourcesApplied,
Status: gardencorev1beta1.ConditionTrue}},
))).To(Succeed())
}
updatedConditions := NewHealth(
garden,
runtimeClient,
gardenClientSet,
fakeClock,
nil,
gardenNamespace,
).Check(ctx, gardenConditions)
Expect(len(updatedConditions)).ToNot(BeZero())
Expect(updatedConditions).To(ContainCondition(OfType(operatorv1alpha1.ObservabilityComponentsHealthy), WithReason("MissingManagedResourceCondition")))
})
})
})
Describe("GardenConditions", func() {
Describe("#NewGardenConditions", func() {
It("should initialize all conditions", func() {
conditions := NewGardenConditions(fakeClock, operatorv1alpha1.GardenStatus{})
Expect(conditions.ConvertToSlice()).To(ConsistOf(
beConditionWithStatusReasonAndMessage("Unknown", "ConditionInitialized", "The condition has been initialized but its semantic check has not been performed yet."),
beConditionWithStatusReasonAndMessage("Unknown", "ConditionInitialized", "The condition has been initialized but its semantic check has not been performed yet."),
beConditionWithStatusReasonAndMessage("Unknown", "ConditionInitialized", "The condition has been initialized but its semantic check has not been performed yet."),
beConditionWithStatusReasonAndMessage("Unknown", "ConditionInitialized", "The condition has been initialized but its semantic check has not been performed yet."),
))
})
It("should only initialize missing conditions", func() {
conditions := NewGardenConditions(fakeClock, operatorv1alpha1.GardenStatus{
Conditions: []gardencorev1beta1.Condition{
{Type: "VirtualGardenAPIServerAvailable"},
{Type: "Foo"},
},
})
Expect(conditions.ConvertToSlice()).To(HaveExactElements(
OfType("VirtualGardenAPIServerAvailable"),
beConditionWithStatusReasonAndMessage("Unknown", "ConditionInitialized", "The condition has been initialized but its semantic check has not been performed yet."),
beConditionWithStatusReasonAndMessage("Unknown", "ConditionInitialized", "The condition has been initialized but its semantic check has not been performed yet."),
beConditionWithStatusReasonAndMessage("Unknown", "ConditionInitialized", "The condition has been initialized but its semantic check has not been performed yet."),
))
})
})
Describe("#ConvertToSlice", func() {
It("should return the expected conditions", func() {
conditions := NewGardenConditions(fakeClock, operatorv1alpha1.GardenStatus{})
Expect(conditions.ConvertToSlice()).To(HaveExactElements(
OfType("VirtualGardenAPIServerAvailable"),
OfType("RuntimeComponentsHealthy"),
OfType("VirtualComponentsHealthy"),
OfType("ObservabilityComponentsHealthy"),
))
})
})
Describe("#ConditionTypes", func() {
It("should return the expected condition types", func() {
conditions := NewGardenConditions(fakeClock, operatorv1alpha1.GardenStatus{})
Expect(conditions.ConditionTypes()).To(HaveExactElements(
gardencorev1beta1.ConditionType("VirtualGardenAPIServerAvailable"),
gardencorev1beta1.ConditionType("RuntimeComponentsHealthy"),
gardencorev1beta1.ConditionType("VirtualComponentsHealthy"),
gardencorev1beta1.ConditionType("ObservabilityComponentsHealthy"),
))
})
})
})
})
func beConditionWithStatusReasonAndMessage(status gardencorev1beta1.ConditionStatus, reason, message string) types.GomegaMatcher {
return And(WithStatus(status), WithReason(reason), WithMessage(message))
}
func healthyManagedResource(name string) *resourcesv1alpha1.ManagedResource {
return managedResource(
name,
[]gardencorev1beta1.Condition{
{
Type: resourcesv1alpha1.ResourcesApplied,
Status: gardencorev1beta1.ConditionTrue,
},
{
Type: resourcesv1alpha1.ResourcesHealthy,
Status: gardencorev1beta1.ConditionTrue,
},
{
Type: resourcesv1alpha1.ResourcesProgressing,
Status: gardencorev1beta1.ConditionFalse,
},
})
}
func notHealthyManagedResource(name string) *resourcesv1alpha1.ManagedResource {
return managedResource(
name,
[]gardencorev1beta1.Condition{
{
Type: resourcesv1alpha1.ResourcesApplied,
Status: gardencorev1beta1.ConditionTrue,
},
{
Type: resourcesv1alpha1.ResourcesHealthy,
Reason: "NotHealthy",
Message: "Resources are not healthy",
Status: gardencorev1beta1.ConditionFalse,
},
{
Type: resourcesv1alpha1.ResourcesProgressing,
Status: gardencorev1beta1.ConditionFalse,
},
})
}
func notAppliedManagedResource(name string) *resourcesv1alpha1.ManagedResource {
return managedResource(
name,
[]gardencorev1beta1.Condition{
{
Type: resourcesv1alpha1.ResourcesApplied,
Reason: "NotApplied",
Message: "Resources are not applied",
Status: gardencorev1beta1.ConditionFalse,
},
{
Type: resourcesv1alpha1.ResourcesHealthy,
Status: gardencorev1beta1.ConditionTrue,
},
{
Type: resourcesv1alpha1.ResourcesProgressing,
Status: gardencorev1beta1.ConditionFalse,
},
})
}
func progressingManagedResource(name string) *resourcesv1alpha1.ManagedResource {
return managedResource(
name,
[]gardencorev1beta1.Condition{
{
Type: resourcesv1alpha1.ResourcesApplied,
Status: gardencorev1beta1.ConditionTrue,
},
{
Type: resourcesv1alpha1.ResourcesHealthy,
Status: gardencorev1beta1.ConditionTrue,
},
{
Type: resourcesv1alpha1.ResourcesProgressing,
Reason: "ResourcesProgressing",
Message: "Resources are progressing",
Status: gardencorev1beta1.ConditionTrue,
},
})
}
func managedResource(name string, conditions []gardencorev1beta1.Condition) *resourcesv1alpha1.ManagedResource {
namespace := v1beta1constants.GardenNamespace
if name == "istio-system" || strings.HasSuffix(name, "istio") {
namespace = v1beta1constants.IstioSystemNamespace
}
return &resourcesv1alpha1.ManagedResource{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Status: resourcesv1alpha1.ManagedResourceStatus{
Conditions: conditions,
},
}
}
func roleLabels(role string) map[string]string {
return map[string]string{v1beta1constants.GardenRole: role}
}
func newDeployment(namespace, name, role string, healthy bool) *appsv1.Deployment {
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
Labels: roleLabels(role),
},
}
if healthy {
deployment.Status = appsv1.DeploymentStatus{Conditions: []appsv1.DeploymentCondition{{
Type: appsv1.DeploymentAvailable,
Status: corev1.ConditionTrue,
}}}
}
return deployment
}
func newEtcd(namespace, name, role string, healthy bool, lastError *string) *druidv1alpha1.Etcd {
etcd := &druidv1alpha1.Etcd{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
Labels: roleLabels(role),
},
}
if healthy {
etcd.Status.Ready = pointer.Bool(true)
} else {
etcd.Status.Ready = pointer.Bool(false)
etcd.Status.LastError = lastError
}
return etcd
}