add completion index as pod label

This commit is contained in:
Daniel Vega-Myhre 2023-06-26 19:53:14 +00:00
parent 96d853f4b8
commit 2176053415
3 changed files with 17 additions and 0 deletions

View File

@ -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

View File

@ -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)
}

View File

@ -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()