Skip to content

Commit 859b349

Browse files
authored
[slice] Refactor OwnerPodsFinished (#1172)
* unify owner pods finished into one function * Address comments * Simplify
1 parent 8794946 commit 859b349

1 file changed

Lines changed: 22 additions & 102 deletions

File tree

slice/internal/controller/workload_controller.go

Lines changed: 22 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -372,122 +372,42 @@ func (r *WorkloadReconciler) deleteSlicesForEvictedWorkload(ctx context.Context,
372372
return r.deleteSlices(ctx, toDelete)
373373
}
374374

375-
func (r *WorkloadReconciler) ownerPodsFinished(ctx context.Context, wl *kueue.Workload) (bool, error) {
376-
if utilworkload.IsJobSetOwner(wl) {
377-
return r.jobSetPodsFinished(ctx, wl)
378-
}
379-
if utilworkload.IsJobOwner(wl) {
380-
return r.jobPodsFinished(ctx, wl)
381-
}
382-
if utilworkload.IsLeaderWorkerSetOwner(wl) {
383-
return r.lwsPodsFinished(ctx, wl)
375+
func getWorkloadOwnerDetails(wl *kueue.Workload) (*metav1.OwnerReference, client.Object, string) {
376+
switch {
377+
case utilworkload.IsJobSetOwner(wl):
378+
return metav1.GetControllerOf(wl), &jobset.JobSet{}, jobset.JobSetNameKey
379+
case utilworkload.IsJobOwner(wl):
380+
return metav1.GetControllerOf(wl), &batchv1.Job{}, batchv1.JobNameLabel
381+
case utilworkload.IsLeaderWorkerSetOwner(wl):
382+
return utilworkload.GetOwner(wl), &leaderworkersetv1.LeaderWorkerSet{}, leaderworkersetv1.SetNameLabelKey
384383
}
385-
// Finalize Workloads that have no owner or have unsupported owner types.
386-
return true, nil
384+
return nil, nil, ""
387385
}
388386

389-
func (r *WorkloadReconciler) jobSetPodsFinished(ctx context.Context, wl *kueue.Workload) (bool, error) {
390-
owner := metav1.GetControllerOf(wl)
391-
log := ctrl.LoggerFrom(ctx).WithValues("jobSet", klog.KRef(wl.Namespace, owner.Name))
392-
jobSet := &jobset.JobSet{}
393-
jobSetKey := types.NamespacedName{Name: owner.Name, Namespace: wl.Namespace}
394-
if err := r.client.Get(ctx, jobSetKey, jobSet); err != nil {
395-
if apierrors.IsNotFound(err) {
396-
log.V(3).Info("JobSet already deleted")
397-
// That means the JobSet has already been deleted, along with all associated Jobs and Pods
398-
// we should delete Slice and cleanup Workload.
399-
return true, nil
400-
} else {
401-
log.Error(err, "Failed to get JobSet")
402-
return false, err
403-
}
404-
}
405-
406-
pods := &corev1.PodList{}
407-
opts := []client.ListOption{
408-
client.InNamespace(wl.Namespace),
409-
client.MatchingLabels{jobset.JobSetNameKey: owner.Name},
410-
}
411-
if err := r.client.List(ctx, pods, opts...); err != nil {
412-
log.Error(err, "Failed to get Pods")
413-
return false, err
414-
}
415-
416-
for _, pod := range pods.Items {
417-
if !utilpod.IsTerminated(&pod) {
418-
log.V(3).Info("Pods are still running – skipping finalization for now")
419-
return false, nil
420-
}
387+
func (r *WorkloadReconciler) ownerPodsFinished(ctx context.Context, wl *kueue.Workload) (bool, error) {
388+
owner, ownerObj, podLabelKey := getWorkloadOwnerDetails(wl)
389+
// Finalize Workloads that have no owner or have unsupported owner types.
390+
if owner == nil || ownerObj == nil {
391+
return true, nil
421392
}
422393

423-
log.V(3).Info("All Pods in the JobSet have finished")
424-
425-
return true, nil
426-
}
427-
428-
func (r *WorkloadReconciler) jobPodsFinished(ctx context.Context, wl *kueue.Workload) (bool, error) {
429-
owner := metav1.GetControllerOf(wl)
430-
log := ctrl.LoggerFrom(ctx).WithValues("job", klog.KRef(wl.Namespace, owner.Name))
431-
job := &batchv1.Job{}
432-
jobKey := types.NamespacedName{Name: owner.Name, Namespace: wl.Namespace}
433-
if err := r.client.Get(ctx, jobKey, job); err != nil {
394+
log := ctrl.LoggerFrom(ctx).WithValues(owner.Kind, klog.KRef(wl.Namespace, owner.Name))
395+
ownerKey := types.NamespacedName{Name: owner.Name, Namespace: wl.Namespace}
396+
if err := r.client.Get(ctx, ownerKey, ownerObj); err != nil {
434397
if apierrors.IsNotFound(err) {
435-
log.V(3).Info("Job already deleted")
436-
// That means the Job has already been deleted, along with all associated Pods
398+
log.V(3).Info(fmt.Sprintf("%s already deleted", owner.Kind))
399+
// That means the owner has already been deleted, along with all associated Pods
437400
// we should delete Slice and cleanup Workload.
438401
return true, nil
439-
} else {
440-
log.Error(err, "Failed to get Job")
441-
return false, err
442402
}
443-
}
444-
445-
pods := &corev1.PodList{}
446-
opts := []client.ListOption{
447-
client.InNamespace(wl.Namespace),
448-
client.MatchingLabels{"batch.kubernetes.io/job-name": owner.Name},
449-
}
450-
if err := r.client.List(ctx, pods, opts...); err != nil {
451-
log.Error(err, "Failed to get Pods")
403+
log.Error(err, fmt.Sprintf("Failed to get %s", owner.Kind))
452404
return false, err
453405
}
454406

455-
for _, pod := range pods.Items {
456-
if !utilpod.IsTerminated(&pod) {
457-
log.V(3).Info("Pods are still running – skipping finalization for now")
458-
return false, nil
459-
}
460-
}
461-
462-
log.V(3).Info("All Pods in the Job have finished")
463-
464-
return true, nil
465-
}
466-
467-
func (r *WorkloadReconciler) lwsPodsFinished(ctx context.Context, wl *kueue.Workload) (bool, error) {
468-
owner := utilworkload.GetOwner(wl)
469-
if owner == nil {
470-
return true, nil
471-
}
472-
log := ctrl.LoggerFrom(ctx).WithValues("leaderWorkerSet", klog.KRef(wl.Namespace, owner.Name))
473-
lws := &leaderworkersetv1.LeaderWorkerSet{}
474-
lwsKey := types.NamespacedName{Name: owner.Name, Namespace: wl.Namespace}
475-
if err := r.client.Get(ctx, lwsKey, lws); err != nil {
476-
if apierrors.IsNotFound(err) {
477-
log.V(3).Info("LeaderWorkerSet already deleted")
478-
// That means the LeaderWorkerSet has already been deleted, along with all associated Pods
479-
// we should delete Slice and cleanup Workload.
480-
return true, nil
481-
} else {
482-
log.Error(err, "Failed to get LeaderWorkerSet")
483-
return false, err
484-
}
485-
}
486-
487407
pods := &corev1.PodList{}
488408
opts := []client.ListOption{
489409
client.InNamespace(wl.Namespace),
490-
client.MatchingLabels{leaderworkersetv1.SetNameLabelKey: owner.Name},
410+
client.MatchingLabels{podLabelKey: owner.Name},
491411
}
492412
if err := r.client.List(ctx, pods, opts...); err != nil {
493413
log.Error(err, "Failed to get Pods")
@@ -501,7 +421,7 @@ func (r *WorkloadReconciler) lwsPodsFinished(ctx context.Context, wl *kueue.Work
501421
}
502422
}
503423

504-
log.V(3).Info("All Pods in the LeaderWorkerSet have finished")
424+
log.V(3).Info(fmt.Sprintf("All Pods in the %s have finished", owner.Kind))
505425

506426
return true, nil
507427
}

0 commit comments

Comments
 (0)