@@ -50,6 +50,7 @@ import (
50
50
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
51
51
e2ereplicaset "k8s.io/kubernetes/test/e2e/framework/replicaset"
52
52
admissionapi "k8s.io/pod-security-admission/api"
53
+ "k8s.io/utils/ptr"
53
54
)
54
55
55
56
type priorityPair struct {
@@ -317,7 +318,8 @@ var _ = SIGDescribe("SchedulerPreemption", framework.WithSerial(), func() {
317
318
var podRes v1.ResourceList
318
319
// Create 10 pods per node that will eat up all the node's resources.
319
320
ginkgo .By ("Create 10 low-priority pods on each node." )
320
- lowPriorityPods := make ([]* v1.Pod , 0 , 10 * len (nodeList .Items ))
321
+ nodeListLen := len (nodeList .Items )
322
+ lowPriorityPods := make ([]* v1.Pod , 0 , 10 * nodeListLen )
321
323
// Create pods in the cluster.
322
324
for i , node := range nodeList .Items {
323
325
// Update each node to advertise 3 available extended resources
@@ -331,12 +333,6 @@ var _ = SIGDescribe("SchedulerPreemption", framework.WithSerial(), func() {
331
333
pausePod := createPausePod (ctx , f , pausePodConfig {
332
334
Name : fmt .Sprintf ("pod%d-%d-%v" , i , j , lowPriorityClassName ),
333
335
PriorityClassName : lowPriorityClassName ,
334
- // This victim pod will be preempted by the high priority pod.
335
- // But, the deletion will be blocked by the finalizer.
336
- //
337
- // The finalizer is needed to prevent the medium Pods from being scheduled instead of the high Pods,
338
- // depending on when the scheduler notices the existence of all the high Pods we create.
339
- Finalizers : []string {testFinalizer },
340
336
Resources : & v1.ResourceRequirements {
341
337
Requests : podRes ,
342
338
Limits : podRes ,
@@ -354,6 +350,15 @@ var _ = SIGDescribe("SchedulerPreemption", framework.WithSerial(), func() {
354
350
},
355
351
},
356
352
},
353
+ // This victim pod will be preempted by the high priority pod.
354
+ // But, the deletion will be blocked by the preStop hook with
355
+ // TerminationGracePeriodSeconds set.
356
+ //
357
+ // The preStop hook + TerminationGracePeriodSeconds are needed to prevent the medium Pods
358
+ // from being scheduled instead of the high Pods,
359
+ // depending on when the scheduler notices the existence of all the high Pods we create.
360
+ TerminationGracePeriodSeconds : ptr.To [int64 ](80 ),
361
+ PreStopHookSleepSeconds : ptr.To [int64 ](79 ),
357
362
})
358
363
lowPriorityPods = append (lowPriorityPods , pausePod )
359
364
framework .Logf ("Created pod: %v" , pausePod .Name )
@@ -365,8 +370,8 @@ var _ = SIGDescribe("SchedulerPreemption", framework.WithSerial(), func() {
365
370
framework .ExpectNoError (e2epod .WaitForPodRunningInNamespace (ctx , cs , pod ))
366
371
}
367
372
368
- highPriorityPods := make ([]* v1.Pod , 0 , 5 * len ( nodeList . Items ) )
369
- mediumPriorityPods := make ([]* v1.Pod , 0 , 10 * len ( nodeList . Items ) )
373
+ highPriorityPods := make ([]* v1.Pod , 0 , 5 * nodeListLen )
374
+ mediumPriorityPods := make ([]* v1.Pod , 0 , 10 * nodeListLen )
370
375
371
376
ginkgo .By ("Run high/medium priority pods that have same requirements as that of lower priority pod" )
372
377
for i := range nodeList .Items {
@@ -426,18 +431,20 @@ var _ = SIGDescribe("SchedulerPreemption", framework.WithSerial(), func() {
426
431
}))
427
432
}
428
433
429
- ginkgo .By ("Remove the finalizer from all low priority pods to proceed the preemption." )
434
+ ginkgo .By ("Delete all low priority pods to proceed the preemption faster ." )
430
435
for _ , pod := range lowPriorityPods {
431
- // Remove the finalizer so that the pod can be deleted by GC
432
- e2epod .NewPodClient (f ).RemoveFinalizer (ctx , pod .Name , testFinalizer )
436
+ err := cs .CoreV1 ().Pods (pod .Namespace ).Delete (ctx , pod .Name , metav1.DeleteOptions {GracePeriodSeconds : ptr.To [int64 ](0 )})
437
+ if err != nil && ! apierrors .IsNotFound (err ) {
438
+ framework .Logf ("Deleting %v pod failed: %v" , pod .Name , err )
439
+ }
433
440
}
434
441
435
442
ginkgo .By ("Wait for high priority pods to be scheduled." )
436
443
for _ , pod := range highPriorityPods {
437
444
framework .ExpectNoError (e2epod .WaitForPodRunningInNamespace (ctx , cs , pod ))
438
445
}
439
446
440
- ginkgo .By ("Wait for 5 medium priority pods to be scheduled." )
447
+ ginkgo .By (fmt . Sprintf ( "Wait for %v medium priority pods to be scheduled." , 5 * nodeListLen ) )
441
448
framework .ExpectNoError (wait .PollUntilContextTimeout (ctx , time .Second , framework .PodStartTimeout , false , func (ctx context.Context ) (bool , error ) {
442
449
scheduled := 0
443
450
for _ , pod := range mediumPriorityPods {
@@ -450,11 +457,11 @@ var _ = SIGDescribe("SchedulerPreemption", framework.WithSerial(), func() {
450
457
scheduled ++
451
458
}
452
459
}
453
- if scheduled > 5 {
454
- return false , fmt .Errorf ("expected 5 medium priority pods to be scheduled, but got %d" , scheduled )
460
+ if scheduled > 5 * nodeListLen {
461
+ return false , fmt .Errorf ("expected %v medium priority pods to be scheduled, but got %d" , 5 * nodeListLen , scheduled )
455
462
}
456
463
457
- return scheduled == 5 , nil
464
+ return scheduled == 5 * nodeListLen , nil
458
465
}))
459
466
})
460
467
0 commit comments