@@ -25,8 +25,6 @@ import (
25
25
log "github.com/sirupsen/logrus"
26
26
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27
27
"k8s.io/apimachinery/pkg/runtime"
28
- "k8s.io/apimachinery/pkg/runtime/schema"
29
- "k8s.io/client-go/tools/cache"
30
28
"k8s.io/client-go/util/workqueue"
31
29
32
30
"github.com/PaddlePaddle/PaddleFlow/pkg/common/k8s"
@@ -45,24 +43,15 @@ var (
45
43
46
44
// KubeMPIJob is a struct that runs a mpi job
47
45
type KubeMPIJob struct {
48
- GVK schema.GroupVersionKind
49
- frameworkVersion pfschema.FrameworkVersion
50
- runtimeClient framework.RuntimeClientInterface
51
- jobQueue workqueue.RateLimitingInterface
46
+ kuberuntime.KubeBaseJob
52
47
}
53
48
54
49
func New (kubeClient framework.RuntimeClientInterface ) framework.JobInterface {
55
50
return & KubeMPIJob {
56
- runtimeClient : kubeClient ,
57
- GVK : JobGVK ,
58
- frameworkVersion : KubeMPIFwVersion ,
51
+ KubeBaseJob : kuberuntime .NewKubeBaseJob (JobGVK , KubeMPIFwVersion , kubeClient ),
59
52
}
60
53
}
61
54
62
- func (mj * KubeMPIJob ) String (name string ) string {
63
- return fmt .Sprintf ("%s job %s on %s" , mj .GVK .String (), name , mj .runtimeClient .Cluster ())
64
- }
65
-
66
55
func (mj * KubeMPIJob ) Submit (ctx context.Context , job * api.PFJob ) error {
67
56
jobName := job .NamespacedName ()
68
57
mpiJob := & mpiv1.MPIJob {}
@@ -87,7 +76,7 @@ func (mj *KubeMPIJob) Submit(ctx context.Context, job *api.PFJob) error {
87
76
return err
88
77
}
89
78
log .Debugf ("begin to create %s, job info: %v" , mj .String (jobName ), mpiJob )
90
- err = mj .runtimeClient .Create (mpiJob , mj .frameworkVersion )
79
+ err = mj .RuntimeClient .Create (mpiJob , mj .FrameworkVersion )
91
80
if err != nil {
92
81
log .Errorf ("create %s failed, err %v" , mj .String (jobName ), err )
93
82
return err
@@ -141,105 +130,17 @@ func (mj *KubeMPIJob) customMPIJobSpec(mpiJobSpec *mpiv1.MPIJobSpec, job *api.PF
141
130
return kuberuntime .KubeflowRunPolicy (& mpiJobSpec .RunPolicy , nil , job .Conf .GetQueueName (), job .Conf .GetPriority ())
142
131
}
143
132
144
- func (mj * KubeMPIJob ) Stop (ctx context.Context , job * api.PFJob ) error {
145
- if job == nil {
146
- return fmt .Errorf ("job is nil" )
147
- }
148
- jobName := job .NamespacedName ()
149
- log .Infof ("begin to stop %s" , mj .String (jobName ))
150
- if err := mj .runtimeClient .Delete (job .Namespace , job .ID , mj .frameworkVersion ); err != nil {
151
- log .Errorf ("stop %s failed, err: %v" , mj .String (jobName ), err )
152
- return err
153
- }
154
- return nil
155
- }
156
-
157
- func (mj * KubeMPIJob ) Update (ctx context.Context , job * api.PFJob ) error {
158
- if job == nil {
159
- return fmt .Errorf ("job is nil" )
160
- }
161
- jobName := job .NamespacedName ()
162
- log .Infof ("begin to update %s" , mj .String (jobName ))
163
- if err := kuberuntime .UpdateKubeJob (job , mj .runtimeClient , mj .frameworkVersion ); err != nil {
164
- log .Errorf ("update %s failed, err: %v" , mj .String (jobName ), err )
165
- return err
166
- }
167
- return nil
168
- }
169
-
170
- func (mj * KubeMPIJob ) Delete (ctx context.Context , job * api.PFJob ) error {
171
- if job == nil {
172
- return fmt .Errorf ("job is nil" )
173
- }
174
- jobName := job .NamespacedName ()
175
- log .Infof ("begin to delete %s " , mj .String (jobName ))
176
- if err := mj .runtimeClient .Delete (job .Namespace , job .ID , mj .frameworkVersion ); err != nil {
177
- log .Errorf ("delete %s failed, err %v" , mj .String (jobName ), err )
178
- return err
179
- }
180
- return nil
181
- }
182
-
183
- func (mj * KubeMPIJob ) GetLog (ctx context.Context , jobLogRequest pfschema.JobLogRequest ) (pfschema.JobLogInfo , error ) {
184
- // TODO: add get log logic
185
- return pfschema.JobLogInfo {}, nil
186
- }
187
-
188
133
func (mj * KubeMPIJob ) AddEventListener (ctx context.Context , listenerType string , jobQueue workqueue.RateLimitingInterface , listener interface {}) error {
189
134
var err error
190
135
switch listenerType {
191
136
case pfschema .ListenerTypeJob :
192
- err = mj .addJobEventListener (ctx , jobQueue , listener )
137
+ err = mj .AddJobEventListener (ctx , jobQueue , listener , mj . JobStatus , nil )
193
138
default :
194
139
err = fmt .Errorf ("listenerType %s is not supported" , listenerType )
195
140
}
196
141
return err
197
142
}
198
143
199
- func (mj * KubeMPIJob ) addJobEventListener (ctx context.Context , jobQueue workqueue.RateLimitingInterface , listener interface {}) error {
200
- if jobQueue == nil || listener == nil {
201
- return fmt .Errorf ("add job event listener failed, err: listener is nil" )
202
- }
203
- mj .jobQueue = jobQueue
204
- informer := listener .(cache.SharedIndexInformer )
205
- informer .AddEventHandler (cache.FilteringResourceEventHandler {
206
- FilterFunc : kuberuntime .ResponsibleForJob ,
207
- Handler : cache.ResourceEventHandlerFuncs {
208
- AddFunc : mj .addJob ,
209
- UpdateFunc : mj .updateJob ,
210
- DeleteFunc : mj .deleteJob ,
211
- },
212
- })
213
- return nil
214
- }
215
-
216
- func (mj * KubeMPIJob ) addJob (obj interface {}) {
217
- jobSyncInfo , err := kuberuntime .JobAddFunc (obj , mj .JobStatus )
218
- if err != nil {
219
- log .Errorf ("add job failed, err: %v" , err )
220
- return
221
- }
222
- mj .jobQueue .Add (jobSyncInfo )
223
- }
224
-
225
- func (mj * KubeMPIJob ) updateJob (old , new interface {}) {
226
- jobSyncInfo , err := kuberuntime .JobUpdateFunc (old , new , mj .JobStatus )
227
- if err != nil {
228
- log .Errorf ("update job failed, err: %v" , err )
229
- return
230
- }
231
- mj .jobQueue .Add (jobSyncInfo )
232
- }
233
-
234
- func (mj * KubeMPIJob ) deleteJob (obj interface {}) {
235
- jobSyncInfo , err := kuberuntime .JobDeleteFunc (obj , mj .JobStatus )
236
- if err != nil {
237
- log .Errorf ("delete job failed, err: %v" , err )
238
- return
239
- }
240
- mj .jobQueue .Add (jobSyncInfo )
241
- }
242
-
243
144
// JobStatus get the statusInfo of mpi job, including origin status, pf status and message
244
145
func (mj * KubeMPIJob ) JobStatus (obj interface {}) (api.StatusInfo , error ) {
245
146
unObj := obj .(* unstructured.Unstructured )
0 commit comments