Skip to content

Commit 76d0706

Browse files
Perform draining and volume detaching once until completion
Signed-off-by: Danil-Grigorev <danil.grigorev@suse.com>
1 parent 8b08484 commit 76d0706

File tree

2 files changed

+58
-42
lines changed

2 files changed

+58
-42
lines changed

internal/controllers/machine/machine_controller.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -634,17 +634,9 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, s *scope) (ctrl.Result
634634
return ctrl.Result{}, nil
635635
}
636636

637-
const (
638-
// KubeadmControlPlaneAPIVersion inlined from KCP (we want to avoid importing the KCP API package).
639-
KubeadmControlPlaneAPIVersion = "controlplane.cluster.x-k8s.io/v1beta1"
640-
641-
// KubeadmControlPlanePreTerminateHookCleanupAnnotation inlined from KCP (we want to avoid importing the KCP API package).
642-
KubeadmControlPlanePreTerminateHookCleanupAnnotation = clusterv1.PreTerminateDeleteHookAnnotationPrefix + "/kcp-cleanup"
643-
)
644-
645637
func (r *Reconciler) isNodeDrainAllowed(m *clusterv1.Machine) bool {
646-
if util.IsControlPlaneMachine(m) && util.HasOwner(m.GetOwnerReferences(), KubeadmControlPlaneAPIVersion, []string{"KubeadmControlPlane"}) {
647-
if _, exists := m.Annotations[KubeadmControlPlanePreTerminateHookCleanupAnnotation]; !exists {
638+
if util.IsControlPlaneMachine(m) {
639+
if !annotations.HasWithPrefix(clusterv1.PreTerminateDeleteHookAnnotationPrefix, m.Annotations) {
648640
return false
649641
}
650642
}
@@ -657,14 +649,18 @@ func (r *Reconciler) isNodeDrainAllowed(m *clusterv1.Machine) bool {
657649
return false
658650
}
659651

652+
if conditions.IsTrue(m, clusterv1.DrainingSucceededCondition) {
653+
return false
654+
}
655+
660656
return true
661657
}
662658

663659
// isNodeVolumeDetachingAllowed returns False if either ExcludeWaitForNodeVolumeDetachAnnotation annotation is set OR
664660
// nodeVolumeDetachTimeoutExceeded timeout is exceeded, otherwise returns True.
665661
func (r *Reconciler) isNodeVolumeDetachingAllowed(m *clusterv1.Machine) bool {
666-
if util.IsControlPlaneMachine(m) && util.HasOwner(m.GetOwnerReferences(), KubeadmControlPlaneAPIVersion, []string{"KubeadmControlPlane"}) {
667-
if _, exists := m.Annotations[KubeadmControlPlanePreTerminateHookCleanupAnnotation]; !exists {
662+
if util.IsControlPlaneMachine(m) {
663+
if !annotations.HasWithPrefix(clusterv1.PreTerminateDeleteHookAnnotationPrefix, m.Annotations) {
668664
return false
669665
}
670666
}
@@ -677,6 +673,10 @@ func (r *Reconciler) isNodeVolumeDetachingAllowed(m *clusterv1.Machine) bool {
677673
return false
678674
}
679675

676+
if conditions.IsTrue(m, clusterv1.VolumeDetachSucceededCondition) {
677+
return false
678+
}
679+
680680
return true
681681
}
682682

internal/controllers/machine/machine_controller_test.go

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,14 +1363,7 @@ func TestIsNodeDrainedAllowed(t *testing.T) {
13631363
Name: "test-machine",
13641364
Namespace: metav1.NamespaceDefault,
13651365
Labels: map[string]string{clusterv1.MachineControlPlaneLabel: ""},
1366-
Annotations: map[string]string{KubeadmControlPlanePreTerminateHookCleanupAnnotation: ""},
1367-
OwnerReferences: []metav1.OwnerReference{
1368-
{
1369-
APIVersion: KubeadmControlPlaneAPIVersion,
1370-
Kind: "KubeadmControlPlane",
1371-
Name: "Foo",
1372-
},
1373-
},
1366+
Annotations: map[string]string{clusterv1.PreTerminateDeleteHookAnnotationPrefix + "/kcp-cleanup": ""},
13741367
},
13751368
Spec: clusterv1.MachineSpec{
13761369
ClusterName: "test-cluster",
@@ -1388,13 +1381,6 @@ func TestIsNodeDrainedAllowed(t *testing.T) {
13881381
Name: "test-machine",
13891382
Namespace: metav1.NamespaceDefault,
13901383
Labels: map[string]string{clusterv1.MachineControlPlaneLabel: ""},
1391-
OwnerReferences: []metav1.OwnerReference{
1392-
{
1393-
APIVersion: KubeadmControlPlaneAPIVersion,
1394-
Kind: "KubeadmControlPlane",
1395-
Name: "Foo",
1396-
},
1397-
},
13981384
},
13991385
Spec: clusterv1.MachineSpec{
14001386
ClusterName: "test-cluster",
@@ -1428,6 +1414,28 @@ func TestIsNodeDrainedAllowed(t *testing.T) {
14281414
},
14291415
expected: false,
14301416
},
1417+
{
1418+
name: "Node draining succeeded",
1419+
machine: &clusterv1.Machine{
1420+
ObjectMeta: metav1.ObjectMeta{
1421+
Name: "test-machine",
1422+
Namespace: metav1.NamespaceDefault,
1423+
Finalizers: []string{clusterv1.MachineFinalizer},
1424+
},
1425+
Spec: clusterv1.MachineSpec{
1426+
ClusterName: "test-cluster",
1427+
InfrastructureRef: corev1.ObjectReference{},
1428+
Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")},
1429+
},
1430+
Status: clusterv1.MachineStatus{
1431+
Conditions: clusterv1.Conditions{{
1432+
Type: clusterv1.DrainingSucceededCondition,
1433+
Status: corev1.ConditionTrue,
1434+
}},
1435+
},
1436+
},
1437+
expected: false,
1438+
},
14311439
{
14321440
name: "Node draining timeout is not yet over",
14331441
machine: &clusterv1.Machine{
@@ -1924,14 +1932,7 @@ func TestIsNodeVolumeDetachingAllowed(t *testing.T) {
19241932
Name: "test-machine",
19251933
Namespace: metav1.NamespaceDefault,
19261934
Labels: map[string]string{clusterv1.MachineControlPlaneLabel: ""},
1927-
Annotations: map[string]string{KubeadmControlPlanePreTerminateHookCleanupAnnotation: ""},
1928-
OwnerReferences: []metav1.OwnerReference{
1929-
{
1930-
APIVersion: KubeadmControlPlaneAPIVersion,
1931-
Kind: "KubeadmControlPlane",
1932-
Name: "Foo",
1933-
},
1934-
},
1935+
Annotations: map[string]string{clusterv1.PreTerminateDeleteHookAnnotationPrefix + "/kcp-cleanup": ""},
19351936
},
19361937
Spec: clusterv1.MachineSpec{
19371938
ClusterName: "test-cluster",
@@ -1949,13 +1950,6 @@ func TestIsNodeVolumeDetachingAllowed(t *testing.T) {
19491950
Name: "test-machine",
19501951
Namespace: metav1.NamespaceDefault,
19511952
Labels: map[string]string{clusterv1.MachineControlPlaneLabel: ""},
1952-
OwnerReferences: []metav1.OwnerReference{
1953-
{
1954-
APIVersion: KubeadmControlPlaneAPIVersion,
1955-
Kind: "KubeadmControlPlane",
1956-
Name: "Foo",
1957-
},
1958-
},
19591953
},
19601954
Spec: clusterv1.MachineSpec{
19611955
ClusterName: "test-cluster",
@@ -1989,6 +1983,28 @@ func TestIsNodeVolumeDetachingAllowed(t *testing.T) {
19891983
},
19901984
expected: false,
19911985
},
1986+
{
1987+
name: "Volume detach completed",
1988+
machine: &clusterv1.Machine{
1989+
ObjectMeta: metav1.ObjectMeta{
1990+
Name: "test-machine",
1991+
Namespace: metav1.NamespaceDefault,
1992+
Finalizers: []string{clusterv1.MachineFinalizer},
1993+
},
1994+
Spec: clusterv1.MachineSpec{
1995+
ClusterName: "test-cluster",
1996+
InfrastructureRef: corev1.ObjectReference{},
1997+
Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")},
1998+
},
1999+
Status: clusterv1.MachineStatus{
2000+
Conditions: clusterv1.Conditions{{
2001+
Type: clusterv1.VolumeDetachSucceededCondition,
2002+
Status: corev1.ConditionTrue,
2003+
}},
2004+
},
2005+
},
2006+
expected: false,
2007+
},
19922008
{
19932009
name: "Volume detach timeout is not yet over",
19942010
machine: &clusterv1.Machine{

0 commit comments

Comments
 (0)