Skip to content

Commit

Permalink
Propagate PreTerminateHookCleanupAnnotation on old machines
Browse files Browse the repository at this point in the history
- Lint fixes

Signed-off-by: Danil-Grigorev <[email protected]>
  • Loading branch information
Danil-Grigorev committed Sep 10, 2024
1 parent 7dcf788 commit 452143d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
25 changes: 22 additions & 3 deletions controlplane/internal/controllers/rke2controlplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,21 @@ 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 @@ -945,7 +960,7 @@ func (r *RKE2ControlPlaneReconciler) reconcilePreTerminateHook(ctx context.Conte
}

// Return early if there are other pre-terminate hooks for the Machine.
// The KCP pre-terminate hook should be the one executed last, so that kubelet
// The CAPRKE2 pre-terminate hook should be the one executed last, so that kubelet
// is still working while other pre-terminate hooks are run.
// Note: This is done only for Kubernetes >= v1.31 to reduce the blast radius of this check.
if version.Compare(parsedVersion, semver.MustParse("1.31.0"), version.WithoutPreReleases()) >= 0 {
Expand All @@ -967,7 +982,8 @@ func (r *RKE2ControlPlaneReconciler) reconcilePreTerminateHook(ctx context.Conte
if controlPlane.Machines.Len() > 1 {
workloadCluster, err := r.GetWorkloadCluster(ctx, controlPlane)
if err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to remove etcd member for deleting Machine %s: failed to create client to workload cluster", klog.KObj(deletingMachine))
return ctrl.Result{}, errors.Wrapf(err,
"failed to remove etcd member for deleting Machine %s: failed to create client to workload cluster", klog.KObj(deletingMachine))
}

// Note: In regular deletion cases (remediation, scale down) the leader should have been already moved.
Expand All @@ -994,7 +1010,9 @@ func (r *RKE2ControlPlaneReconciler) reconcilePreTerminateHook(ctx context.Conte
return ctrl.Result{}, err
}

log.Info("Waiting for Machines to be deleted", "machines", strings.Join(controlPlane.Machines.Filter(collections.HasDeletionTimestamp).Names(), ", "))
log.Info("Waiting for Machines to be deleted", "machines",
strings.Join(controlPlane.Machines.Filter(collections.HasDeletionTimestamp).Names(), ", "))

return ctrl.Result{RequeueAfter: deleteRequeueAfter}, nil
}

Expand All @@ -1004,6 +1022,7 @@ func machineHasOtherPreTerminateHooks(machine *clusterv1.Machine) bool {
return true
}
}

return false
}

Expand Down
3 changes: 2 additions & 1 deletion controlplane/internal/controllers/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ func (r *RKE2ControlPlaneReconciler) scaleDownControlPlane(
// Also in this case the reconcileDelete code of the Machine controller won't execute Node drain
// and wait for volume detach.
if err := r.removePreTerminateHookAnnotationFromMachine(ctx, machineToDelete); err != nil {

return ctrl.Result{}, err
}
}
Expand Down Expand Up @@ -196,9 +195,11 @@ func (r *RKE2ControlPlaneReconciler) removePreTerminateHookAnnotationFromMachine

machineOriginal := machine.DeepCopy()
delete(machine.Annotations, controlplanev1.PreTerminateHookCleanupAnnotation)

if err := r.Client.Patch(ctx, machine, client.MergeFrom(machineOriginal)); err != nil {
return errors.Wrapf(err, "failed to remove pre-terminate hook from control plane Machine %s", klog.KObj(machine))
}

return nil
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/rke2/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ func (c *ControlPlane) SortedByDeletionTimestamp(s collections.Machines) []*clus
for _, value := range s {
res = append(res, value)
}

sort.Sort(res)

return res
}

Expand Down Expand Up @@ -422,5 +424,6 @@ func (o machinesByDeletionTimestamp) Less(i, j int) bool {
if o[i].DeletionTimestamp.Equal(o[j].DeletionTimestamp) {
return o[i].Name < o[j].Name
}

return o[i].DeletionTimestamp.Before(o[j].DeletionTimestamp)
}

0 comments on commit 452143d

Please sign in to comment.