Skip to content

Commit 631d83b

Browse files
authored
Merge pull request kubernetes#128569 from tenzen-y/add-job-evaluation-orders-comment
Job: Add evaluation step comments in the syncJob
2 parents af41aa1 + 5dda60e commit 631d83b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

pkg/controller/job/job_controller.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,20 +902,31 @@ func (jm *Controller) syncJob(ctx context.Context, key string) (rErr error) {
902902

903903
var manageJobErr error
904904

905+
// This is the starting point for evaluating the end state of the Job.
906+
// Note that we need to order evaluations since a Job could satisfy multiple criteria at the same time in some cases:
907+
// 1. Evaluate the pre-existing SuccessCriteriaMet and FailureTarget to respect the previous reconcile results, then transform FailureTarget to Failed.
908+
// 2. Evaluate failure scenarios.
909+
// 3. Evaluate success scenarios.
910+
// 4. Evaluate jobCtx.finishedCondition (see trackJobStatusAndRemoveFinalizers), then transform FailureTarget to Failed and SuccessCriteriaMet to Complete once the job is finished.
911+
905912
exceedsBackoffLimit := jobCtx.failed > *job.Spec.BackoffLimit
913+
// Evaluate the pre-existing SuccessCriteriaMet.
906914
jobCtx.finishedCondition = hasSuccessCriteriaMetCondition(&job)
907915

908916
// Given that the Job already has the SuccessCriteriaMet condition, the termination condition already had confirmed in another cycle.
909917
// So, the job-controller evaluates the podFailurePolicy only when the Job doesn't have the SuccessCriteriaMet condition.
910918
if jobCtx.finishedCondition == nil {
919+
// Evaluate the pre-existing FailureTarget.
911920
failureTargetCondition := findConditionByType(job.Status.Conditions, batch.JobFailureTarget)
912921
if failureTargetCondition != nil && failureTargetCondition.Status == v1.ConditionTrue {
913922
jobCtx.finishedCondition = newFailedConditionForFailureTarget(failureTargetCondition, jm.clock.Now())
923+
// Evaluate failure scenarios for PodFailurePolicy.
914924
} else if failJobMessage := getFailJobMessage(&job, pods); failJobMessage != nil {
915925
// Prepare the interim FailureTarget condition to record the failure message before the finalizers (allowing removal of the pods) are removed.
916926
jobCtx.finishedCondition = newCondition(batch.JobFailureTarget, v1.ConditionTrue, batch.JobReasonPodFailurePolicy, *failJobMessage, jm.clock.Now())
917927
}
918928
}
929+
// Evaluate failure scenarios for BackoffLimit and ActiveDeadlineSeconds.
919930
if jobCtx.finishedCondition == nil {
920931
if exceedsBackoffLimit || pastBackoffLimitOnFailure(&job, pods) {
921932
// check if the number of pod restart exceeds backoff (for restart OnFailure only)
@@ -933,6 +944,7 @@ func (jm *Controller) syncJob(ctx context.Context, key string) (rErr error) {
933944
if isIndexedJob(&job) {
934945
jobCtx.prevSucceededIndexes, jobCtx.succeededIndexes = calculateSucceededIndexes(logger, &job, pods)
935946
jobCtx.succeeded = int32(jobCtx.succeededIndexes.total())
947+
// Evaluate failure scenarios for BackoffLimitPerIndex.
936948
if hasBackoffLimitPerIndex(&job) {
937949
jobCtx.failedIndexes = calculateFailedIndexes(logger, &job, pods)
938950
if jobCtx.finishedCondition == nil {
@@ -944,6 +956,7 @@ func (jm *Controller) syncJob(ctx context.Context, key string) (rErr error) {
944956
}
945957
jobCtx.podsWithDelayedDeletionPerIndex = getPodsWithDelayedDeletionPerIndex(logger, jobCtx)
946958
}
959+
// Evaluate success scenarios for SuccessPolicy.
947960
if jobCtx.finishedCondition == nil {
948961
if msg, met := matchSuccessPolicy(logger, job.Spec.SuccessPolicy, *job.Spec.Completions, jobCtx.succeededIndexes); met {
949962
jobCtx.finishedCondition = newCondition(batch.JobSuccessCriteriaMet, v1.ConditionTrue, batch.JobReasonSuccessPolicy, msg, jm.clock.Now())
@@ -971,6 +984,7 @@ func (jm *Controller) syncJob(ctx context.Context, key string) (rErr error) {
971984
active, action, manageJobErr = jm.manageJob(ctx, &job, jobCtx)
972985
manageJobCalled = true
973986
}
987+
// Evaluate success scenarios for Completions.
974988
complete := false
975989
if job.Spec.Completions == nil {
976990
// This type of job is complete when any pod exits with success.
@@ -1253,6 +1267,7 @@ func (jm *Controller) trackJobStatusAndRemoveFinalizers(ctx context.Context, job
12531267
needsFlush = true
12541268
}
12551269
}
1270+
// Evaluate jobCtx.finishedCondition and transform FailureTarget to Failed.
12561271
if jobCtx.finishedCondition != nil && jobCtx.finishedCondition.Type == batch.JobFailureTarget {
12571272

12581273
// Append the interim FailureTarget condition to update the job status with before finalizers are removed.
@@ -1263,6 +1278,7 @@ func (jm *Controller) trackJobStatusAndRemoveFinalizers(ctx context.Context, job
12631278
// It is also used in the enactJobFinished function for reporting.
12641279
jobCtx.finishedCondition = newFailedConditionForFailureTarget(jobCtx.finishedCondition, jm.clock.Now())
12651280
}
1281+
// Evaluate jobCtx.finishedCondition and transform SuccessCriteriaMet to Complete.
12661282
if isSuccessCriteriaMetCondition(jobCtx.finishedCondition) {
12671283
// Append the interim SuccessCriteriaMet condition to update the job status with before finalizers are removed.
12681284
if hasSuccessCriteriaMetCondition(jobCtx.job) == nil {

0 commit comments

Comments
 (0)