@@ -17,6 +17,7 @@ limitations under the License.
17
17
package controllers
18
18
19
19
import (
20
+ "bytes"
20
21
"context"
21
22
"fmt"
22
23
"reflect"
@@ -30,6 +31,7 @@ import (
30
31
"k8s.io/apimachinery/pkg/types"
31
32
fakeclient "k8s.io/client-go/kubernetes/fake"
32
33
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
34
+ bootstrapapi "k8s.io/cluster-bootstrap/token/api"
33
35
"k8s.io/klog/klogr"
34
36
bootstrapv1 "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/api/v1alpha2"
35
37
internalcluster "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/internal/cluster"
@@ -613,11 +615,114 @@ func TestReconcileIfJoinNodesAndControlPlaneIsReady(t *testing.T) {
613
615
myremoteclient , _ := k .SecretsClientFactory .NewSecretsClient (nil , nil )
614
616
l , err := myremoteclient .List (metav1.ListOptions {})
615
617
if err != nil {
616
- t .Fatalf ("Failed to reconcile :\n %+v" , err )
618
+ t .Fatalf ("Failed to read secrets :\n %+v" , err )
617
619
}
618
620
619
621
if len (l .Items ) != 2 {
620
- t .Fatalf ("Failed to reconcile:\n %+v" , err )
622
+ t .Fatalf ("Expected two bootstrap tokens, saw:\n %+d" , len (l .Items ))
623
+ }
624
+
625
+ // ensure that the token is refreshed...
626
+ tokenExpires := make ([][]byte , len (l .Items ))
627
+
628
+ for i , item := range l .Items {
629
+ tokenExpires [i ] = item .Data [bootstrapapi .BootstrapTokenExpirationKey ]
630
+ }
631
+
632
+ <- time .After (1 * time .Second )
633
+
634
+ for _ , req := range []ctrl.Request {
635
+ {
636
+ NamespacedName : types.NamespacedName {
637
+ Namespace : "default" ,
638
+ Name : "worker-join-cfg" ,
639
+ },
640
+ },
641
+ {
642
+ NamespacedName : types.NamespacedName {
643
+ Namespace : "default" ,
644
+ Name : "control-plane-join-cfg" ,
645
+ },
646
+ },
647
+ } {
648
+
649
+ result , err := k .Reconcile (req )
650
+ if err != nil {
651
+ t .Fatalf ("Failed to reconcile:\n %+v" , err )
652
+ }
653
+ if result .Requeue == true {
654
+ t .Fatal ("did not expect to requeue" )
655
+ }
656
+ if result .RequeueAfter != time .Duration (0 ) {
657
+ t .Fatal ("did not expect to requeue after" )
658
+ }
659
+ }
660
+
661
+ l , err = myremoteclient .List (metav1.ListOptions {})
662
+ if err != nil {
663
+ t .Fatalf ("Failed to read secrets:\n %+v" , err )
664
+ }
665
+
666
+ if len (l .Items ) != 2 {
667
+ t .Fatalf ("Expected two bootstrap tokens, saw:\n %+d" , len (l .Items ))
668
+ }
669
+
670
+ for i , item := range l .Items {
671
+ if bytes .Equal (tokenExpires [i ], item .Data [bootstrapapi .BootstrapTokenExpirationKey ]) {
672
+ t .Fatal ("Reconcile should have refreshed bootstrap token's expiration until the infrastructure was ready" )
673
+ }
674
+ tokenExpires [i ] = item .Data [bootstrapapi .BootstrapTokenExpirationKey ]
675
+ }
676
+
677
+ // ...until the infrastructure is marked "ready"
678
+ workerMachine .Status .InfrastructureReady = true
679
+ myclient .Update (context .Background (), workerMachine )
680
+
681
+ controlPlaneJoinMachine .Status .InfrastructureReady = true
682
+ myclient .Update (context .Background (), controlPlaneJoinMachine )
683
+
684
+ <- time .After (1 * time .Second )
685
+
686
+ for _ , req := range []ctrl.Request {
687
+ {
688
+ NamespacedName : types.NamespacedName {
689
+ Namespace : "default" ,
690
+ Name : "worker-join-cfg" ,
691
+ },
692
+ },
693
+ {
694
+ NamespacedName : types.NamespacedName {
695
+ Namespace : "default" ,
696
+ Name : "control-plane-join-cfg" ,
697
+ },
698
+ },
699
+ } {
700
+
701
+ result , err := k .Reconcile (req )
702
+ if err != nil {
703
+ t .Fatalf ("Failed to reconcile:\n %+v" , err )
704
+ }
705
+ if result .Requeue == true {
706
+ t .Fatal ("did not expect to requeue" )
707
+ }
708
+ if result .RequeueAfter != time .Duration (0 ) {
709
+ t .Fatal ("did not expect to requeue after" )
710
+ }
711
+ }
712
+
713
+ l , err = myremoteclient .List (metav1.ListOptions {})
714
+ if err != nil {
715
+ t .Fatalf ("Failed to read secrets:\n %+v" , err )
716
+ }
717
+
718
+ if len (l .Items ) != 2 {
719
+ t .Fatalf ("Expected two bootstrap tokens, saw:\n %+d" , len (l .Items ))
720
+ }
721
+
722
+ for i , item := range l .Items {
723
+ if ! bytes .Equal (tokenExpires [i ], item .Data [bootstrapapi .BootstrapTokenExpirationKey ]) {
724
+ t .Fatal ("Reconcile should have let the bootstrap token expire after the infrastructure was ready" )
725
+ }
621
726
}
622
727
}
623
728
0 commit comments