From 7e3b53042b152529452f1610e3dc292119870357 Mon Sep 17 00:00:00 2001 From: Michal Wozniak Date: Thu, 13 Jul 2023 15:57:31 +0200 Subject: [PATCH] Pass Job context down to firstPendingIndexes --- pkg/controller/job/indexed_job_utils.go | 6 +++--- pkg/controller/job/indexed_job_utils_test.go | 7 +++++-- pkg/controller/job/job_controller.go | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/controller/job/indexed_job_utils.go b/pkg/controller/job/indexed_job_utils.go index 6a8ba5787fa..b04f803e0b7 100644 --- a/pkg/controller/job/indexed_job_utils.go +++ b/pkg/controller/job/indexed_job_utils.go @@ -190,19 +190,19 @@ func succeededIndexesFromString(logger klog.Logger, completedIndexes string, com // firstPendingIndexes returns `count` indexes less than `completions` that are // not covered by `activePods` or `succeededIndexes`. -func firstPendingIndexes(activePods []*v1.Pod, succeededIndexes orderedIntervals, count, completions int) []int { +func firstPendingIndexes(jobCtx *syncJobCtx, count, completions int) []int { if count == 0 { return nil } active := sets.New[int]() - for _, p := range activePods { + for _, p := range jobCtx.activePods { ix := getCompletionIndex(p.Annotations) if ix != unknownCompletionIndex { active.Insert(ix) } } result := make([]int, 0, count) - nonPending := succeededIndexes.withOrderedIndexes(sets.List(active)) + nonPending := jobCtx.succeededIndexes.withOrderedIndexes(sets.List(active)) // The following algorithm is bounded by len(nonPending) and count. candidate := 0 for _, sInterval := range nonPending { diff --git a/pkg/controller/job/indexed_job_utils_test.go b/pkg/controller/job/indexed_job_utils_test.go index f2796ac8431..1280bc3ab56 100644 --- a/pkg/controller/job/indexed_job_utils_test.go +++ b/pkg/controller/job/indexed_job_utils_test.go @@ -313,8 +313,11 @@ func TestFirstPendingIndexes(t *testing.T) { } for name, tc := range cases { t.Run(name, func(t *testing.T) { - pods := hollowPodsWithIndexPhase(tc.activePods) - got := firstPendingIndexes(pods, tc.succeededIndexes, tc.cnt, tc.completions) + jobCtx := &syncJobCtx{ + activePods: hollowPodsWithIndexPhase(tc.activePods), + succeededIndexes: tc.succeededIndexes, + } + got := firstPendingIndexes(jobCtx, tc.cnt, tc.completions) if diff := cmp.Diff(tc.want, got); diff != "" { t.Errorf("Wrong first pending indexes (-want,+got):\n%s", diff) } diff --git a/pkg/controller/job/job_controller.go b/pkg/controller/job/job_controller.go index 057e6939814..fa8c2f9669e 100644 --- a/pkg/controller/job/job_controller.go +++ b/pkg/controller/job/job_controller.go @@ -1461,7 +1461,7 @@ func (jm *Controller) manageJob(ctx context.Context, job *batch.Job, jobCtx *syn var indexesToAdd []int if isIndexedJob(job) { - indexesToAdd = firstPendingIndexes(jobCtx.activePods, jobCtx.succeededIndexes, int(diff), int(*job.Spec.Completions)) + indexesToAdd = firstPendingIndexes(jobCtx, int(diff), int(*job.Spec.Completions)) diff = int32(len(indexesToAdd)) } active += diff