@@ -43,7 +43,7 @@ import (
4343
4444const deploymentReplicas = 4
4545
46- func tooManyRestartsPolicy (targetNamespace string , podRestartThresholds int32 , includingInitContainers bool ) * apiv1alpha2.DeschedulerPolicy {
46+ func tooManyRestartsPolicy (targetNamespace string , podRestartThresholds int32 , includingInitContainers bool , gracePeriodSeconds int64 ) * apiv1alpha2.DeschedulerPolicy {
4747 return & apiv1alpha2.DeschedulerPolicy {
4848 Profiles : []apiv1alpha2.DeschedulerProfile {
4949 {
@@ -84,6 +84,7 @@ func tooManyRestartsPolicy(targetNamespace string, podRestartThresholds int32, i
8484 },
8585 },
8686 },
87+ GracePeriodSeconds : & gracePeriodSeconds ,
8788 }
8889}
8990
@@ -127,16 +128,23 @@ func TestTooManyRestarts(t *testing.T) {
127128 tests := []struct {
128129 name string
129130 policy * apiv1alpha2.DeschedulerPolicy
131+ enableGracePeriod bool
130132 expectedEvictedPodCount uint
131133 }{
132134 {
133135 name : "test-no-evictions" ,
134- policy : tooManyRestartsPolicy (testNamespace .Name , 10000 , true ),
136+ policy : tooManyRestartsPolicy (testNamespace .Name , 10000 , true , 0 ),
135137 expectedEvictedPodCount : 0 ,
136138 },
137139 {
138140 name : "test-one-evictions" ,
139- policy : tooManyRestartsPolicy (testNamespace .Name , 3 , true ),
141+ policy : tooManyRestartsPolicy (testNamespace .Name , 3 , true , 0 ),
142+ expectedEvictedPodCount : 4 ,
143+ },
144+ {
145+ name : "test-one-evictions-use-gracePeriodSeconds" ,
146+ policy : tooManyRestartsPolicy (testNamespace .Name , 3 , true , 60 ),
147+ enableGracePeriod : true ,
140148 expectedEvictedPodCount : 4 ,
141149 },
142150 }
@@ -197,8 +205,32 @@ func TestTooManyRestarts(t *testing.T) {
197205 deschedulerPodName = deschedulerPods [0 ].Name
198206 }
199207
208+ // Check if grace period is enabled and wait accordingly
209+ if tc .enableGracePeriod {
210+ // Ensure no pods are evicted during the grace period
211+ // Wait for 55 seconds to ensure that the pods are not evicted during the grace period
212+ // We do not want to use an extreme waiting time, such as 59 seconds,
213+ // because the grace period is set to 60 seconds.
214+ // In order to avoid unnecessary flake failures,
215+ // we only need to make sure that the pod is not evicted within a certain range.
216+ t .Logf ("Waiting for grace period of %d seconds" , 55 )
217+ if err := wait .PollUntilContextTimeout (ctx , 1 * time .Second , time .Duration (55 )* time .Second , true , func (ctx context.Context ) (bool , error ) {
218+ currentRunNames := sets .NewString (getCurrentPodNames (ctx , clientSet , testNamespace .Name , t )... )
219+ actualEvictedPod := preRunNames .Difference (currentRunNames )
220+ actualEvictedPodCount := uint (actualEvictedPod .Len ())
221+ t .Logf ("preRunNames: %v, currentRunNames: %v, actualEvictedPodCount: %v\n " , preRunNames .List (), currentRunNames .List (), actualEvictedPodCount )
222+ if actualEvictedPodCount > 0 {
223+ t .Fatalf ("Pods were evicted during grace period; expected 0, got %v" , actualEvictedPodCount )
224+ return false , nil
225+ }
226+ return true , nil
227+ }); err != nil {
228+ t .Fatalf ("Error waiting during grace period: %v" , err )
229+ }
230+ }
231+
200232 // Run RemovePodsHavingTooManyRestarts strategy
201- if err := wait .PollUntilContextTimeout (ctx , 1 * time .Second , 20 * time .Second , true , func (ctx context.Context ) (bool , error ) {
233+ if err := wait .PollUntilContextTimeout (ctx , 1 * time .Second , 30 * time .Second , true , func (ctx context.Context ) (bool , error ) {
202234 currentRunNames := sets .NewString (getCurrentPodNames (ctx , clientSet , testNamespace .Name , t )... )
203235 actualEvictedPod := preRunNames .Difference (currentRunNames )
204236 actualEvictedPodCount := uint (actualEvictedPod .Len ())
@@ -210,7 +242,7 @@ func TestTooManyRestarts(t *testing.T) {
210242
211243 return true , nil
212244 }); err != nil {
213- t .Errorf ("Error waiting for descheduler running: %v" , err )
245+ t .Fatalf ("Error waiting for descheduler running: %v" , err )
214246 }
215247 waitForTerminatingPodsToDisappear (ctx , t , clientSet , testNamespace .Name )
216248 })
0 commit comments