Skip to content

Commit e08a75e

Browse files
committed
Issue-10544 ignore unreachable cluster while deleting machinePool
Signed-off-by: melserngawy <[email protected]>
1 parent e1a701f commit e08a75e

File tree

5 files changed

+210
-52
lines changed

5 files changed

+210
-52
lines changed

exp/internal/controllers/machinepool_controller.go

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ func (r *MachinePoolReconciler) Reconcile(ctx context.Context, req ctrl.Request)
200200
log.V(5).Info("Requeuing because another worker has the lock on the ClusterCacheTracker")
201201
return ctrl.Result{RequeueAfter: time.Minute}, nil
202202
}
203+
203204
return ctrl.Result{}, err
204205
}
205206

@@ -253,41 +254,67 @@ func (r *MachinePoolReconciler) reconcile(ctx context.Context, cluster *clusterv
253254
return res, kerrors.NewAggregate(errs)
254255
}
255256

256-
func (r *MachinePoolReconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Cluster, mp *expv1.MachinePool) error {
257-
if ok, err := r.reconcileDeleteExternal(ctx, mp); !ok || err != nil {
257+
// reconcileDelete delete machinePool related resources.
258+
func (r *MachinePoolReconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Cluster, machinePool *expv1.MachinePool) error {
259+
if ok, err := r.reconcileDeleteExternal(ctx, machinePool); !ok || err != nil {
258260
// Return early and don't remove the finalizer if we got an error or
259261
// the external reconciliation deletion isn't ready.
260262
return err
261263
}
262264

263-
if err := r.reconcileDeleteNodes(ctx, cluster, mp); err != nil {
264-
// Return early and don't remove the finalizer if we got an error.
265-
return err
265+
// check nodes delete timeout passed.
266+
if !r.isMachinePoolNodeDeleteTimeoutPassed(machinePool) {
267+
if err := r.reconcileDeleteNodes(ctx, cluster, machinePool); err != nil {
268+
// Return early and don't remove the finalizer if we got an error.
269+
return err
270+
}
271+
} else {
272+
ctrl.LoggerFrom(ctx).Info("NodeDeleteTimeout passed, Nodes will be deleted")
266273
}
267274

268-
controllerutil.RemoveFinalizer(mp, expv1.MachinePoolFinalizer)
275+
controllerutil.RemoveFinalizer(machinePool, expv1.MachinePoolFinalizer)
269276
return nil
270277
}
271278

272-
func (r *MachinePoolReconciler) reconcileDeleteNodes(ctx context.Context, cluster *clusterv1.Cluster, machinepool *expv1.MachinePool) error {
273-
if len(machinepool.Status.NodeRefs) == 0 {
279+
// reconcileDeleteNodes delete the cluster nodes.
280+
func (r *MachinePoolReconciler) reconcileDeleteNodes(ctx context.Context, cluster *clusterv1.Cluster, machinePool *expv1.MachinePool) error {
281+
if len(machinePool.Status.NodeRefs) == 0 {
274282
return nil
275283
}
276284

285+
if r.Tracker == nil {
286+
return errors.New("Cannot establish cluster client to delete nodes")
287+
}
288+
277289
clusterClient, err := r.Tracker.GetClient(ctx, util.ObjectKey(cluster))
278290
if err != nil {
279291
return err
280292
}
281293

282-
return r.deleteRetiredNodes(ctx, clusterClient, machinepool.Status.NodeRefs, machinepool.Spec.ProviderIDList)
294+
return r.deleteRetiredNodes(ctx, clusterClient, machinePool.Status.NodeRefs, machinePool.Spec.ProviderIDList)
295+
}
296+
297+
// isMachinePoolDeleteTimeoutPassed check the machinePool node delete time out.
298+
func (r *MachinePoolReconciler) isMachinePoolNodeDeleteTimeoutPassed(machinePool *expv1.MachinePool) bool {
299+
if !machinePool.DeletionTimestamp.IsZero() && machinePool.Spec.Template.Spec.NodeDeletionTimeout != nil {
300+
if machinePool.Spec.Template.Spec.NodeDeletionTimeout.Duration.Nanoseconds() != 0 {
301+
deleteTimePlusDuration := machinePool.DeletionTimestamp.Add(machinePool.Spec.Template.Spec.NodeDeletionTimeout.Duration)
302+
return deleteTimePlusDuration.Before(time.Now())
303+
}
304+
}
305+
return false
283306
}
284307

285308
// reconcileDeleteExternal tries to delete external references, returning true if it cannot find any.
286-
func (r *MachinePoolReconciler) reconcileDeleteExternal(ctx context.Context, m *expv1.MachinePool) (bool, error) {
309+
func (r *MachinePoolReconciler) reconcileDeleteExternal(ctx context.Context, machinePool *expv1.MachinePool) (bool, error) {
287310
objects := []*unstructured.Unstructured{}
288-
references := []*corev1.ObjectReference{
289-
m.Spec.Template.Spec.Bootstrap.ConfigRef,
290-
&m.Spec.Template.Spec.InfrastructureRef,
311+
references := []*corev1.ObjectReference{}
312+
// check for external ref
313+
if machinePool.Spec.Template.Spec.Bootstrap.ConfigRef != nil {
314+
references = append(references, machinePool.Spec.Template.Spec.Bootstrap.ConfigRef)
315+
}
316+
if machinePool.Spec.Template.Spec.InfrastructureRef != (corev1.ObjectReference{}) {
317+
references = append(references, &machinePool.Spec.Template.Spec.InfrastructureRef)
291318
}
292319

293320
// Loop over the references and try to retrieve it with the client.
@@ -296,10 +323,10 @@ func (r *MachinePoolReconciler) reconcileDeleteExternal(ctx context.Context, m *
296323
continue
297324
}
298325

299-
obj, err := external.Get(ctx, r.Client, ref, m.Namespace)
326+
obj, err := external.Get(ctx, r.Client, ref, machinePool.Namespace)
300327
if err != nil && !apierrors.IsNotFound(errors.Cause(err)) {
301328
return false, errors.Wrapf(err, "failed to get %s %q for MachinePool %q in namespace %q",
302-
ref.GroupVersionKind(), ref.Name, m.Name, m.Namespace)
329+
ref.GroupVersionKind(), ref.Name, machinePool.Name, machinePool.Namespace)
303330
}
304331
if obj != nil {
305332
objects = append(objects, obj)
@@ -311,7 +338,7 @@ func (r *MachinePoolReconciler) reconcileDeleteExternal(ctx context.Context, m *
311338
if err := r.Client.Delete(ctx, obj); err != nil && !apierrors.IsNotFound(err) {
312339
return false, errors.Wrapf(err,
313340
"failed to delete %v %q for MachinePool %q in namespace %q",
314-
obj.GroupVersionKind(), obj.GetName(), m.Name, m.Namespace)
341+
obj.GroupVersionKind(), obj.GetName(), machinePool.Name, machinePool.Namespace)
315342
}
316343
}
317344

0 commit comments

Comments
 (0)