@@ -45,6 +45,7 @@ import (
45
45
"sigs.k8s.io/cluster-api/util"
46
46
"sigs.k8s.io/cluster-api/util/certs"
47
47
"sigs.k8s.io/cluster-api/util/conditions"
48
+ v1beta2conditions "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
48
49
"sigs.k8s.io/cluster-api/util/patch"
49
50
"sigs.k8s.io/cluster-api/util/secret"
50
51
"sigs.k8s.io/cluster-api/util/test/builder"
@@ -2594,3 +2595,96 @@ func assertHasTrueCondition(g *WithT, myclient client.Client, req ctrl.Request,
2594
2595
g .Expect (c ).ToNot (BeNil ())
2595
2596
g .Expect (c .Status ).To (Equal (corev1 .ConditionTrue ))
2596
2597
}
2598
+
2599
+ func TestKubeadmConfigReconciler_Reconcile_v1beta2_conditions (t * testing.T ) {
2600
+ // Setup work for an initialized cluster
2601
+ clusterName := "my-cluster"
2602
+ cluster := builder .Cluster (metav1 .NamespaceDefault , clusterName ).Build ()
2603
+ conditions .MarkTrue (cluster , clusterv1 .ControlPlaneInitializedCondition )
2604
+ cluster .Status .InfrastructureReady = true
2605
+ cluster .Spec .ControlPlaneEndpoint = clusterv1.APIEndpoint {
2606
+ Host : "example.com" ,
2607
+ Port : 6443 ,
2608
+ }
2609
+
2610
+ machine := builder .Machine (metav1 .NamespaceDefault , "my-machine" ).
2611
+ WithVersion ("v1.19.1" ).
2612
+ WithClusterName (cluster .Name ).
2613
+ WithBootstrapTemplate (bootstrapbuilder .KubeadmConfig (metav1 .NamespaceDefault , "" ).Unstructured ()).
2614
+ Build ()
2615
+
2616
+ kubeadmConfig := newKubeadmConfig (metav1 .NamespaceDefault , "kubeadmconfig" )
2617
+
2618
+ tests := []struct {
2619
+ name string
2620
+ config * bootstrapv1.KubeadmConfig
2621
+ machine * clusterv1.Machine
2622
+ }{
2623
+ {
2624
+ name : "conditions should be true again after reconciling" ,
2625
+ config : kubeadmConfig .DeepCopy (),
2626
+ machine : machine .DeepCopy (),
2627
+ },
2628
+ {
2629
+ name : "conditions should be true again after status got emptied out" ,
2630
+ config : kubeadmConfig .DeepCopy (),
2631
+ machine : func () * clusterv1.Machine {
2632
+ m := machine .DeepCopy ()
2633
+ m .Spec .Bootstrap .DataSecretName = ptr .To ("foo" )
2634
+ return m
2635
+ }(),
2636
+ },
2637
+ {
2638
+ name : "conditions should be true after upgrading to v1beta2" ,
2639
+ config : func () * bootstrapv1.KubeadmConfig {
2640
+ c := kubeadmConfig .DeepCopy ()
2641
+ c .Status .Ready = true
2642
+ return c
2643
+ }(),
2644
+ machine : machine .DeepCopy (),
2645
+ },
2646
+ }
2647
+ for _ , tt := range tests {
2648
+ t .Run (tt .name , func (t * testing.T ) {
2649
+ g := NewWithT (t )
2650
+ cluster := cluster .DeepCopy ()
2651
+ tt .config .SetOwnerReferences ([]metav1.OwnerReference {{
2652
+ APIVersion : clusterv1 .GroupVersion .String (),
2653
+ Kind : "Machine" ,
2654
+ Name : tt .machine .Name ,
2655
+ }})
2656
+
2657
+ objects := []client.Object {cluster , tt .machine , tt .config }
2658
+ objects = append (objects , createSecrets (t , cluster , tt .config )... )
2659
+
2660
+ myclient := fake .NewClientBuilder ().WithObjects (objects ... ).WithStatusSubresource (& bootstrapv1.KubeadmConfig {}).Build ()
2661
+
2662
+ r := & KubeadmConfigReconciler {
2663
+ Client : myclient ,
2664
+ SecretCachingClient : myclient ,
2665
+ ClusterCache : clustercache .NewFakeClusterCache (myclient , client.ObjectKey {Name : cluster .Name , Namespace : cluster .Namespace }),
2666
+ KubeadmInitLock : & myInitLocker {},
2667
+ }
2668
+
2669
+ key := client.ObjectKey {Namespace : tt .config .Namespace , Name : tt .config .Name }
2670
+ _ , err := r .Reconcile (ctx , ctrl.Request {NamespacedName : key })
2671
+ g .Expect (err ).ToNot (HaveOccurred ())
2672
+
2673
+ newConfig := & bootstrapv1.KubeadmConfig {}
2674
+ g .Expect (myclient .Get (ctx , key , newConfig )).To (Succeed ())
2675
+
2676
+ for _ , conditionType := range []string {bootstrapv1 .KubeadmConfigReadyV1Beta2Condition , bootstrapv1 .KubeadmConfigCertificatesAvailableV1Beta2Condition , bootstrapv1 .KubeadmConfigDataSecretAvailableV1Beta2Condition } {
2677
+ condition := v1beta2conditions .Get (newConfig , conditionType )
2678
+ g .Expect (condition ).ToNot (BeNil (), "condition %s is missing" , conditionType )
2679
+ g .Expect (condition .Status ).To (Equal (metav1 .ConditionTrue ))
2680
+ g .Expect (condition .Message ).To (BeEmpty ())
2681
+ }
2682
+ for _ , conditionType := range []string {clusterv1 .PausedV1Beta2Condition } {
2683
+ condition := v1beta2conditions .Get (newConfig , conditionType )
2684
+ g .Expect (condition ).ToNot (BeNil (), "condition %s is missing" , conditionType )
2685
+ g .Expect (condition .Status ).To (Equal (metav1 .ConditionFalse ))
2686
+ g .Expect (condition .Message ).To (BeEmpty ())
2687
+ }
2688
+ })
2689
+ }
2690
+ }
0 commit comments