Skip to content
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

🌱 Improve Drain for control plane machines #11457

Merged
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
15 changes: 15 additions & 0 deletions internal/controllers/machine/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,16 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, s *scope) (ctrl.Result
return ctrl.Result{}, nil
}

// KubeadmControlPlanePreTerminateHookCleanupAnnotation inlined from KCP (we want to avoid importing the KCP API package).
const KubeadmControlPlanePreTerminateHookCleanupAnnotation = clusterv1.PreTerminateDeleteHookAnnotationPrefix + "/kcp-cleanup"

func (r *Reconciler) isNodeDrainAllowed(m *clusterv1.Machine) bool {
if util.IsControlPlaneMachine(m) && util.HasOwner(m.GetOwnerReferences(), clusterv1.GroupVersion.String(), []string{"KubeadmControlPlane"}) {
if _, exists := m.Annotations[KubeadmControlPlanePreTerminateHookCleanupAnnotation]; !exists {
return false
}
}

if _, exists := m.ObjectMeta.Annotations[clusterv1.ExcludeNodeDrainingAnnotation]; exists {
return false
}
Expand All @@ -648,6 +657,12 @@ 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 {
if util.IsControlPlaneMachine(m) && util.HasOwner(m.GetOwnerReferences(), clusterv1.GroupVersion.String(), []string{"KubeadmControlPlane"}) {
if _, exists := m.Annotations[KubeadmControlPlanePreTerminateHookCleanupAnnotation]; !exists {
return false
}
}

if _, exists := m.ObjectMeta.Annotations[clusterv1.ExcludeWaitForNodeVolumeDetachAnnotation]; exists {
return false
}
Expand Down
98 changes: 98 additions & 0 deletions internal/controllers/machine/machine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,55 @@ func TestIsNodeDrainedAllowed(t *testing.T) {
},
expected: false,
},
{
name: "KCP machine with the pre terminate hook should drain",
machine: &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
Namespace: metav1.NamespaceDefault,
Labels: map[string]string{clusterv1.MachineControlPlaneLabel: ""},
Annotations: map[string]string{KubeadmControlPlanePreTerminateHookCleanupAnnotation: ""},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "KubeadmControlPlane",
Name: "Foo",
},
},
},
Spec: clusterv1.MachineSpec{
ClusterName: "test-cluster",
InfrastructureRef: corev1.ObjectReference{},
Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")},
},
Status: clusterv1.MachineStatus{},
},
expected: true,
},
{
name: "KCP machine without the pre terminate hook should stop draining",
machine: &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
Namespace: metav1.NamespaceDefault,
Labels: map[string]string{clusterv1.MachineControlPlaneLabel: ""},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "KubeadmControlPlane",
Name: "Foo",
},
},
},
Spec: clusterv1.MachineSpec{
ClusterName: "test-cluster",
InfrastructureRef: corev1.ObjectReference{},
Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")},
},
Status: clusterv1.MachineStatus{},
},
expected: false,
},
{
name: "Node draining timeout is over",
machine: &clusterv1.Machine{
Expand Down Expand Up @@ -1868,6 +1917,55 @@ func TestIsNodeVolumeDetachingAllowed(t *testing.T) {
},
expected: false,
},
{
name: "KCP machine with the pre terminate hook should wait",
machine: &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
Namespace: metav1.NamespaceDefault,
Labels: map[string]string{clusterv1.MachineControlPlaneLabel: ""},
Annotations: map[string]string{KubeadmControlPlanePreTerminateHookCleanupAnnotation: ""},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "KubeadmControlPlane",
Name: "Foo",
},
},
},
Spec: clusterv1.MachineSpec{
ClusterName: "test-cluster",
InfrastructureRef: corev1.ObjectReference{},
Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")},
},
Status: clusterv1.MachineStatus{},
},
expected: true,
},
{
name: "KCP machine without the pre terminate hook should stop waiting",
machine: &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
Namespace: metav1.NamespaceDefault,
Labels: map[string]string{clusterv1.MachineControlPlaneLabel: ""},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "KubeadmControlPlane",
Name: "Foo",
},
},
},
Spec: clusterv1.MachineSpec{
ClusterName: "test-cluster",
InfrastructureRef: corev1.ObjectReference{},
Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")},
},
Status: clusterv1.MachineStatus{},
},
expected: false,
},
{
name: "Volume detach timeout is over",
machine: &clusterv1.Machine{
Expand Down