@@ -56,6 +56,7 @@ import (
5656 "tpu-slice-controller/internal/util/api"
5757 "tpu-slice-controller/internal/util/node"
5858 utilpod "tpu-slice-controller/internal/util/pod"
59+ utilworkload "tpu-slice-controller/internal/util/workload"
5960)
6061
6162const (
@@ -120,7 +121,7 @@ func (r *WorkloadReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
120121
121122 // If the workload has been deleted, evicted, has finished, or is no longer active,
122123 // finalize the Workload by removing its slices and then the finalizer.
123- if finalize , reason := shouldFinalize (wl ); finalize {
124+ if finalize , reason := utilworkload . ShouldFinalize (wl ); finalize {
124125 if controllerutil .ContainsFinalizer (wl , SliceControllerName ) {
125126 log .V (3 ).Info ("Cleaning up the Slices and finalizing the Workload" , "reason" , reason )
126127 cleanedUp , err := r .cleanupSlices (ctx , wl )
@@ -250,52 +251,6 @@ func (r *WorkloadReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
250251 return ctrl.Result {}, nil
251252}
252253
253- func shouldFinalize (wl * kueue.Workload ) (bool , string ) {
254- if ! wl .DeletionTimestamp .IsZero () {
255- return true , "it has been deleted"
256- }
257-
258- if workload .IsFinished (wl ) {
259- return true , "it has finished"
260- }
261-
262- if workload .IsEvicted (wl ) {
263- return true , "it was evicted"
264- }
265-
266- if ! workload .IsActive (wl ) {
267- return true , "it is no longer active"
268- }
269-
270- if ! controllerutil .HasControllerReference (wl ) {
271- return true , "it doesn't have owner"
272- }
273-
274- if ! hasSupportedOwner (wl ) {
275- return true , "it has an unsupported owner"
276- }
277-
278- return false , ""
279- }
280-
281- func hasSupportedOwner (wl * kueue.Workload ) bool {
282- return isJobSetOwner (wl ) || isJobOwner (wl )
283- }
284-
285- func isJobSetOwner (wl * kueue.Workload ) bool {
286- if owner := metav1 .GetControllerOf (wl ); owner != nil {
287- return owner .APIVersion == jobset .SchemeGroupVersion .String () && owner .Kind == "JobSet"
288- }
289- return false
290- }
291-
292- func isJobOwner (wl * kueue.Workload ) bool {
293- if owner := metav1 .GetControllerOf (wl ); owner != nil {
294- return owner .APIVersion == batchv1 .SchemeGroupVersion .String () && owner .Kind == "Job"
295- }
296- return false
297- }
298-
299254func (r * WorkloadReconciler ) cleanupSlices (ctx context.Context , wl * kueue.Workload ) (bool , error ) {
300255 log := ctrl .LoggerFrom (ctx )
301256
@@ -417,10 +372,10 @@ func (r *WorkloadReconciler) deleteSlicesForEvictedWorkload(ctx context.Context,
417372}
418373
419374func (r * WorkloadReconciler ) ownerPodsFinished (ctx context.Context , wl * kueue.Workload ) (bool , error ) {
420- if isJobSetOwner (wl ) {
375+ if utilworkload . IsJobSetOwner (wl ) {
421376 return r .jobSetPodsFinished (ctx , wl )
422377 }
423- if isJobOwner (wl ) {
378+ if utilworkload . IsJobOwner (wl ) {
424379 return r .jobPodsFinished (ctx , wl )
425380 }
426381 // Finalize Workloads that have no owner or have unsupported owner types.
@@ -519,7 +474,7 @@ func (r *WorkloadReconciler) finalizeWorkload(ctx context.Context, wl *kueue.Wor
519474}
520475
521476func validateRelevantWorkload (wl * kueue.Workload , nodes map [string ]corev1.Node ) error {
522- if ! hasSupportedOwner (wl ) {
477+ if ! utilworkload . HasSupportedOwner (wl ) {
523478 return errors .New ("does not have a supported owner" )
524479 }
525480 if ! hasRelevantPodSet (wl .Spec .PodSets ) {
0 commit comments