Skip to content

Commit 4c05471

Browse files
authored
fix(machine): give load balancers some time after control plane machine deletion (#115)
1 parent 4e2a97c commit 4c05471

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

internal/controller/metalstackmachine_controller.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
4343
"sigs.k8s.io/controller-runtime/pkg/handler"
4444
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
45+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
4546

4647
"github.com/go-logr/logr"
4748
"github.com/metal-stack/cluster-api-provider-metal-stack/api/v1alpha1"
@@ -52,7 +53,10 @@ import (
5253
"github.com/metal-stack/metal-lib/pkg/tag"
5354
)
5455

55-
const defaultProviderMachineRequeueTime = time.Second * 30
56+
const (
57+
defaultProviderMachineRequeueTime = time.Second * 30
58+
defaultControlPlaneMachineFreeRequeueTime = time.Second * 30
59+
)
5660

5761
var errProviderMachineNotFound = errors.New("provider machine not found")
5862

@@ -153,7 +157,7 @@ func (r *MetalStackMachineReconciler) Reconcile(ctx context.Context, req ctrl.Re
153157
case annotations.IsPaused(cluster, infraMachine):
154158
log.Info("reconciliation is paused")
155159
case !infraMachine.DeletionTimestamp.IsZero():
156-
err = reconciler.delete()
160+
result, err = reconciler.delete()
157161
case !controllerutil.ContainsFinalizer(infraMachine, v1alpha1.MachineFinalizer):
158162
log.Info("adding finalizer")
159163
controllerutil.AddFinalizer(infraMachine, v1alpha1.MachineFinalizer)
@@ -380,34 +384,49 @@ func (r *machineReconciler) reconcile() (ctrl.Result, error) {
380384
return result, nil
381385
}
382386

383-
func (r *machineReconciler) delete() error {
387+
func (r *machineReconciler) delete() (ctrl.Result, error) {
384388
if !controllerutil.ContainsFinalizer(r.infraMachine, v1alpha1.MachineFinalizer) {
385-
return nil
389+
return ctrl.Result{}, nil
386390
}
387391

388392
r.log.Info("reconciling resource deletion flow")
389393

390394
m, err := r.findProviderMachine()
391395
if errors.Is(err, errProviderMachineNotFound) {
392-
r.log.Info("machine already freed, removing finalizer")
393-
controllerutil.RemoveFinalizer(r.infraMachine, v1alpha1.MachineFinalizer)
394-
return nil
396+
if !util.IsControlPlaneMachine(r.clusterMachine) {
397+
r.log.Info("worker machine not found at provider, removing finalizer")
398+
controllerutil.RemoveFinalizer(r.infraMachine, v1alpha1.MachineFinalizer)
399+
return ctrl.Result{}, nil
400+
}
401+
402+
if r.infraMachine.DeletionTimestamp.Add(defaultControlPlaneMachineFreeRequeueTime).Before(time.Now()) {
403+
r.log.Info("control plane machine deleted, removing finalizer")
404+
controllerutil.RemoveFinalizer(r.infraMachine, v1alpha1.MachineFinalizer)
405+
return ctrl.Result{}, nil
406+
}
407+
408+
r.log.Info("control plane machine deleted, giving load balancers some time to adjust")
409+
return ctrl.Result{RequeueAfter: defaultControlPlaneMachineFreeRequeueTime / 2}, nil
395410
}
396411
if err != nil {
397-
return fmt.Errorf("failed to find provider machine: %w", err)
412+
return reconcile.Result{}, fmt.Errorf("failed to find provider machine: %w", err)
398413
}
399414

400415
_, err = r.metalClient.Machine().FreeMachine(metalmachine.NewFreeMachineParamsWithContext(r.ctx).WithID(*m.ID), nil)
401416
if err != nil {
402-
return fmt.Errorf("failed to delete provider machine: %w", err)
417+
return reconcile.Result{}, fmt.Errorf("failed to delete provider machine: %w", err)
403418
}
404419

405420
r.log.Info("freed provider machine")
406421

407-
r.log.Info("deletion finished, removing finalizer")
408-
controllerutil.RemoveFinalizer(r.infraMachine, v1alpha1.MachineFinalizer)
422+
if !util.IsControlPlaneMachine(r.clusterMachine) {
423+
r.log.Info("worker machine delete finished, removing finalizer")
424+
controllerutil.RemoveFinalizer(r.infraMachine, v1alpha1.MachineFinalizer)
425+
return ctrl.Result{}, nil
426+
}
409427

410-
return nil
428+
r.log.Info("control plane machine delete finished, giving load balancers some to adjust")
429+
return ctrl.Result{RequeueAfter: time.Minute}, nil
411430
}
412431

413432
func (r *machineReconciler) create() (*models.V1MachineResponse, error) {

0 commit comments

Comments
 (0)