Skip to content

Commit

Permalink
Ensure running machines always contain hook annoation
Browse files Browse the repository at this point in the history
Signed-off-by: Danil-Grigorev <[email protected]>
  • Loading branch information
Danil-Grigorev committed Sep 12, 2024
1 parent a7ba02f commit 5257503
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions controlplane/internal/controllers/rke2controlplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,21 +550,6 @@ func (r *RKE2ControlPlaneReconciler) reconcileNormal(
desiredReplicas := int(*rcp.Spec.Replicas)

switch {
case numMachines == desiredReplicas:
nonDeleteingMachines := controlPlane.Machines.Filter(collections.Not(collections.HasDeletionTimestamp))
for _, machine := range nonDeleteingMachines {
annotaions := machine.GetAnnotations()

if _, found := annotaions[controlplanev1.PreTerminateHookCleanupAnnotation]; !found {
annotaions[controlplanev1.PreTerminateHookCleanupAnnotation] = ""
machine.SetAnnotations(annotaions)
}
}

// Patch machine annoations
if err := controlPlane.PatchMachines(ctx); err != nil {
return ctrl.Result{}, err
}
// We are creating the first replica
case numMachines < desiredReplicas && numMachines == 0:
// Create new Machine w/ init
Expand Down Expand Up @@ -951,6 +936,23 @@ func (r *RKE2ControlPlaneReconciler) ClusterToRKE2ControlPlane(ctx context.Conte
}

func (r *RKE2ControlPlaneReconciler) reconcilePreTerminateHook(ctx context.Context, controlPlane *rke2.ControlPlane) (ctrl.Result, error) {
// Ensure that every active machine has the drain hook set
patchHookAnnotation := false

for _, machine := range controlPlane.Machines.Filter(collections.ActiveMachines) {
if _, exists := machine.Annotations[controlplanev1.PreTerminateHookCleanupAnnotation]; !exists {
machine.Annotations[controlplanev1.PreTerminateHookCleanupAnnotation] = ""
patchHookAnnotation = true
}
}

if patchHookAnnotation {
// Patch machine annoations
if err := controlPlane.PatchMachines(ctx); err != nil {
return ctrl.Result{}, err
}
}

if !controlPlane.HasDeletingMachine() {
return ctrl.Result{}, nil
}
Expand Down

0 comments on commit 5257503

Please sign in to comment.