-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcluster_controller_test.go
421 lines (373 loc) · 18 KB
/
cluster_controller_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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
//go:build !kind_tests
package cluster
import (
"context"
"errors"
"log"
"os"
"path/filepath"
"reflect"
"testing"
"time"
"sigs.k8s.io/controller-runtime/pkg/config"
"go.uber.org/zap/zapcore"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"k8s.io/utils/pointer"
proberpackage "github.com/gardener/dependency-watchdog/internal/prober"
testutil "github.com/gardener/dependency-watchdog/internal/test"
"github.com/gardener/dependency-watchdog/internal/util"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
gardenerv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
const (
testdataPath = "testdata"
maxConcurrentReconcilesProber = 1
)
var (
shootKCMNodeMonitorGracePeriod = &metav1.Duration{Duration: 80 * time.Second}
defaultKCMNodeMonitorGracePeriod = metav1.Duration{Duration: proberpackage.DefaultKCMNodeMonitorGraceDuration}
)
func setupProberEnv(ctx context.Context, g *WithT) (client.Client, *envtest.Environment, *Reconciler, manager.Manager) {
scheme := buildScheme()
crdDirectoryPaths := []string{testdataPath}
opts := zap.Options{
Development: true,
Level: zapcore.DebugLevel,
TimeEncoder: zapcore.RFC3339TimeEncoder,
}
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
testLogger := ctrl.Log.WithName("test-logger")
controllerTestEnv, err := testutil.CreateControllerTestEnv(scheme, crdDirectoryPaths, nil)
g.Expect(err).ToNot(HaveOccurred())
testEnv := controllerTestEnv.GetEnv()
cfg := controllerTestEnv.GetConfig()
crClient := controllerTestEnv.GetClient()
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme,
Logger: testLogger,
Controller: config.Controller{
SkipNameValidation: pointer.Bool(true),
},
})
g.Expect(err).ToNot(HaveOccurred())
scalesGetter, err := util.CreateScalesGetter(cfg)
g.Expect(err).ToNot(HaveOccurred())
probeConfigPath := filepath.Join(testdataPath, "prober-config.yaml")
validateIfFileExists(probeConfigPath, g)
proberConfig, err := proberpackage.LoadConfig(probeConfigPath, scheme)
g.Expect(err).ToNot(HaveOccurred())
clusterReconciler := &Reconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ScaleGetter: scalesGetter,
ProberMgr: proberpackage.NewManager(),
DefaultProbeConfig: proberConfig,
MaxConcurrentReconciles: maxConcurrentReconcilesProber,
}
err = clusterReconciler.SetupWithManager(mgr)
g.Expect(err).ToNot(HaveOccurred())
go func() {
err = mgr.Start(ctx)
g.Expect(err).ToNot(HaveOccurred())
}()
return crClient, testEnv, clusterReconciler, mgr
}
func TestClusterControllerSuite(t *testing.T) {
tests := []struct {
title string
run func(t *testing.T)
}{
{"tests with common environment", testProberSharedEnvTest},
{"tests with dedicated environment for each test", testProberDedicatedEnvTest},
}
for _, test := range tests {
t.Run(test.title, func(t *testing.T) {
test.run(t)
})
}
}
// testProberDedicatedEnvTest creates a new envTest at the start of each subtest and destroys it at the end of each subtest.
func testProberDedicatedEnvTest(t *testing.T) {
g := NewWithT(t)
tests := []struct {
title string
run func(ctx context.Context, t *testing.T, envTest *envtest.Environment, crClient client.Client, reconciler *Reconciler, mgr manager.Manager, cancelFn context.CancelFunc)
}{
{"calling reconciler after shutting down API server", testReconciliationAfterAPIServerIsDown},
}
for _, test := range tests {
ctx, cancelFn := context.WithCancel(context.Background())
crClient, testEnv, reconciler, mgr := setupProberEnv(ctx, g)
t.Run(test.title, func(t *testing.T) {
test.run(ctx, t, testEnv, crClient, reconciler, mgr, cancelFn)
})
testutil.TeardownEnv(g, testEnv, cancelFn)
}
}
func testReconciliationAfterAPIServerIsDown(ctx context.Context, t *testing.T, testEnv *envtest.Environment, _ client.Client, reconciler *Reconciler, _ manager.Manager, cancelFn context.CancelFunc) {
var err error
g := NewWithT(t)
cluster, _, err := testutil.NewClusterBuilder().WithWorkerCount(1).Build()
g.Expect(err).ToNot(HaveOccurred())
cancelFn()
err = testEnv.ControlPlane.APIServer.Stop()
g.Expect(err).ToNot(HaveOccurred())
_, err = reconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: types.NamespacedName{Name: cluster.ObjectMeta.Name, Namespace: ""}})
g.Expect(err).To(HaveOccurred())
err = testEnv.ControlPlane.APIServer.Start()
g.Expect(err).ToNot(HaveOccurred())
}
// testProberSharedEnvTest creates an envTest just once and that is then shared by all the subtests. Shared envTest is destroyed once all subtests have run.
func testProberSharedEnvTest(t *testing.T) {
g := NewWithT(t)
ctx, cancelFn := context.WithCancel(context.Background())
crClient, testEnv, reconciler, _ := setupProberEnv(ctx, g)
defer testutil.TeardownEnv(g, testEnv, cancelFn)
tests := []struct {
title string
run func(g *WithT, crClient client.Client, reconciler *Reconciler)
}{
{"changing hibernation spec and status", testShootHibernation},
{"invalid shoot in cluster spec", testInvalidShootInClusterSpec},
{"deletion time stamp check", testProberShouldBeRemovedIfDeletionTimeStampIsSet},
{"no prober if shoot creation is not successful", testShootCreationNotComplete},
{"no prober if shoot control plane is migrating", testShootIsMigrating},
{"no prober if shoot control plane is restoring after migrate", testShootRestoringIsNotComplete},
{"start prober if last operation is restore and successfully", testLastOperationIsRestoreAndSuccessful},
{"start prober if last operation is reconciliation of shoot", testLastOperationIsShootReconciliation},
{"no prober if shoot has no workers", testShootHasNoWorkers},
{"prober should start with correct worker node conditions mapping", testShootWorkerNodeConditions},
}
for _, test := range tests {
t.Run(test.title, func(_ *testing.T) {
test.run(g, crClient, reconciler)
})
deleteAllClusters(g, crClient)
}
}
func testShootWorkerNodeConditions(g *WithT, crClient client.Client, reconciler *Reconciler) {
workerNodeConditions := [][]string{{testutil.NodeConditionDiskPressure, testutil.NodeConditionMemoryPressure}}
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerCount(1).WithWorkerNodeConditions(workerNodeConditions).Build()
g.Expect(err).ToNot(HaveOccurred())
createCluster(g, crClient, cluster)
expectedWorkerNodeConditions := util.GetEffectiveNodeConditionsForWorkers(shoot)
proberShouldBePresent(g, reconciler, cluster, defaultKCMNodeMonitorGracePeriod, expectedWorkerNodeConditions)
// update the workers
updatedWorkerNodeConditions := []string{testutil.NodeConditionPIDPressure, testutil.NodeConditionNetworkReady}
shoot.Spec.Provider.Workers[0].MachineControllerManagerSettings.NodeConditions = updatedWorkerNodeConditions
cluster.Spec.Shoot = runtime.RawExtension{
Object: shoot,
}
updateCluster(g, crClient, cluster)
expectedWorkerNodeConditions = util.GetEffectiveNodeConditionsForWorkers(shoot)
proberShouldBePresent(g, reconciler, cluster, defaultKCMNodeMonitorGracePeriod, expectedWorkerNodeConditions)
deleteClusterAndCheckIfProberRemoved(g, crClient, reconciler, cluster)
}
func deleteAllClusters(g *WithT, crClient client.Client) {
err := crClient.DeleteAllOf(context.Background(), &gardenerv1alpha1.Cluster{})
g.Expect(err).ToNot(HaveOccurred())
}
func createCluster(g *WithT, crClient client.Client, cluster *gardenerv1alpha1.Cluster) {
err := crClient.Create(context.Background(), cluster)
g.Expect(err).ToNot(HaveOccurred())
time.Sleep(2 * time.Second) // giving some time for controller to take action
}
func updateCluster(g *WithT, crClient client.Client, cluster *gardenerv1alpha1.Cluster) {
err := crClient.Update(context.Background(), cluster)
g.Expect(err).ToNot(HaveOccurred())
time.Sleep(2 * time.Second) // giving some time for controller to take action
}
func deleteClusterAndCheckIfProberRemoved(g *WithT, crClient client.Client, reconciler *Reconciler, cluster *gardenerv1alpha1.Cluster) {
err := crClient.Delete(context.Background(), cluster)
g.Expect(err).ToNot(HaveOccurred())
proberShouldNotBePresent(g, reconciler, cluster)
}
func updateShootHibernationSpec(g *WithT, crClient client.Client, cluster *gardenerv1alpha1.Cluster, shoot *gardencorev1beta1.Shoot, isHibernationEnabled *bool) {
shoot.Spec.Hibernation.Enabled = isHibernationEnabled
cluster.Spec.Shoot = runtime.RawExtension{
Object: shoot,
}
updateCluster(g, crClient, cluster)
}
func testShootHibernation(g *WithT, crClient client.Client, reconciler *Reconciler) {
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerCount(1).Build()
g.Expect(err).ToNot(HaveOccurred())
createCluster(g, crClient, cluster)
expectedWorkerNodeConditions := util.GetEffectiveNodeConditionsForWorkers(shoot)
proberShouldBePresent(g, reconciler, cluster, defaultKCMNodeMonitorGracePeriod, expectedWorkerNodeConditions)
// update spec to indicate start of hibernation
updateShootHibernationSpec(g, crClient, cluster, shoot, pointer.Bool(true))
proberShouldNotBePresent(g, reconciler, cluster)
// update the status to show hibernation is finished
updateShootHibernationStatus(g, crClient, cluster, shoot, true)
// update spec to indicate cluster is waking up
updateShootHibernationSpec(g, crClient, cluster, shoot, pointer.Bool(false))
proberShouldNotBePresent(g, reconciler, cluster)
// update status to indicate cluster has successfully woken up
updateShootHibernationStatus(g, crClient, cluster, shoot, false)
proberShouldBePresent(g, reconciler, cluster, defaultKCMNodeMonitorGracePeriod, expectedWorkerNodeConditions)
deleteClusterAndCheckIfProberRemoved(g, crClient, reconciler, cluster)
}
func updateShootHibernationStatus(g *WithT, crClient client.Client, cluster *gardenerv1alpha1.Cluster, shoot *gardencorev1beta1.Shoot, IsHibernated bool) {
shoot.Status.IsHibernated = IsHibernated
cluster.Spec.Shoot = runtime.RawExtension{
Object: shoot,
}
updateCluster(g, crClient, cluster)
}
func testInvalidShootInClusterSpec(g *WithT, crClient client.Client, reconciler *Reconciler) {
cluster, _, err := testutil.NewClusterBuilder().WithWorkerCount(1).Build()
g.Expect(err).ToNot(HaveOccurred())
cluster.Spec.Shoot.Object = nil
cluster.Spec.Shoot.Raw = []byte(`{"apiVersion": 8}`)
createCluster(g, crClient, cluster)
proberShouldNotBePresent(g, reconciler, cluster)
deleteClusterAndCheckIfProberRemoved(g, crClient, reconciler, cluster)
}
func updateShootDeletionTimeStamp(g *WithT, crClient client.Client, cluster *gardenerv1alpha1.Cluster, shoot *gardencorev1beta1.Shoot) {
deletionTimeStamp, _ := time.Parse(time.RFC3339, "2022-05-05T08:34:05Z")
shoot.DeletionTimestamp = &metav1.Time{
Time: deletionTimeStamp,
}
cluster.Spec.Shoot = runtime.RawExtension{
Object: shoot,
}
err := crClient.Update(context.Background(), cluster)
g.Expect(err).ToNot(HaveOccurred())
}
func testProberShouldBeRemovedIfDeletionTimeStampIsSet(g *WithT, crClient client.Client, reconciler *Reconciler) {
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerCount(1).Build()
g.Expect(err).ToNot(HaveOccurred())
createCluster(g, crClient, cluster)
expectedWorkerNodeConditions := util.GetEffectiveNodeConditionsForWorkers(shoot)
proberShouldBePresent(g, reconciler, cluster, defaultKCMNodeMonitorGracePeriod, expectedWorkerNodeConditions)
updateShootDeletionTimeStamp(g, crClient, cluster, shoot)
proberShouldNotBePresent(g, reconciler, cluster)
deleteClusterAndCheckIfProberRemoved(g, crClient, reconciler, cluster)
}
func setShootLastOperationStatus(cluster *gardenerv1alpha1.Cluster, shoot *gardencorev1beta1.Shoot, opType gardencorev1beta1.LastOperationType, opState gardencorev1beta1.LastOperationState) {
shoot.Status.LastOperation = &gardencorev1beta1.LastOperation{
Type: opType,
State: opState,
}
cluster.Spec.Shoot = runtime.RawExtension{
Object: shoot,
}
}
func testShootCreationNotComplete(g *WithT, crClient client.Client, reconciler *Reconciler) {
testCases := []struct {
lastOpState gardencorev1beta1.LastOperationState
shouldCreateProber bool
}{
{gardencorev1beta1.LastOperationStateProcessing, false},
{gardencorev1beta1.LastOperationStatePending, false},
{gardencorev1beta1.LastOperationStateError, false},
{gardencorev1beta1.LastOperationStateAborted, false},
{gardencorev1beta1.LastOperationStateSucceeded, true},
}
for _, testCase := range testCases {
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerCount(1).WithNodeMonitorGracePeriod(shootKCMNodeMonitorGracePeriod).Build()
g.Expect(err).ToNot(HaveOccurred())
setShootLastOperationStatus(cluster, shoot, gardencorev1beta1.LastOperationTypeCreate, testCase.lastOpState)
createCluster(g, crClient, cluster)
if testCase.shouldCreateProber {
expectedWorkerNodeConditions := util.GetEffectiveNodeConditionsForWorkers(shoot)
proberShouldBePresent(g, reconciler, cluster, *shootKCMNodeMonitorGracePeriod, expectedWorkerNodeConditions)
} else {
proberShouldNotBePresent(g, reconciler, cluster)
}
deleteClusterAndCheckIfProberRemoved(g, crClient, reconciler, cluster)
}
}
func testShootIsMigrating(g *WithT, crClient client.Client, reconciler *Reconciler) {
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerCount(1).Build()
g.Expect(err).ToNot(HaveOccurred())
createCluster(g, crClient, cluster)
setShootLastOperationStatus(cluster, shoot, gardencorev1beta1.LastOperationTypeMigrate, "")
updateCluster(g, crClient, cluster)
proberShouldNotBePresent(g, reconciler, cluster)
deleteClusterAndCheckIfProberRemoved(g, crClient, reconciler, cluster)
}
func testShootRestoringIsNotComplete(g *WithT, crClient client.Client, reconciler *Reconciler) {
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerCount(1).Build()
g.Expect(err).ToNot(HaveOccurred())
createCluster(g, crClient, cluster)
expectedWorkerNodeConditions := util.GetEffectiveNodeConditionsForWorkers(shoot)
proberShouldBePresent(g, reconciler, cluster, defaultKCMNodeMonitorGracePeriod, expectedWorkerNodeConditions)
// cluster migration starts
setShootLastOperationStatus(cluster, shoot, gardencorev1beta1.LastOperationTypeMigrate, "")
updateCluster(g, crClient, cluster)
proberShouldNotBePresent(g, reconciler, cluster)
// cluster migrate done, restore in progress
setShootLastOperationStatus(cluster, shoot, gardencorev1beta1.LastOperationTypeRestore, gardencorev1beta1.LastOperationStateProcessing)
updateCluster(g, crClient, cluster)
proberShouldNotBePresent(g, reconciler, cluster)
deleteClusterAndCheckIfProberRemoved(g, crClient, reconciler, cluster)
}
func testLastOperationIsRestoreAndSuccessful(g *WithT, crClient client.Client, reconciler *Reconciler) {
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerCount(1).Build()
g.Expect(err).ToNot(HaveOccurred())
setShootLastOperationStatus(cluster, shoot, gardencorev1beta1.LastOperationTypeRestore, gardencorev1beta1.LastOperationStateSucceeded)
createCluster(g, crClient, cluster)
expectedWorkerNodeConditions := util.GetEffectiveNodeConditionsForWorkers(shoot)
proberShouldBePresent(g, reconciler, cluster, defaultKCMNodeMonitorGracePeriod, expectedWorkerNodeConditions)
deleteClusterAndCheckIfProberRemoved(g, crClient, reconciler, cluster)
}
func testLastOperationIsShootReconciliation(g *WithT, crClient client.Client, reconciler *Reconciler) {
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerCount(1).Build()
g.Expect(err).ToNot(HaveOccurred())
setShootLastOperationStatus(cluster, shoot, gardencorev1beta1.LastOperationTypeReconcile, "")
createCluster(g, crClient, cluster)
expectedWorkerNodeConditions := util.GetEffectiveNodeConditionsForWorkers(shoot)
proberShouldBePresent(g, reconciler, cluster, defaultKCMNodeMonitorGracePeriod, expectedWorkerNodeConditions)
deleteClusterAndCheckIfProberRemoved(g, crClient, reconciler, cluster)
}
func testShootHasNoWorkers(g *WithT, crClient client.Client, reconciler *Reconciler) {
cluster, _, err := testutil.NewClusterBuilder().Build()
g.Expect(err).ToNot(HaveOccurred())
createCluster(g, crClient, cluster)
proberShouldNotBePresent(g, reconciler, cluster)
}
func proberShouldBePresent(g *WithT, reconciler *Reconciler, cluster *gardenerv1alpha1.Cluster, expectedKCMNodeMonitorGraceDuration metav1.Duration, expectedWorkerNodeConditions map[string][]string) {
g.Eventually(func() bool {
prober, ok := reconciler.ProberMgr.GetProber(cluster.ObjectMeta.Name)
return ok && reflect.DeepEqual(*prober.GetConfig().KCMNodeMonitorGraceDuration, expectedKCMNodeMonitorGraceDuration) && !prober.AreWorkerNodeConditionsStale(expectedWorkerNodeConditions) && !prober.IsClosed()
}, 10*time.Second, 1*time.Second).Should(BeTrue())
}
func proberShouldNotBePresent(g *WithT, reconciler *Reconciler, cluster *gardenerv1alpha1.Cluster) {
g.Eventually(func() int { return len(reconciler.ProberMgr.GetAllProbers()) }, 10*time.Second, 1*time.Second).Should(Equal(0))
prober, ok := reconciler.ProberMgr.GetProber(cluster.ObjectMeta.Name)
g.Expect(ok).To(BeFalse())
g.Expect(prober).To(Equal(proberpackage.Prober{}))
}
func validateIfFileExists(file string, g *WithT) {
var err error
if _, err := os.Stat(file); errors.Is(err, os.ErrNotExist) {
log.Fatalf("%s does not exist. This should not have happened. Check testdata directory.\n", file)
}
g.Expect(err).ToNot(HaveOccurred(), "File at path %v should exist")
}
func buildScheme() *runtime.Scheme {
scheme := runtime.NewScheme()
localSchemeBuilder := runtime.NewSchemeBuilder(
clientgoscheme.AddToScheme,
gardenerv1alpha1.AddToScheme,
)
utilruntime.Must(localSchemeBuilder.AddToScheme(scheme))
return scheme
}