1
1
package rke2
2
2
3
3
import (
4
+ "context"
5
+
4
6
. "github.com/onsi/ginkgo/v2"
5
7
. "github.com/onsi/gomega"
8
+ "github.com/pkg/errors"
6
9
bootstrapv1 "github.com/rancher-sandbox/cluster-api-provider-rke2/bootstrap/api/v1beta1"
7
10
controlplanev1 "github.com/rancher-sandbox/cluster-api-provider-rke2/controlplane/api/v1beta1"
8
11
corev1 "k8s.io/api/core/v1"
@@ -12,6 +15,8 @@ import (
12
15
"sigs.k8s.io/cluster-api/util/collections"
13
16
"sigs.k8s.io/cluster-api/util/conditions"
14
17
"sigs.k8s.io/controller-runtime/pkg/client"
18
+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
19
+ "sigs.k8s.io/controller-runtime/pkg/client/interceptor"
15
20
)
16
21
17
22
var _ = Describe ("Node metadata propagation" , func () {
@@ -394,3 +399,113 @@ var _ = Describe("Cloud-init fields validation", func() {
394
399
}})).ToNot (Succeed ())
395
400
})
396
401
})
402
+
403
+ var _ = Describe ("ClusterStatus validation" , func () {
404
+ var (
405
+ err error
406
+ ns * corev1.Namespace
407
+ node1 * corev1.Node
408
+ node2 * corev1.Node
409
+ servingSecret * corev1.Secret
410
+ connectionErr interceptor.Funcs
411
+ )
412
+
413
+ BeforeEach (func () {
414
+ ns , err = testEnv .CreateNamespace (ctx , "ns" )
415
+ Expect (err ).ToNot (HaveOccurred ())
416
+
417
+ node1 = & corev1.Node {
418
+ ObjectMeta : metav1.ObjectMeta {
419
+ Name : "node1" ,
420
+ Labels : map [string ]string {
421
+ "node-role.kubernetes.io/master" : "true" ,
422
+ },
423
+ Annotations : map [string ]string {
424
+ clusterv1 .MachineAnnotation : "node1" ,
425
+ },
426
+ },
427
+ Status : corev1.NodeStatus {
428
+ Conditions : []corev1.NodeCondition {{
429
+ Type : corev1 .NodeReady ,
430
+ Status : corev1 .ConditionTrue ,
431
+ }},
432
+ }}
433
+
434
+ node2 = & corev1.Node {
435
+ ObjectMeta : metav1.ObjectMeta {
436
+ Name : "node2" ,
437
+ Labels : map [string ]string {
438
+ "node-role.kubernetes.io/master" : "true" ,
439
+ },
440
+ Annotations : map [string ]string {
441
+ clusterv1 .MachineAnnotation : "node2" ,
442
+ },
443
+ },
444
+ Status : corev1.NodeStatus {
445
+ Conditions : []corev1.NodeCondition {{
446
+ Type : corev1 .NodeReady ,
447
+ Status : corev1 .ConditionFalse ,
448
+ }},
449
+ }}
450
+
451
+ servingSecret = & corev1.Secret {
452
+ ObjectMeta : metav1.ObjectMeta {
453
+ Name : rke2ServingSecretKey ,
454
+ Namespace : metav1 .NamespaceSystem ,
455
+ },
456
+ }
457
+
458
+ connectionErr = interceptor.Funcs {
459
+ Get : func (ctx context.Context , client client.WithWatch , key client.ObjectKey , obj client.Object , _ ... client.GetOption ) error {
460
+ return errors .New ("test connection error" )
461
+ },
462
+ }
463
+ })
464
+
465
+ AfterEach (func () {
466
+ testEnv .Cleanup (ctx , ns )
467
+ })
468
+
469
+ It ("should set HasRKE2ServingSecret if servingSecret found" , func () {
470
+ fakeClient := fake .NewClientBuilder ().WithObjects ([]client.Object {servingSecret }... ).Build ()
471
+ w := & Workload {
472
+ Client : fakeClient ,
473
+ Nodes : map [string ]* corev1.Node {
474
+ node1 .Name : node1 ,
475
+ node2 .Name : node2 ,
476
+ },
477
+ }
478
+ status := w .ClusterStatus (ctx )
479
+ Expect (status .Nodes ).To (BeEquivalentTo (2 ), "There are 2 nodes in this cluster" )
480
+ Expect (status .ReadyNodes ).To (BeEquivalentTo (1 ), "Only 1 node has the NodeReady condition" )
481
+ Expect (status .HasRKE2ServingSecret ).To (BeTrue (), "rke2-serving Secret exists in kube-system namespace" )
482
+ })
483
+ It ("should not set HasRKE2ServingSecret if servingSecret not existing" , func () {
484
+ fakeClient := fake .NewClientBuilder ().Build ()
485
+ w := & Workload {
486
+ Client : fakeClient ,
487
+ Nodes : map [string ]* corev1.Node {
488
+ node1 .Name : node1 ,
489
+ node2 .Name : node2 ,
490
+ },
491
+ }
492
+ status := w .ClusterStatus (ctx )
493
+ Expect (status .Nodes ).To (BeEquivalentTo (2 ), "There are 2 nodes in this cluster" )
494
+ Expect (status .ReadyNodes ).To (BeEquivalentTo (1 ), "Only 1 node has the NodeReady condition" )
495
+ Expect (status .HasRKE2ServingSecret ).To (BeFalse (), "rke2-serving Secret does not exists in kube-system namespace" )
496
+ })
497
+ It ("should not set HasRKE2ServingSecret if client returns error" , func () {
498
+ fakeClient := fake .NewClientBuilder ().WithInterceptorFuncs (connectionErr ).Build ()
499
+ w := & Workload {
500
+ Client : fakeClient ,
501
+ Nodes : map [string ]* corev1.Node {
502
+ node1 .Name : node1 ,
503
+ node2 .Name : node2 ,
504
+ },
505
+ }
506
+ status := w .ClusterStatus (ctx )
507
+ Expect (status .Nodes ).To (BeEquivalentTo (2 ), "There are 2 nodes in this cluster" )
508
+ Expect (status .ReadyNodes ).To (BeEquivalentTo (1 ), "Only 1 node has the NodeReady condition" )
509
+ Expect (status .HasRKE2ServingSecret ).To (BeFalse (), "On connection error assume control plane not initialized" )
510
+ })
511
+ })
0 commit comments