@@ -1148,7 +1148,7 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
11481148 expectErr bool
11491149 }{
11501150 {
1151- name : "error if a MachineDeploymentClass in use gets removed" ,
1151+ name : "pass if a MachineDeploymentClass not in use gets removed" ,
11521152 clusters : []client.Object {
11531153 builder .Cluster (metav1 .NamespaceDefault , "cluster1" ).
11541154 WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
@@ -1190,53 +1190,19 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
11901190 builder .ControlPlaneTemplate (metav1 .NamespaceDefault , "cp1" ).
11911191 Build ()).
11921192 WithWorkerMachineDeploymentClasses (
1193- * builder .MachineDeploymentClass ("aa " ).
1193+ * builder .MachineDeploymentClass ("bb " ).
11941194 WithInfrastructureTemplate (
11951195 builder .InfrastructureMachineTemplate (metav1 .NamespaceDefault , "infra1" ).Build ()).
11961196 WithBootstrapTemplate (
11971197 builder .BootstrapTemplate (metav1 .NamespaceDefault , "bootstrap1" ).Build ()).
11981198 Build ()).
11991199 Build (),
1200- expectErr : true ,
1200+ expectErr : false ,
12011201 },
12021202 {
1203- name : "error if many MachineDeploymentClasses, used in multiple Clusters using the modified ClusterClass, are removed" ,
1203+ name : "error if a MachineDeploymentClass in use gets removed" ,
12041204 clusters : []client.Object {
12051205 builder .Cluster (metav1 .NamespaceDefault , "cluster1" ).
1206- WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
1207- WithTopology (
1208- builder .ClusterTopology ().
1209- WithClass ("class1" ).
1210- WithMachineDeployment (
1211- builder .MachineDeploymentTopology ("workers1" ).
1212- WithClass ("bb" ).
1213- Build (),
1214- ).
1215- WithMachineDeployment (
1216- builder .MachineDeploymentTopology ("workers2" ).
1217- WithClass ("aa" ).
1218- Build (),
1219- ).
1220- Build ()).
1221- Build (),
1222- builder .Cluster (metav1 .NamespaceDefault , "cluster2" ).
1223- WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
1224- WithTopology (
1225- builder .ClusterTopology ().
1226- WithClass ("class1" ).
1227- WithMachineDeployment (
1228- builder .MachineDeploymentTopology ("workers1" ).
1229- WithClass ("aa" ).
1230- Build (),
1231- ).
1232- WithMachineDeployment (
1233- builder .MachineDeploymentTopology ("workers2" ).
1234- WithClass ("aa" ).
1235- Build (),
1236- ).
1237- Build ()).
1238- Build (),
1239- builder .Cluster (metav1 .NamespaceDefault , "cluster3" ).
12401206 WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
12411207 WithTopology (
12421208 builder .ClusterTopology ().
@@ -1276,7 +1242,7 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
12761242 builder .ControlPlaneTemplate (metav1 .NamespaceDefault , "cp1" ).
12771243 Build ()).
12781244 WithWorkerMachineDeploymentClasses (
1279- * builder .MachineDeploymentClass ("bb " ).
1245+ * builder .MachineDeploymentClass ("aa " ).
12801246 WithInfrastructureTemplate (
12811247 builder .InfrastructureMachineTemplate (metav1 .NamespaceDefault , "infra1" ).Build ()).
12821248 WithBootstrapTemplate (
@@ -1286,7 +1252,7 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
12861252 expectErr : true ,
12871253 },
12881254 {
1289- name : "pass if a similar MachineDeploymentClass is deleted when it is only used in Clusters not belonging to the ClusterClass" ,
1255+ name : "error if many MachineDeploymentClasses, used in multiple Clusters using the modified ClusterClass, are removed " ,
12901256 clusters : []client.Object {
12911257 builder .Cluster (metav1 .NamespaceDefault , "cluster1" ).
12921258 WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
@@ -1298,64 +1264,31 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
12981264 WithClass ("bb" ).
12991265 Build (),
13001266 ).
1267+ WithMachineDeployment (
1268+ builder .MachineDeploymentTopology ("workers2" ).
1269+ WithClass ("aa" ).
1270+ Build (),
1271+ ).
13011272 Build ()).
13021273 Build (),
13031274 builder .Cluster (metav1 .NamespaceDefault , "cluster2" ).
13041275 WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
13051276 WithTopology (
13061277 builder .ClusterTopology ().
1307- WithClass ("class2 " ).
1278+ WithClass ("class1 " ).
13081279 WithMachineDeployment (
13091280 builder .MachineDeploymentTopology ("workers1" ).
1310-
1311- // A MachineDeploymentClass with the same name is in ClusterClass "class1" but
1312- // this cluster is based on ClusterClass "class2" and does not impact deletion.
1281+ WithClass ("aa" ).
1282+ Build (),
1283+ ).
1284+ WithMachineDeployment (
1285+ builder .MachineDeploymentTopology ("workers2" ).
13131286 WithClass ("aa" ).
13141287 Build (),
13151288 ).
13161289 Build ()).
13171290 Build (),
1318- },
1319- oldClusterClass : builder .ClusterClass (metav1 .NamespaceDefault , "class1" ).
1320- WithInfrastructureClusterTemplate (
1321- builder .InfrastructureClusterTemplate (metav1 .NamespaceDefault , "inf" ).Build ()).
1322- WithControlPlaneTemplate (
1323- builder .ControlPlaneTemplate (metav1 .NamespaceDefault , "cp1" ).
1324- Build ()).
1325- WithWorkerMachineDeploymentClasses (
1326- * builder .MachineDeploymentClass ("aa" ).
1327- WithInfrastructureTemplate (
1328- builder .InfrastructureMachineTemplate (metav1 .NamespaceDefault , "infra1" ).Build ()).
1329- WithBootstrapTemplate (
1330- builder .BootstrapTemplate (metav1 .NamespaceDefault , "bootstrap1" ).Build ()).
1331- Build (),
1332- * builder .MachineDeploymentClass ("bb" ).
1333- WithInfrastructureTemplate (
1334- builder .InfrastructureMachineTemplate (metav1 .NamespaceDefault , "infra1" ).Build ()).
1335- WithBootstrapTemplate (
1336- builder .BootstrapTemplate (metav1 .NamespaceDefault , "bootstrap1" ).Build ()).
1337- Build ()).
1338- Build (),
1339- newClusterClass : builder .ClusterClass (metav1 .NamespaceDefault , "class1" ).
1340- WithInfrastructureClusterTemplate (
1341- builder .InfrastructureClusterTemplate (metav1 .NamespaceDefault , "inf" ).Build ()).
1342- WithControlPlaneTemplate (
1343- builder .ControlPlaneTemplate (metav1 .NamespaceDefault , "cp1" ).
1344- Build ()).
1345- WithWorkerMachineDeploymentClasses (
1346- * builder .MachineDeploymentClass ("bb" ).
1347- WithInfrastructureTemplate (
1348- builder .InfrastructureMachineTemplate (metav1 .NamespaceDefault , "infra1" ).Build ()).
1349- WithBootstrapTemplate (
1350- builder .BootstrapTemplate (metav1 .NamespaceDefault , "bootstrap1" ).Build ()).
1351- Build ()).
1352- Build (),
1353- expectErr : false ,
1354- },
1355- {
1356- name : "pass if a MachineDeploymentClass not in use gets removed" ,
1357- clusters : []client.Object {
1358- builder .Cluster (metav1 .NamespaceDefault , "cluster1" ).
1291+ builder .Cluster (metav1 .NamespaceDefault , "cluster3" ).
13591292 WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
13601293 WithTopology (
13611294 builder .ClusterTopology ().
@@ -1402,7 +1335,7 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
14021335 builder .BootstrapTemplate (metav1 .NamespaceDefault , "bootstrap1" ).Build ()).
14031336 Build ()).
14041337 Build (),
1405- expectErr : false ,
1338+ expectErr : true ,
14061339 },
14071340 }
14081341
@@ -2450,128 +2383,3 @@ func TestClusterClassValidationWithVariableChecks(t *testing.T) {
24502383 })
24512384 }
24522385}
2453-
2454- func TestClusterClass_ValidateDelete (t * testing.T ) {
2455- class := builder .ClusterClass (metav1 .NamespaceDefault , "class1" ).Build ()
2456-
2457- tests := []struct {
2458- name string
2459- clusters []client.Object
2460- expectErr bool
2461- }{
2462- {
2463- name : "allow deletion if a cluster exists but does not reference the ClusterClass for deletion" ,
2464- clusters : []client.Object {
2465- builder .Cluster (metav1 .NamespaceDefault , "cluster1" ).
2466- WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
2467- WithTopology (
2468- builder .ClusterTopology ().
2469- WithClass ("class2" ).
2470- Build ()).
2471- Build (),
2472- },
2473- expectErr : false ,
2474- },
2475- {
2476- name : "error if cluster exists with a reference to the ClusterClass for deletion" ,
2477- clusters : []client.Object {
2478- builder .Cluster (metav1 .NamespaceDefault , "cluster1" ).
2479- WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
2480- WithTopology (
2481- builder .ClusterTopology ().
2482- WithClass ("class1" ).
2483- Build ()).
2484- Build (),
2485- },
2486- expectErr : true ,
2487- },
2488- {
2489- name : "error if multiple clusters exist and at least one references to the ClusterClass for deletion" ,
2490- clusters : []client.Object {
2491- builder .Cluster (metav1 .NamespaceDefault , "cluster1" ).
2492- WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
2493- WithTopology (
2494- builder .ClusterTopology ().
2495- WithClass ("class1" ).
2496- Build ()).
2497- Build (),
2498- builder .Cluster (metav1 .NamespaceDefault , "cluster2" ).
2499- WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
2500- WithTopology (
2501- builder .ClusterTopology ().
2502- WithClass ("class2" ).
2503- Build ()).
2504- Build (),
2505- builder .Cluster (metav1 .NamespaceDefault , "cluster3" ).
2506- WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
2507- WithTopology (
2508- builder .ClusterTopology ().
2509- WithClass ("class3" ).
2510- Build ()).
2511- Build (),
2512- builder .Cluster (metav1 .NamespaceDefault , "cluster4" ).
2513- WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
2514- WithTopology (
2515- builder .ClusterTopology ().
2516- WithClass ("class4" ).
2517- Build ()).
2518- Build (),
2519- },
2520- expectErr : true ,
2521- },
2522- {
2523- name : "allow deletion if multiple clusters exist and none of them references to the ClusterClass for deletion" ,
2524- clusters : []client.Object {
2525- builder .Cluster (metav1 .NamespaceDefault , "cluster1" ).
2526- WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
2527- WithTopology (
2528- builder .ClusterTopology ().
2529- WithClass ("class5" ).
2530- Build ()).
2531- Build (),
2532- builder .Cluster (metav1 .NamespaceDefault , "cluster2" ).
2533- WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
2534- WithTopology (
2535- builder .ClusterTopology ().
2536- WithClass ("class2" ).
2537- Build ()).
2538- Build (),
2539- builder .Cluster (metav1 .NamespaceDefault , "cluster3" ).
2540- WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
2541- WithTopology (
2542- builder .ClusterTopology ().
2543- WithClass ("class3" ).
2544- Build ()).
2545- Build (),
2546- builder .Cluster (metav1 .NamespaceDefault , "cluster4" ).
2547- WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
2548- WithTopology (
2549- builder .ClusterTopology ().
2550- WithClass ("class4" ).
2551- Build ()).
2552- Build (),
2553- },
2554- expectErr : false ,
2555- },
2556- }
2557-
2558- for _ , tt := range tests {
2559- t .Run (tt .name , func (t * testing.T ) {
2560- g := NewWithT (t )
2561- // Sets up the fakeClient for the test case.
2562- fakeClient := fake .NewClientBuilder ().
2563- WithObjects (tt .clusters ... ).
2564- WithScheme (fakeScheme ).
2565- Build ()
2566-
2567- // Create the webhook and add the fakeClient as its client.
2568- webhook := & ClusterClass {Client : fakeClient }
2569- err := webhook .ValidateDelete (ctx , class )
2570- if tt .expectErr {
2571- g .Expect (err ).To (HaveOccurred ())
2572- return
2573- }
2574- g .Expect (err ).ToNot (HaveOccurred ())
2575- })
2576- }
2577- }
0 commit comments