@@ -1148,7 +1148,7 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
1148
1148
expectErr bool
1149
1149
}{
1150
1150
{
1151
- name : "error if a MachineDeploymentClass in use gets removed" ,
1151
+ name : "pass if a MachineDeploymentClass not in use gets removed" ,
1152
1152
clusters : []client.Object {
1153
1153
builder .Cluster (metav1 .NamespaceDefault , "cluster1" ).
1154
1154
WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
@@ -1190,53 +1190,19 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
1190
1190
builder .ControlPlaneTemplate (metav1 .NamespaceDefault , "cp1" ).
1191
1191
Build ()).
1192
1192
WithWorkerMachineDeploymentClasses (
1193
- * builder .MachineDeploymentClass ("aa " ).
1193
+ * builder .MachineDeploymentClass ("bb " ).
1194
1194
WithInfrastructureTemplate (
1195
1195
builder .InfrastructureMachineTemplate (metav1 .NamespaceDefault , "infra1" ).Build ()).
1196
1196
WithBootstrapTemplate (
1197
1197
builder .BootstrapTemplate (metav1 .NamespaceDefault , "bootstrap1" ).Build ()).
1198
1198
Build ()).
1199
1199
Build (),
1200
- expectErr : true ,
1200
+ expectErr : false ,
1201
1201
},
1202
1202
{
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" ,
1204
1204
clusters : []client.Object {
1205
1205
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" ).
1240
1206
WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
1241
1207
WithTopology (
1242
1208
builder .ClusterTopology ().
@@ -1276,7 +1242,7 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
1276
1242
builder .ControlPlaneTemplate (metav1 .NamespaceDefault , "cp1" ).
1277
1243
Build ()).
1278
1244
WithWorkerMachineDeploymentClasses (
1279
- * builder .MachineDeploymentClass ("bb " ).
1245
+ * builder .MachineDeploymentClass ("aa " ).
1280
1246
WithInfrastructureTemplate (
1281
1247
builder .InfrastructureMachineTemplate (metav1 .NamespaceDefault , "infra1" ).Build ()).
1282
1248
WithBootstrapTemplate (
@@ -1286,7 +1252,7 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
1286
1252
expectErr : true ,
1287
1253
},
1288
1254
{
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 " ,
1290
1256
clusters : []client.Object {
1291
1257
builder .Cluster (metav1 .NamespaceDefault , "cluster1" ).
1292
1258
WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
@@ -1298,64 +1264,31 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
1298
1264
WithClass ("bb" ).
1299
1265
Build (),
1300
1266
).
1267
+ WithMachineDeployment (
1268
+ builder .MachineDeploymentTopology ("workers2" ).
1269
+ WithClass ("aa" ).
1270
+ Build (),
1271
+ ).
1301
1272
Build ()).
1302
1273
Build (),
1303
1274
builder .Cluster (metav1 .NamespaceDefault , "cluster2" ).
1304
1275
WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
1305
1276
WithTopology (
1306
1277
builder .ClusterTopology ().
1307
- WithClass ("class2 " ).
1278
+ WithClass ("class1 " ).
1308
1279
WithMachineDeployment (
1309
1280
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" ).
1313
1286
WithClass ("aa" ).
1314
1287
Build (),
1315
1288
).
1316
1289
Build ()).
1317
1290
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" ).
1359
1292
WithLabels (map [string ]string {clusterv1 .ClusterTopologyOwnedLabel : "" }).
1360
1293
WithTopology (
1361
1294
builder .ClusterTopology ().
@@ -1402,7 +1335,7 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
1402
1335
builder .BootstrapTemplate (metav1 .NamespaceDefault , "bootstrap1" ).Build ()).
1403
1336
Build ()).
1404
1337
Build (),
1405
- expectErr : false ,
1338
+ expectErr : true ,
1406
1339
},
1407
1340
}
1408
1341
@@ -2450,128 +2383,3 @@ func TestClusterClassValidationWithVariableChecks(t *testing.T) {
2450
2383
})
2451
2384
}
2452
2385
}
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