generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide Kubeflow PaddleJob support for MultiKueue
- Loading branch information
Showing
15 changed files
with
628 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -253,6 +253,7 @@ rules: | |
- paddlejobs/status | ||
verbs: | ||
- get | ||
- patch | ||
- update | ||
- apiGroups: | ||
- kubeflow.org | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -252,6 +252,7 @@ rules: | |
- paddlejobs/status | ||
verbs: | ||
- get | ||
- patch | ||
- update | ||
- apiGroups: | ||
- kubeflow.org | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
pkg/controller/jobs/kubeflow/jobs/paddlejob/paddlejob_multikueue_adapter.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
Copyright 2024 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 paddlejob | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
kftraining "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/klog/v2" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
kueuealpha "sigs.k8s.io/kueue/apis/kueue/v1alpha1" | ||
"sigs.k8s.io/kueue/pkg/controller/constants" | ||
"sigs.k8s.io/kueue/pkg/controller/jobframework" | ||
"sigs.k8s.io/kueue/pkg/util/api" | ||
clientutil "sigs.k8s.io/kueue/pkg/util/client" | ||
) | ||
|
||
type multikueueAdapter struct{} | ||
|
||
var _ jobframework.MultiKueueAdapter = (*multikueueAdapter)(nil) | ||
|
||
func (b *multikueueAdapter) SyncJob(ctx context.Context, localClient client.Client, remoteClient client.Client, key types.NamespacedName, workloadName, origin string) error { | ||
localJob := kftraining.PaddleJob{} | ||
err := localClient.Get(ctx, key, &localJob) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
remoteJob := &kftraining.PaddleJob{} | ||
err = remoteClient.Get(ctx, key, remoteJob) | ||
if client.IgnoreNotFound(err) != nil { | ||
return err | ||
} | ||
|
||
// if the remote exists, just copy the status | ||
if err == nil { | ||
return clientutil.PatchStatus(ctx, localClient, &localJob, func() (bool, error) { | ||
localJob.Status = remoteJob.Status | ||
return true, nil | ||
}) | ||
} | ||
|
||
remoteJob = &kftraining.PaddleJob{ | ||
ObjectMeta: api.CloneObjectMetaForCreation(&localJob.ObjectMeta), | ||
Spec: *localJob.Spec.DeepCopy(), | ||
} | ||
|
||
// add the prebuilt workload | ||
if remoteJob.Labels == nil { | ||
remoteJob.Labels = map[string]string{} | ||
} | ||
remoteJob.Labels[constants.PrebuiltWorkloadLabel] = workloadName | ||
remoteJob.Labels[kueuealpha.MultiKueueOriginLabel] = origin | ||
|
||
return remoteClient.Create(ctx, remoteJob) | ||
} | ||
|
||
func (b *multikueueAdapter) DeleteRemoteObject(ctx context.Context, remoteClient client.Client, key types.NamespacedName) error { | ||
job := kftraining.PaddleJob{} | ||
err := remoteClient.Get(ctx, key, &job) | ||
if err != nil { | ||
return client.IgnoreNotFound(err) | ||
} | ||
return client.IgnoreNotFound(remoteClient.Delete(ctx, &job)) | ||
} | ||
|
||
func (b *multikueueAdapter) KeepAdmissionCheckPending() bool { | ||
return false | ||
} | ||
|
||
func (b *multikueueAdapter) IsJobManagedByKueue(ctx context.Context, c client.Client, key types.NamespacedName) (bool, string, error) { | ||
return true, "", nil | ||
} | ||
|
||
func (b *multikueueAdapter) GVK() schema.GroupVersionKind { | ||
return gvk | ||
} | ||
|
||
var _ jobframework.MultiKueueWatcher = (*multikueueAdapter)(nil) | ||
|
||
func (*multikueueAdapter) GetEmptyList() client.ObjectList { | ||
return &kftraining.PaddleJobList{} | ||
} | ||
|
||
func (*multikueueAdapter) WorkloadKeyFor(o runtime.Object) (types.NamespacedName, error) { | ||
paddleJob, isPaddleJob := o.(*kftraining.PaddleJob) | ||
if !isPaddleJob { | ||
return types.NamespacedName{}, errors.New("not a PaddleJob") | ||
} | ||
|
||
prebuiltWl, hasPrebuiltWorkload := paddleJob.Labels[constants.PrebuiltWorkloadLabel] | ||
if !hasPrebuiltWorkload { | ||
return types.NamespacedName{}, fmt.Errorf("no prebuilt workload found for PaddleJob: %s", klog.KObj(paddleJob)) | ||
} | ||
|
||
return types.NamespacedName{Name: prebuiltWl, Namespace: paddleJob.Namespace}, nil | ||
} |
159 changes: 159 additions & 0 deletions
159
pkg/controller/jobs/kubeflow/jobs/paddlejob/paddlejob_multikueue_adapter_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
Copyright 2024 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 paddlejob | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
kftraining "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/interceptor" | ||
|
||
kueuealpha "sigs.k8s.io/kueue/apis/kueue/v1alpha1" | ||
"sigs.k8s.io/kueue/pkg/controller/constants" | ||
"sigs.k8s.io/kueue/pkg/util/slices" | ||
utiltesting "sigs.k8s.io/kueue/pkg/util/testing" | ||
kfutiltesting "sigs.k8s.io/kueue/pkg/util/testingjobs/paddlejob" | ||
) | ||
|
||
const ( | ||
TestNamespace = "ns" | ||
) | ||
|
||
func TestMultikueueAdapter(t *testing.T) { | ||
objCheckOpts := []cmp.Option{ | ||
cmpopts.IgnoreFields(metav1.ObjectMeta{}, "ResourceVersion"), | ||
cmpopts.EquateEmpty(), | ||
} | ||
|
||
paddleJobBuilder := kfutiltesting.MakePaddleJob("paddlejob1", TestNamespace).Queue("queue").Suspend(false) | ||
|
||
cases := map[string]struct { | ||
managersPaddleJobs []kftraining.PaddleJob | ||
workerPaddleJobs []kftraining.PaddleJob | ||
|
||
operation func(ctx context.Context, adapter *multikueueAdapter, managerClient, workerClient client.Client) error | ||
|
||
wantError error | ||
wantManagersPaddleJobs []kftraining.PaddleJob | ||
wantWorkerPaddleJobs []kftraining.PaddleJob | ||
}{ | ||
"sync creates missing remote PaddleJob": { | ||
managersPaddleJobs: []kftraining.PaddleJob{ | ||
*paddleJobBuilder.Clone().Obj(), | ||
}, | ||
operation: func(ctx context.Context, adapter *multikueueAdapter, managerClient, workerClient client.Client) error { | ||
return adapter.SyncJob(ctx, managerClient, workerClient, types.NamespacedName{Name: "paddlejob1", Namespace: TestNamespace}, "wl1", "origin1") | ||
}, | ||
|
||
wantManagersPaddleJobs: []kftraining.PaddleJob{ | ||
*paddleJobBuilder.Clone().Obj(), | ||
}, | ||
wantWorkerPaddleJobs: []kftraining.PaddleJob{ | ||
*paddleJobBuilder.Clone(). | ||
Label(constants.PrebuiltWorkloadLabel, "wl1"). | ||
Label(kueuealpha.MultiKueueOriginLabel, "origin1"). | ||
Obj(), | ||
}, | ||
}, | ||
"sync status from remote PaddleJob": { | ||
managersPaddleJobs: []kftraining.PaddleJob{ | ||
*paddleJobBuilder.Clone().Obj(), | ||
}, | ||
workerPaddleJobs: []kftraining.PaddleJob{ | ||
*paddleJobBuilder.Clone(). | ||
Label(constants.PrebuiltWorkloadLabel, "wl1"). | ||
Label(kueuealpha.MultiKueueOriginLabel, "origin1"). | ||
StatusConditions(kftraining.JobCondition{Type: kftraining.JobSucceeded, Status: corev1.ConditionTrue}). | ||
Obj(), | ||
}, | ||
operation: func(ctx context.Context, adapter *multikueueAdapter, managerClient, workerClient client.Client) error { | ||
return adapter.SyncJob(ctx, managerClient, workerClient, types.NamespacedName{Name: "paddlejob1", Namespace: TestNamespace}, "wl1", "origin1") | ||
}, | ||
|
||
wantManagersPaddleJobs: []kftraining.PaddleJob{ | ||
*paddleJobBuilder.Clone(). | ||
StatusConditions(kftraining.JobCondition{Type: kftraining.JobSucceeded, Status: corev1.ConditionTrue}). | ||
Obj(), | ||
}, | ||
wantWorkerPaddleJobs: []kftraining.PaddleJob{ | ||
*paddleJobBuilder.Clone(). | ||
Label(constants.PrebuiltWorkloadLabel, "wl1"). | ||
Label(kueuealpha.MultiKueueOriginLabel, "origin1"). | ||
StatusConditions(kftraining.JobCondition{Type: kftraining.JobSucceeded, Status: corev1.ConditionTrue}). | ||
Obj(), | ||
}, | ||
}, | ||
"remote PaddleJob is deleted": { | ||
workerPaddleJobs: []kftraining.PaddleJob{ | ||
*paddleJobBuilder.Clone(). | ||
Label(constants.PrebuiltWorkloadLabel, "wl1"). | ||
Label(kueuealpha.MultiKueueOriginLabel, "origin1"). | ||
Obj(), | ||
}, | ||
operation: func(ctx context.Context, adapter *multikueueAdapter, managerClient, workerClient client.Client) error { | ||
return adapter.DeleteRemoteObject(ctx, workerClient, types.NamespacedName{Name: "paddlejob1", Namespace: TestNamespace}) | ||
}, | ||
}, | ||
} | ||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
managerBuilder := utiltesting.NewClientBuilder(kftraining.AddToScheme).WithInterceptorFuncs(interceptor.Funcs{SubResourcePatch: utiltesting.TreatSSAAsStrategicMerge}) | ||
managerBuilder = managerBuilder.WithLists(&kftraining.PaddleJobList{Items: tc.managersPaddleJobs}) | ||
managerBuilder = managerBuilder.WithStatusSubresource(slices.Map(tc.managersPaddleJobs, func(w *kftraining.PaddleJob) client.Object { return w })...) | ||
managerClient := managerBuilder.Build() | ||
|
||
workerBuilder := utiltesting.NewClientBuilder(kftraining.AddToScheme).WithInterceptorFuncs(interceptor.Funcs{SubResourcePatch: utiltesting.TreatSSAAsStrategicMerge}) | ||
workerBuilder = workerBuilder.WithLists(&kftraining.PaddleJobList{Items: tc.workerPaddleJobs}) | ||
workerClient := workerBuilder.Build() | ||
|
||
ctx, _ := utiltesting.ContextWithLog(t) | ||
|
||
adapter := &multikueueAdapter{} | ||
|
||
gotErr := tc.operation(ctx, adapter, managerClient, workerClient) | ||
|
||
if diff := cmp.Diff(tc.wantError, gotErr, cmpopts.EquateErrors()); diff != "" { | ||
t.Errorf("unexpected error (-want/+got):\n%s", diff) | ||
} | ||
|
||
gotManagersPaddleJob := &kftraining.PaddleJobList{} | ||
if err := managerClient.List(ctx, gotManagersPaddleJob); err != nil { | ||
t.Errorf("unexpected list manager's PaddleJobs error %s", err) | ||
} else { | ||
if diff := cmp.Diff(tc.wantManagersPaddleJobs, gotManagersPaddleJob.Items, objCheckOpts...); diff != "" { | ||
t.Errorf("unexpected manager's PaddleJobs (-want/+got):\n%s", diff) | ||
} | ||
} | ||
|
||
gotWorkerPaddleJobs := &kftraining.PaddleJobList{} | ||
if err := workerClient.List(ctx, gotWorkerPaddleJobs); err != nil { | ||
t.Errorf("unexpected list worker's PaddleJobs error %s", err) | ||
} else { | ||
if diff := cmp.Diff(tc.wantWorkerPaddleJobs, gotWorkerPaddleJobs.Items, objCheckOpts...); diff != "" { | ||
t.Errorf("unexpected worker's PaddleJobs (-want/+got):\n%s", diff) | ||
} | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.