Skip to content

Commit 5d8e3b4

Browse files
committed
add node deletion test
1 parent 6fe56d4 commit 5d8e3b4

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

controllers/machine_controller.go

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ type MachineReconciler struct {
8282
// WatchFilterValue is the label value used to filter events prior to reconciliation.
8383
WatchFilterValue string
8484

85+
// WaitForNodeDeletion causes the reconciler to error when a node deletion fails, instead of ignoring it.
8586
WaitForNodeDeletion bool
8687

8788
controller controller.Controller

controllers/machine_controller_test.go

+110
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20+
"context"
2021
"testing"
2122
"time"
2223

@@ -1855,6 +1856,115 @@ func TestNodeToMachine(t *testing.T) {
18551856
}
18561857
}
18571858

1859+
// TODO: We should add a test for the --wait-for-node-deletion flag. This requires error injection
1860+
// in the fakeclient, which isn't supported yet. There is a very recent issue to track this:
1861+
// https://github.com/kubernetes-sigs/controller-runtime/issues/1702
1862+
func TestNodeDeletion(t *testing.T) {
1863+
g := NewWithT(t)
1864+
1865+
time := metav1.Now()
1866+
1867+
testCluster := clusterv1.Cluster{
1868+
ObjectMeta: metav1.ObjectMeta{
1869+
Name: "test-cluster",
1870+
Namespace: metav1.NamespaceDefault,
1871+
},
1872+
}
1873+
1874+
node := &corev1.Node{
1875+
ObjectMeta: metav1.ObjectMeta{
1876+
Name: "test",
1877+
},
1878+
Spec: corev1.NodeSpec{ProviderID: "test://id-1"},
1879+
}
1880+
1881+
testMachine := clusterv1.Machine{
1882+
ObjectMeta: metav1.ObjectMeta{
1883+
Name: "test",
1884+
Namespace: metav1.NamespaceDefault,
1885+
Labels: map[string]string{
1886+
clusterv1.MachineControlPlaneLabelName: "",
1887+
},
1888+
Annotations: map[string]string{
1889+
"machine.cluster.x-k8s.io/exclude-node-draining": "",
1890+
},
1891+
Finalizers: []string{clusterv1.MachineFinalizer},
1892+
DeletionTimestamp: &time,
1893+
},
1894+
Spec: clusterv1.MachineSpec{
1895+
ClusterName: "test-cluster",
1896+
InfrastructureRef: corev1.ObjectReference{
1897+
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1",
1898+
Kind: "GenericInfrastructureMachine",
1899+
Name: "infra-config1",
1900+
},
1901+
Bootstrap: clusterv1.Bootstrap{DataSecretName: pointer.StringPtr("data")},
1902+
},
1903+
Status: clusterv1.MachineStatus{
1904+
NodeRef: &corev1.ObjectReference{
1905+
Name: "test",
1906+
},
1907+
},
1908+
}
1909+
1910+
cpmachine1 := &clusterv1.Machine{
1911+
ObjectMeta: metav1.ObjectMeta{
1912+
Name: "cp1",
1913+
Namespace: metav1.NamespaceDefault,
1914+
Labels: map[string]string{
1915+
clusterv1.ClusterLabelName: "test-cluster",
1916+
clusterv1.MachineControlPlaneLabelName: "",
1917+
},
1918+
Finalizers: []string{clusterv1.MachineFinalizer},
1919+
},
1920+
Spec: clusterv1.MachineSpec{
1921+
ClusterName: "test-cluster",
1922+
InfrastructureRef: corev1.ObjectReference{},
1923+
Bootstrap: clusterv1.Bootstrap{DataSecretName: pointer.StringPtr("data")},
1924+
},
1925+
Status: clusterv1.MachineStatus{
1926+
NodeRef: &corev1.ObjectReference{
1927+
Name: "cp1",
1928+
},
1929+
},
1930+
}
1931+
1932+
testCases := []struct {
1933+
waitForDeletion bool
1934+
resultErr bool
1935+
}{
1936+
{
1937+
waitForDeletion: false,
1938+
resultErr: false,
1939+
},
1940+
}
1941+
1942+
for _, tc := range testCases {
1943+
m := testMachine.DeepCopy()
1944+
1945+
fc := fake.NewClientBuilder().
1946+
WithObjects(node, m, cpmachine1).
1947+
Build()
1948+
tracker := remote.NewTestClusterCacheTracker(log.NullLogger{}, fc, fakeScheme, client.ObjectKeyFromObject(&testCluster))
1949+
1950+
r := &MachineReconciler{
1951+
WaitForNodeDeletion: tc.waitForDeletion,
1952+
Client: fc,
1953+
Tracker: tracker,
1954+
}
1955+
1956+
_, err := r.reconcileDelete(context.Background(), &testCluster, m)
1957+
1958+
if tc.resultErr {
1959+
g.Expect(err).To(HaveOccurred())
1960+
} else {
1961+
g.Expect(err).NotTo(HaveOccurred())
1962+
n := &corev1.Node{}
1963+
g.Expect(fc.Get(context.Background(), client.ObjectKeyFromObject(node), n)).NotTo(Succeed())
1964+
}
1965+
}
1966+
}
1967+
18581968
// adds a condition list to an external object.
18591969
func addConditionsToExternal(u *unstructured.Unstructured, newConditions clusterv1.Conditions) {
18601970
existingConditions := clusterv1.Conditions{}

0 commit comments

Comments
 (0)