2019-01-12 04:58:27 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package job
|
|
|
|
|
|
|
|
import (
|
2020-03-26 21:07:15 +00:00
|
|
|
"context"
|
2019-01-12 04:58:27 +00:00
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"sync"
|
2021-03-18 22:40:29 +00:00
|
|
|
"sync/atomic"
|
2019-01-12 04:58:27 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
batch "k8s.io/api/batch/v1"
|
|
|
|
"k8s.io/api/core/v1"
|
2020-08-10 17:43:49 +00:00
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
2019-01-12 04:58:27 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2021-07-02 08:43:15 +00:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
"k8s.io/apimachinery/pkg/util/json"
|
2019-01-12 04:58:27 +00:00
|
|
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
2021-07-02 08:43:15 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2019-01-12 04:58:27 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2021-07-02 08:43:15 +00:00
|
|
|
"k8s.io/apiserver/pkg/util/feature"
|
2019-01-12 04:58:27 +00:00
|
|
|
batchinformers "k8s.io/client-go/informers/batch/v1"
|
|
|
|
coreinformers "k8s.io/client-go/informers/core/v1"
|
|
|
|
clientset "k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/kubernetes/scheme"
|
|
|
|
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
|
|
|
|
batchv1listers "k8s.io/client-go/listers/batch/v1"
|
|
|
|
corelisters "k8s.io/client-go/listers/core/v1"
|
|
|
|
"k8s.io/client-go/tools/cache"
|
|
|
|
"k8s.io/client-go/tools/record"
|
|
|
|
"k8s.io/client-go/util/workqueue"
|
2019-12-12 01:27:03 +00:00
|
|
|
"k8s.io/component-base/metrics/prometheus/ratelimiter"
|
2020-08-10 17:43:49 +00:00
|
|
|
"k8s.io/klog/v2"
|
2019-01-12 04:58:27 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller"
|
2021-07-02 08:43:15 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller/job/metrics"
|
2021-03-18 22:40:29 +00:00
|
|
|
"k8s.io/kubernetes/pkg/features"
|
2019-04-07 17:07:55 +00:00
|
|
|
"k8s.io/utils/integer"
|
2019-01-12 04:58:27 +00:00
|
|
|
)
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
const (
|
|
|
|
statusUpdateRetries = 3
|
|
|
|
|
|
|
|
// maxUncountedPods is the maximum size the slices in
|
|
|
|
// .status.uncountedTerminatedPods should have to keep their representation
|
|
|
|
// roughly below 20 KB.
|
|
|
|
maxUncountedPods = 500
|
|
|
|
)
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
// controllerKind contains the schema.GroupVersionKind for this controller type.
|
|
|
|
var controllerKind = batch.SchemeGroupVersion.WithKind("Job")
|
|
|
|
|
|
|
|
var (
|
2020-08-10 17:43:49 +00:00
|
|
|
// DefaultJobBackOff is the default backoff period, exported for the e2e test
|
2019-01-12 04:58:27 +00:00
|
|
|
DefaultJobBackOff = 10 * time.Second
|
|
|
|
// MaxJobBackOff is the max backoff period, exported for the e2e test
|
2021-07-02 08:43:15 +00:00
|
|
|
MaxJobBackOff = 360 * time.Second
|
|
|
|
maxPodCreateDeletePerSync = 500
|
2019-01-12 04:58:27 +00:00
|
|
|
)
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
// Controller ensures that all Job objects have corresponding pods to
|
|
|
|
// run their configured workload.
|
|
|
|
type Controller struct {
|
2019-01-12 04:58:27 +00:00
|
|
|
kubeClient clientset.Interface
|
|
|
|
podControl controller.PodControlInterface
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
// To allow injection of the following for testing.
|
|
|
|
updateStatusHandler func(job *batch.Job) error
|
|
|
|
patchJobHandler func(job *batch.Job, patch []byte) error
|
|
|
|
syncHandler func(jobKey string) (bool, error)
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
// podStoreSynced returns true if the pod store has been synced at least once.
|
|
|
|
// Added as a member to the struct to allow injection for testing.
|
|
|
|
podStoreSynced cache.InformerSynced
|
|
|
|
// jobStoreSynced returns true if the job store has been synced at least once.
|
|
|
|
// Added as a member to the struct to allow injection for testing.
|
|
|
|
jobStoreSynced cache.InformerSynced
|
|
|
|
|
|
|
|
// A TTLCache of pod creates/deletes each rc expects to see
|
|
|
|
expectations controller.ControllerExpectationsInterface
|
|
|
|
|
|
|
|
// A store of jobs
|
|
|
|
jobLister batchv1listers.JobLister
|
|
|
|
|
|
|
|
// A store of pods, populated by the podController
|
|
|
|
podStore corelisters.PodLister
|
|
|
|
|
|
|
|
// Jobs that need to be updated
|
|
|
|
queue workqueue.RateLimitingInterface
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
// Orphan deleted pods that still have a Job tracking finalizer to be removed
|
|
|
|
orphanQueue workqueue.RateLimitingInterface
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
recorder record.EventRecorder
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
// NewController creates a new Job controller that keeps the relevant pods
|
|
|
|
// in sync with their corresponding Job objects.
|
|
|
|
func NewController(podInformer coreinformers.PodInformer, jobInformer batchinformers.JobInformer, kubeClient clientset.Interface) *Controller {
|
2019-01-12 04:58:27 +00:00
|
|
|
eventBroadcaster := record.NewBroadcaster()
|
2020-08-10 17:43:49 +00:00
|
|
|
eventBroadcaster.StartStructuredLogging(0)
|
2019-01-12 04:58:27 +00:00
|
|
|
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: kubeClient.CoreV1().Events("")})
|
|
|
|
|
|
|
|
if kubeClient != nil && kubeClient.CoreV1().RESTClient().GetRateLimiter() != nil {
|
2019-12-12 01:27:03 +00:00
|
|
|
ratelimiter.RegisterMetricAndTrackRateLimiterUsage("job_controller", kubeClient.CoreV1().RESTClient().GetRateLimiter())
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
jm := &Controller{
|
2019-01-12 04:58:27 +00:00
|
|
|
kubeClient: kubeClient,
|
|
|
|
podControl: controller.RealPodControl{
|
|
|
|
KubeClient: kubeClient,
|
|
|
|
Recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "job-controller"}),
|
|
|
|
},
|
|
|
|
expectations: controller.NewControllerExpectations(),
|
|
|
|
queue: workqueue.NewNamedRateLimitingQueue(workqueue.NewItemExponentialFailureRateLimiter(DefaultJobBackOff, MaxJobBackOff), "job"),
|
2021-07-02 08:43:15 +00:00
|
|
|
orphanQueue: workqueue.NewNamedRateLimitingQueue(workqueue.NewItemExponentialFailureRateLimiter(DefaultJobBackOff, MaxJobBackOff), "job_orphan_pod"),
|
2019-01-12 04:58:27 +00:00
|
|
|
recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "job-controller"}),
|
|
|
|
}
|
|
|
|
|
|
|
|
jobInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: func(obj interface{}) {
|
|
|
|
jm.enqueueController(obj, true)
|
|
|
|
},
|
|
|
|
UpdateFunc: jm.updateJob,
|
|
|
|
DeleteFunc: func(obj interface{}) {
|
|
|
|
jm.enqueueController(obj, true)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
jm.jobLister = jobInformer.Lister()
|
|
|
|
jm.jobStoreSynced = jobInformer.Informer().HasSynced
|
|
|
|
|
|
|
|
podInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: jm.addPod,
|
|
|
|
UpdateFunc: jm.updatePod,
|
|
|
|
DeleteFunc: jm.deletePod,
|
|
|
|
})
|
|
|
|
jm.podStore = podInformer.Lister()
|
|
|
|
jm.podStoreSynced = podInformer.Informer().HasSynced
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
jm.updateStatusHandler = jm.updateJobStatus
|
|
|
|
jm.patchJobHandler = jm.patchJob
|
2019-01-12 04:58:27 +00:00
|
|
|
jm.syncHandler = jm.syncJob
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
metrics.Register()
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
return jm
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the main goroutine responsible for watching and syncing jobs.
|
2020-08-10 17:43:49 +00:00
|
|
|
func (jm *Controller) Run(workers int, stopCh <-chan struct{}) {
|
2019-01-12 04:58:27 +00:00
|
|
|
defer utilruntime.HandleCrash()
|
|
|
|
defer jm.queue.ShutDown()
|
2021-07-02 08:43:15 +00:00
|
|
|
defer jm.orphanQueue.ShutDown()
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
klog.Infof("Starting job controller")
|
|
|
|
defer klog.Infof("Shutting down job controller")
|
|
|
|
|
2019-09-27 21:51:53 +00:00
|
|
|
if !cache.WaitForNamedCacheSync("job", stopCh, jm.podStoreSynced, jm.jobStoreSynced) {
|
2019-01-12 04:58:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < workers; i++ {
|
|
|
|
go wait.Until(jm.worker, time.Second, stopCh)
|
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
go wait.Until(jm.orphanWorker, time.Second, stopCh)
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
<-stopCh
|
|
|
|
}
|
|
|
|
|
|
|
|
// getPodJobs returns a list of Jobs that potentially match a Pod.
|
2020-08-10 17:43:49 +00:00
|
|
|
func (jm *Controller) getPodJobs(pod *v1.Pod) []*batch.Job {
|
2019-01-12 04:58:27 +00:00
|
|
|
jobs, err := jm.jobLister.GetPodJobs(pod)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(jobs) > 1 {
|
|
|
|
// ControllerRef will ensure we don't do anything crazy, but more than one
|
|
|
|
// item in this list nevertheless constitutes user error.
|
|
|
|
utilruntime.HandleError(fmt.Errorf("user error! more than one job is selecting pods with labels: %+v", pod.Labels))
|
|
|
|
}
|
|
|
|
ret := make([]*batch.Job, 0, len(jobs))
|
|
|
|
for i := range jobs {
|
|
|
|
ret = append(ret, &jobs[i])
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolveControllerRef returns the controller referenced by a ControllerRef,
|
|
|
|
// or nil if the ControllerRef could not be resolved to a matching controller
|
|
|
|
// of the correct Kind.
|
2020-08-10 17:43:49 +00:00
|
|
|
func (jm *Controller) resolveControllerRef(namespace string, controllerRef *metav1.OwnerReference) *batch.Job {
|
2019-01-12 04:58:27 +00:00
|
|
|
// We can't look up by UID, so look up by Name and then verify UID.
|
|
|
|
// Don't even try to look up by Name if it's the wrong Kind.
|
|
|
|
if controllerRef.Kind != controllerKind.Kind {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
job, err := jm.jobLister.Jobs(namespace).Get(controllerRef.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if job.UID != controllerRef.UID {
|
|
|
|
// The controller we found with this Name is not the same one that the
|
|
|
|
// ControllerRef points to.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return job
|
|
|
|
}
|
|
|
|
|
|
|
|
// When a pod is created, enqueue the controller that manages it and update it's expectations.
|
2020-08-10 17:43:49 +00:00
|
|
|
func (jm *Controller) addPod(obj interface{}) {
|
2019-01-12 04:58:27 +00:00
|
|
|
pod := obj.(*v1.Pod)
|
|
|
|
if pod.DeletionTimestamp != nil {
|
2020-08-10 17:43:49 +00:00
|
|
|
// on a restart of the controller, it's possible a new pod shows up in a state that
|
2019-01-12 04:58:27 +00:00
|
|
|
// is already pending deletion. Prevent the pod from being a creation observation.
|
|
|
|
jm.deletePod(pod)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it has a ControllerRef, that's all that matters.
|
|
|
|
if controllerRef := metav1.GetControllerOf(pod); controllerRef != nil {
|
|
|
|
job := jm.resolveControllerRef(pod.Namespace, controllerRef)
|
|
|
|
if job == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jobKey, err := controller.KeyFunc(job)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jm.expectations.CreationObserved(jobKey)
|
|
|
|
jm.enqueueController(job, true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, it's an orphan. Get a list of all matching controllers and sync
|
|
|
|
// them to see if anyone wants to adopt it.
|
|
|
|
// DO NOT observe creation because no controller should be waiting for an
|
|
|
|
// orphan.
|
|
|
|
for _, job := range jm.getPodJobs(pod) {
|
|
|
|
jm.enqueueController(job, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// When a pod is updated, figure out what job/s manage it and wake them up.
|
|
|
|
// If the labels of the pod have changed we need to awaken both the old
|
|
|
|
// and new job. old and cur must be *v1.Pod types.
|
2020-08-10 17:43:49 +00:00
|
|
|
func (jm *Controller) updatePod(old, cur interface{}) {
|
2019-01-12 04:58:27 +00:00
|
|
|
curPod := cur.(*v1.Pod)
|
|
|
|
oldPod := old.(*v1.Pod)
|
|
|
|
if curPod.ResourceVersion == oldPod.ResourceVersion {
|
|
|
|
// Periodic resync will send update events for all known pods.
|
|
|
|
// Two different versions of the same pod will always have different RVs.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if curPod.DeletionTimestamp != nil {
|
|
|
|
// when a pod is deleted gracefully it's deletion timestamp is first modified to reflect a grace period,
|
|
|
|
// and after such time has passed, the kubelet actually deletes it from the store. We receive an update
|
|
|
|
// for modification of the deletion timestamp and expect an job to create more pods asap, not wait
|
|
|
|
// until the kubelet actually deletes the pod.
|
|
|
|
jm.deletePod(curPod)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// the only time we want the backoff to kick-in, is when the pod failed
|
|
|
|
immediate := curPod.Status.Phase != v1.PodFailed
|
|
|
|
|
|
|
|
curControllerRef := metav1.GetControllerOf(curPod)
|
|
|
|
oldControllerRef := metav1.GetControllerOf(oldPod)
|
|
|
|
controllerRefChanged := !reflect.DeepEqual(curControllerRef, oldControllerRef)
|
|
|
|
if controllerRefChanged && oldControllerRef != nil {
|
|
|
|
// The ControllerRef was changed. Sync the old controller, if any.
|
|
|
|
if job := jm.resolveControllerRef(oldPod.Namespace, oldControllerRef); job != nil {
|
|
|
|
jm.enqueueController(job, immediate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it has a ControllerRef, that's all that matters.
|
|
|
|
if curControllerRef != nil {
|
|
|
|
job := jm.resolveControllerRef(curPod.Namespace, curControllerRef)
|
|
|
|
if job == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jm.enqueueController(job, immediate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, it's an orphan. If anything changed, sync matching controllers
|
|
|
|
// to see if anyone wants to adopt it now.
|
|
|
|
labelChanged := !reflect.DeepEqual(curPod.Labels, oldPod.Labels)
|
|
|
|
if labelChanged || controllerRefChanged {
|
|
|
|
for _, job := range jm.getPodJobs(curPod) {
|
|
|
|
jm.enqueueController(job, immediate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// When a pod is deleted, enqueue the job that manages the pod and update its expectations.
|
2021-07-02 08:43:15 +00:00
|
|
|
// obj could be an *v1.Pod, or a DeleteFinalStateUnknown marker item.
|
2020-08-10 17:43:49 +00:00
|
|
|
func (jm *Controller) deletePod(obj interface{}) {
|
2019-01-12 04:58:27 +00:00
|
|
|
pod, ok := obj.(*v1.Pod)
|
|
|
|
|
|
|
|
// When a delete is dropped, the relist will notice a pod in the store not
|
|
|
|
// in the list, leading to the insertion of a tombstone object which contains
|
|
|
|
// the deleted key/value. Note that this value might be stale. If the pod
|
|
|
|
// changed labels the new job will not be woken up till the periodic resync.
|
|
|
|
if !ok {
|
|
|
|
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("couldn't get object from tombstone %+v", obj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pod, ok = tombstone.Obj.(*v1.Pod)
|
|
|
|
if !ok {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("tombstone contained object that is not a pod %+v", obj))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
controllerRef := metav1.GetControllerOf(pod)
|
|
|
|
if controllerRef == nil {
|
|
|
|
// No controller should care about orphans being deleted.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
job := jm.resolveControllerRef(pod.Namespace, controllerRef)
|
|
|
|
if job == nil {
|
2021-07-02 08:43:15 +00:00
|
|
|
if hasJobTrackingFinalizer(pod) {
|
|
|
|
jm.enqueueOrphanPod(pod)
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
jobKey, err := controller.KeyFunc(job)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jm.expectations.DeletionObserved(jobKey)
|
|
|
|
jm.enqueueController(job, true)
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func (jm *Controller) updateJob(old, cur interface{}) {
|
2019-01-12 04:58:27 +00:00
|
|
|
oldJob := old.(*batch.Job)
|
|
|
|
curJob := cur.(*batch.Job)
|
|
|
|
|
|
|
|
// never return error
|
|
|
|
key, err := controller.KeyFunc(curJob)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jm.enqueueController(curJob, true)
|
|
|
|
// check if need to add a new rsync for ActiveDeadlineSeconds
|
|
|
|
if curJob.Status.StartTime != nil {
|
|
|
|
curADS := curJob.Spec.ActiveDeadlineSeconds
|
|
|
|
if curADS == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
oldADS := oldJob.Spec.ActiveDeadlineSeconds
|
|
|
|
if oldADS == nil || *oldADS != *curADS {
|
|
|
|
now := metav1.Now()
|
|
|
|
start := curJob.Status.StartTime.Time
|
|
|
|
passed := now.Time.Sub(start)
|
|
|
|
total := time.Duration(*curADS) * time.Second
|
|
|
|
// AddAfter will handle total < passed
|
|
|
|
jm.queue.AddAfter(key, total-passed)
|
2021-03-18 22:40:29 +00:00
|
|
|
klog.V(4).Infof("job %q ActiveDeadlineSeconds updated, will rsync after %d seconds", key, total-passed)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// obj could be an *batch.Job, or a DeletionFinalStateUnknown marker item,
|
|
|
|
// immediate tells the controller to update the status right away, and should
|
|
|
|
// happen ONLY when there was a successful pod run.
|
2020-08-10 17:43:49 +00:00
|
|
|
func (jm *Controller) enqueueController(obj interface{}, immediate bool) {
|
2019-01-12 04:58:27 +00:00
|
|
|
key, err := controller.KeyFunc(obj)
|
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("Couldn't get key for object %+v: %v", obj, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
backoff := time.Duration(0)
|
|
|
|
if !immediate {
|
|
|
|
backoff = getBackoff(jm.queue, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Handle overlapping controllers better. Either disallow them at admission time or
|
|
|
|
// deterministically avoid syncing controllers that fight over pods. Currently, we only
|
|
|
|
// ensure that the same controller is synced for a given pod. When we periodically relist
|
|
|
|
// all controllers there will still be some replica instability. One way to handle this is
|
|
|
|
// by querying the store for all controllers that this rc overlaps, as well as all
|
|
|
|
// controllers that overlap this rc, and sorting them.
|
2021-07-02 08:43:15 +00:00
|
|
|
klog.Infof("enqueueing job %s", key)
|
2019-01-12 04:58:27 +00:00
|
|
|
jm.queue.AddAfter(key, backoff)
|
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
func (jm *Controller) enqueueOrphanPod(obj *v1.Pod) {
|
|
|
|
key, err := controller.KeyFunc(obj)
|
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("couldn't get key for object %+v: %v", obj, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jm.orphanQueue.Add(key)
|
|
|
|
}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
// worker runs a worker thread that just dequeues items, processes them, and marks them done.
|
|
|
|
// It enforces that the syncHandler is never invoked concurrently with the same key.
|
2020-08-10 17:43:49 +00:00
|
|
|
func (jm *Controller) worker() {
|
2019-01-12 04:58:27 +00:00
|
|
|
for jm.processNextWorkItem() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func (jm *Controller) processNextWorkItem() bool {
|
2019-01-12 04:58:27 +00:00
|
|
|
key, quit := jm.queue.Get()
|
|
|
|
if quit {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer jm.queue.Done(key)
|
|
|
|
|
|
|
|
forget, err := jm.syncHandler(key.(string))
|
|
|
|
if err == nil {
|
|
|
|
if forget {
|
|
|
|
jm.queue.Forget(key)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
utilruntime.HandleError(fmt.Errorf("Error syncing job: %v", err))
|
|
|
|
jm.queue.AddRateLimited(key)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
func (jm *Controller) orphanWorker() {
|
|
|
|
for jm.processNextOrphanPod() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jm Controller) processNextOrphanPod() bool {
|
|
|
|
key, quit := jm.orphanQueue.Get()
|
|
|
|
if quit {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer jm.orphanQueue.Done(key)
|
|
|
|
err := jm.syncOrphanPod(key.(string))
|
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("Error syncing orphan pod: %v", err))
|
|
|
|
jm.orphanQueue.AddRateLimited(key)
|
|
|
|
} else {
|
|
|
|
jm.orphanQueue.Forget(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// syncOrphanPod removes the tracking finalizer from an orphan pod if found.
|
|
|
|
func (jm Controller) syncOrphanPod(key string) error {
|
|
|
|
startTime := time.Now()
|
|
|
|
defer func() {
|
|
|
|
klog.V(4).Infof("Finished syncing orphan pod %q (%v)", key, time.Since(startTime))
|
|
|
|
}()
|
|
|
|
|
|
|
|
ns, name, err := cache.SplitMetaNamespaceKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sharedPod, err := jm.podStore.Pods(ns).Get(name)
|
|
|
|
if err != nil {
|
|
|
|
if apierrors.IsNotFound(err) {
|
|
|
|
klog.V(4).Infof("Orphan pod has been deleted: %v", key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if patch := removeTrackingFinalizerPatch(sharedPod); patch != nil {
|
|
|
|
if err := jm.podControl.PatchPod(ns, name, patch); err != nil && !apierrors.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
// getPodsForJob returns the set of pods that this Job should manage.
|
2021-07-02 08:43:15 +00:00
|
|
|
// It also reconciles ControllerRef by adopting/orphaning, adding tracking
|
|
|
|
// finalizers, if enabled.
|
2019-01-12 04:58:27 +00:00
|
|
|
// Note that the returned Pods are pointers into the cache.
|
2021-07-02 08:43:15 +00:00
|
|
|
func (jm *Controller) getPodsForJob(j *batch.Job, withFinalizers bool) ([]*v1.Pod, error) {
|
2019-01-12 04:58:27 +00:00
|
|
|
selector, err := metav1.LabelSelectorAsSelector(j.Spec.Selector)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't convert Job selector: %v", err)
|
|
|
|
}
|
|
|
|
// List all pods to include those that don't match the selector anymore
|
|
|
|
// but have a ControllerRef pointing to this controller.
|
|
|
|
pods, err := jm.podStore.Pods(j.Namespace).List(labels.Everything())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// If any adoptions are attempted, we should first recheck for deletion
|
|
|
|
// with an uncached quorum read sometime after listing Pods (see #42639).
|
|
|
|
canAdoptFunc := controller.RecheckDeletionTimestamp(func() (metav1.Object, error) {
|
2020-03-26 21:07:15 +00:00
|
|
|
fresh, err := jm.kubeClient.BatchV1().Jobs(j.Namespace).Get(context.TODO(), j.Name, metav1.GetOptions{})
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if fresh.UID != j.UID {
|
|
|
|
return nil, fmt.Errorf("original Job %v/%v is gone: got uid %v, wanted %v", j.Namespace, j.Name, fresh.UID, j.UID)
|
|
|
|
}
|
|
|
|
return fresh, nil
|
|
|
|
})
|
2021-07-02 08:43:15 +00:00
|
|
|
var finalizers []string
|
|
|
|
if withFinalizers {
|
|
|
|
finalizers = append(finalizers, batch.JobTrackingFinalizer)
|
|
|
|
}
|
|
|
|
cm := controller.NewPodControllerRefManager(jm.podControl, j, selector, controllerKind, canAdoptFunc, finalizers...)
|
|
|
|
// When adopting Pods, this operation adds an ownerRef and finalizers.
|
|
|
|
pods, err = cm.ClaimPods(pods)
|
|
|
|
if err != nil || !withFinalizers {
|
|
|
|
return pods, err
|
|
|
|
}
|
|
|
|
// Set finalizer on adopted pods for the remaining calculations.
|
|
|
|
for i, p := range pods {
|
|
|
|
adopted := true
|
|
|
|
for _, r := range p.OwnerReferences {
|
|
|
|
if r.UID == j.UID {
|
|
|
|
adopted = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if adopted && !hasJobTrackingFinalizer(p) {
|
|
|
|
pods[i] = p.DeepCopy()
|
|
|
|
pods[i].Finalizers = append(p.Finalizers, batch.JobTrackingFinalizer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pods, err
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// syncJob will sync the job with the given key if it has had its expectations fulfilled, meaning
|
|
|
|
// it did not expect to see any more of its pods created or deleted. This function is not meant to be invoked
|
|
|
|
// concurrently with the same key.
|
2021-07-02 08:43:15 +00:00
|
|
|
func (jm *Controller) syncJob(key string) (forget bool, rErr error) {
|
2019-01-12 04:58:27 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
defer func() {
|
|
|
|
klog.V(4).Infof("Finished syncing job %q (%v)", key, time.Since(startTime))
|
|
|
|
}()
|
|
|
|
|
|
|
|
ns, name, err := cache.SplitMetaNamespaceKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if len(ns) == 0 || len(name) == 0 {
|
|
|
|
return false, fmt.Errorf("invalid job key %q: either namespace or name is missing", key)
|
|
|
|
}
|
|
|
|
sharedJob, err := jm.jobLister.Jobs(ns).Get(name)
|
|
|
|
if err != nil {
|
2021-03-18 22:40:29 +00:00
|
|
|
if apierrors.IsNotFound(err) {
|
2019-01-12 04:58:27 +00:00
|
|
|
klog.V(4).Infof("Job has been deleted: %v", key)
|
|
|
|
jm.expectations.DeleteExpectations(key)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
2021-03-18 22:40:29 +00:00
|
|
|
// make a copy so we don't mutate the shared cache
|
|
|
|
job := *sharedJob.DeepCopy()
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
// if job was finished previously, we don't want to redo the termination
|
|
|
|
if IsJobFinished(&job) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
// Cannot create Pods if this is an Indexed Job and the feature is disabled.
|
2021-07-02 08:43:15 +00:00
|
|
|
if !feature.DefaultFeatureGate.Enabled(features.IndexedJob) && isIndexedJob(&job) {
|
2021-03-18 22:40:29 +00:00
|
|
|
jm.recorder.Event(&job, v1.EventTypeWarning, "IndexedJobDisabled", "Skipped Indexed Job sync because feature is disabled.")
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if job.Spec.CompletionMode != nil && *job.Spec.CompletionMode != batch.NonIndexedCompletion && *job.Spec.CompletionMode != batch.IndexedCompletion {
|
|
|
|
jm.recorder.Event(&job, v1.EventTypeWarning, "UnknownCompletionMode", "Skipped Job sync because completion mode is unknown")
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
completionMode := string(batch.NonIndexedCompletion)
|
|
|
|
if isIndexedJob(&job) {
|
|
|
|
completionMode = string(batch.IndexedCompletion)
|
|
|
|
}
|
|
|
|
action := metrics.JobSyncActionReconciling
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
result := "success"
|
|
|
|
if rErr != nil {
|
|
|
|
result = "error"
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics.JobSyncDurationSeconds.WithLabelValues(completionMode, result, action).Observe(time.Since(startTime).Seconds())
|
|
|
|
metrics.JobSyncNum.WithLabelValues(completionMode, result, action).Inc()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var uncounted *uncountedTerminatedPods
|
|
|
|
if trackingUncountedPods(&job) {
|
|
|
|
klog.V(4).InfoS("Tracking uncounted Pods with pod finalizers", "job", klog.KObj(&job))
|
|
|
|
if job.Status.UncountedTerminatedPods == nil {
|
|
|
|
job.Status.UncountedTerminatedPods = &batch.UncountedTerminatedPods{}
|
|
|
|
}
|
|
|
|
uncounted = newUncountedTerminatedPods(*job.Status.UncountedTerminatedPods)
|
|
|
|
} else if patch := removeTrackingAnnotationPatch(&job); patch != nil {
|
|
|
|
if err := jm.patchJobHandler(&job, patch); err != nil {
|
|
|
|
return false, fmt.Errorf("removing tracking finalizer from job %s: %w", key, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
// Check the expectations of the job before counting active pods, otherwise a new pod can sneak in
|
|
|
|
// and update the expectations after we've retrieved active pods from the store. If a new pod enters
|
|
|
|
// the store after we've checked the expectation, the job sync is just deferred till the next relist.
|
|
|
|
jobNeedsSync := jm.expectations.SatisfiedExpectations(key)
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
pods, err := jm.getPodsForJob(&job, uncounted != nil)
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-07-02 08:43:15 +00:00
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
activePods := controller.FilterActivePods(pods)
|
|
|
|
active := int32(len(activePods))
|
2021-07-02 08:43:15 +00:00
|
|
|
succeeded, failed := getStatus(&job, pods, uncounted)
|
2021-03-18 22:40:29 +00:00
|
|
|
// Job first start. Set StartTime and start the ActiveDeadlineSeconds timer
|
|
|
|
// only if the job is not in the suspended state.
|
|
|
|
if job.Status.StartTime == nil && !jobSuspended(&job) {
|
2019-01-12 04:58:27 +00:00
|
|
|
now := metav1.Now()
|
|
|
|
job.Status.StartTime = &now
|
|
|
|
// enqueue a sync to check if job past ActiveDeadlineSeconds
|
|
|
|
if job.Spec.ActiveDeadlineSeconds != nil {
|
2020-03-26 21:07:15 +00:00
|
|
|
klog.V(4).Infof("Job %s has ActiveDeadlineSeconds will sync after %d seconds",
|
2019-01-12 04:58:27 +00:00
|
|
|
key, *job.Spec.ActiveDeadlineSeconds)
|
|
|
|
jm.queue.AddAfter(key, time.Duration(*job.Spec.ActiveDeadlineSeconds)*time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var manageJobErr error
|
2021-07-02 08:43:15 +00:00
|
|
|
var finishedCondition *batch.JobCondition
|
2019-01-12 04:58:27 +00:00
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
jobHasNewFailure := failed > job.Status.Failed
|
2019-01-12 04:58:27 +00:00
|
|
|
// new failures happen when status does not reflect the failures and active
|
|
|
|
// is different than parallelism, otherwise the previous controller loop
|
|
|
|
// failed updating status so even if we pick up failure it is not a new one
|
2021-07-02 08:43:15 +00:00
|
|
|
exceedsBackoffLimit := jobHasNewFailure && (active != *job.Spec.Parallelism) &&
|
2020-12-01 01:06:26 +00:00
|
|
|
(failed > *job.Spec.BackoffLimit)
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
if exceedsBackoffLimit || pastBackoffLimitOnFailure(&job, pods) {
|
|
|
|
// check if the number of pod restart exceeds backoff (for restart OnFailure only)
|
|
|
|
// OR if the number of failed jobs increased since the last syncJob
|
2021-07-02 08:43:15 +00:00
|
|
|
finishedCondition = newCondition(batch.JobFailed, v1.ConditionTrue, "BackoffLimitExceeded", "Job has reached the specified backoff limit")
|
2019-01-12 04:58:27 +00:00
|
|
|
} else if pastActiveDeadline(&job) {
|
2021-07-02 08:43:15 +00:00
|
|
|
finishedCondition = newCondition(batch.JobFailed, v1.ConditionTrue, "DeadlineExceeded", "Job was active longer than specified deadline")
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
var prevSucceededIndexes, succeededIndexes orderedIntervals
|
2021-03-18 22:40:29 +00:00
|
|
|
if isIndexedJob(&job) {
|
2021-07-02 08:43:15 +00:00
|
|
|
prevSucceededIndexes, succeededIndexes = calculateSucceededIndexes(&job, pods)
|
|
|
|
succeeded = int32(succeededIndexes.total())
|
|
|
|
}
|
|
|
|
suspendCondChanged := false
|
|
|
|
// Remove active pods if Job failed.
|
|
|
|
if finishedCondition != nil {
|
|
|
|
deleted, err := jm.deleteActivePods(&job, activePods)
|
|
|
|
if uncounted == nil {
|
|
|
|
// Legacy behavior: pretend all active pods were successfully removed.
|
|
|
|
deleted = active
|
|
|
|
} else if deleted != active {
|
|
|
|
// Can't declare the Job as finished yet, as there might be remaining
|
|
|
|
// pod finalizers.
|
|
|
|
finishedCondition = nil
|
|
|
|
}
|
|
|
|
active -= deleted
|
|
|
|
failed += deleted
|
|
|
|
manageJobErr = err
|
2019-01-12 04:58:27 +00:00
|
|
|
} else {
|
2021-07-02 08:43:15 +00:00
|
|
|
manageJobCalled := false
|
2019-01-12 04:58:27 +00:00
|
|
|
if jobNeedsSync && job.DeletionTimestamp == nil {
|
2021-07-02 08:43:15 +00:00
|
|
|
active, action, manageJobErr = jm.manageJob(&job, activePods, succeeded, succeededIndexes)
|
2021-03-18 22:40:29 +00:00
|
|
|
manageJobCalled = true
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
complete := false
|
|
|
|
if job.Spec.Completions == nil {
|
|
|
|
// This type of job is complete when any pod exits with success.
|
|
|
|
// Each pod is capable of
|
|
|
|
// determining whether or not the entire Job is done. Subsequent pods are
|
|
|
|
// not expected to fail, but if they do, the failure is ignored. Once any
|
|
|
|
// pod succeeds, the controller waits for remaining pods to finish, and
|
|
|
|
// then the job is complete.
|
2021-07-02 08:43:15 +00:00
|
|
|
complete = succeeded > 0 && active == 0
|
2019-01-12 04:58:27 +00:00
|
|
|
} else {
|
|
|
|
// Job specifies a number of completions. This type of job signals
|
|
|
|
// success by having that number of successes. Since we do not
|
|
|
|
// start more pods than there are remaining completions, there should
|
|
|
|
// not be any remaining active pods once this count is reached.
|
2021-07-02 08:43:15 +00:00
|
|
|
complete = succeeded >= *job.Spec.Completions && active == 0
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
if complete {
|
2021-07-02 08:43:15 +00:00
|
|
|
finishedCondition = newCondition(batch.JobComplete, v1.ConditionTrue, "", "")
|
|
|
|
} else if feature.DefaultFeatureGate.Enabled(features.SuspendJob) && manageJobCalled {
|
2021-03-18 22:40:29 +00:00
|
|
|
// Update the conditions / emit events only if manageJob was called in
|
|
|
|
// this syncJob. Otherwise wait for the right syncJob call to make
|
|
|
|
// updates.
|
|
|
|
if job.Spec.Suspend != nil && *job.Spec.Suspend {
|
|
|
|
// Job can be in the suspended state only if it is NOT completed.
|
|
|
|
var isUpdated bool
|
|
|
|
job.Status.Conditions, isUpdated = ensureJobConditionStatus(job.Status.Conditions, batch.JobSuspended, v1.ConditionTrue, "JobSuspended", "Job suspended")
|
|
|
|
if isUpdated {
|
2021-07-02 08:43:15 +00:00
|
|
|
suspendCondChanged = true
|
2021-03-18 22:40:29 +00:00
|
|
|
jm.recorder.Event(&job, v1.EventTypeNormal, "Suspended", "Job suspended")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Job not suspended.
|
|
|
|
var isUpdated bool
|
|
|
|
job.Status.Conditions, isUpdated = ensureJobConditionStatus(job.Status.Conditions, batch.JobSuspended, v1.ConditionFalse, "JobResumed", "Job resumed")
|
|
|
|
if isUpdated {
|
2021-07-02 08:43:15 +00:00
|
|
|
suspendCondChanged = true
|
2021-03-18 22:40:29 +00:00
|
|
|
jm.recorder.Event(&job, v1.EventTypeNormal, "Resumed", "Job resumed")
|
|
|
|
// Resumed jobs will always reset StartTime to current time. This is
|
|
|
|
// done because the ActiveDeadlineSeconds timer shouldn't go off
|
|
|
|
// whilst the Job is still suspended and resetting StartTime is
|
|
|
|
// consistent with resuming a Job created in the suspended state.
|
|
|
|
// (ActiveDeadlineSeconds is interpreted as the number of seconds a
|
|
|
|
// Job is continuously active.)
|
|
|
|
now := metav1.Now()
|
|
|
|
job.Status.StartTime = &now
|
|
|
|
}
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
forget = false
|
2019-01-12 04:58:27 +00:00
|
|
|
// Check if the number of jobs succeeded increased since the last check. If yes "forget" should be true
|
|
|
|
// This logic is linked to the issue: https://github.com/kubernetes/kubernetes/issues/56853 that aims to
|
|
|
|
// improve the Job backoff policy when parallelism > 1 and few Jobs failed but others succeed.
|
|
|
|
// In this case, we should clear the backoff delay.
|
|
|
|
if job.Status.Succeeded < succeeded {
|
|
|
|
forget = true
|
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
if uncounted != nil {
|
|
|
|
needsStatusUpdate := suspendCondChanged || active != job.Status.Active
|
|
|
|
job.Status.Active = active
|
|
|
|
err = jm.trackJobStatusAndRemoveFinalizers(&job, pods, prevSucceededIndexes, *uncounted, finishedCondition, needsStatusUpdate)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
jobFinished := IsJobFinished(&job)
|
|
|
|
if jobHasNewFailure && !jobFinished {
|
|
|
|
// returning an error will re-enqueue Job after the backoff period
|
|
|
|
return forget, fmt.Errorf("failed pod(s) detected for job key %q", key)
|
|
|
|
}
|
|
|
|
forget = true
|
|
|
|
return forget, manageJobErr
|
|
|
|
}
|
|
|
|
// Legacy path: tracking without finalizers.
|
|
|
|
|
|
|
|
// Ensure that there are no leftover tracking finalizers.
|
|
|
|
if err := jm.removeTrackingFinalizersFromAllPods(pods); err != nil {
|
|
|
|
return false, fmt.Errorf("removing disabled finalizers from job pods %s: %w", key, err)
|
|
|
|
}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
// no need to update the job if the status hasn't changed since last time
|
2021-07-02 08:43:15 +00:00
|
|
|
if job.Status.Active != active || job.Status.Succeeded != succeeded || job.Status.Failed != failed || suspendCondChanged || finishedCondition != nil {
|
2019-01-12 04:58:27 +00:00
|
|
|
job.Status.Active = active
|
|
|
|
job.Status.Succeeded = succeeded
|
|
|
|
job.Status.Failed = failed
|
2021-03-18 22:40:29 +00:00
|
|
|
if isIndexedJob(&job) {
|
2021-07-02 08:43:15 +00:00
|
|
|
job.Status.CompletedIndexes = succeededIndexes.String()
|
2021-03-18 22:40:29 +00:00
|
|
|
}
|
2021-07-02 08:43:15 +00:00
|
|
|
job.Status.UncountedTerminatedPods = nil
|
|
|
|
jm.enactJobFinished(&job, finishedCondition)
|
2019-01-12 04:58:27 +00:00
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
if err := jm.updateStatusHandler(&job); err != nil {
|
2019-01-12 04:58:27 +00:00
|
|
|
return forget, err
|
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
if jobHasNewFailure && !IsJobFinished(&job) {
|
2019-01-12 04:58:27 +00:00
|
|
|
// returning an error will re-enqueue Job after the backoff period
|
|
|
|
return forget, fmt.Errorf("failed pod(s) detected for job key %q", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
forget = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return forget, manageJobErr
|
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
// deleteActivePods issues deletion for active Pods, preserving finalizers.
|
|
|
|
// This is done through DELETE calls that set deletion timestamps.
|
|
|
|
// The method trackJobStatusAndRemoveFinalizers removes the finalizers, after
|
|
|
|
// which the objects can actually be deleted.
|
|
|
|
// Returns number of successfully deletions issued.
|
|
|
|
func (jm *Controller) deleteActivePods(job *batch.Job, pods []*v1.Pod) (int32, error) {
|
|
|
|
errCh := make(chan error, len(pods))
|
|
|
|
successfulDeletes := int32(len(pods))
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(len(pods))
|
|
|
|
for i := range pods {
|
|
|
|
go func(pod *v1.Pod) {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := jm.podControl.DeletePod(job.Namespace, pod.Name, job); err != nil && !apierrors.IsNotFound(err) {
|
|
|
|
atomic.AddInt32(&successfulDeletes, -1)
|
|
|
|
errCh <- err
|
|
|
|
utilruntime.HandleError(err)
|
|
|
|
}
|
|
|
|
}(pods[i])
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
return successfulDeletes, errorFromChannel(errCh)
|
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
// deleteJobPods deletes the pods, returns the number of successful removals
|
|
|
|
// and any error.
|
|
|
|
func (jm *Controller) deleteJobPods(job *batch.Job, jobKey string, pods []*v1.Pod) (int32, error) {
|
|
|
|
errCh := make(chan error, len(pods))
|
|
|
|
successfulDeletes := int32(len(pods))
|
2021-07-02 08:43:15 +00:00
|
|
|
|
|
|
|
failDelete := func(pod *v1.Pod, err error) {
|
|
|
|
// Decrement the expected number of deletes because the informer won't observe this deletion
|
|
|
|
jm.expectations.DeletionObserved(jobKey)
|
|
|
|
if !apierrors.IsNotFound(err) {
|
|
|
|
klog.V(2).Infof("Failed to delete Pod", "job", klog.KObj(job), "pod", klog.KObj(pod), "err", err)
|
|
|
|
atomic.AddInt32(&successfulDeletes, -1)
|
|
|
|
errCh <- err
|
|
|
|
utilruntime.HandleError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(len(pods))
|
|
|
|
for i := range pods {
|
|
|
|
go func(pod *v1.Pod) {
|
|
|
|
defer wg.Done()
|
2021-07-02 08:43:15 +00:00
|
|
|
if patch := removeTrackingFinalizerPatch(pod); patch != nil {
|
|
|
|
if err := jm.podControl.PatchPod(pod.Namespace, pod.Name, patch); err != nil {
|
|
|
|
failDelete(pod, fmt.Errorf("removing completion finalizer: %w", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 22:40:29 +00:00
|
|
|
if err := jm.podControl.DeletePod(job.Namespace, pod.Name, job); err != nil {
|
2021-07-02 08:43:15 +00:00
|
|
|
failDelete(pod, err)
|
|
|
|
}
|
|
|
|
}(pods[i])
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
return successfulDeletes, errorFromChannel(errCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeTrackingFinalizersFromAllPods removes finalizers from any Job Pod. This is called
|
|
|
|
// when Job tracking with finalizers is disabled.
|
|
|
|
func (jm *Controller) removeTrackingFinalizersFromAllPods(pods []*v1.Pod) error {
|
|
|
|
var podsWithFinalizer []*v1.Pod
|
|
|
|
for _, pod := range pods {
|
|
|
|
if hasJobTrackingFinalizer(pod) {
|
|
|
|
podsWithFinalizer = append(podsWithFinalizer, pod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(podsWithFinalizer) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
_, err := jm.removeTrackingFinalizerFromPods(podsWithFinalizer)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// trackJobStatusAndRemoveFinalizers does:
|
|
|
|
// 1. Add finished Pods to .status.uncountedTerminatedPods
|
|
|
|
// 2. Remove the finalizers from the Pods if they completed or were removed
|
|
|
|
// or the job was removed.
|
|
|
|
// 3. Increment job counters for pods that no longer have a finalizer.
|
|
|
|
// 4. Add Complete condition if satisfied with current counters.
|
|
|
|
// It does this in a controlled way such that the size of .status doesn't grow
|
|
|
|
// too much.
|
|
|
|
func (jm *Controller) trackJobStatusAndRemoveFinalizers(job *batch.Job, pods []*v1.Pod, succeededIndexes orderedIntervals, uncounted uncountedTerminatedPods, finishedCond *batch.JobCondition, needsFlush bool) error {
|
|
|
|
isIndexed := isIndexedJob(job)
|
|
|
|
var podsToRemoveFinalizer []*v1.Pod
|
|
|
|
uncountedStatus := job.Status.UncountedTerminatedPods
|
|
|
|
var newSucceededIndexes []int
|
|
|
|
if isIndexed {
|
|
|
|
// Sort to introduce completed Indexes First.
|
|
|
|
sort.Sort(byCompletionIndex(pods))
|
|
|
|
}
|
|
|
|
for _, pod := range pods {
|
|
|
|
if !hasJobTrackingFinalizer(pod) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
podFinished := pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodFailed
|
|
|
|
// Terminating pods are counted as failed. This guarantees that orphan Pods
|
|
|
|
// count as failures.
|
|
|
|
// Active pods are terminated when the job has completed, thus they count as
|
|
|
|
// failures as well.
|
|
|
|
podTerminating := pod.DeletionTimestamp != nil || finishedCond != nil
|
|
|
|
if podFinished || podTerminating || job.DeletionTimestamp != nil {
|
|
|
|
podsToRemoveFinalizer = append(podsToRemoveFinalizer, pod)
|
|
|
|
}
|
|
|
|
if pod.Status.Phase == v1.PodSucceeded {
|
|
|
|
if isIndexed {
|
|
|
|
// The completion index is enough to avoid recounting succeeded pods.
|
|
|
|
// No need to track UIDs.
|
|
|
|
ix := getCompletionIndex(pod.Annotations)
|
|
|
|
if ix != unknownCompletionIndex && ix < int(*job.Spec.Completions) && !succeededIndexes.has(ix) {
|
|
|
|
newSucceededIndexes = append(newSucceededIndexes, ix)
|
|
|
|
needsFlush = true
|
2021-03-18 22:40:29 +00:00
|
|
|
}
|
2021-07-02 08:43:15 +00:00
|
|
|
} else if !uncounted.succeeded.Has(string(pod.UID)) {
|
|
|
|
needsFlush = true
|
|
|
|
uncountedStatus.Succeeded = append(uncountedStatus.Succeeded, pod.UID)
|
|
|
|
}
|
|
|
|
} else if pod.Status.Phase == v1.PodFailed || podTerminating {
|
|
|
|
ix := getCompletionIndex(pod.Annotations)
|
|
|
|
if !uncounted.failed.Has(string(pod.UID)) && (!isIndexed || (ix != unknownCompletionIndex && ix < int(*job.Spec.Completions))) {
|
|
|
|
needsFlush = true
|
|
|
|
uncountedStatus.Failed = append(uncountedStatus.Failed, pod.UID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(uncountedStatus.Succeeded)+len(uncountedStatus.Failed) >= maxUncountedPods {
|
|
|
|
if len(newSucceededIndexes) > 0 {
|
|
|
|
succeededIndexes = succeededIndexes.withOrderedIndexes(newSucceededIndexes)
|
|
|
|
job.Status.Succeeded = int32(succeededIndexes.total())
|
|
|
|
job.Status.CompletedIndexes = succeededIndexes.String()
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
if needsFlush, err = jm.flushUncountedAndRemoveFinalizers(job, podsToRemoveFinalizer, needsFlush); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
podsToRemoveFinalizer = nil
|
|
|
|
newSucceededIndexes = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(newSucceededIndexes) > 0 {
|
|
|
|
succeededIndexes = succeededIndexes.withOrderedIndexes(newSucceededIndexes)
|
|
|
|
job.Status.Succeeded = int32(succeededIndexes.total())
|
|
|
|
job.Status.CompletedIndexes = succeededIndexes.String()
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
if needsFlush, err = jm.flushUncountedAndRemoveFinalizers(job, podsToRemoveFinalizer, needsFlush); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if jm.enactJobFinished(job, finishedCond) {
|
|
|
|
needsFlush = true
|
|
|
|
}
|
|
|
|
if needsFlush {
|
|
|
|
if err := jm.updateStatusHandler(job); err != nil {
|
|
|
|
return fmt.Errorf("removing uncounted pods from status: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// flushUncountedAndRemoveFinalizers does:
|
|
|
|
// 1. flush the Job status that might include new uncounted Pod UIDs.
|
|
|
|
// 2. perform the removal of finalizers from Pods which are in the uncounted
|
|
|
|
// lists.
|
|
|
|
// 3. update the counters based on the Pods for which it successfully removed
|
|
|
|
// the finalizers.
|
|
|
|
// 4. (if not all removals succeeded) flush Job status again.
|
|
|
|
// Returns whether there are pending changes in the Job status that need to be
|
|
|
|
// flushed in subsequent calls.
|
|
|
|
func (jm *Controller) flushUncountedAndRemoveFinalizers(job *batch.Job, podsToRemoveFinalizer []*v1.Pod, needsFlush bool) (bool, error) {
|
|
|
|
if needsFlush {
|
|
|
|
if err := jm.updateStatusHandler(job); err != nil {
|
|
|
|
return needsFlush, fmt.Errorf("adding uncounted pods to status: %w", err)
|
|
|
|
}
|
|
|
|
needsFlush = false
|
|
|
|
}
|
|
|
|
var failedToRm []*v1.Pod
|
|
|
|
var rmErr error
|
|
|
|
if len(podsToRemoveFinalizer) > 0 {
|
|
|
|
failedToRm, rmErr = jm.removeTrackingFinalizerFromPods(podsToRemoveFinalizer)
|
|
|
|
}
|
|
|
|
uncountedStatus := job.Status.UncountedTerminatedPods
|
|
|
|
if rmErr == nil {
|
|
|
|
needsFlush = len(uncountedStatus.Succeeded) > 0 || len(uncountedStatus.Failed) > 0
|
|
|
|
job.Status.Succeeded += int32(len(uncountedStatus.Succeeded))
|
|
|
|
uncountedStatus.Succeeded = nil
|
|
|
|
job.Status.Failed += int32(len(uncountedStatus.Failed))
|
|
|
|
uncountedStatus.Failed = nil
|
|
|
|
return needsFlush, nil
|
|
|
|
}
|
|
|
|
uidsWithFinalizer := make(sets.String, len(failedToRm))
|
|
|
|
for _, p := range failedToRm {
|
|
|
|
uidsWithFinalizer.Insert(string(p.UID))
|
|
|
|
}
|
|
|
|
newUncounted := uncountedWithFailedFinalizerRemovals(uncountedStatus.Succeeded, uidsWithFinalizer)
|
|
|
|
if len(newUncounted) != len(uncountedStatus.Succeeded) {
|
|
|
|
needsFlush = true
|
|
|
|
job.Status.Succeeded += int32(len(uncountedStatus.Succeeded) - len(newUncounted))
|
|
|
|
uncountedStatus.Succeeded = newUncounted
|
|
|
|
}
|
|
|
|
newUncounted = uncountedWithFailedFinalizerRemovals(uncountedStatus.Failed, uidsWithFinalizer)
|
|
|
|
if len(newUncounted) != len(uncountedStatus.Failed) {
|
|
|
|
needsFlush = true
|
|
|
|
job.Status.Failed += int32(len(uncountedStatus.Failed) - len(newUncounted))
|
|
|
|
uncountedStatus.Failed = newUncounted
|
|
|
|
}
|
|
|
|
if needsFlush {
|
|
|
|
if err := jm.updateStatusHandler(job); err != nil {
|
|
|
|
return needsFlush, fmt.Errorf("removing uncounted pods from status: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return needsFlush, rmErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeTrackingFinalizerFromPods removes tracking finalizers from Pods and
|
|
|
|
// returns the pod for which the operation failed (if the pod was deleted when
|
|
|
|
// this function was called, it's considered as the finalizer was removed
|
|
|
|
// successfully).
|
|
|
|
func (jm *Controller) removeTrackingFinalizerFromPods(pods []*v1.Pod) ([]*v1.Pod, error) {
|
|
|
|
errCh := make(chan error, len(pods))
|
|
|
|
var failed []*v1.Pod
|
|
|
|
var lock sync.Mutex
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(len(pods))
|
|
|
|
for i := range pods {
|
|
|
|
go func(i int) {
|
|
|
|
pod := pods[i]
|
|
|
|
defer wg.Done()
|
|
|
|
if patch := removeTrackingFinalizerPatch(pod); patch != nil {
|
|
|
|
if err := jm.podControl.PatchPod(pod.Namespace, pod.Name, patch); err != nil && !apierrors.IsNotFound(err) {
|
2021-03-18 22:40:29 +00:00
|
|
|
errCh <- err
|
|
|
|
utilruntime.HandleError(err)
|
2021-07-02 08:43:15 +00:00
|
|
|
lock.Lock()
|
|
|
|
failed = append(failed, pod)
|
|
|
|
lock.Unlock()
|
|
|
|
return
|
2021-03-18 22:40:29 +00:00
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
2021-07-02 08:43:15 +00:00
|
|
|
}(i)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
2021-03-18 22:40:29 +00:00
|
|
|
wg.Wait()
|
2021-07-02 08:43:15 +00:00
|
|
|
return failed, errorFromChannel(errCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
// enactJobFinished adds the Complete or Failed condition and records events.
|
|
|
|
// Returns whether the Job was considered finished.
|
|
|
|
func (jm *Controller) enactJobFinished(job *batch.Job, finishedCond *batch.JobCondition) bool {
|
|
|
|
if finishedCond == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if uncounted := job.Status.UncountedTerminatedPods; uncounted != nil {
|
|
|
|
if len(uncounted.Succeeded) > 0 || len(uncounted.Failed) > 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
completionMode := string(batch.NonIndexedCompletion)
|
|
|
|
if isIndexedJob(job) {
|
|
|
|
completionMode = string(*job.Spec.CompletionMode)
|
|
|
|
}
|
|
|
|
job.Status.Conditions = append(job.Status.Conditions, *finishedCond)
|
|
|
|
if finishedCond.Type == batch.JobComplete {
|
|
|
|
if job.Spec.Completions != nil && job.Status.Succeeded > *job.Spec.Completions {
|
|
|
|
jm.recorder.Event(job, v1.EventTypeWarning, "TooManySucceededPods", "Too many succeeded pods running after completion count reached")
|
|
|
|
}
|
|
|
|
job.Status.CompletionTime = &finishedCond.LastTransitionTime
|
|
|
|
jm.recorder.Event(job, v1.EventTypeNormal, "Completed", "Job completed")
|
|
|
|
metrics.JobFinishedNum.WithLabelValues(completionMode, "succeeded").Inc()
|
|
|
|
} else {
|
|
|
|
jm.recorder.Event(job, v1.EventTypeWarning, finishedCond.Reason, finishedCond.Message)
|
|
|
|
metrics.JobFinishedNum.WithLabelValues(completionMode, "failed").Inc()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func uncountedWithFailedFinalizerRemovals(uncounted []types.UID, uidsWithFinalizer sets.String) []types.UID {
|
|
|
|
var newUncounted []types.UID
|
|
|
|
for _, uid := range uncounted {
|
|
|
|
if uidsWithFinalizer.Has(string(uid)) {
|
|
|
|
newUncounted = append(newUncounted, uid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newUncounted
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pastBackoffLimitOnFailure checks if container restartCounts sum exceeds BackoffLimit
|
|
|
|
// this method applies only to pods with restartPolicy == OnFailure
|
|
|
|
func pastBackoffLimitOnFailure(job *batch.Job, pods []*v1.Pod) bool {
|
|
|
|
if job.Spec.Template.Spec.RestartPolicy != v1.RestartPolicyOnFailure {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
result := int32(0)
|
|
|
|
for i := range pods {
|
|
|
|
po := pods[i]
|
2019-08-30 18:33:25 +00:00
|
|
|
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
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if *job.Spec.BackoffLimit == 0 {
|
|
|
|
return result > 0
|
|
|
|
}
|
|
|
|
return result >= *job.Spec.BackoffLimit
|
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
// pastActiveDeadline checks if job has ActiveDeadlineSeconds field set and if
|
|
|
|
// it is exceeded. If the job is currently suspended, the function will always
|
|
|
|
// return false.
|
2019-01-12 04:58:27 +00:00
|
|
|
func pastActiveDeadline(job *batch.Job) bool {
|
2021-03-18 22:40:29 +00:00
|
|
|
if job.Spec.ActiveDeadlineSeconds == nil || job.Status.StartTime == nil || jobSuspended(job) {
|
2019-01-12 04:58:27 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
now := metav1.Now()
|
|
|
|
start := job.Status.StartTime.Time
|
|
|
|
duration := now.Time.Sub(start)
|
|
|
|
allowedDuration := time.Duration(*job.Spec.ActiveDeadlineSeconds) * time.Second
|
|
|
|
return duration >= allowedDuration
|
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
func newCondition(conditionType batch.JobConditionType, status v1.ConditionStatus, reason, message string) *batch.JobCondition {
|
|
|
|
return &batch.JobCondition{
|
2019-01-12 04:58:27 +00:00
|
|
|
Type: conditionType,
|
2021-03-18 22:40:29 +00:00
|
|
|
Status: status,
|
2019-01-12 04:58:27 +00:00
|
|
|
LastProbeTime: metav1.Now(),
|
|
|
|
LastTransitionTime: metav1.Now(),
|
|
|
|
Reason: reason,
|
|
|
|
Message: message,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
// getStatus returns number of succeeded and failed pods running a job
|
|
|
|
func getStatus(job *batch.Job, pods []*v1.Pod, uncounted *uncountedTerminatedPods) (succeeded, failed int32) {
|
|
|
|
if uncounted != nil {
|
|
|
|
succeeded = job.Status.Succeeded
|
|
|
|
failed = job.Status.Failed
|
|
|
|
}
|
|
|
|
succeeded += int32(countValidPodsWithFilter(job, pods, uncounted.Succeeded(), func(p *v1.Pod) bool {
|
|
|
|
return p.Status.Phase == v1.PodSucceeded
|
|
|
|
}))
|
|
|
|
failed += int32(countValidPodsWithFilter(job, pods, uncounted.Failed(), func(p *v1.Pod) bool {
|
|
|
|
if p.Status.Phase == v1.PodFailed {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// When tracking with finalizers: counting deleted Pods as failures to
|
|
|
|
// account for orphan Pods that never have a chance to reach the Failed
|
|
|
|
// phase.
|
|
|
|
return uncounted != nil && p.DeletionTimestamp != nil && p.Status.Phase != v1.PodSucceeded
|
|
|
|
}))
|
|
|
|
return succeeded, failed
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
// jobSuspended returns whether a Job is suspended while taking the feature
|
|
|
|
// gate into account.
|
|
|
|
func jobSuspended(job *batch.Job) bool {
|
2021-07-02 08:43:15 +00:00
|
|
|
return feature.DefaultFeatureGate.Enabled(features.SuspendJob) && job.Spec.Suspend != nil && *job.Spec.Suspend
|
2021-03-18 22:40:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
// manageJob is the core method responsible for managing the number of running
|
|
|
|
// pods according to what is specified in the job.Spec.
|
|
|
|
// Does NOT modify <activePods>.
|
2021-07-02 08:43:15 +00:00
|
|
|
func (jm *Controller) manageJob(job *batch.Job, activePods []*v1.Pod, succeeded int32, succeededIndexes []interval) (int32, string, error) {
|
2019-01-12 04:58:27 +00:00
|
|
|
active := int32(len(activePods))
|
|
|
|
parallelism := *job.Spec.Parallelism
|
|
|
|
jobKey, err := controller.KeyFunc(job)
|
|
|
|
if err != nil {
|
|
|
|
utilruntime.HandleError(fmt.Errorf("Couldn't get key for job %#v: %v", job, err))
|
2021-07-02 08:43:15 +00:00
|
|
|
return 0, metrics.JobSyncActionTracking, nil
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
if jobSuspended(job) {
|
|
|
|
klog.V(4).InfoS("Deleting all active pods in suspended job", "job", klog.KObj(job), "active", active)
|
|
|
|
podsToDelete := activePodsForRemoval(job, activePods, int(active))
|
|
|
|
jm.expectations.ExpectDeletions(jobKey, len(podsToDelete))
|
|
|
|
removed, err := jm.deleteJobPods(job, jobKey, podsToDelete)
|
|
|
|
active -= removed
|
2021-07-02 08:43:15 +00:00
|
|
|
return active, metrics.JobSyncActionPodsDeleted, err
|
|
|
|
}
|
|
|
|
|
|
|
|
wantActive := int32(0)
|
|
|
|
if job.Spec.Completions == nil {
|
|
|
|
// Job does not specify a number of completions. Therefore, number active
|
|
|
|
// should be equal to parallelism, unless the job has seen at least
|
|
|
|
// once success, in which leave whatever is running, running.
|
|
|
|
if succeeded > 0 {
|
|
|
|
wantActive = active
|
|
|
|
} else {
|
|
|
|
wantActive = parallelism
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Job specifies a specific number of completions. Therefore, number
|
|
|
|
// active should not ever exceed number of remaining completions.
|
|
|
|
wantActive = *job.Spec.Completions - succeeded
|
|
|
|
if wantActive > parallelism {
|
|
|
|
wantActive = parallelism
|
|
|
|
}
|
|
|
|
if wantActive < 0 {
|
|
|
|
wantActive = 0
|
|
|
|
}
|
2021-03-18 22:40:29 +00:00
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
rmAtLeast := active - wantActive
|
2021-03-18 22:40:29 +00:00
|
|
|
if rmAtLeast < 0 {
|
|
|
|
rmAtLeast = 0
|
|
|
|
}
|
|
|
|
podsToDelete := activePodsForRemoval(job, activePods, int(rmAtLeast))
|
2021-07-02 08:43:15 +00:00
|
|
|
if len(podsToDelete) > maxPodCreateDeletePerSync {
|
|
|
|
podsToDelete = podsToDelete[:maxPodCreateDeletePerSync]
|
|
|
|
}
|
2021-03-18 22:40:29 +00:00
|
|
|
if len(podsToDelete) > 0 {
|
|
|
|
jm.expectations.ExpectDeletions(jobKey, len(podsToDelete))
|
2021-07-02 08:43:15 +00:00
|
|
|
klog.V(4).InfoS("Too many pods running for job", "job", klog.KObj(job), "deleted", len(podsToDelete), "target", parallelism)
|
2021-03-18 22:40:29 +00:00
|
|
|
removed, err := jm.deleteJobPods(job, jobKey, podsToDelete)
|
|
|
|
active -= removed
|
2021-07-02 08:43:15 +00:00
|
|
|
// While it is possible for a Job to require both pod creations and
|
|
|
|
// deletions at the same time (e.g. indexed Jobs with repeated indexes), we
|
|
|
|
// restrict ourselves to either just pod deletion or pod creation in any
|
|
|
|
// given sync cycle. Of these two, pod deletion takes precedence.
|
|
|
|
return active, metrics.JobSyncActionPodsDeleted, err
|
2021-03-18 22:40:29 +00:00
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
if active < wantActive {
|
2019-01-12 04:58:27 +00:00
|
|
|
diff := wantActive - active
|
2021-07-02 08:43:15 +00:00
|
|
|
if diff > int32(maxPodCreateDeletePerSync) {
|
|
|
|
diff = int32(maxPodCreateDeletePerSync)
|
2019-09-27 21:51:53 +00:00
|
|
|
}
|
2021-03-18 22:40:29 +00:00
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
jm.expectations.ExpectCreations(jobKey, int(diff))
|
2021-03-18 22:40:29 +00:00
|
|
|
errCh := make(chan error, diff)
|
2019-01-12 04:58:27 +00:00
|
|
|
klog.V(4).Infof("Too few pods running job %q, need %d, creating %d", jobKey, wantActive, diff)
|
|
|
|
|
|
|
|
wait := sync.WaitGroup{}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
var indexesToAdd []int
|
2021-07-02 08:43:15 +00:00
|
|
|
if isIndexedJob(job) {
|
|
|
|
indexesToAdd = firstPendingIndexes(activePods, succeededIndexes, int(diff), int(*job.Spec.Completions))
|
2021-03-18 22:40:29 +00:00
|
|
|
diff = int32(len(indexesToAdd))
|
|
|
|
}
|
|
|
|
active += diff
|
|
|
|
|
|
|
|
podTemplate := job.Spec.Template.DeepCopy()
|
|
|
|
if isIndexedJob(job) {
|
|
|
|
addCompletionIndexEnvVariables(podTemplate)
|
|
|
|
}
|
2021-07-02 08:43:15 +00:00
|
|
|
if trackingUncountedPods(job) {
|
|
|
|
podTemplate.Finalizers = appendJobCompletionFinalizerIfNotFound(podTemplate.Finalizers)
|
|
|
|
}
|
2021-03-18 22:40:29 +00:00
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
// Batch the pod creates. Batch sizes start at SlowStartInitialBatchSize
|
|
|
|
// and double with each successful iteration in a kind of "slow start".
|
|
|
|
// This handles attempts to start large numbers of pods that would
|
|
|
|
// likely all fail with the same error. For example a project with a
|
|
|
|
// low quota that attempts to create a large number of pods will be
|
|
|
|
// prevented from spamming the API service with the pod create requests
|
|
|
|
// after one of its pods fails. Conveniently, this also prevents the
|
|
|
|
// event spam that those failures would generate.
|
|
|
|
for batchSize := int32(integer.IntMin(int(diff), controller.SlowStartInitialBatchSize)); diff > 0; batchSize = integer.Int32Min(2*batchSize, diff) {
|
|
|
|
errorCount := len(errCh)
|
|
|
|
wait.Add(int(batchSize))
|
|
|
|
for i := int32(0); i < batchSize; i++ {
|
2021-03-18 22:40:29 +00:00
|
|
|
completionIndex := unknownCompletionIndex
|
2021-07-02 08:43:15 +00:00
|
|
|
if len(indexesToAdd) > 0 {
|
2021-03-18 22:40:29 +00:00
|
|
|
completionIndex = indexesToAdd[0]
|
|
|
|
indexesToAdd = indexesToAdd[1:]
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
go func() {
|
2021-03-18 22:40:29 +00:00
|
|
|
template := podTemplate
|
|
|
|
if completionIndex != unknownCompletionIndex {
|
|
|
|
template = podTemplate.DeepCopy()
|
|
|
|
addCompletionIndexAnnotation(template, completionIndex)
|
2021-07-02 08:43:15 +00:00
|
|
|
template.Spec.Hostname = fmt.Sprintf("%s-%d", job.Name, completionIndex)
|
2021-03-18 22:40:29 +00:00
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
defer wait.Done()
|
2021-07-02 08:43:15 +00:00
|
|
|
generateName := podGenerateNameWithIndex(job.Name, completionIndex)
|
|
|
|
err := jm.podControl.CreatePodsWithGenerateName(job.Namespace, template, job, metav1.NewControllerRef(job, controllerKind), generateName)
|
2019-12-12 01:27:03 +00:00
|
|
|
if err != nil {
|
2021-03-18 22:40:29 +00:00
|
|
|
if apierrors.HasStatusCause(err, v1.NamespaceTerminatingCause) {
|
2019-12-12 01:27:03 +00:00
|
|
|
// If the namespace is being torn down, we can safely ignore
|
|
|
|
// this error since all subsequent creations will fail.
|
|
|
|
return
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
defer utilruntime.HandleError(err)
|
|
|
|
// Decrement the expected number of creates because the informer won't observe this pod
|
|
|
|
klog.V(2).Infof("Failed creation, decrementing expectations for job %q/%q", job.Namespace, job.Name)
|
|
|
|
jm.expectations.CreationObserved(jobKey)
|
2021-03-18 22:40:29 +00:00
|
|
|
atomic.AddInt32(&active, -1)
|
2019-01-12 04:58:27 +00:00
|
|
|
errCh <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wait.Wait()
|
|
|
|
// any skipped pods that we never attempted to start shouldn't be expected.
|
|
|
|
skippedPods := diff - batchSize
|
|
|
|
if errorCount < len(errCh) && skippedPods > 0 {
|
|
|
|
klog.V(2).Infof("Slow-start failure. Skipping creation of %d pods, decrementing expectations for job %q/%q", skippedPods, job.Namespace, job.Name)
|
|
|
|
active -= skippedPods
|
|
|
|
for i := int32(0); i < skippedPods; i++ {
|
|
|
|
// Decrement the expected number of creates because the informer won't observe this pod
|
|
|
|
jm.expectations.CreationObserved(jobKey)
|
|
|
|
}
|
|
|
|
// The skipped pods will be retried later. The next controller resync will
|
|
|
|
// retry the slow start process.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
diff -= batchSize
|
|
|
|
}
|
2021-07-02 08:43:15 +00:00
|
|
|
return active, metrics.JobSyncActionPodsCreated, errorFromChannel(errCh)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
return active, metrics.JobSyncActionTracking, nil
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
// activePodsForRemoval returns Pods that should be removed because there
|
|
|
|
// are too many pods running or, if this is an indexed job, there are repeated
|
2021-07-02 08:43:15 +00:00
|
|
|
// indexes or invalid indexes or some pods don't have indexes.
|
2021-03-18 22:40:29 +00:00
|
|
|
// Sorts candidate pods in the order such that not-ready < ready, unscheduled
|
|
|
|
// < scheduled, and pending < running. This ensures that we delete pods
|
|
|
|
// in the earlier stages whenever possible.
|
|
|
|
func activePodsForRemoval(job *batch.Job, pods []*v1.Pod, rmAtLeast int) []*v1.Pod {
|
|
|
|
var rm, left []*v1.Pod
|
|
|
|
|
|
|
|
if isIndexedJob(job) {
|
|
|
|
rm = make([]*v1.Pod, 0, rmAtLeast)
|
|
|
|
left = make([]*v1.Pod, 0, len(pods)-rmAtLeast)
|
2021-07-02 08:43:15 +00:00
|
|
|
rm, left = appendDuplicatedIndexPodsForRemoval(rm, left, pods, int(*job.Spec.Completions))
|
2021-03-18 22:40:29 +00:00
|
|
|
} else {
|
|
|
|
left = pods
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rm) < rmAtLeast {
|
|
|
|
sort.Sort(controller.ActivePods(left))
|
|
|
|
rm = append(rm, left[:rmAtLeast-len(rm)]...)
|
|
|
|
}
|
|
|
|
return rm
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func (jm *Controller) updateJobStatus(job *batch.Job) error {
|
2019-01-12 04:58:27 +00:00
|
|
|
jobClient := jm.kubeClient.BatchV1().Jobs(job.Namespace)
|
|
|
|
var err error
|
|
|
|
for i := 0; i <= statusUpdateRetries; i = i + 1 {
|
|
|
|
var newJob *batch.Job
|
2020-03-26 21:07:15 +00:00
|
|
|
newJob, err = jobClient.Get(context.TODO(), job.Name, metav1.GetOptions{})
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
newJob.Status = job.Status
|
2020-03-26 21:07:15 +00:00
|
|
|
if _, err = jobClient.UpdateStatus(context.TODO(), newJob, metav1.UpdateOptions{}); err == nil {
|
2019-01-12 04:58:27 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
func (jm *Controller) patchJob(job *batch.Job, data []byte) error {
|
|
|
|
_, err := jm.kubeClient.BatchV1().Jobs(job.Namespace).Patch(
|
|
|
|
context.TODO(), job.Name, types.StrategicMergePatchType, data, metav1.PatchOptions{})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
func getBackoff(queue workqueue.RateLimitingInterface, key interface{}) time.Duration {
|
|
|
|
exp := queue.NumRequeues(key)
|
|
|
|
|
|
|
|
if exp <= 0 {
|
|
|
|
return time.Duration(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The backoff is capped such that 'calculated' value never overflows.
|
|
|
|
backoff := float64(DefaultJobBackOff.Nanoseconds()) * math.Pow(2, float64(exp-1))
|
|
|
|
if backoff > math.MaxInt64 {
|
|
|
|
return MaxJobBackOff
|
|
|
|
}
|
|
|
|
|
|
|
|
calculated := time.Duration(backoff)
|
|
|
|
if calculated > MaxJobBackOff {
|
|
|
|
return MaxJobBackOff
|
|
|
|
}
|
|
|
|
return calculated
|
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
// countValidPodsWithFilter returns number of valid pods that pass the filter.
|
|
|
|
// Pods are valid if they have a finalizer and, for Indexed Jobs, a valid
|
|
|
|
// completion index.
|
|
|
|
func countValidPodsWithFilter(job *batch.Job, pods []*v1.Pod, uncounted sets.String, filter func(*v1.Pod) bool) int {
|
|
|
|
result := len(uncounted)
|
2021-03-18 22:40:29 +00:00
|
|
|
for _, p := range pods {
|
2021-07-02 08:43:15 +00:00
|
|
|
// Pods that don't have a completion finalizer are in the uncounted set or
|
|
|
|
// have already been accounted for in the Job status.
|
|
|
|
if uncounted != nil && (!hasJobTrackingFinalizer(p) || uncounted.Has(string(p.UID))) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if isIndexedJob(job) {
|
|
|
|
idx := getCompletionIndex(p.Annotations)
|
|
|
|
if idx == unknownCompletionIndex || idx >= int(*job.Spec.Completions) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if filter(p) {
|
2019-01-12 04:58:27 +00:00
|
|
|
result++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2021-03-18 22:40:29 +00:00
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
func trackingUncountedPods(job *batch.Job) bool {
|
|
|
|
return feature.DefaultFeatureGate.Enabled(features.JobTrackingWithFinalizers) && hasJobTrackingAnnotation(job)
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasJobTrackingAnnotation(job *batch.Job) bool {
|
|
|
|
if job.Annotations == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, ok := job.Annotations[batch.JobTrackingFinalizer]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendJobCompletionFinalizerIfNotFound(finalizers []string) []string {
|
|
|
|
for _, fin := range finalizers {
|
|
|
|
if fin == batch.JobTrackingFinalizer {
|
|
|
|
return finalizers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(finalizers, batch.JobTrackingFinalizer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeTrackingFinalizerPatch(pod *v1.Pod) []byte {
|
|
|
|
if !hasJobTrackingFinalizer(pod) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
patch := map[string]interface{}{
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"$deleteFromPrimitiveList/finalizers": []string{batch.JobTrackingFinalizer},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
patchBytes, _ := json.Marshal(patch)
|
|
|
|
return patchBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeTrackingAnnotationPatch(job *batch.Job) []byte {
|
|
|
|
if !hasJobTrackingAnnotation(job) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
patch := map[string]interface{}{
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"annotations": map[string]interface{}{
|
|
|
|
batch.JobTrackingFinalizer: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
patchBytes, _ := json.Marshal(patch)
|
|
|
|
return patchBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasJobTrackingFinalizer(pod *v1.Pod) bool {
|
|
|
|
for _, fin := range pod.Finalizers {
|
|
|
|
if fin == batch.JobTrackingFinalizer {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
type uncountedTerminatedPods struct {
|
|
|
|
succeeded sets.String
|
|
|
|
failed sets.String
|
|
|
|
}
|
|
|
|
|
|
|
|
func newUncountedTerminatedPods(in batch.UncountedTerminatedPods) *uncountedTerminatedPods {
|
|
|
|
obj := uncountedTerminatedPods{
|
|
|
|
succeeded: make(sets.String, len(in.Succeeded)),
|
|
|
|
failed: make(sets.String, len(in.Failed)),
|
|
|
|
}
|
|
|
|
for _, v := range in.Succeeded {
|
|
|
|
obj.succeeded.Insert(string(v))
|
|
|
|
}
|
|
|
|
for _, v := range in.Failed {
|
|
|
|
obj.failed.Insert(string(v))
|
|
|
|
}
|
|
|
|
return &obj
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *uncountedTerminatedPods) Succeeded() sets.String {
|
|
|
|
if u == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return u.succeeded
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *uncountedTerminatedPods) Failed() sets.String {
|
|
|
|
if u == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return u.failed
|
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
func errorFromChannel(errCh <-chan error) error {
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensureJobConditionStatus appends or updates an existing job condition of the
|
|
|
|
// given type with the given status value. Note that this function will not
|
|
|
|
// append to the conditions list if the new condition's status is false
|
|
|
|
// (because going from nothing to false is meaningless); it can, however,
|
|
|
|
// update the status condition to false. The function returns a bool to let the
|
|
|
|
// caller know if the list was changed (either appended or updated).
|
|
|
|
func ensureJobConditionStatus(list []batch.JobCondition, cType batch.JobConditionType, status v1.ConditionStatus, reason, message string) ([]batch.JobCondition, bool) {
|
|
|
|
for i := range list {
|
|
|
|
if list[i].Type == cType {
|
|
|
|
if list[i].Status != status || list[i].Reason != reason || list[i].Message != message {
|
|
|
|
list[i].Status = status
|
|
|
|
list[i].LastTransitionTime = metav1.Now()
|
|
|
|
list[i].Reason = reason
|
|
|
|
list[i].Message = message
|
|
|
|
return list, true
|
|
|
|
}
|
|
|
|
return list, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// A condition with that type doesn't exist in the list.
|
|
|
|
if status != v1.ConditionFalse {
|
2021-07-02 08:43:15 +00:00
|
|
|
return append(list, *newCondition(cType, status, reason, message)), true
|
2021-03-18 22:40:29 +00:00
|
|
|
}
|
|
|
|
return list, false
|
|
|
|
}
|