@@ -35,6 +35,7 @@ import (
3535 internalcluster "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/internal/cluster"
3636 kubeadmv1beta1 "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/kubeadm/v1beta1"
3737 clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha2"
38+ "sigs.k8s.io/cluster-api/util/secret"
3839 ctrl "sigs.k8s.io/controller-runtime"
3940 "sigs.k8s.io/controller-runtime/pkg/client"
4041 "sigs.k8s.io/controller-runtime/pkg/client/fake"
@@ -1133,6 +1134,67 @@ func TestKubeadmConfigReconciler_Reconcile_ExactlyOneControlPlaneMachineInitiali
11331134 }
11341135}
11351136
1137+ // No patch should be applied if there is an error in reconcile
1138+ func TestKubeadmConfigReconciler_Reconcile_DoNotPatchWhenErrorOccurred (t * testing.T ) {
1139+ cluster := newCluster ("cluster" )
1140+ cluster .Status .InfrastructureReady = true
1141+
1142+ controlPlaneInitMachine := newControlPlaneMachine (cluster , "control-plane-init-machine" )
1143+ controlPlaneInitConfig := newControlPlaneInitKubeadmConfig (controlPlaneInitMachine , "control-plane-init-cfg" )
1144+
1145+ // set InitConfiguration as nil, we will check this to determine if the kubeadm config has been patched
1146+ controlPlaneInitConfig .Spec .InitConfiguration = nil
1147+
1148+ objects := []runtime.Object {
1149+ cluster ,
1150+ controlPlaneInitMachine ,
1151+ controlPlaneInitConfig ,
1152+ }
1153+
1154+ secrets := createSecrets (t , cluster , controlPlaneInitConfig )
1155+ for _ , obj := range secrets {
1156+ s := obj .(* corev1.Secret )
1157+ delete (s .Data , secret .TLSCrtDataName ) // destroy the secrets, which will cause Reconcile to fail
1158+ objects = append (objects , s )
1159+ }
1160+
1161+ myclient := fake .NewFakeClientWithScheme (setupScheme (), objects ... )
1162+ k := & KubeadmConfigReconciler {
1163+ Log : log .Log ,
1164+ Client : myclient ,
1165+ SecretsClientFactory : newFakeSecretFactory (),
1166+ KubeadmInitLock : & myInitLocker {},
1167+ }
1168+
1169+ request := ctrl.Request {
1170+ NamespacedName : types.NamespacedName {
1171+ Namespace : "default" ,
1172+ Name : "control-plane-init-cfg" ,
1173+ },
1174+ }
1175+
1176+ result , err := k .Reconcile (request )
1177+ if err == nil {
1178+ t .Fatal ("Expected error, got nil" )
1179+ }
1180+ if result .Requeue != false {
1181+ t .Fatal ("did not expect to requeue" )
1182+ }
1183+ if result .RequeueAfter != time .Duration (0 ) {
1184+ t .Fatal ("did not expect to requeue after" )
1185+ }
1186+
1187+ cfg , err := getKubeadmConfig (myclient , "control-plane-init-cfg" )
1188+ if err != nil {
1189+ t .Fatalf ("Failed to reconcile:\n %+v" , err )
1190+ }
1191+
1192+ // check if the kubeadm config has been patched
1193+ if cfg .Spec .InitConfiguration != nil {
1194+ t .Fatal ("did not expect to patch the kubeadm config if there was an error in Reconcile" )
1195+ }
1196+ }
1197+
11361198// test utils
11371199
11381200// newCluster return a CAPI cluster object
0 commit comments