@@ -1315,208 +1315,6 @@ func TestDropRunAsGroup(t *testing.T) {
1315
1315
}
1316
1316
}
1317
1317
1318
- func TestDropGMSAFields (t * testing.T ) {
1319
- defaultContainerSecurityContextFactory := func () * api.SecurityContext {
1320
- defaultProcMount := api .DefaultProcMount
1321
- return & api.SecurityContext {ProcMount : & defaultProcMount }
1322
- }
1323
- podWithoutWindowsOptionsFactory := func () * api.Pod {
1324
- return & api.Pod {
1325
- Spec : api.PodSpec {
1326
- RestartPolicy : api .RestartPolicyNever ,
1327
- SecurityContext : & api.PodSecurityContext {},
1328
- Containers : []api.Container {{Name : "container1" , Image : "testimage" , SecurityContext : defaultContainerSecurityContextFactory ()}},
1329
- InitContainers : []api.Container {{Name : "initContainer1" , Image : "testimage" , SecurityContext : defaultContainerSecurityContextFactory ()}},
1330
- },
1331
- }
1332
- }
1333
-
1334
- type podFactoryInfo struct {
1335
- description string
1336
- hasGMSAField bool
1337
- // this factory should generate the input pod whose spec will be fed to dropDisabledFields
1338
- podFactory func () * api.Pod
1339
- // this factory should generate the expected pod after the GMSA fields have been dropped
1340
- // we can't just use podWithoutWindowsOptionsFactory as is for this, since in some cases
1341
- // we'll be left with a WindowsSecurityContextOptions struct with no GMSA field set, as opposed
1342
- // to a nil pointer in the pod generated by podWithoutWindowsOptionsFactory
1343
- // if this field is not set, it will default to the podFactory
1344
- strippedPodFactory func () * api.Pod
1345
- }
1346
- podFactoryInfos := []podFactoryInfo {
1347
- {
1348
- description : "does not have any GMSA field set" ,
1349
- hasGMSAField : false ,
1350
- podFactory : podWithoutWindowsOptionsFactory ,
1351
- },
1352
- {
1353
- description : "has a pod-level WindowsSecurityContextOptions struct with no GMSA field set" ,
1354
- hasGMSAField : false ,
1355
- podFactory : func () * api.Pod {
1356
- pod := podWithoutWindowsOptionsFactory ()
1357
- pod .Spec .SecurityContext .WindowsOptions = & api.WindowsSecurityContextOptions {}
1358
- return pod
1359
- },
1360
- },
1361
- {
1362
- description : "has a WindowsSecurityContextOptions struct with no GMSA field set on a container" ,
1363
- hasGMSAField : false ,
1364
- podFactory : func () * api.Pod {
1365
- pod := podWithoutWindowsOptionsFactory ()
1366
- pod .Spec .Containers [0 ].SecurityContext .WindowsOptions = & api.WindowsSecurityContextOptions {}
1367
- return pod
1368
- },
1369
- },
1370
- {
1371
- description : "has a WindowsSecurityContextOptions struct with no GMSA field set on an init container" ,
1372
- hasGMSAField : false ,
1373
- podFactory : func () * api.Pod {
1374
- pod := podWithoutWindowsOptionsFactory ()
1375
- pod .Spec .InitContainers [0 ].SecurityContext .WindowsOptions = & api.WindowsSecurityContextOptions {}
1376
- return pod
1377
- },
1378
- },
1379
- {
1380
- description : "is nil" ,
1381
- hasGMSAField : false ,
1382
- podFactory : func () * api.Pod { return nil },
1383
- },
1384
- }
1385
-
1386
- toPtr := func (s string ) * string {
1387
- return & s
1388
- }
1389
- addGMSACredentialSpecName := func (windowsOptions * api.WindowsSecurityContextOptions ) {
1390
- windowsOptions .GMSACredentialSpecName = toPtr ("dummy-gmsa-cred-spec-name" )
1391
- }
1392
- addGMSACredentialSpec := func (windowsOptions * api.WindowsSecurityContextOptions ) {
1393
- windowsOptions .GMSACredentialSpec = toPtr ("dummy-gmsa-cred-spec-contents" )
1394
- }
1395
- addBothGMSAFields := func (windowsOptions * api.WindowsSecurityContextOptions ) {
1396
- addGMSACredentialSpecName (windowsOptions )
1397
- addGMSACredentialSpec (windowsOptions )
1398
- }
1399
-
1400
- for fieldName , windowsOptionsTransformingFunc := range map [string ]func (* api.WindowsSecurityContextOptions ){
1401
- "GMSACredentialSpecName field" : addGMSACredentialSpecName ,
1402
- "GMSACredentialSpec field" : addGMSACredentialSpec ,
1403
- "both GMSA fields" : addBothGMSAFields ,
1404
- } {
1405
- // yes, these variables are indeed needed for the closure to work
1406
- // properly, please do NOT remove them
1407
- name := fieldName
1408
- transformingFunc := windowsOptionsTransformingFunc
1409
-
1410
- windowsOptionsWithGMSAFieldFactory := func () * api.WindowsSecurityContextOptions {
1411
- windowsOptions := & api.WindowsSecurityContextOptions {}
1412
- transformingFunc (windowsOptions )
1413
- return windowsOptions
1414
- }
1415
-
1416
- podFactoryInfos = append (podFactoryInfos ,
1417
- podFactoryInfo {
1418
- description : fmt .Sprintf ("has %s in Pod" , name ),
1419
- hasGMSAField : true ,
1420
- podFactory : func () * api.Pod {
1421
- pod := podWithoutWindowsOptionsFactory ()
1422
- pod .Spec .SecurityContext .WindowsOptions = windowsOptionsWithGMSAFieldFactory ()
1423
- return pod
1424
- },
1425
- strippedPodFactory : func () * api.Pod {
1426
- pod := podWithoutWindowsOptionsFactory ()
1427
- pod .Spec .SecurityContext .WindowsOptions = & api.WindowsSecurityContextOptions {}
1428
- return pod
1429
- },
1430
- },
1431
- podFactoryInfo {
1432
- description : fmt .Sprintf ("has %s in Container" , name ),
1433
- hasGMSAField : true ,
1434
- podFactory : func () * api.Pod {
1435
- pod := podWithoutWindowsOptionsFactory ()
1436
- pod .Spec .Containers [0 ].SecurityContext .WindowsOptions = windowsOptionsWithGMSAFieldFactory ()
1437
- return pod
1438
- },
1439
- strippedPodFactory : func () * api.Pod {
1440
- pod := podWithoutWindowsOptionsFactory ()
1441
- pod .Spec .Containers [0 ].SecurityContext .WindowsOptions = & api.WindowsSecurityContextOptions {}
1442
- return pod
1443
- },
1444
- },
1445
- podFactoryInfo {
1446
- description : fmt .Sprintf ("has %s in InitContainer" , name ),
1447
- hasGMSAField : true ,
1448
- podFactory : func () * api.Pod {
1449
- pod := podWithoutWindowsOptionsFactory ()
1450
- pod .Spec .InitContainers [0 ].SecurityContext .WindowsOptions = windowsOptionsWithGMSAFieldFactory ()
1451
- return pod
1452
- },
1453
- strippedPodFactory : func () * api.Pod {
1454
- pod := podWithoutWindowsOptionsFactory ()
1455
- pod .Spec .InitContainers [0 ].SecurityContext .WindowsOptions = & api.WindowsSecurityContextOptions {}
1456
- return pod
1457
- },
1458
- })
1459
- }
1460
-
1461
- for _ , enabled := range []bool {true , false } {
1462
- for _ , oldPodFactoryInfo := range podFactoryInfos {
1463
- for _ , newPodFactoryInfo := range podFactoryInfos {
1464
- newPodHasGMSAField , newPod := newPodFactoryInfo .hasGMSAField , newPodFactoryInfo .podFactory ()
1465
- if newPod == nil {
1466
- continue
1467
- }
1468
- oldPodHasGMSAField , oldPod := oldPodFactoryInfo .hasGMSAField , oldPodFactoryInfo .podFactory ()
1469
-
1470
- t .Run (fmt .Sprintf ("feature enabled=%v, old pod %s, new pod %s" , enabled , oldPodFactoryInfo .description , newPodFactoryInfo .description ), func (t * testing.T ) {
1471
- defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .WindowsGMSA , enabled )()
1472
-
1473
- var oldPodSpec * api.PodSpec
1474
- if oldPod != nil {
1475
- oldPodSpec = & oldPod .Spec
1476
- }
1477
- dropDisabledFields (& newPod .Spec , nil , oldPodSpec , nil )
1478
-
1479
- // old pod should never be changed
1480
- if ! reflect .DeepEqual (oldPod , oldPodFactoryInfo .podFactory ()) {
1481
- t .Errorf ("old pod changed: %v" , diff .ObjectReflectDiff (oldPod , oldPodFactoryInfo .podFactory ()))
1482
- }
1483
-
1484
- switch {
1485
- case enabled || oldPodHasGMSAField :
1486
- // new pod should not be changed if the feature is enabled, or if the old pod had any GMSA field set
1487
- if ! reflect .DeepEqual (newPod , newPodFactoryInfo .podFactory ()) {
1488
- t .Errorf ("new pod changed: %v" , diff .ObjectReflectDiff (newPod , newPodFactoryInfo .podFactory ()))
1489
- }
1490
- case newPodHasGMSAField :
1491
- // new pod should be changed
1492
- if reflect .DeepEqual (newPod , newPodFactoryInfo .podFactory ()) {
1493
- t .Errorf ("%v" , oldPod )
1494
- t .Errorf ("%v" , newPod )
1495
- t .Errorf ("new pod was not changed" )
1496
- }
1497
- // new pod should not have any GMSA field set
1498
- var expectedStrippedPod * api.Pod
1499
- if newPodFactoryInfo .strippedPodFactory == nil {
1500
- expectedStrippedPod = newPodFactoryInfo .podFactory ()
1501
- } else {
1502
- expectedStrippedPod = newPodFactoryInfo .strippedPodFactory ()
1503
- }
1504
-
1505
- if ! reflect .DeepEqual (newPod , expectedStrippedPod ) {
1506
- t .Errorf ("new pod had some GMSA field set: %v" , diff .ObjectReflectDiff (newPod , expectedStrippedPod ))
1507
- }
1508
- default :
1509
- // new pod should not need to be changed
1510
- if ! reflect .DeepEqual (newPod , newPodFactoryInfo .podFactory ()) {
1511
- t .Errorf ("new pod changed: %v" , diff .ObjectReflectDiff (newPod , newPodFactoryInfo .podFactory ()))
1512
- }
1513
- }
1514
- })
1515
- }
1516
- }
1517
- }
1518
- }
1519
-
1520
1318
func TestDropPodSysctls (t * testing.T ) {
1521
1319
podWithSysctls := func () * api.Pod {
1522
1320
return & api.Pod {
0 commit comments