Skip to content

🐛 Perform draining and volume detachment once until completion #11590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions api/core/v1beta1/machine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,18 @@ type MachineDeletionStatus struct {
// Only present when the Machine has a deletionTimestamp and waiting for volume detachments had been started.
// +optional
WaitForNodeVolumeDetachStartTime *metav1.Time `json:"waitForNodeVolumeDetachStartTime,omitempty"`

// waitForPreDrainHookStartTime is the time when waiting for pre-drain hooks started
// and is used to determine if the pre-drain hooks are taking too long.
// Only present when the Machine has a deletionTimestamp and waiting for pre-drain hooks had been started.
// +optional
WaitForPreDrainHookStartTime *metav1.Time `json:"waitForPreDrainHookStartTime,omitempty"`

// waitForPreTerminateHookStartTime is the time when waiting for pre-terminate hooks started
// and is used to determine if the pre-terminate hooks are taking too long.
// Only present when the Machine has a deletionTimestamp and waiting for pre-terminate hooks had been started.
// +optional
WaitForPreTerminateHookStartTime *metav1.Time `json:"waitForPreTerminateHookStartTime,omitempty"`
}

// SetTypedPhase sets the Phase field to the string representation of MachinePhase.
Expand Down
4 changes: 4 additions & 0 deletions api/core/v1beta1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions api/core/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions api/core/v1beta1/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions api/core/v1beta2/machine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,18 @@ type MachineDeletionStatus struct {
// Only present when the Machine has a deletionTimestamp and waiting for volume detachments had been started.
// +optional
WaitForNodeVolumeDetachStartTime *metav1.Time `json:"waitForNodeVolumeDetachStartTime,omitempty"`

// waitForPreDrainHookStartTime is the time when waiting for pre-drain hooks started
// and is used to determine if the pre-drain hooks are taking too long.
// Only present when the Machine has a deletionTimestamp and waiting for pre-drain hooks had been started.
// +optional
WaitForPreDrainHookStartTime *metav1.Time `json:"waitForPreDrainHookStartTime,omitempty"`

// waitForPreTerminateHookStartTime is the time when waiting for pre-terminate hooks started
// and is used to determine if the pre-terminate hooks are taking too long.
// Only present when the Machine has a deletionTimestamp and waiting for pre-terminate hooks had been started.
// +optional
WaitForPreTerminateHookStartTime *metav1.Time `json:"waitForPreTerminateHookStartTime,omitempty"`
}

// SetTypedPhase sets the Phase field to the string representation of MachinePhase.
Expand Down
8 changes: 8 additions & 0 deletions api/core/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions api/core/v1beta2/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions config/crd/bases/cluster.x-k8s.io_machines.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 34 additions & 5 deletions internal/controllers/machine/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,12 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, s *scope) (ctrl.Result
}
slices.Sort(hooks)
log.Info("Waiting for pre-drain hooks to succeed", "hooks", strings.Join(hooks, ","))
if m.Status.Deletion == nil {
m.Status.Deletion = &clusterv1.MachineDeletionStatus{}
}
if m.Status.Deletion.WaitForPreDrainHookStartTime == nil {
m.Status.Deletion.WaitForPreDrainHookStartTime = ptr.To(metav1.Now())
}
v1beta1conditions.MarkFalse(m, clusterv1.PreDrainDeleteHookSucceededV1Beta1Condition, clusterv1.WaitingExternalHookV1Beta1Reason, clusterv1.ConditionSeverityInfo, "")
s.deletingReason = clusterv1.MachineDeletingWaitingForPreDrainHookReason
s.deletingMessage = fmt.Sprintf("Waiting for pre-drain hooks to succeed (hooks: %s)", strings.Join(hooks, ","))
Expand All @@ -474,7 +480,8 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, s *scope) (ctrl.Result
v1beta1conditions.MarkTrue(m, clusterv1.PreDrainDeleteHookSucceededV1Beta1Condition)

// Drain node before deletion and issue a patch in order to make this operation visible to the users.
if r.isNodeDrainAllowed(m) {
// In case the preTerminateHook is started or the infra machine is not found the detachment is skipped.
if r.isNodeDrainAllowed(m, s.infraMachine) {
patchHelper, err := patch.NewHelper(m, r.Client)
if err != nil {
s.deletingReason = clusterv1.MachineDeletingInternalErrorReason
Expand Down Expand Up @@ -521,8 +528,8 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, s *scope) (ctrl.Result

// After node draining is completed, and if isNodeVolumeDetachingAllowed returns True, make sure all
// volumes are detached before proceeding to delete the Node.
// In case the node is unreachable, the detachment is skipped.
if r.isNodeVolumeDetachingAllowed(m) {
// In case the node is unreachable, preTerminateHook is started or the infra machine is not found the detachment is skipped.
if r.isNodeVolumeDetachingAllowed(m, s.infraMachine) {
if m.Status.Deletion == nil {
m.Status.Deletion = &clusterv1.MachineDeletionStatus{}
}
Expand Down Expand Up @@ -563,6 +570,12 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, s *scope) (ctrl.Result
}
slices.Sort(hooks)
log.Info("Waiting for pre-terminate hooks to succeed", "hooks", strings.Join(hooks, ","))
if m.Status.Deletion == nil {
m.Status.Deletion = &clusterv1.MachineDeletionStatus{}
}
if m.Status.Deletion.WaitForPreTerminateHookStartTime == nil {
m.Status.Deletion.WaitForPreTerminateHookStartTime = ptr.To(metav1.Now())
}
v1beta1conditions.MarkFalse(m, clusterv1.PreTerminateDeleteHookSucceededV1Beta1Condition, clusterv1.WaitingExternalHookV1Beta1Reason, clusterv1.ConditionSeverityInfo, "")
s.deletingReason = clusterv1.MachineDeletingWaitingForPreTerminateHookReason
s.deletingMessage = fmt.Sprintf("Waiting for pre-terminate hooks to succeed (hooks: %s)", strings.Join(hooks, ","))
Expand Down Expand Up @@ -637,7 +650,7 @@ const (
KubeadmControlPlanePreTerminateHookCleanupAnnotation = clusterv1.PreTerminateDeleteHookAnnotationPrefix + "/kcp-cleanup"
)

func (r *Reconciler) isNodeDrainAllowed(m *clusterv1.Machine) bool {
func (r *Reconciler) isNodeDrainAllowed(m *clusterv1.Machine, infraMachine *unstructured.Unstructured) bool {
if util.IsControlPlaneMachine(m) && util.HasOwner(m.GetOwnerReferences(), clusterv1.GroupVersionControlPlane.String(), []string{"KubeadmControlPlane"}) {
if _, exists := m.Annotations[KubeadmControlPlanePreTerminateHookCleanupAnnotation]; !exists {
return false
Expand All @@ -648,6 +661,14 @@ func (r *Reconciler) isNodeDrainAllowed(m *clusterv1.Machine) bool {
return false
}

if m.Status.Deletion != nil && m.Status.Deletion.WaitForPreTerminateHookStartTime != nil {
return false
}

if infraMachine == nil || !infraMachine.GetDeletionTimestamp().IsZero() {
return false
}

if r.nodeDrainTimeoutExceeded(m) {
return false
}
Expand All @@ -657,7 +678,7 @@ func (r *Reconciler) isNodeDrainAllowed(m *clusterv1.Machine) bool {

// isNodeVolumeDetachingAllowed returns False if either ExcludeWaitForNodeVolumeDetachAnnotation annotation is set OR
// nodeVolumeDetachTimeoutExceeded timeout is exceeded, otherwise returns True.
func (r *Reconciler) isNodeVolumeDetachingAllowed(m *clusterv1.Machine) bool {
func (r *Reconciler) isNodeVolumeDetachingAllowed(m *clusterv1.Machine, infraMachine *unstructured.Unstructured) bool {
if util.IsControlPlaneMachine(m) && util.HasOwner(m.GetOwnerReferences(), clusterv1.GroupVersionControlPlane.String(), []string{"KubeadmControlPlane"}) {
if _, exists := m.Annotations[KubeadmControlPlanePreTerminateHookCleanupAnnotation]; !exists {
return false
Expand All @@ -668,6 +689,14 @@ func (r *Reconciler) isNodeVolumeDetachingAllowed(m *clusterv1.Machine) bool {
return false
}

if m.Status.Deletion != nil && m.Status.Deletion.WaitForPreTerminateHookStartTime != nil {
return false
}

if infraMachine == nil || !infraMachine.GetDeletionTimestamp().IsZero() {
return false
}

if r.nodeVolumeDetachTimeoutExceeded(m) {
return false
}
Expand Down
Loading