Skip to content

Commit 4b5116f

Browse files
authored
[slice] Extract workload utils (#1147)
Extract workload utils
1 parent 123e517 commit 4b5116f

2 files changed

Lines changed: 71 additions & 50 deletions

File tree

‎slice/internal/controller/workload_controller.go‎

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6162
const (
@@ -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-
299254
func (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

419374
func (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

521476
func 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) {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package workload
18+
19+
import (
20+
batchv1 "k8s.io/api/batch/v1"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
23+
jobset "sigs.k8s.io/jobset/api/jobset/v1alpha2"
24+
kueue "sigs.k8s.io/kueue/apis/kueue/v1beta2"
25+
kueueworkload "sigs.k8s.io/kueue/pkg/workload"
26+
)
27+
28+
func ShouldFinalize(wl *kueue.Workload) (bool, string) {
29+
if !wl.DeletionTimestamp.IsZero() {
30+
return true, "it has been deleted"
31+
}
32+
if kueueworkload.IsFinished(wl) {
33+
return true, "it has finished"
34+
}
35+
if kueueworkload.IsEvicted(wl) {
36+
return true, "it was evicted"
37+
}
38+
if !kueueworkload.IsActive(wl) {
39+
return true, "it is no longer active"
40+
}
41+
if !controllerutil.HasControllerReference(wl) {
42+
return true, "it doesn't have owner"
43+
}
44+
if !HasSupportedOwner(wl) {
45+
return true, "it has an unsupported owner"
46+
}
47+
return false, ""
48+
}
49+
50+
func HasSupportedOwner(wl *kueue.Workload) bool {
51+
return IsJobSetOwner(wl) || IsJobOwner(wl)
52+
}
53+
54+
func IsJobSetOwner(wl *kueue.Workload) bool {
55+
if owner := metav1.GetControllerOf(wl); owner != nil {
56+
return owner.APIVersion == jobset.SchemeGroupVersion.String() && owner.Kind == "JobSet"
57+
}
58+
return false
59+
}
60+
61+
func IsJobOwner(wl *kueue.Workload) bool {
62+
if owner := metav1.GetControllerOf(wl); owner != nil {
63+
return owner.APIVersion == batchv1.SchemeGroupVersion.String() && owner.Kind == "Job"
64+
}
65+
return false
66+
}

0 commit comments

Comments
 (0)