Skip to content

Commit e416ba3

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

File tree

5 files changed

+208
-56
lines changed

5 files changed

+208
-56
lines changed

exp/internal/controllers/machinepool_controller.go

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ func (r *MachinePoolReconciler) Reconcile(ctx context.Context, req ctrl.Request)
214214
log.V(5).Info("Requeuing because another worker has the lock on the ClusterCacheTracker")
215215
return ctrl.Result{RequeueAfter: time.Minute}, nil
216216
}
217+
217218
return ctrl.Result{}, err
218219
}
219220

@@ -273,41 +274,67 @@ func (r *MachinePoolReconciler) reconcile(ctx context.Context, s *scope) (ctrl.R
273274
return res, kerrors.NewAggregate(errs)
274275
}
275276

276-
func (r *MachinePoolReconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Cluster, mp *expv1.MachinePool) error {
277-
if ok, err := r.reconcileDeleteExternal(ctx, mp); !ok || err != nil {
277+
// reconcileDelete delete machinePool related resources.
278+
func (r *MachinePoolReconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Cluster, machinePool *expv1.MachinePool) error {
279+
if ok, err := r.reconcileDeleteExternal(ctx, machinePool); !ok || err != nil {
278280
// Return early and don't remove the finalizer if we got an error or
279281
// the external reconciliation deletion isn't ready.
280282
return err
281283
}
282284

283-
if err := r.reconcileDeleteNodes(ctx, cluster, mp); err != nil {
284-
// Return early and don't remove the finalizer if we got an error.
285-
return err
285+
// check nodes delete timeout passed.
286+
if !r.isMachinePoolNodeDeleteTimeoutPassed(machinePool) {
287+
if err := r.reconcileDeleteNodes(ctx, cluster, machinePool); err != nil {
288+
// Return early and don't remove the finalizer if we got an error.
289+
return err
290+
}
291+
} else {
292+
ctrl.LoggerFrom(ctx).Info("NodeDeleteTimeout passed, skipping Nodes deletion")
286293
}
287294

288-
controllerutil.RemoveFinalizer(mp, expv1.MachinePoolFinalizer)
295+
controllerutil.RemoveFinalizer(machinePool, expv1.MachinePoolFinalizer)
289296
return nil
290297
}
291298

292-
func (r *MachinePoolReconciler) reconcileDeleteNodes(ctx context.Context, cluster *clusterv1.Cluster, machinepool *expv1.MachinePool) error {
293-
if len(machinepool.Status.NodeRefs) == 0 {
299+
// reconcileDeleteNodes delete the cluster nodes.
300+
func (r *MachinePoolReconciler) reconcileDeleteNodes(ctx context.Context, cluster *clusterv1.Cluster, machinePool *expv1.MachinePool) error {
301+
if len(machinePool.Status.NodeRefs) == 0 {
294302
return nil
295303
}
296304

305+
if r.Tracker == nil {
306+
return errors.New("Cannot establish cluster client to delete nodes")
307+
}
308+
297309
clusterClient, err := r.Tracker.GetClient(ctx, util.ObjectKey(cluster))
298310
if err != nil {
299311
return err
300312
}
301313

302-
return r.deleteRetiredNodes(ctx, clusterClient, machinepool.Status.NodeRefs, machinepool.Spec.ProviderIDList)
314+
return r.deleteRetiredNodes(ctx, clusterClient, machinePool.Status.NodeRefs, machinePool.Spec.ProviderIDList)
315+
}
316+
317+
// isMachinePoolDeleteTimeoutPassed check the machinePool node delete time out.
318+
func (r *MachinePoolReconciler) isMachinePoolNodeDeleteTimeoutPassed(machinePool *expv1.MachinePool) bool {
319+
if !machinePool.DeletionTimestamp.IsZero() && machinePool.Spec.Template.Spec.NodeDeletionTimeout != nil {
320+
if machinePool.Spec.Template.Spec.NodeDeletionTimeout.Duration.Nanoseconds() != 0 {
321+
deleteTimePlusDuration := machinePool.DeletionTimestamp.Add(machinePool.Spec.Template.Spec.NodeDeletionTimeout.Duration)
322+
return deleteTimePlusDuration.Before(time.Now())
323+
}
324+
}
325+
return false
303326
}
304327

305328
// reconcileDeleteExternal tries to delete external references, returning true if it cannot find any.
306-
func (r *MachinePoolReconciler) reconcileDeleteExternal(ctx context.Context, m *expv1.MachinePool) (bool, error) {
329+
func (r *MachinePoolReconciler) reconcileDeleteExternal(ctx context.Context, machinePool *expv1.MachinePool) (bool, error) {
307330
objects := []*unstructured.Unstructured{}
308-
references := []*corev1.ObjectReference{
309-
m.Spec.Template.Spec.Bootstrap.ConfigRef,
310-
&m.Spec.Template.Spec.InfrastructureRef,
331+
references := []*corev1.ObjectReference{}
332+
// check for external ref
333+
if machinePool.Spec.Template.Spec.Bootstrap.ConfigRef != nil {
334+
references = append(references, machinePool.Spec.Template.Spec.Bootstrap.ConfigRef)
335+
}
336+
if machinePool.Spec.Template.Spec.InfrastructureRef != (corev1.ObjectReference{}) {
337+
references = append(references, &machinePool.Spec.Template.Spec.InfrastructureRef)
311338
}
312339

313340
// Loop over the references and try to retrieve it with the client.
@@ -316,10 +343,10 @@ func (r *MachinePoolReconciler) reconcileDeleteExternal(ctx context.Context, m *
316343
continue
317344
}
318345

319-
obj, err := external.Get(ctx, r.Client, ref, m.Namespace)
346+
obj, err := external.Get(ctx, r.Client, ref, machinePool.Namespace)
320347
if err != nil && !apierrors.IsNotFound(errors.Cause(err)) {
321348
return false, errors.Wrapf(err, "failed to get %s %q for MachinePool %q in namespace %q",
322-
ref.GroupVersionKind(), ref.Name, m.Name, m.Namespace)
349+
ref.GroupVersionKind(), ref.Name, machinePool.Name, machinePool.Namespace)
323350
}
324351
if obj != nil {
325352
objects = append(objects, obj)
@@ -331,7 +358,7 @@ func (r *MachinePoolReconciler) reconcileDeleteExternal(ctx context.Context, m *
331358
if err := r.Client.Delete(ctx, obj); err != nil && !apierrors.IsNotFound(err) {
332359
return false, errors.Wrapf(err,
333360
"failed to delete %v %q for MachinePool %q in namespace %q",
334-
obj.GroupVersionKind(), obj.GetName(), m.Name, m.Namespace)
361+
obj.GroupVersionKind(), obj.GetName(), machinePool.Name, machinePool.Namespace)
335362
}
336363
}
337364

0 commit comments

Comments
 (0)