@@ -429,3 +429,92 @@ func TestDropSubPath(t *testing.T) {
429
429
}
430
430
}
431
431
}
432
+
433
+ func TestDropRuntimeClass (t * testing.T ) {
434
+ runtimeClassName := "some_container_engine"
435
+ podWithoutRuntimeClass := func () * api.Pod {
436
+ return & api.Pod {
437
+ Spec : api.PodSpec {
438
+ RuntimeClassName : nil ,
439
+ },
440
+ }
441
+ }
442
+ podWithRuntimeClass := func () * api.Pod {
443
+ return & api.Pod {
444
+ Spec : api.PodSpec {
445
+ RuntimeClassName : & runtimeClassName ,
446
+ },
447
+ }
448
+ }
449
+
450
+ podInfo := []struct {
451
+ description string
452
+ hasPodRuntimeClassName bool
453
+ pod func () * api.Pod
454
+ }{
455
+ {
456
+ description : "pod Without RuntimeClassName" ,
457
+ hasPodRuntimeClassName : false ,
458
+ pod : podWithoutRuntimeClass ,
459
+ },
460
+ {
461
+ description : "pod With RuntimeClassName" ,
462
+ hasPodRuntimeClassName : true ,
463
+ pod : podWithRuntimeClass ,
464
+ },
465
+ {
466
+ description : "is nil" ,
467
+ hasPodRuntimeClassName : false ,
468
+ pod : func () * api.Pod { return nil },
469
+ },
470
+ }
471
+
472
+ for _ , enabled := range []bool {true , false } {
473
+ for _ , oldPodInfo := range podInfo {
474
+ for _ , newPodInfo := range podInfo {
475
+ oldPodHasRuntimeClassName , oldPod := oldPodInfo .hasPodRuntimeClassName , oldPodInfo .pod ()
476
+ newPodHasRuntimeClassName , newPod := newPodInfo .hasPodRuntimeClassName , newPodInfo .pod ()
477
+ if newPod == nil {
478
+ continue
479
+ }
480
+
481
+ t .Run (fmt .Sprintf ("feature enabled=%v, old pod %v, new pod %v" , enabled , oldPodInfo .description , newPodInfo .description ), func (t * testing.T ) {
482
+ defer utilfeaturetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .RuntimeClass , enabled )()
483
+
484
+ var oldPodSpec * api.PodSpec
485
+ if oldPod != nil {
486
+ oldPodSpec = & oldPod .Spec
487
+ }
488
+ DropDisabledFields (& newPod .Spec , oldPodSpec )
489
+
490
+ // old pod should never be changed
491
+ if ! reflect .DeepEqual (oldPod , oldPodInfo .pod ()) {
492
+ t .Errorf ("old pod changed: %v" , diff .ObjectReflectDiff (oldPod , oldPodInfo .pod ()))
493
+ }
494
+
495
+ switch {
496
+ case enabled || oldPodHasRuntimeClassName :
497
+ // new pod should not be changed if the feature is enabled, or if the old pod had RuntimeClass
498
+ if ! reflect .DeepEqual (newPod , newPodInfo .pod ()) {
499
+ t .Errorf ("new pod changed: %v" , diff .ObjectReflectDiff (newPod , newPodInfo .pod ()))
500
+ }
501
+ case newPodHasRuntimeClassName :
502
+ // new pod should be changed
503
+ if reflect .DeepEqual (newPod , newPodInfo .pod ()) {
504
+ t .Errorf ("new pod was not changed" )
505
+ }
506
+ // new pod should not have RuntimeClass
507
+ if ! reflect .DeepEqual (newPod , podWithoutRuntimeClass ()) {
508
+ t .Errorf ("new pod had PodRuntimeClassName: %v" , diff .ObjectReflectDiff (newPod , podWithoutRuntimeClass ()))
509
+ }
510
+ default :
511
+ // new pod should not need to be changed
512
+ if ! reflect .DeepEqual (newPod , newPodInfo .pod ()) {
513
+ t .Errorf ("new pod changed: %v" , diff .ObjectReflectDiff (newPod , newPodInfo .pod ()))
514
+ }
515
+ }
516
+ })
517
+ }
518
+ }
519
+ }
520
+ }
0 commit comments