@@ -195,12 +195,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
195
195
return ctrl.Result {}, nil
196
196
}
197
197
198
- // Initialize the patch helper
199
198
s := & scope {
200
199
cluster : cluster ,
201
200
machine : m ,
202
201
}
203
202
203
+ // Initialize the patch helper
204
204
patchHelper , err := patch .NewHelper (m , r .Client )
205
205
if err != nil {
206
206
return ctrl.Result {}, err
@@ -220,12 +220,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
220
220
}
221
221
}()
222
222
223
- // Always add the cluster label labels.
224
- if m .Labels == nil {
225
- m .Labels = make (map [string ]string )
226
- }
227
- m .Labels [clusterv1 .ClusterNameLabel ] = m .Spec .ClusterName
228
-
229
223
// Add finalizer first if not set to avoid the race condition between init and delete.
230
224
// Note: Finalizers in general can only be added when the deletionTimestamp is not set.
231
225
if ! controllerutil .ContainsFinalizer (m , clusterv1 .MachineFinalizer ) && m .ObjectMeta .DeletionTimestamp .IsZero () {
@@ -234,6 +228,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
234
228
}
235
229
236
230
alwaysReconcile := []machineReconcileFunc {
231
+ r .reconcileMachineOwnerAndLabels ,
237
232
r .reconcileBootstrap ,
238
233
r .reconcileInfrastructure ,
239
234
r .reconcileNode ,
@@ -258,12 +253,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
258
253
}
259
254
260
255
// Handle normal reconciliation loop.
261
- reconcileNormal := append (
262
- []machineReconcileFunc {r .reconcileMachineOwner },
263
- alwaysReconcile ... ,
264
- )
265
-
266
- res , err := doReconcile (ctx , reconcileNormal , s )
256
+ res , err := doReconcile (ctx , alwaysReconcile , s )
267
257
// Requeue if the reconcile failed because the ClusterCacheTracker was locked for
268
258
// the current cluster because of concurrent access.
269
259
if errors .Is (err , remote .ErrClusterLocked ) {
@@ -355,15 +345,21 @@ type scope struct {
355
345
// Machine. It is set after reconcileInfrastructure is called.
356
346
infraMachine * unstructured.Unstructured
357
347
348
+ // infraMachineNotFound is true if getting the infra machine object failed with an IsNotFound err
349
+ infraMachineIsNotFound bool
350
+
358
351
// bootstrapConfig is the BootstrapConfig object that is referenced by the
359
352
// Machine. It is set after reconcileBootstrap is called.
360
353
bootstrapConfig * unstructured.Unstructured
361
354
355
+ // bootstrapConfigNotFound is true if getting the BootstrapConfig object failed with an IsNotFound err
356
+ bootstrapConfigIsNotFound bool
357
+
362
358
// node is the Kubernetes node hosted on the machine.
363
359
node * corev1.Node
364
360
}
365
361
366
- func (r * Reconciler ) reconcileMachineOwner (_ context.Context , s * scope ) (ctrl.Result , error ) {
362
+ func (r * Reconciler ) reconcileMachineOwnerAndLabels (_ context.Context , s * scope ) (ctrl.Result , error ) {
367
363
// If the machine is a stand-alone one, meaning not originated from a MachineDeployment, then set it as directly
368
364
// owned by the Cluster (if not already present).
369
365
if r .shouldAdopt (s .machine ) {
@@ -375,6 +371,12 @@ func (r *Reconciler) reconcileMachineOwner(_ context.Context, s *scope) (ctrl.Re
375
371
}))
376
372
}
377
373
374
+ // Always add the cluster label.
375
+ if s .machine .Labels == nil {
376
+ s .machine .Labels = make (map [string ]string )
377
+ }
378
+ s .machine .Labels [clusterv1 .ClusterNameLabel ] = s .machine .Spec .ClusterName
379
+
378
380
return ctrl.Result {}, nil
379
381
}
380
382
@@ -504,13 +506,15 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, s *scope) (ctrl.Result
504
506
return ctrl.Result {}, nil
505
507
}
506
508
507
- bootstrapDeleted , err := r .reconcileDeleteBootstrap (ctx , s )
508
- if err != nil {
509
- return ctrl.Result {}, err
510
- }
511
- if ! bootstrapDeleted {
512
- log .Info ("Waiting for bootstrap to be deleted" , m .Spec .Bootstrap .ConfigRef .Kind , klog .KRef (m .Spec .Bootstrap .ConfigRef .Namespace , m .Spec .Bootstrap .ConfigRef .Name ))
513
- return ctrl.Result {}, nil
509
+ if m .Spec .Bootstrap .ConfigRef != nil {
510
+ bootstrapDeleted , err := r .reconcileDeleteBootstrap (ctx , s )
511
+ if err != nil {
512
+ return ctrl.Result {}, err
513
+ }
514
+ if ! bootstrapDeleted {
515
+ log .Info ("Waiting for bootstrap to be deleted" , m .Spec .Bootstrap .ConfigRef .Kind , klog .KRef (m .Spec .Bootstrap .ConfigRef .Namespace , m .Spec .Bootstrap .ConfigRef .Name ))
516
+ return ctrl.Result {}, nil
517
+ }
514
518
}
515
519
516
520
// We only delete the node after the underlying infrastructure is gone.
@@ -869,30 +873,34 @@ func (r *Reconciler) deleteNode(ctx context.Context, cluster *clusterv1.Cluster,
869
873
}
870
874
871
875
func (r * Reconciler ) reconcileDeleteBootstrap (ctx context.Context , s * scope ) (bool , error ) {
872
- if s .bootstrapConfig == nil {
876
+ if s .bootstrapConfig == nil && s . bootstrapConfigIsNotFound {
873
877
conditions .MarkFalse (s .machine , clusterv1 .BootstrapReadyCondition , clusterv1 .DeletedReason , clusterv1 .ConditionSeverityInfo , "" )
874
878
return true , nil
875
879
}
876
880
877
- if err := r .Client .Delete (ctx , s .bootstrapConfig ); err != nil && ! apierrors .IsNotFound (err ) {
878
- return false , errors .Wrapf (err ,
879
- "failed to delete %v %q for Machine %q in namespace %q" ,
880
- s .bootstrapConfig .GroupVersionKind (), s .bootstrapConfig .GetName (), s .machine .Name , s .machine .Namespace )
881
+ if s .bootstrapConfig != nil {
882
+ if err := r .Client .Delete (ctx , s .bootstrapConfig ); err != nil && ! apierrors .IsNotFound (err ) {
883
+ return false , errors .Wrapf (err ,
884
+ "failed to delete %v %q for Machine %q in namespace %q" ,
885
+ s .bootstrapConfig .GroupVersionKind (), s .bootstrapConfig .GetName (), s .machine .Name , s .machine .Namespace )
886
+ }
881
887
}
882
888
883
889
return false , nil
884
890
}
885
891
886
892
func (r * Reconciler ) reconcileDeleteInfrastructure (ctx context.Context , s * scope ) (bool , error ) {
887
- if s .infraMachine == nil {
893
+ if s .infraMachine == nil && s . infraMachineIsNotFound {
888
894
conditions .MarkFalse (s .machine , clusterv1 .InfrastructureReadyCondition , clusterv1 .DeletedReason , clusterv1 .ConditionSeverityInfo , "" )
889
895
return true , nil
890
896
}
891
897
892
- if err := r .Client .Delete (ctx , s .infraMachine ); err != nil && ! apierrors .IsNotFound (err ) {
893
- return false , errors .Wrapf (err ,
894
- "failed to delete %v %q for Machine %q in namespace %q" ,
895
- s .infraMachine .GroupVersionKind (), s .infraMachine .GetName (), s .machine .Name , s .machine .Namespace )
898
+ if s .infraMachine != nil {
899
+ if err := r .Client .Delete (ctx , s .infraMachine ); err != nil && ! apierrors .IsNotFound (err ) {
900
+ return false , errors .Wrapf (err ,
901
+ "failed to delete %v %q for Machine %q in namespace %q" ,
902
+ s .infraMachine .GroupVersionKind (), s .infraMachine .GetName (), s .machine .Name , s .machine .Namespace )
903
+ }
896
904
}
897
905
898
906
return false , nil
0 commit comments