Skip to content

Commit a827644

Browse files
committed
add node deletion test
1 parent a9ae325 commit a827644

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
@@ -81,6 +81,7 @@ type MachineReconciler struct {
8181
// WatchFilterValue is the label value used to filter events prior to reconciliation.
8282
WatchFilterValue string
8383

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

8687
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

@@ -1853,6 +1854,115 @@ func TestNodeToMachine(t *testing.T) {
18531854
}
18541855
}
18551856

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

0 commit comments

Comments
 (0)