@@ -210,46 +210,7 @@ func (r *KubeadmControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.
210
210
}
211
211
212
212
defer func () {
213
- // Always attempt to update status.
214
- if err := r .updateStatus (ctx , controlPlane ); err != nil {
215
- if errors .Is (err , & internal.RemoteClusterConnectionError {}) {
216
- log .Error (err , "Could not connect to workload cluster to fetch status" )
217
- } else {
218
- log .Error (err , "Failed to update KubeadmControlPlane status" )
219
- reterr = kerrors .NewAggregate ([]error {reterr , err })
220
- }
221
- }
222
-
223
- r .updateV1Beta2Status (ctx , controlPlane )
224
-
225
- // Always attempt to Patch the KubeadmControlPlane object and status after each reconciliation.
226
- patchOpts := []patch.Option {}
227
- if reterr == nil {
228
- patchOpts = append (patchOpts , patch.WithStatusObservedGeneration {})
229
- }
230
- if err := patchKubeadmControlPlane (ctx , patchHelper , kcp , patchOpts ... ); err != nil {
231
- log .Error (err , "Failed to patch KubeadmControlPlane" )
232
- reterr = kerrors .NewAggregate ([]error {reterr , err })
233
- }
234
-
235
- // Only requeue if there is no error, Requeue or RequeueAfter and the object does not have a deletion timestamp.
236
- if reterr == nil && res .IsZero () && kcp .ObjectMeta .DeletionTimestamp .IsZero () {
237
- // Make KCP requeue in case node status is not ready, so we can check for node status without waiting for a full
238
- // resync (by default 10 minutes).
239
- // The alternative solution would be to watch the control plane nodes in the Cluster - similar to how the
240
- // MachineSet and MachineHealthCheck controllers watch the nodes under their control.
241
- if ! kcp .Status .Ready {
242
- res = ctrl.Result {RequeueAfter : 20 * time .Second }
243
- }
244
-
245
- // Make KCP requeue if ControlPlaneComponentsHealthyCondition is false so we can check for control plane component
246
- // status without waiting for a full resync (by default 10 minutes).
247
- // Otherwise this condition can lead to a delay in provisioning MachineDeployments when MachineSet preflight checks are enabled.
248
- // The alternative solution to this requeue would be watching the relevant pods inside each workload cluster which would be very expensive.
249
- if conditions .IsFalse (kcp , controlplanev1 .ControlPlaneComponentsHealthyCondition ) {
250
- res = ctrl.Result {RequeueAfter : 20 * time .Second }
251
- }
252
- }
213
+ res , reterr = r .deferPatch (ctx , kcp , controlPlane , patchHelper )
253
214
}()
254
215
255
216
if ! kcp .ObjectMeta .DeletionTimestamp .IsZero () {
@@ -261,6 +222,52 @@ func (r *KubeadmControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.
261
222
return r .reconcile (ctx , controlPlane )
262
223
}
263
224
225
+ func (r * KubeadmControlPlaneReconciler ) deferPatch (ctx context.Context , kcp * controlplanev1.KubeadmControlPlane , controlPlane * internal.ControlPlane , patchHelper * patch.Helper ) (res ctrl.Result , reterr error ) {
226
+ log := ctrl .LoggerFrom (ctx )
227
+ // Always attempt to update status.
228
+ if err := r .updateStatus (ctx , controlPlane ); err != nil {
229
+ var connFailure * internal.RemoteClusterConnectionError
230
+ if errors .As (err , & connFailure ) {
231
+ log .Error (err , "Could not connect to workload cluster to fetch status" )
232
+ } else {
233
+ log .Error (err , "Failed to update KubeadmControlPlane status" )
234
+ reterr = kerrors .NewAggregate ([]error {reterr , err })
235
+ }
236
+ }
237
+
238
+ r .updateV1Beta2Status (ctx , controlPlane )
239
+
240
+ // Always attempt to Patch the KubeadmControlPlane object and status after each reconciliation.
241
+ patchOpts := []patch.Option {}
242
+ if reterr == nil {
243
+ patchOpts = append (patchOpts , patch.WithStatusObservedGeneration {})
244
+ }
245
+ if err := patchKubeadmControlPlane (ctx , patchHelper , kcp , patchOpts ... ); err != nil {
246
+ log .Error (err , "Failed to patch KubeadmControlPlane" )
247
+ reterr = kerrors .NewAggregate ([]error {reterr , err })
248
+ }
249
+
250
+ // Only requeue if there is no error, Requeue or RequeueAfter and the object does not have a deletion timestamp.
251
+ if reterr == nil && res .IsZero () && kcp .ObjectMeta .DeletionTimestamp .IsZero () {
252
+ // Make KCP requeue in case node status is not ready, so we can check for node status without waiting for a full
253
+ // resync (by default 10 minutes).
254
+ // The alternative solution would be to watch the control plane nodes in the Cluster - similar to how the
255
+ // MachineSet and MachineHealthCheck controllers watch the nodes under their control.
256
+ if ! kcp .Status .Ready {
257
+ res = ctrl.Result {RequeueAfter : 20 * time .Second }
258
+ }
259
+
260
+ // Make KCP requeue if ControlPlaneComponentsHealthyCondition is false so we can check for control plane component
261
+ // status without waiting for a full resync (by default 10 minutes).
262
+ // Otherwise this condition can lead to a delay in provisioning MachineDeployments when MachineSet preflight checks are enabled.
263
+ // The alternative solution to this requeue would be watching the relevant pods inside each workload cluster which would be very expensive.
264
+ if conditions .IsFalse (kcp , controlplanev1 .ControlPlaneComponentsHealthyCondition ) {
265
+ res = ctrl.Result {RequeueAfter : 20 * time .Second }
266
+ }
267
+ }
268
+ return
269
+ }
270
+
264
271
// initControlPlaneScope initializes the control plane scope; this includes also checking for orphan machines and
265
272
// adopt them if necessary.
266
273
// The func also returns a boolean indicating if adoptableMachine have been found and processed, but this doesn't imply those machines
0 commit comments