Skip to content

Commit 15de135

Browse files
authored
Merge pull request #11457 from fabriziopandini/improve-drain-for-cp-machines
🌱 Improve Drain for control plane machines
2 parents b7eb8f7 + c3904f7 commit 15de135

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

internal/controllers/machine/machine_controller.go

+15
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,16 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, s *scope) (ctrl.Result
633633
return ctrl.Result{}, nil
634634
}
635635

636+
// KubeadmControlPlanePreTerminateHookCleanupAnnotation inlined from KCP (we want to avoid importing the KCP API package).
637+
const KubeadmControlPlanePreTerminateHookCleanupAnnotation = clusterv1.PreTerminateDeleteHookAnnotationPrefix + "/kcp-cleanup"
638+
636639
func (r *Reconciler) isNodeDrainAllowed(m *clusterv1.Machine) bool {
640+
if util.IsControlPlaneMachine(m) && util.HasOwner(m.GetOwnerReferences(), clusterv1.GroupVersion.String(), []string{"KubeadmControlPlane"}) {
641+
if _, exists := m.Annotations[KubeadmControlPlanePreTerminateHookCleanupAnnotation]; !exists {
642+
return false
643+
}
644+
}
645+
637646
if _, exists := m.ObjectMeta.Annotations[clusterv1.ExcludeNodeDrainingAnnotation]; exists {
638647
return false
639648
}
@@ -648,6 +657,12 @@ func (r *Reconciler) isNodeDrainAllowed(m *clusterv1.Machine) bool {
648657
// isNodeVolumeDetachingAllowed returns False if either ExcludeWaitForNodeVolumeDetachAnnotation annotation is set OR
649658
// nodeVolumeDetachTimeoutExceeded timeout is exceeded, otherwise returns True.
650659
func (r *Reconciler) isNodeVolumeDetachingAllowed(m *clusterv1.Machine) bool {
660+
if util.IsControlPlaneMachine(m) && util.HasOwner(m.GetOwnerReferences(), clusterv1.GroupVersion.String(), []string{"KubeadmControlPlane"}) {
661+
if _, exists := m.Annotations[KubeadmControlPlanePreTerminateHookCleanupAnnotation]; !exists {
662+
return false
663+
}
664+
}
665+
651666
if _, exists := m.ObjectMeta.Annotations[clusterv1.ExcludeWaitForNodeVolumeDetachAnnotation]; exists {
652667
return false
653668
}

internal/controllers/machine/machine_controller_test.go

+98
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,55 @@ func TestIsNodeDrainedAllowed(t *testing.T) {
13561356
},
13571357
expected: false,
13581358
},
1359+
{
1360+
name: "KCP machine with the pre terminate hook should drain",
1361+
machine: &clusterv1.Machine{
1362+
ObjectMeta: metav1.ObjectMeta{
1363+
Name: "test-machine",
1364+
Namespace: metav1.NamespaceDefault,
1365+
Labels: map[string]string{clusterv1.MachineControlPlaneLabel: ""},
1366+
Annotations: map[string]string{KubeadmControlPlanePreTerminateHookCleanupAnnotation: ""},
1367+
OwnerReferences: []metav1.OwnerReference{
1368+
{
1369+
APIVersion: clusterv1.GroupVersion.String(),
1370+
Kind: "KubeadmControlPlane",
1371+
Name: "Foo",
1372+
},
1373+
},
1374+
},
1375+
Spec: clusterv1.MachineSpec{
1376+
ClusterName: "test-cluster",
1377+
InfrastructureRef: corev1.ObjectReference{},
1378+
Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")},
1379+
},
1380+
Status: clusterv1.MachineStatus{},
1381+
},
1382+
expected: true,
1383+
},
1384+
{
1385+
name: "KCP machine without the pre terminate hook should stop draining",
1386+
machine: &clusterv1.Machine{
1387+
ObjectMeta: metav1.ObjectMeta{
1388+
Name: "test-machine",
1389+
Namespace: metav1.NamespaceDefault,
1390+
Labels: map[string]string{clusterv1.MachineControlPlaneLabel: ""},
1391+
OwnerReferences: []metav1.OwnerReference{
1392+
{
1393+
APIVersion: clusterv1.GroupVersion.String(),
1394+
Kind: "KubeadmControlPlane",
1395+
Name: "Foo",
1396+
},
1397+
},
1398+
},
1399+
Spec: clusterv1.MachineSpec{
1400+
ClusterName: "test-cluster",
1401+
InfrastructureRef: corev1.ObjectReference{},
1402+
Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")},
1403+
},
1404+
Status: clusterv1.MachineStatus{},
1405+
},
1406+
expected: false,
1407+
},
13591408
{
13601409
name: "Node draining timeout is over",
13611410
machine: &clusterv1.Machine{
@@ -1868,6 +1917,55 @@ func TestIsNodeVolumeDetachingAllowed(t *testing.T) {
18681917
},
18691918
expected: false,
18701919
},
1920+
{
1921+
name: "KCP machine with the pre terminate hook should wait",
1922+
machine: &clusterv1.Machine{
1923+
ObjectMeta: metav1.ObjectMeta{
1924+
Name: "test-machine",
1925+
Namespace: metav1.NamespaceDefault,
1926+
Labels: map[string]string{clusterv1.MachineControlPlaneLabel: ""},
1927+
Annotations: map[string]string{KubeadmControlPlanePreTerminateHookCleanupAnnotation: ""},
1928+
OwnerReferences: []metav1.OwnerReference{
1929+
{
1930+
APIVersion: clusterv1.GroupVersion.String(),
1931+
Kind: "KubeadmControlPlane",
1932+
Name: "Foo",
1933+
},
1934+
},
1935+
},
1936+
Spec: clusterv1.MachineSpec{
1937+
ClusterName: "test-cluster",
1938+
InfrastructureRef: corev1.ObjectReference{},
1939+
Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")},
1940+
},
1941+
Status: clusterv1.MachineStatus{},
1942+
},
1943+
expected: true,
1944+
},
1945+
{
1946+
name: "KCP machine without the pre terminate hook should stop waiting",
1947+
machine: &clusterv1.Machine{
1948+
ObjectMeta: metav1.ObjectMeta{
1949+
Name: "test-machine",
1950+
Namespace: metav1.NamespaceDefault,
1951+
Labels: map[string]string{clusterv1.MachineControlPlaneLabel: ""},
1952+
OwnerReferences: []metav1.OwnerReference{
1953+
{
1954+
APIVersion: clusterv1.GroupVersion.String(),
1955+
Kind: "KubeadmControlPlane",
1956+
Name: "Foo",
1957+
},
1958+
},
1959+
},
1960+
Spec: clusterv1.MachineSpec{
1961+
ClusterName: "test-cluster",
1962+
InfrastructureRef: corev1.ObjectReference{},
1963+
Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")},
1964+
},
1965+
Status: clusterv1.MachineStatus{},
1966+
},
1967+
expected: false,
1968+
},
18711969
{
18721970
name: "Volume detach timeout is over",
18731971
machine: &clusterv1.Machine{

0 commit comments

Comments
 (0)