Change:
<!-- Please use this template while reporting a bug and provide as much info as possible. Not doing so may result in your bug not being addressed in a timely manner. Thanks! If the matter is security related, please disclose it privately via --What happened: I create a job with initcontainer with restartPolicy : when the initContainer exec failed and it will be restarted, the pod Phase will be pending all the time. but in JobController, the only care the running pod. So this pod will be always restarted. What you expected to happen: when restart count to backoffLimit, the job failed. How to reproduce it (as minimally and precisely as possible): Anything else we need to know?: Environment: Kubernetes version (use ): Cloud provider or hardware configuration: OS (e.g: ): Kernel (e.g. ): Install tools: Network plugin and version (if this is a network-related bug): Others:
/sig apps
/assign

Discussion:
result := int32(0) for i := range pods { po := pods[i] <del> if po.Status.Phase != v1.PodRunning { continue } for j := range po.Status.InitContainerStatuses { stat := po.Status.InitContainerStatuses[j] result += stat.RestartCount } for j := range po.Status.ContainerStatuses { stat := po.Status.ContainerStatuses[j] result += stat.RestartCount </del> <ins> if po.Status.Phase == v1.PodRunning || po.Status.Phase == v1.PodPending { for j := range po.Status.InitContainerStatuses { stat := po.Status.InitContainerStatuses[j] result += stat.RestartCount } for j := range po.Status.ContainerStatuses { stat := po.Status.ContainerStatuses[j] result += stat.RestartCount } </ins> } } if *job.Spec.BackoffLimit == 0 {

// pod setup jobKeyForget  bool restartCounts []int32 <ins> podPhase      v1.PodPhase </ins> // expectations expectedActive          int32

}{ "backoffLimit 0 should have 1 pod active": { 1, 1, 0, <del> true, []int32{0}, </del> <ins> true, []int32{0}, v1.PodRunning, </ins> 1, 0, 0, nil, "", }, "backoffLimit 1 with restartCount 0 should have 1 pod active": { 1, 1, 1, <del> true, []int32{0}, </del> <ins> true, []int32{0}, v1.PodRunning, </ins> 1, 0, 0, nil, "", }, <del> "backoffLimit 1 with restartCount 1 should have 0 pod active": { </del> <ins> "backoffLimit 1 with restartCount 1 and podRunning should have 0 pod active": { </ins> 1, 1, 1, <del> true, []int32{1}, </del> <ins> true, []int32{1}, v1.PodRunning, </ins> 0, 0, 1, &jobConditionFailed, "BackoffLimitExceeded", }, <del> "too many job failures - single pod": { </del> <ins> "backoffLimit 1 with restartCount 1 and podPending should have 0 pod active": { 1, 1, 1, true, []int32{1}, v1.PodPending, 0, 0, 1, &jobConditionFailed, "BackoffLimitExceeded", }, "too many job failures with podRunning - single pod": { </ins> 1, 5, 2, <del> true, []int32{2}, </del> <ins> true, []int32{2}, v1.PodRunning, </ins> 0, 0, 1, &jobConditionFailed, "BackoffLimitExceeded", }, <del> "too many job failures - multiple pods": { </del> <ins> "too many job failures with podPending - single pod": { 1, 5, 2, true, []int32{2}, v1.PodPending, 0, 0, 1, &jobConditionFailed, "BackoffLimitExceeded", }, "too many job failures with podRunning - multiple pods": { 2, 5, 2, true, []int32{1, 1}, v1.PodRunning, 0, 0, 2, &jobConditionFailed, "BackoffLimitExceeded", }, "too many job failures with podPending - multiple pods": { </ins> 2, 5, 2, <del> true, []int32{1, 1}, </del> <ins> true, []int32{1, 1}, v1.PodPending, </ins> 0, 0, 2, &jobConditionFailed, "BackoffLimitExceeded", }, "not enough failures": { 2, 5, 3, <del> true, []int32{1, 1}, </del> <ins> true, []int32{1, 1}, v1.PodRunning, </ins> 2, 0, 0, nil, "", }, }

job.Spec.Template.Spec.RestartPolicy = v1.RestartPolicyOnFailure sharedInformerFactory.Batch().V1().Jobs().Informer().GetIndexer().Add(job) podIndexer := sharedInformerFactory.Core().V1().Pods().Informer().GetIndexer() <del> for i, pod := range newPodList(int32(len(tc.restartCounts)), v1.PodRunning, job) { </del> <ins> for i, pod := range newPodList(int32(len(tc.restartCounts)), tc.podPhase, job) { </ins> pod.Status.ContainerStatuses = []v1.ContainerStatus{{RestartCount: tc.restartCounts[i]}} podIndexer.Add(&pod) }