Skip to content

Commit e45eef1

Browse files
Increase Job Controller Performance
1 parent 0c5e832 commit e45eef1

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

pkg/controller/job/job_controller.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,6 @@ func (jm *Controller) updateJob(logger klog.Logger, old, cur interface{}) {
479479
jm.enqueueSyncJobImmediately(logger, curJob)
480480
}
481481

482-
// The job shouldn't be marked as finished until all pod finalizers are removed.
483-
// This is a backup operation in this case.
484-
if util.IsJobFinished(curJob) {
485-
jm.cleanupPodFinalizers(curJob)
486-
}
487-
488482
// check if need to add a new rsync for ActiveDeadlineSeconds
489483
if curJob.Status.StartTime != nil {
490484
curADS := curJob.Spec.ActiveDeadlineSeconds
@@ -772,6 +766,8 @@ func (jm *Controller) syncJob(ctx context.Context, key string) (rErr error) {
772766

773767
// if job was finished previously, we don't want to redo the termination
774768
if util.IsJobFinished(&job) {
769+
// The job shouldn't be marked as finished until all pod finalizers are removed.
770+
jm.cleanupPodFinalizers(&job)
775771
err := jm.podBackoffStore.removeBackoffRecord(key)
776772
if err != nil {
777773
// re-syncing here as the record has to be removed for finished/deleted jobs

pkg/controller/job/job_controller_test.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7624,9 +7624,9 @@ func TestFinalizerCleanup(t *testing.T) {
76247624
// Start the Pod and Job informers.
76257625
sharedInformers.Start(ctx.Done())
76267626
sharedInformers.WaitForCacheSync(ctx.Done())
7627-
// Initialize the controller with 0 workers to make sure the
7628-
// pod finalizers are not removed by the "syncJob" function.
7629-
go manager.Run(ctx, 0)
7627+
// Initialize the controller with 1 worker to make sure the
7628+
// pod finalizers are removed by the "syncJob" function.
7629+
go manager.Run(ctx, 1)
76307630
// Make sure the pod finalizers are removed by the "orphanWorker" function.
76317631
go wait.UntilWithContext(ctx, manager.orphanWorker, time.Second)
76327632

@@ -7645,22 +7645,24 @@ func TestFinalizerCleanup(t *testing.T) {
76457645
t.Fatalf("Waiting for Job object to appear in jobLister: %v", err)
76467646
}
76477647

7648-
// Create a Pod with the job tracking finalizer
7649-
pod := newPod("test-pod", job)
7650-
pod.Finalizers = append(pod.Finalizers, batch.JobTrackingFinalizer)
7651-
pod, err = clientset.CoreV1().Pods(pod.GetNamespace()).Create(ctx, pod, metav1.CreateOptions{})
7648+
selector, err := metav1.LabelSelectorAsSelector(job.Spec.Selector)
76527649
if err != nil {
7653-
t.Fatalf("Creating pod: %v", err)
7650+
t.Fatalf("Error parsing job selector: %v", err)
76547651
}
76557652

7656-
// Await for the Pod to appear in the podStore to ensure that the pod exists when cleaning up the Job.
7657-
// In a production environment, there wouldn't be these guarantees, but the Pod would be cleaned up
7658-
// by the orphan pod worker, when the Pod finishes.
7653+
var pods []*v1.Pod
7654+
// Wait for the Pod to be created by the Job controller
76597655
if err := wait.PollUntilContextTimeout(ctx, 10*time.Millisecond, wait.ForeverTestTimeout, false, func(ctx context.Context) (bool, error) {
7660-
pod, _ := manager.podStore.Pods(pod.GetNamespace()).Get(pod.Name)
7661-
return pod != nil, nil
7656+
pods, err = manager.podStore.Pods(job.Namespace).List(selector)
7657+
if err != nil {
7658+
return false, err
7659+
}
7660+
if len(pods) > 0 {
7661+
return true, nil
7662+
}
7663+
return false, nil
76627664
}); err != nil {
7663-
t.Fatalf("Waiting for Pod to appear in podLister: %v", err)
7665+
t.Fatalf("Waiting for Pod to be created by Job: %v", err)
76647666
}
76657667

76667668
// Mark Job as complete.
@@ -7673,10 +7675,9 @@ func TestFinalizerCleanup(t *testing.T) {
76737675
t.Fatalf("Updating job status: %v", err)
76747676
}
76757677

7676-
// Verify the pod finalizer is removed for a finished Job,
7677-
// even if the jobs pods are not tracked by the main reconciliation loop.
7678+
// Verify the pod finalizer is removed for a finished Job
76787679
if err := wait.PollUntilContextTimeout(ctx, 100*time.Millisecond, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
7679-
p, err := clientset.CoreV1().Pods(pod.Namespace).Get(ctx, pod.Name, metav1.GetOptions{})
7680+
p, err := clientset.CoreV1().Pods(pods[0].Namespace).Get(ctx, pods[0].Name, metav1.GetOptions{})
76807681
if err != nil {
76817682
return false, err
76827683
}

0 commit comments

Comments
 (0)