@@ -3436,6 +3436,148 @@ func TestDropPodLifecycleSleepAction(t *testing.T) {
3436
3436
}
3437
3437
}
3438
3438
3439
+ func TestDropContainerStopSignals (t * testing.T ) {
3440
+ makeContainer := func (lifecycle * api.Lifecycle ) api.Container {
3441
+ container := api.Container {Name : "foo" }
3442
+ if lifecycle != nil {
3443
+ container .Lifecycle = lifecycle
3444
+ }
3445
+ return container
3446
+ }
3447
+
3448
+ makeEphemeralContainer := func (lifecycle * api.Lifecycle ) api.EphemeralContainer {
3449
+ container := api.EphemeralContainer {
3450
+ EphemeralContainerCommon : api.EphemeralContainerCommon {Name : "foo" },
3451
+ }
3452
+ if lifecycle != nil {
3453
+ container .Lifecycle = lifecycle
3454
+ }
3455
+ return container
3456
+ }
3457
+
3458
+ makePod := func (os api.OSName , containers []api.Container , initContainers []api.Container , ephemeralContainers []api.EphemeralContainer ) * api.PodSpec {
3459
+ return & api.PodSpec {
3460
+ OS : & api.PodOS {Name : os },
3461
+ Containers : containers ,
3462
+ InitContainers : initContainers ,
3463
+ EphemeralContainers : ephemeralContainers ,
3464
+ }
3465
+ }
3466
+
3467
+ testCases := []struct {
3468
+ featuregateEnabled bool
3469
+ oldLifecycle * api.Lifecycle
3470
+ newLifecycle * api.Lifecycle
3471
+ expectedLifecycle * api.Lifecycle
3472
+ }{
3473
+ // feature gate is turned on and stopsignal is not in use - Lifecycle stays nil
3474
+ {
3475
+ featuregateEnabled : true ,
3476
+ oldLifecycle : nil ,
3477
+ newLifecycle : nil ,
3478
+ expectedLifecycle : nil ,
3479
+ },
3480
+ // feature gate is turned off and StopSignal is in use - StopSignal is not dropped
3481
+ {
3482
+ featuregateEnabled : false ,
3483
+ oldLifecycle : & api.Lifecycle {StopSignal : ptr .To (api .SIGTERM )},
3484
+ newLifecycle : & api.Lifecycle {StopSignal : ptr .To (api .SIGTERM )},
3485
+ expectedLifecycle : & api.Lifecycle {StopSignal : ptr .To (api .SIGTERM )},
3486
+ },
3487
+ // feature gate is turned off and StopSignal is not in use - Entire lifecycle is dropped
3488
+ {
3489
+ featuregateEnabled : false ,
3490
+ oldLifecycle : & api.Lifecycle {StopSignal : nil },
3491
+ newLifecycle : & api.Lifecycle {StopSignal : ptr .To (api .SIGTERM )},
3492
+ expectedLifecycle : nil ,
3493
+ },
3494
+ // feature gate is turned on and StopSignal is in use - StopSignal is not dropped
3495
+ {
3496
+ featuregateEnabled : true ,
3497
+ oldLifecycle : & api.Lifecycle {StopSignal : nil },
3498
+ newLifecycle : & api.Lifecycle {StopSignal : ptr .To (api .SIGTERM )},
3499
+ expectedLifecycle : & api.Lifecycle {StopSignal : ptr .To (api .SIGTERM )},
3500
+ },
3501
+ // feature gate is turned off and PreStop is in use - StopSignal alone is dropped
3502
+ {
3503
+ featuregateEnabled : false ,
3504
+ oldLifecycle : & api.Lifecycle {StopSignal : nil , PreStop : & api.LifecycleHandler {
3505
+ Exec : & api.ExecAction {Command : []string {"foo" }},
3506
+ }},
3507
+ newLifecycle : & api.Lifecycle {StopSignal : ptr .To (api .SIGTERM ), PreStop : & api.LifecycleHandler {
3508
+ Exec : & api.ExecAction {Command : []string {"foo" }},
3509
+ }},
3510
+ expectedLifecycle : & api.Lifecycle {StopSignal : nil , PreStop : & api.LifecycleHandler {
3511
+ Exec : & api.ExecAction {Command : []string {"foo" }},
3512
+ }},
3513
+ },
3514
+ // feature gate is turned on and PreStop is in use - StopSignal is not dropped
3515
+ {
3516
+ featuregateEnabled : true ,
3517
+ oldLifecycle : & api.Lifecycle {StopSignal : nil , PreStop : & api.LifecycleHandler {
3518
+ Exec : & api.ExecAction {Command : []string {"foo" }},
3519
+ }},
3520
+ newLifecycle : & api.Lifecycle {StopSignal : ptr .To (api .SIGTERM ), PreStop : & api.LifecycleHandler {
3521
+ Exec : & api.ExecAction {Command : []string {"foo" }},
3522
+ }},
3523
+ expectedLifecycle : & api.Lifecycle {StopSignal : ptr .To (api .SIGTERM ), PreStop : & api.LifecycleHandler {
3524
+ Exec : & api.ExecAction {Command : []string {"foo" }},
3525
+ }},
3526
+ },
3527
+ // feature gate is turned off and PreStop and StopSignal are in use - nothing is dropped
3528
+ {
3529
+ featuregateEnabled : true ,
3530
+ oldLifecycle : & api.Lifecycle {StopSignal : ptr .To (api .SIGTERM ), PreStop : & api.LifecycleHandler {
3531
+ Exec : & api.ExecAction {Command : []string {"foo" }},
3532
+ }},
3533
+ newLifecycle : & api.Lifecycle {StopSignal : ptr .To (api .SIGTERM ), PreStop : & api.LifecycleHandler {
3534
+ Exec : & api.ExecAction {Command : []string {"foo" }},
3535
+ }},
3536
+ expectedLifecycle : & api.Lifecycle {StopSignal : ptr .To (api .SIGTERM ), PreStop : & api.LifecycleHandler {
3537
+ Exec : & api.ExecAction {Command : []string {"foo" }},
3538
+ }},
3539
+ },
3540
+ }
3541
+
3542
+ for i , tc := range testCases {
3543
+ t .Run (fmt .Sprintf ("test_%d" , i ), func (t * testing.T ) {
3544
+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .ContainerStopSignals , tc .featuregateEnabled )
3545
+ // Containers
3546
+ {
3547
+ oldPod := makePod (api .Linux , []api.Container {makeContainer (tc .oldLifecycle .DeepCopy ())}, nil , nil )
3548
+ newPod := makePod (api .Linux , []api.Container {makeContainer (tc .newLifecycle .DeepCopy ())}, nil , nil )
3549
+ expectedPod := makePod (api .Linux , []api.Container {makeContainer (tc .expectedLifecycle .DeepCopy ())}, nil , nil )
3550
+ dropDisabledFields (newPod , nil , oldPod , nil )
3551
+
3552
+ if diff := cmp .Diff (expectedPod , newPod ); diff != "" {
3553
+ t .Fatalf ("Unexpected modification to new pod; diff (-got +want)\n %s" , diff )
3554
+ }
3555
+ }
3556
+ // InitContainers
3557
+ {
3558
+ oldPod := makePod (api .Linux , nil , []api.Container {makeContainer (tc .oldLifecycle .DeepCopy ())}, nil )
3559
+ newPod := makePod (api .Linux , nil , []api.Container {makeContainer (tc .newLifecycle .DeepCopy ())}, nil )
3560
+ expectPod := makePod (api .Linux , nil , []api.Container {makeContainer (tc .expectedLifecycle .DeepCopy ())}, nil )
3561
+ dropDisabledFields (newPod , nil , oldPod , nil )
3562
+ if diff := cmp .Diff (expectPod , newPod ); diff != "" {
3563
+ t .Fatalf ("Unexpected modification to new pod; diff (-got +want)\n %s" , diff )
3564
+ }
3565
+ }
3566
+ // EphemeralContainers
3567
+ {
3568
+ oldPod := makePod (api .Linux , nil , nil , []api.EphemeralContainer {makeEphemeralContainer (tc .oldLifecycle .DeepCopy ())})
3569
+ newPod := makePod (api .Linux , nil , nil , []api.EphemeralContainer {makeEphemeralContainer (tc .newLifecycle .DeepCopy ())})
3570
+ expectPod := makePod (api .Linux , nil , nil , []api.EphemeralContainer {makeEphemeralContainer (tc .expectedLifecycle .DeepCopy ())})
3571
+ dropDisabledFields (newPod , nil , oldPod , nil )
3572
+ if diff := cmp .Diff (expectPod , newPod ); diff != "" {
3573
+ t .Fatalf ("Unexpected modification to new pod; diff (-got +want)\n %s" , diff )
3574
+ }
3575
+ }
3576
+
3577
+ })
3578
+ }
3579
+ }
3580
+
3439
3581
func TestDropSupplementalGroupsPolicy (t * testing.T ) {
3440
3582
supplementalGroupsPolicyMerge := api .SupplementalGroupsPolicyMerge
3441
3583
podWithSupplementalGroupsPolicy := func () * api.Pod {
0 commit comments