diff --git a/pkg/controller/job/indexed_job_utils.go b/pkg/controller/job/indexed_job_utils.go index 6a8ba5787fa..3eb30bb7d63 100644 --- a/pkg/controller/job/indexed_job_utils.go +++ b/pkg/controller/job/indexed_job_utils.go @@ -311,6 +311,14 @@ func addCompletionIndexAnnotation(template *v1.PodTemplateSpec, index int) { template.Annotations[batch.JobCompletionIndexAnnotation] = strconv.Itoa(index) } +func addCompletionIndexLabel(template *v1.PodTemplateSpec, index int) { + if template.Labels == nil { + template.Labels = make(map[string]string, 1) + } + // Use completion index annotation as label as well for consistency. + template.Labels[batch.JobCompletionIndexAnnotation] = strconv.Itoa(index) +} + func podGenerateNameWithIndex(jobName string, index int) string { appendIndex := "-" + strconv.Itoa(index) + "-" generateNamePrefix := jobName + appendIndex diff --git a/pkg/controller/job/job_controller.go b/pkg/controller/job/job_controller.go index 888cb7de6b1..cce20c08b5f 100644 --- a/pkg/controller/job/job_controller.go +++ b/pkg/controller/job/job_controller.go @@ -1482,6 +1482,7 @@ func (jm *Controller) manageJob(ctx context.Context, job *batch.Job, activePods if completionIndex != unknownCompletionIndex { template = podTemplate.DeepCopy() addCompletionIndexAnnotation(template, completionIndex) + addCompletionIndexLabel(template, completionIndex) template.Spec.Hostname = fmt.Sprintf("%s-%d", job.Name, completionIndex) generateName = podGenerateNameWithIndex(job.Name, completionIndex) } diff --git a/pkg/controller/job/job_controller_test.go b/pkg/controller/job/job_controller_test.go index e583e466563..8087c16e8d2 100644 --- a/pkg/controller/job/job_controller_test.go +++ b/pkg/controller/job/job_controller_test.go @@ -962,6 +962,7 @@ func checkIndexedJobPods(t *testing.T, control *controller.FakePodControl, wantI gotIndexes := sets.New[int]() for _, p := range control.Templates { checkJobCompletionEnvVariable(t, &p.Spec) + checkJobCompletionLabel(t, &p) ix := getCompletionIndex(p.Annotations) if ix == -1 { t.Errorf("Created pod %s didn't have completion index", p.Name) @@ -4394,6 +4395,13 @@ func TestFinalizersRemovedExpectations(t *testing.T) { t.Errorf("Timeout waiting for expectations (-want, +got):\n%s", diff) } } +func checkJobCompletionLabel(t *testing.T, p *v1.PodTemplateSpec) { + t.Helper() + labels := p.GetLabels() + if labels == nil || labels[batch.JobCompletionIndexAnnotation] == "" { + t.Errorf("missing expected pod label %s", batch.JobCompletionIndexAnnotation) + } +} func checkJobCompletionEnvVariable(t *testing.T, spec *v1.PodSpec) { t.Helper()