Skip to content

Commit df2f637

Browse files
Perform draining and volume detaching once until completion
Signed-off-by: Danil-Grigorev <[email protected]>
1 parent 8b08484 commit df2f637

File tree

2 files changed

+74
-42
lines changed

2 files changed

+74
-42
lines changed

internal/controllers/machine/machine_controller.go

+28-12
Original file line numberDiff line numberDiff line change
@@ -634,17 +634,17 @@ 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+
exists := false
640+
for annotation := range m.Annotations {
641+
if strings.HasPrefix(annotation, clusterv1.PreTerminateDeleteHookAnnotationPrefix) {
642+
exists = true
643+
break
644+
}
645+
}
646+
647+
if !exists {
648648
return false
649649
}
650650
}
@@ -657,14 +657,26 @@ func (r *Reconciler) isNodeDrainAllowed(m *clusterv1.Machine) bool {
657657
return false
658658
}
659659

660+
if conditions.IsTrue(m, clusterv1.DrainingSucceededCondition) {
661+
return false
662+
}
663+
660664
return true
661665
}
662666

663667
// isNodeVolumeDetachingAllowed returns False if either ExcludeWaitForNodeVolumeDetachAnnotation annotation is set OR
664668
// nodeVolumeDetachTimeoutExceeded timeout is exceeded, otherwise returns True.
665669
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 {
670+
if util.IsControlPlaneMachine(m) {
671+
exists := false
672+
for annotation := range m.Annotations {
673+
if strings.HasPrefix(annotation, clusterv1.PreTerminateDeleteHookAnnotationPrefix) {
674+
exists = true
675+
break
676+
}
677+
}
678+
679+
if !exists {
668680
return false
669681
}
670682
}
@@ -677,6 +689,10 @@ func (r *Reconciler) isNodeVolumeDetachingAllowed(m *clusterv1.Machine) bool {
677689
return false
678690
}
679691

692+
if conditions.IsTrue(m, clusterv1.VolumeDetachSucceededCondition) {
693+
return false
694+
}
695+
680696
return true
681697
}
682698

internal/controllers/machine/machine_controller_test.go

+46-30
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)