forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth_test.go
359 lines (302 loc) · 12.6 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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package care_test
import (
"context"
"strings"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
testclock "k8s.io/utils/clock/testing"
"k8s.io/utils/ptr"
"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"
resourcesv1alpha1 "github.com/gardener/gardener/pkg/apis/resources/v1alpha1"
"github.com/gardener/gardener/pkg/client/kubernetes"
. "github.com/gardener/gardener/pkg/gardenlet/controller/seed/care"
. "github.com/gardener/gardener/pkg/utils/test/matchers"
)
var _ = Describe("Seed health", func() {
var (
ctx context.Context
c client.Client
fakeClock *testclock.FakeClock
seed *gardencorev1beta1.Seed
seedSystemComponentsHealthyCondition gardencorev1beta1.Condition
)
BeforeEach(func() {
ctx = context.Background()
c = fakeclient.NewClientBuilder().WithScheme(kubernetes.SeedScheme).Build()
seed = &gardencorev1beta1.Seed{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Spec: gardencorev1beta1.SeedSpec{
Ingress: &gardencorev1beta1.Ingress{
Controller: gardencorev1beta1.IngressController{
Kind: "nginx",
},
},
Settings: &gardencorev1beta1.SeedSettings{
DependencyWatchdog: &gardencorev1beta1.SeedSettingDependencyWatchdog{
Weeder: &gardencorev1beta1.SeedSettingDependencyWatchdogWeeder{
Enabled: true,
},
Prober: &gardencorev1beta1.SeedSettingDependencyWatchdogProber{
Enabled: true,
},
},
},
},
}
fakeClock = testclock.NewFakeClock(time.Now())
seedSystemComponentsHealthyCondition = gardencorev1beta1.Condition{
Type: gardencorev1beta1.SeedSystemComponentsHealthy,
LastTransitionTime: metav1.Time{Time: fakeClock.Now()},
}
})
Describe("#Check", func() {
managedResourceName := "foo"
Context("When all managed resources are deployed successfully", func() {
JustBeforeEach(func() {
Expect(c.Create(ctx, healthyManagedResource(managedResourceName))).To(Succeed())
})
It("should set SeedSystemComponentsHealthy condition to true", func() {
healthCheck := NewHealth(seed, c, fakeClock, nil, nil)
conditions := NewSeedConditions(fakeClock, gardencorev1beta1.SeedStatus{
Conditions: []gardencorev1beta1.Condition{seedSystemComponentsHealthyCondition},
})
updatedConditions := healthCheck.Check(ctx, conditions)
Expect(updatedConditions).ToNot(BeEmpty())
Expect(updatedConditions[0]).To(beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionTrue, "SystemComponentsRunning", "All system components are healthy."))
})
})
Context("When there are issues with seed managed resources", func() {
var (
tests = func(reason, message string) {
It("should set SeedSystemComponentsHealthy condition to False if there is no Progressing threshold duration mapping", func() {
healthCheck := NewHealth(seed, c, fakeClock, nil, nil)
conditions := NewSeedConditions(fakeClock, gardencorev1beta1.SeedStatus{
Conditions: []gardencorev1beta1.Condition{seedSystemComponentsHealthyCondition},
})
updatedConditions := healthCheck.Check(ctx, conditions)
Expect(updatedConditions).ToNot(BeEmpty())
Expect(updatedConditions[0]).To(beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionFalse, reason, message))
})
It("should set SeedSystemComponentsHealthy condition to Progressing if time is within threshold duration and condition is currently False", func() {
seedSystemComponentsHealthyCondition.Status = gardencorev1beta1.ConditionFalse
fakeClock.Step(30 * time.Second)
healthCheck := NewHealth(seed, c, fakeClock, nil, map[gardencorev1beta1.ConditionType]time.Duration{gardencorev1beta1.SeedSystemComponentsHealthy: time.Minute})
conditions := NewSeedConditions(fakeClock, gardencorev1beta1.SeedStatus{
Conditions: []gardencorev1beta1.Condition{seedSystemComponentsHealthyCondition},
})
updatedConditions := healthCheck.Check(ctx, conditions)
Expect(updatedConditions).ToNot(BeEmpty())
Expect(updatedConditions[0]).To(beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionProgressing, reason, message))
})
It("should set SeedSystemComponentsHealthy condition to Progressing if time is within threshold duration and condition is currently True", func() {
seedSystemComponentsHealthyCondition.Status = gardencorev1beta1.ConditionTrue
fakeClock.Step(30 * time.Second)
healthCheck := NewHealth(seed, c, fakeClock, nil, map[gardencorev1beta1.ConditionType]time.Duration{gardencorev1beta1.SeedSystemComponentsHealthy: time.Minute})
conditions := NewSeedConditions(fakeClock, gardencorev1beta1.SeedStatus{
Conditions: []gardencorev1beta1.Condition{seedSystemComponentsHealthyCondition},
})
updatedConditions := healthCheck.Check(ctx, conditions)
Expect(updatedConditions).ToNot(BeEmpty())
Expect(updatedConditions[0]).To(beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionProgressing, reason, message))
})
It("should not set SeedSystemComponentsHealthy condition to false if Progressing threshold duration has not expired", func() {
seedSystemComponentsHealthyCondition.Status = gardencorev1beta1.ConditionProgressing
fakeClock.Step(30 * time.Second)
healthCheck := NewHealth(seed, c, fakeClock, nil, map[gardencorev1beta1.ConditionType]time.Duration{gardencorev1beta1.SeedSystemComponentsHealthy: time.Minute})
conditions := NewSeedConditions(fakeClock, gardencorev1beta1.SeedStatus{
Conditions: []gardencorev1beta1.Condition{seedSystemComponentsHealthyCondition},
})
updatedConditions := healthCheck.Check(ctx, conditions)
Expect(updatedConditions).ToNot(BeEmpty())
Expect(updatedConditions[0]).To(beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionProgressing, reason, message))
})
It("should set SeedSystemComponentsHealthy condition to false if Progressing threshold duration has expired", func() {
seedSystemComponentsHealthyCondition.Status = gardencorev1beta1.ConditionProgressing
fakeClock.Step(90 * time.Second)
healthCheck := NewHealth(seed, c, fakeClock, nil, map[gardencorev1beta1.ConditionType]time.Duration{gardencorev1beta1.SeedSystemComponentsHealthy: time.Minute})
conditions := NewSeedConditions(fakeClock, gardencorev1beta1.SeedStatus{
Conditions: []gardencorev1beta1.Condition{seedSystemComponentsHealthyCondition},
})
updatedConditions := healthCheck.Check(ctx, conditions)
Expect(updatedConditions).ToNot(BeEmpty())
Expect(updatedConditions[0]).To(beConditionWithStatusReasonAndMessage(gardencorev1beta1.ConditionFalse, reason, message))
})
}
)
Context("When all managed resources are deployed, but not healthy", func() {
JustBeforeEach(func() {
Expect(c.Create(ctx, notHealthyManagedResource(managedResourceName))).To(Succeed())
})
tests("NotHealthy", "Resources are not healthy")
})
Context("When all managed resources are deployed but their resources are not applied", func() {
JustBeforeEach(func() {
Expect(c.Create(ctx, notAppliedManagedResource(managedResourceName))).To(Succeed())
})
tests("NotApplied", "Resources are not applied")
})
Context("When all managed resources are deployed but their resources are still progressing", func() {
JustBeforeEach(func() {
Expect(c.Create(ctx, progressingManagedResource(managedResourceName))).To(Succeed())
})
tests("ResourcesProgressing", "Resources are progressing")
})
Context("When all managed resources are deployed but not all required conditions are present", func() {
JustBeforeEach(func() {
Expect(c.Create(ctx, managedResource(managedResourceName, []gardencorev1beta1.Condition{{
Type: resourcesv1alpha1.ResourcesApplied,
Status: gardencorev1beta1.ConditionTrue}},
))).To(Succeed())
})
tests("MissingManagedResourceCondition", "is missing the following condition(s)")
})
})
})
Describe("SeedConditions", func() {
Describe("#NewSeedConditions", func() {
It("should initialize all conditions", func() {
conditions := NewSeedConditions(fakeClock, gardencorev1beta1.SeedStatus{})
Expect(conditions.ConvertToSlice()).To(ConsistOf(
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 := NewSeedConditions(fakeClock, gardencorev1beta1.SeedStatus{
Conditions: []gardencorev1beta1.Condition{
{Type: "SeedSystemComponentsHealthy"},
{Type: "Foo"},
},
})
Expect(conditions.ConvertToSlice()).To(HaveExactElements(
OfType("SeedSystemComponentsHealthy"),
))
})
})
Describe("#ConvertToSlice", func() {
It("should return the expected conditions", func() {
conditions := NewSeedConditions(fakeClock, gardencorev1beta1.SeedStatus{})
Expect(conditions.ConvertToSlice()).To(HaveExactElements(
OfType("SeedSystemComponentsHealthy"),
))
})
})
Describe("#ConditionTypes", func() {
It("should return the expected condition types", func() {
conditions := NewSeedConditions(fakeClock, gardencorev1beta1.SeedStatus{})
Expect(conditions.ConditionTypes()).To(HaveExactElements(
gardencorev1beta1.ConditionType("SeedSystemComponentsHealthy"),
))
})
})
})
})
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,
},
Spec: resourcesv1alpha1.ManagedResourceSpec{
Class: ptr.To("seed"),
},
Status: resourcesv1alpha1.ManagedResourceStatus{
Conditions: conditions,
},
}
}