-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcontroller_test.go
775 lines (685 loc) · 27.3 KB
/
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
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
package remotedatabasenodeset_test
import (
"context"
"fmt"
"path/filepath"
"strings"
"testing"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/kubectl/pkg/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
testobjects "github.com/ydb-platform/ydb-kubernetes-operator/e2e/tests/test-objects"
ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
. "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/database"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/databasenodeset"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/remotedatabasenodeset"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/storage"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/resources"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/test"
)
const (
testRemoteCluster = "remote-cluster"
testNodeSetName = "nodeset"
testSecretName = "remote-secret"
)
var (
localClient client.Client
remoteClient client.Client
localEnv *envtest.Environment
remoteEnv *envtest.Environment
ctx context.Context
cancel context.CancelFunc
)
func TestRemoteDatabaseNodeSetApis(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "RemoteDatabaseNodeSet controller tests")
}
var _ = BeforeSuite(func() {
By("bootstrapping test environment")
ctx, cancel = context.WithCancel(context.TODO())
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
localEnv = &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join("..", "..", "..", "deploy", "ydb-operator", "crds"),
},
ErrorIfCRDPathMissing: true,
}
remoteEnv = &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join("..", "..", "..", "deploy", "ydb-operator", "crds"),
},
ErrorIfCRDPathMissing: true,
}
err := v1alpha1.AddToScheme(scheme.Scheme)
Expect(err).ShouldNot(HaveOccurred())
localCfg, err := localEnv.Start()
Expect(err).ToNot(HaveOccurred())
Expect(localCfg).ToNot(BeNil())
remoteCfg, err := remoteEnv.Start()
Expect(err).ToNot(HaveOccurred())
Expect(remoteCfg).ToNot(BeNil())
// +kubebuilder:scaffold:scheme
localManager, err := ctrl.NewManager(localCfg, ctrl.Options{
MetricsBindAddress: "0",
Scheme: scheme.Scheme,
})
Expect(err).ShouldNot(HaveOccurred())
//+kubebuilder:scaffold:scheme
remoteManager, err := ctrl.NewManager(remoteCfg, ctrl.Options{
MetricsBindAddress: "0",
Scheme: scheme.Scheme,
})
Expect(err).ShouldNot(HaveOccurred())
databaseSelector, err := remotedatabasenodeset.BuildRemoteSelector(testRemoteCluster)
Expect(err).ShouldNot(HaveOccurred())
remoteCluster, err := cluster.New(localCfg, func(o *cluster.Options) {
o.Scheme = scheme.Scheme
o.NewCache = cache.BuilderWithOptions(cache.Options{
SelectorsByObject: cache.SelectorsByObject{
&v1alpha1.RemoteDatabaseNodeSet{}: {Label: databaseSelector},
},
})
})
Expect(err).ShouldNot(HaveOccurred())
err = remoteManager.Add(remoteCluster)
Expect(err).ShouldNot(HaveOccurred())
err = (&storage.Reconciler{
Client: localManager.GetClient(),
Scheme: localManager.GetScheme(),
Config: localManager.GetConfig(),
Log: logf.Log,
}).SetupWithManager(localManager)
Expect(err).ShouldNot(HaveOccurred())
err = (&database.Reconciler{
Client: localManager.GetClient(),
Scheme: localManager.GetScheme(),
Config: localManager.GetConfig(),
Log: logf.Log,
}).SetupWithManager(localManager)
Expect(err).ShouldNot(HaveOccurred())
err = (&databasenodeset.Reconciler{
Client: localManager.GetClient(),
Scheme: localManager.GetScheme(),
Config: localManager.GetConfig(),
Log: logf.Log,
}).SetupWithManager(localManager)
Expect(err).ShouldNot(HaveOccurred())
err = (&databasenodeset.Reconciler{
Client: remoteManager.GetClient(),
Scheme: remoteManager.GetScheme(),
Config: remoteManager.GetConfig(),
Log: logf.Log,
}).SetupWithManager(remoteManager)
Expect(err).ShouldNot(HaveOccurred())
err = (&remotedatabasenodeset.Reconciler{
Client: remoteManager.GetClient(),
Scheme: remoteManager.GetScheme(),
Log: logf.Log,
}).SetupWithManager(remoteManager, &remoteCluster)
Expect(err).ShouldNot(HaveOccurred())
go func() {
defer GinkgoRecover()
err = localManager.Start(ctx)
Expect(err).ShouldNot(HaveOccurred())
}()
go func() {
defer GinkgoRecover()
err = remoteManager.Start(ctx)
Expect(err).ShouldNot(HaveOccurred())
}()
localClient = localManager.GetClient()
Expect(localClient).ToNot(BeNil())
remoteClient = remoteManager.GetClient()
Expect(remoteClient).ToNot(BeNil())
})
var _ = AfterSuite(func() {
By("tearing down the test environment")
cancel()
err := localEnv.Stop()
Expect(err).ToNot(HaveOccurred())
err = remoteEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})
var _ = Describe("RemoteDatabaseNodeSet controller tests", func() {
var localNamespace corev1.Namespace
var remoteNamespace corev1.Namespace
var storageSample *v1alpha1.Storage
var databaseSample *v1alpha1.Database
BeforeEach(func() {
storageSample = testobjects.DefaultStorage(filepath.Join("..", "..", "..", "e2e", "tests", "data", "storage-block-4-2-config.yaml"))
databaseSample = testobjects.DefaultDatabase()
databaseSample.Spec.NodeSets = append(databaseSample.Spec.NodeSets, v1alpha1.DatabaseNodeSetSpecInline{
Name: testNodeSetName + "-local",
DatabaseNodeSpec: v1alpha1.DatabaseNodeSpec{
Nodes: 4,
},
})
databaseSample.Spec.NodeSets = append(databaseSample.Spec.NodeSets, v1alpha1.DatabaseNodeSetSpecInline{
Name: testNodeSetName + "-remote",
Remote: &v1alpha1.RemoteSpec{
Cluster: testRemoteCluster,
},
DatabaseNodeSpec: v1alpha1.DatabaseNodeSpec{
Nodes: 4,
},
})
databaseSample.Spec.NodeSets = append(databaseSample.Spec.NodeSets, v1alpha1.DatabaseNodeSetSpecInline{
Name: testNodeSetName + "-remote-dedicated",
Remote: &v1alpha1.RemoteSpec{
Cluster: testRemoteCluster,
},
DatabaseNodeSpec: v1alpha1.DatabaseNodeSpec{
Nodes: 4,
},
})
By("issuing create Namespace commands...")
localNamespace = corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: testobjects.YdbNamespace,
},
}
remoteNamespace = corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: testobjects.YdbNamespace,
},
}
Expect(localClient.Create(ctx, &localNamespace)).Should(Succeed())
Expect(remoteClient.Create(ctx, &remoteNamespace)).Should(Succeed())
By("issuing create Storage commands...")
Expect(localClient.Create(ctx, storageSample)).Should(Succeed())
By("checking that Storage created on local cluster...")
foundStorage := v1alpha1.Storage{}
Eventually(func() bool {
Expect(localClient.Get(ctx, types.NamespacedName{
Name: storageSample.Name,
Namespace: testobjects.YdbNamespace,
}, &foundStorage))
return foundStorage.Status.State == StorageInitializing
}, test.Timeout, test.Interval).Should(BeTrue())
By("set status Ready to Storage...")
Eventually(func() error {
foundStorage := v1alpha1.Storage{}
Expect(localClient.Get(ctx, types.NamespacedName{
Name: storageSample.Name,
Namespace: testobjects.YdbNamespace,
}, &foundStorage))
foundStorage.Status.State = StorageReady
return localClient.Status().Update(ctx, &foundStorage)
}, test.Timeout, test.Interval).ShouldNot(HaveOccurred())
By("issuing create Database commands...")
Expect(localClient.Create(ctx, databaseSample)).Should(Succeed())
By("checking that Database created on local cluster...")
foundDatabase := v1alpha1.Database{}
Eventually(func() bool {
Expect(localClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name,
Namespace: testobjects.YdbNamespace,
}, &foundDatabase))
return foundDatabase.Status.State == DatabaseInitializing
}, test.Timeout, test.Interval).Should(BeTrue())
By("checking that DatabaseNodeSet created on local cluster...")
Eventually(func() error {
foundDatabaseNodeSet := &v1alpha1.DatabaseNodeSet{}
return localClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name + "-" + testNodeSetName + "-local",
Namespace: testobjects.YdbNamespace,
}, foundDatabaseNodeSet)
}, test.Timeout, test.Interval).ShouldNot(HaveOccurred())
By("checking that RemoteDatabaseNodeSet created on local cluster...")
Eventually(func() error {
foundRemoteDatabaseNodeSet := &v1alpha1.RemoteDatabaseNodeSet{}
return localClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name + "-" + testNodeSetName + "-remote",
Namespace: testobjects.YdbNamespace,
}, foundRemoteDatabaseNodeSet)
}, test.Timeout, test.Interval).ShouldNot(HaveOccurred())
By("checking that dedicated RemoteDatabaseNodeSet created on local cluster...")
Eventually(func() error {
foundRemoteDatabaseNodeSet := &v1alpha1.RemoteDatabaseNodeSet{}
return localClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name + "-" + testNodeSetName + "-remote-dedicated",
Namespace: testobjects.YdbNamespace,
}, foundRemoteDatabaseNodeSet)
}, test.Timeout, test.Interval).ShouldNot(HaveOccurred())
By("checking that DatabaseNodeSet created on remote cluster...")
Eventually(func() bool {
foundDatabaseNodeSetOnRemote := v1alpha1.DatabaseNodeSetList{}
Expect(remoteClient.List(ctx, &foundDatabaseNodeSetOnRemote, client.InNamespace(
testobjects.YdbNamespace,
))).Should(Succeed())
for _, nodeset := range foundDatabaseNodeSetOnRemote.Items {
if nodeset.Name == databaseSample.Name+"-"+testNodeSetName+"-remote" {
return true
}
}
return false
}, test.Timeout, test.Interval).Should(BeTrue())
By("checking that dedicated DatabaseNodeSet created on remote cluster...")
Eventually(func() bool {
foundDatabaseNodeSetOnRemote := v1alpha1.DatabaseNodeSetList{}
Expect(remoteClient.List(ctx, &foundDatabaseNodeSetOnRemote, client.InNamespace(
testobjects.YdbNamespace,
))).Should(Succeed())
for _, nodeset := range foundDatabaseNodeSetOnRemote.Items {
if nodeset.Name == databaseSample.Name+"-"+testNodeSetName+"-remote-dedicated" {
return true
}
}
return false
}, test.Timeout, test.Interval).Should(BeTrue())
})
AfterEach(func() {
deleteAll(localEnv, localClient, &localNamespace)
deleteAll(remoteEnv, remoteClient, &localNamespace)
})
When("Created RemoteDatabaseNodeSet in k8s-mgmt-cluster", func() {
It("Should receive status from k8s-data-cluster", func() {
By("set dedicated DatabaseNodeSet status to Ready on remote cluster...")
foundDedicatedDatabaseNodeSetOnRemote := v1alpha1.DatabaseNodeSet{}
Expect(remoteClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name + "-" + testNodeSetName + "-remote-dedicated",
Namespace: testobjects.YdbNamespace,
}, &foundDedicatedDatabaseNodeSetOnRemote)).Should(Succeed())
foundDedicatedDatabaseNodeSetOnRemote.Status.State = DatabaseNodeSetReady
foundDedicatedDatabaseNodeSetOnRemote.Status.Conditions = append(
foundDedicatedDatabaseNodeSetOnRemote.Status.Conditions,
metav1.Condition{
Type: DatabaseNodeSetReadyCondition,
Status: "True",
Reason: ReasonCompleted,
LastTransitionTime: metav1.NewTime(time.Now()),
Message: fmt.Sprintf("Scaled databaseNodeSet to %d successfully", foundDedicatedDatabaseNodeSetOnRemote.Spec.Nodes),
},
)
Expect(remoteClient.Status().Update(ctx, &foundDedicatedDatabaseNodeSetOnRemote)).Should(Succeed())
By("set DatabaseNodeSet status to Ready on remote cluster...")
foundDatabaseNodeSetOnRemote := v1alpha1.DatabaseNodeSet{}
Expect(remoteClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name + "-" + testNodeSetName + "-remote",
Namespace: testobjects.YdbNamespace,
}, &foundDatabaseNodeSetOnRemote)).Should(Succeed())
foundDatabaseNodeSetOnRemote.Status.State = DatabaseNodeSetReady
foundDatabaseNodeSetOnRemote.Status.Conditions = append(
foundDatabaseNodeSetOnRemote.Status.Conditions,
metav1.Condition{
Type: DatabaseNodeSetReadyCondition,
Status: "True",
Reason: ReasonCompleted,
LastTransitionTime: metav1.NewTime(time.Now()),
Message: fmt.Sprintf("Scaled databaseNodeSet to %d successfully", foundDatabaseNodeSetOnRemote.Spec.Nodes),
},
)
Expect(remoteClient.Status().Update(ctx, &foundDatabaseNodeSetOnRemote)).Should(Succeed())
By("checking that dedicated RemoteDatabaseNodeSet status updated on local cluster...")
Eventually(func() bool {
foundRemoteDatabaseNodeSetOnRemote := v1alpha1.RemoteDatabaseNodeSet{}
Expect(localClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name + "-" + testNodeSetName + "-remote-dedicated",
Namespace: testobjects.YdbNamespace,
}, &foundRemoteDatabaseNodeSetOnRemote)).Should(Succeed())
return meta.IsStatusConditionPresentAndEqual(
foundRemoteDatabaseNodeSetOnRemote.Status.Conditions,
DatabaseNodeSetReadyCondition,
metav1.ConditionTrue,
) && foundRemoteDatabaseNodeSetOnRemote.Status.State == DatabaseNodeSetReady
}, test.Timeout, test.Interval).Should(BeTrue())
By("checking that RemoteDatabaseNodeSet status updated on local cluster...")
Eventually(func() bool {
foundRemoteDatabaseNodeSetOnRemote := v1alpha1.RemoteDatabaseNodeSet{}
Expect(localClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name + "-" + testNodeSetName + "-remote",
Namespace: testobjects.YdbNamespace,
}, &foundRemoteDatabaseNodeSetOnRemote)).Should(Succeed())
return meta.IsStatusConditionPresentAndEqual(
foundRemoteDatabaseNodeSetOnRemote.Status.Conditions,
DatabaseNodeSetReadyCondition,
metav1.ConditionTrue,
) && foundRemoteDatabaseNodeSetOnRemote.Status.State == DatabaseNodeSetReady
}, test.Timeout, test.Interval).Should(BeTrue())
})
})
When("Created RemoteDatabaseNodeSet with Secrets in k8s-mgmt-cluster", func() {
It("Should sync Secrets into k8s-data-cluster", func() {
By("create simple Secret in Database namespace")
simpleSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: testSecretName,
Namespace: testobjects.YdbNamespace,
},
StringData: map[string]string{
"message": "Hello from k8s-mgmt-cluster",
},
}
Expect(localClient.Create(ctx, simpleSecret))
By("checking that Database updated on local cluster...")
Eventually(func() error {
foundDatabase := &v1alpha1.Database{}
Expect(localClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name,
Namespace: testobjects.YdbNamespace,
}, foundDatabase))
foundDatabase.Spec.Secrets = append(
foundDatabase.Spec.Secrets,
&corev1.LocalObjectReference{
Name: testSecretName,
},
)
return localClient.Update(ctx, foundDatabase)
}, test.Timeout, test.Interval).ShouldNot(HaveOccurred())
By("checking that Secrets are synced...")
Eventually(func() error {
foundRemoteDatabaseNodeSet := &v1alpha1.RemoteDatabaseNodeSet{}
Expect(localClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name + "-" + testNodeSetName + "-remote",
Namespace: testobjects.YdbNamespace,
}, foundRemoteDatabaseNodeSet)).Should(Succeed())
localSecret := &corev1.Secret{}
err := localClient.Get(ctx, types.NamespacedName{
Name: testSecretName,
Namespace: testobjects.YdbNamespace,
}, localSecret)
if err != nil {
return err
}
remoteSecret := &corev1.Secret{}
err = remoteClient.Get(ctx, types.NamespacedName{
Name: testSecretName,
Namespace: testobjects.YdbNamespace,
}, remoteSecret)
if err != nil {
return err
}
primaryResourceName, exist := remoteSecret.Annotations[ydbannotations.PrimaryResourceDatabaseAnnotation]
if !exist {
return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.PrimaryResourceDatabaseAnnotation, remoteSecret.Name)
}
if primaryResourceName != foundRemoteDatabaseNodeSet.Spec.DatabaseRef.Name {
return fmt.Errorf("primaryResourceName %s does not equal databaseRef name %s", primaryResourceName, foundRemoteDatabaseNodeSet.Spec.DatabaseRef.Name)
}
remoteRV, exist := remoteSecret.Annotations[ydbannotations.RemoteResourceVersionAnnotation]
if !exist {
return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.RemoteResourceVersionAnnotation, remoteSecret.Name)
}
if localSecret.GetResourceVersion() != remoteRV {
return fmt.Errorf("localRV %s does not equal remoteRV %s", localSecret.GetResourceVersion(), remoteRV)
}
return nil
}, test.Timeout, test.Interval).ShouldNot(HaveOccurred())
})
})
When("Created RemoteDatabaseNodeSet with Services in k8s-mgmt-cluster", func() {
It("Should sync Services into k8s-data-cluster", func() {
By("checking that Services are synced...")
Eventually(func() error {
foundRemoteDatabaseNodeSet := &v1alpha1.RemoteDatabaseNodeSet{}
Expect(localClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name + "-" + testNodeSetName + "-remote",
Namespace: testobjects.YdbNamespace,
}, foundRemoteDatabaseNodeSet)).Should(Succeed())
foundServices := corev1.ServiceList{}
Expect(localClient.List(ctx, &foundServices, client.InNamespace(
testobjects.YdbNamespace,
))).Should(Succeed())
for _, localService := range foundServices.Items {
if !strings.HasPrefix(localService.Name, databaseSample.Name) {
continue
}
remoteService := &corev1.Service{}
err := remoteClient.Get(ctx, types.NamespacedName{
Name: localService.Name,
Namespace: localService.Namespace,
}, remoteService)
if err != nil {
return err
}
}
return nil
}, test.Timeout, test.Interval).ShouldNot(HaveOccurred())
By("checking that dedicated RemoteDatabaseNodeSet RemoteStatus are updated...")
Eventually(func() bool {
foundRemoteDatabaseNodeSet := &v1alpha1.RemoteDatabaseNodeSet{}
Expect(localClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name + "-" + testNodeSetName + "-remote-dedicated",
Namespace: testobjects.YdbNamespace,
}, foundRemoteDatabaseNodeSet)).Should(Succeed())
foundConfigMap := corev1.ConfigMap{}
Expect(remoteClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name,
Namespace: testobjects.YdbNamespace,
}, &foundConfigMap)).Should(Succeed())
gvk, err := apiutil.GVKForObject(foundConfigMap.DeepCopy(), scheme.Scheme)
Expect(err).ShouldNot(HaveOccurred())
for idx := range foundRemoteDatabaseNodeSet.Status.RemoteResources {
remoteResource := foundRemoteDatabaseNodeSet.Status.RemoteResources[idx]
if resources.EqualRemoteResourceWithObject(
&remoteResource,
testobjects.YdbNamespace,
foundConfigMap.DeepCopy(),
gvk,
) {
if meta.IsStatusConditionPresentAndEqual(
remoteResource.Conditions,
RemoteResourceSyncedCondition,
metav1.ConditionTrue,
) {
return true
}
}
}
return false
}, test.Timeout, test.Interval).Should(BeTrue())
By("checking that RemoteDatabaseNodeSet RemoteStatus are updated...")
Eventually(func() bool {
foundRemoteDatabaseNodeSet := &v1alpha1.RemoteDatabaseNodeSet{}
Expect(localClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name + "-" + testNodeSetName + "-remote",
Namespace: testobjects.YdbNamespace,
}, foundRemoteDatabaseNodeSet)).Should(Succeed())
foundConfigMap := corev1.ConfigMap{}
Expect(remoteClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name,
Namespace: testobjects.YdbNamespace,
}, &foundConfigMap)).Should(Succeed())
gvk, err := apiutil.GVKForObject(foundConfigMap.DeepCopy(), scheme.Scheme)
Expect(err).ShouldNot(HaveOccurred())
for idx := range foundRemoteDatabaseNodeSet.Status.RemoteResources {
remoteResource := foundRemoteDatabaseNodeSet.Status.RemoteResources[idx]
if resources.EqualRemoteResourceWithObject(
&remoteResource,
testobjects.YdbNamespace,
foundConfigMap.DeepCopy(),
gvk,
) {
if meta.IsStatusConditionPresentAndEqual(
remoteResource.Conditions,
RemoteResourceSyncedCondition,
metav1.ConditionTrue,
) {
return true
}
}
}
return false
}, test.Timeout, test.Interval).Should(BeTrue())
})
})
When("Delete RemoteDatabaseNodeSet in k8s-mgmt-cluster", func() {
It("Should delete resources in k8s-data-cluster", func() {
By("delete RemoteDatabaseNodeSet on local cluster...")
Eventually(func() error {
foundDatabase := v1alpha1.Database{}
Expect(localClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name,
Namespace: testobjects.YdbNamespace,
}, &foundDatabase)).Should(Succeed())
foundDatabase.Spec.NodeSets = []v1alpha1.DatabaseNodeSetSpecInline{
{
Name: testNodeSetName + "-local",
DatabaseNodeSpec: v1alpha1.DatabaseNodeSpec{
Nodes: 4,
},
},
{
Name: testNodeSetName + "-remote-dedicated",
Remote: &v1alpha1.RemoteSpec{
Cluster: testRemoteCluster,
},
DatabaseNodeSpec: v1alpha1.DatabaseNodeSpec{
Nodes: 4,
},
},
}
return localClient.Update(ctx, &foundDatabase)
}, test.Timeout, test.Interval).Should(Succeed())
By("checking that DatabaseNodeSet deleted from remote cluster...")
Eventually(func() bool {
foundDatabaseNodeSetOnRemote := v1alpha1.DatabaseNodeSet{}
err := remoteClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name + "-" + testNodeSetName + "-remote",
Namespace: testobjects.YdbNamespace,
}, &foundDatabaseNodeSetOnRemote)
return apierrors.IsNotFound(err)
}, test.Timeout, test.Interval).Should(BeTrue())
By("checking that RemoteDatabaseNodeSet deleted from local cluster...")
Eventually(func() bool {
foundRemoteDatabaseNodeSet := v1alpha1.RemoteDatabaseNodeSet{}
err := localClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name + "-" + testNodeSetName + "-remote",
Namespace: testobjects.YdbNamespace,
}, &foundRemoteDatabaseNodeSet)
return apierrors.IsNotFound(err)
}, test.Timeout, test.Interval).Should(BeTrue())
By("checking that Services for dedicated DatabaseNodeSet exisiting in remote cluster...")
Eventually(func() error {
foundDedicatedDatabaseNodeSetOnRemote := &v1alpha1.DatabaseNodeSet{}
Expect(remoteClient.Get(ctx, types.NamespacedName{
Name: databaseSample.Name + "-" + testNodeSetName + "-remote-dedicated",
Namespace: testobjects.YdbNamespace,
}, foundDedicatedDatabaseNodeSetOnRemote)).Should(Succeed())
databaseServices := corev1.ServiceList{}
Expect(localClient.List(ctx, &databaseServices,
client.InNamespace(testobjects.YdbNamespace),
)).Should(Succeed())
for _, databaseService := range databaseServices.Items {
remoteService := &corev1.Service{}
if !strings.HasPrefix(databaseService.GetName(), databaseSample.Name) {
continue
}
if err := remoteClient.Get(ctx, types.NamespacedName{
Name: databaseService.GetName(),
Namespace: databaseService.GetNamespace(),
}, remoteService); err != nil {
return err
}
}
return nil
}, test.Timeout, test.Interval).ShouldNot(HaveOccurred())
})
})
})
func deleteAll(env *envtest.Environment, k8sClient client.Client, objs ...client.Object) {
for _, obj := range objs {
ctx := context.Background()
clientGo, err := kubernetes.NewForConfig(env.Config)
Expect(err).ShouldNot(HaveOccurred())
Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, obj))).Should(Succeed())
//nolint:nestif
if ns, ok := obj.(*corev1.Namespace); ok {
// Normally the kube-controller-manager would handle finalization
// and garbage collection of namespaces, but with envtest, we aren't
// running a kube-controller-manager. Instead we're gonna approximate
// (poorly) the kube-controller-manager by explicitly deleting some
// resources within the namespace and then removing the `kubernetes`
// finalizer from the namespace resource so it can finish deleting.
// Note that any resources within the namespace that we don't
// successfully delete could reappear if the namespace is ever
// recreated with the same name.
// Look up all namespaced resources under the discovery API
_, apiResources, err := clientGo.Discovery().ServerGroupsAndResources()
Expect(err).ShouldNot(HaveOccurred())
namespacedGVKs := make(map[string]schema.GroupVersionKind)
for _, apiResourceList := range apiResources {
defaultGV, err := schema.ParseGroupVersion(apiResourceList.GroupVersion)
Expect(err).ShouldNot(HaveOccurred())
for _, r := range apiResourceList.APIResources {
if !r.Namespaced || strings.Contains(r.Name, "/") {
// skip non-namespaced and subresources
continue
}
gvk := schema.GroupVersionKind{
Group: defaultGV.Group,
Version: defaultGV.Version,
Kind: r.Kind,
}
if r.Group != "" {
gvk.Group = r.Group
}
if r.Version != "" {
gvk.Version = r.Version
}
namespacedGVKs[gvk.String()] = gvk
}
}
// Delete all namespaced resources in this namespace
for _, gvk := range namespacedGVKs {
var u unstructured.Unstructured
u.SetGroupVersionKind(gvk)
err := k8sClient.DeleteAllOf(ctx, &u, client.InNamespace(ns.Name))
Expect(client.IgnoreNotFound(ignoreMethodNotAllowed(err))).ShouldNot(HaveOccurred())
}
Eventually(func() error {
key := client.ObjectKeyFromObject(ns)
if err := k8sClient.Get(ctx, key, ns); err != nil {
return client.IgnoreNotFound(err)
}
// remove `kubernetes` finalizer
const kubernetes = "kubernetes"
finalizers := []corev1.FinalizerName{}
for _, f := range ns.Spec.Finalizers {
if f != kubernetes {
finalizers = append(finalizers, f)
}
}
ns.Spec.Finalizers = finalizers
// We have to use the k8s.io/client-go library here to expose
// ability to patch the /finalize subresource on the namespace
_, err = clientGo.CoreV1().Namespaces().Finalize(ctx, ns, metav1.UpdateOptions{})
return err
}, test.Timeout, test.Interval).Should(Succeed())
}
Eventually(func() metav1.StatusReason {
key := client.ObjectKeyFromObject(obj)
if err := k8sClient.Get(ctx, key, obj); err != nil {
return apierrors.ReasonForError(err)
}
return ""
}, test.Timeout, test.Interval).Should(Equal(metav1.StatusReasonNotFound))
}
}
func ignoreMethodNotAllowed(err error) error {
if err != nil {
if apierrors.ReasonForError(err) == metav1.StatusReasonMethodNotAllowed {
return nil
}
}
return err
}