-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
390 lines (342 loc) · 13.5 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
Copyright 2018 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 client
import (
"context"
"fmt"
"log"
kubeflowv1 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
unstructured "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
pkgApi "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
restclient "k8s.io/client-go/rest"
)
//go:generate mockgen -source=./client.go -destination=./mock/client_generated.go -package=mock
type Client interface {
// PyTorchJob CRUD operations
CreatePyTorchJob(job *kubeflowv1.PyTorchJob) error
GetPyTorchJob(namespace string, name string) (*kubeflowv1.PyTorchJob, error)
UpdatePyTorchJob(namespace string, name string, job *kubeflowv1.PyTorchJob, data []byte) error
DeletePyTorchJob(namespace string, name string) error
// generate TFJob, MPIJob, XGBoostJob, PaddleJob CRUD
CreateTFJob(job *kubeflowv1.TFJob) error
GetTFJob(namespace string, name string) (*kubeflowv1.TFJob, error)
UpdateTFJob(namespace string, name string, job *kubeflowv1.TFJob, data []byte) error
DeleteTFJob(namespace string, name string) error
CreateMPIJob(job *kubeflowv1.MPIJob) error
GetMPIJob(namespace string, name string) (*kubeflowv1.MPIJob, error)
UpdateMPIJob(namespace string, name string, job *kubeflowv1.MPIJob, data []byte) error
DeleteMPIJob(namespace string, name string) error
CreateXGBoostJob(job *kubeflowv1.XGBoostJob) error
GetXGBoostJob(namespace string, name string) (*kubeflowv1.XGBoostJob, error)
UpdateXGBoostJob(namespace string, name string, job *kubeflowv1.XGBoostJob, data []byte) error
DeleteXGBoostJob(namespace string, name string) error
CreatePaddleJob(job *kubeflowv1.PaddleJob) error
GetPaddleJob(namespace string, name string) (*kubeflowv1.PaddleJob, error)
UpdatePaddleJob(namespace string, name string, job *kubeflowv1.PaddleJob, data []byte) error
DeletePaddleJob(namespace string, name string) error
}
type client struct {
dynamicClient dynamic.Interface
}
// CreateMPIJob implements Client
func (c *client) CreateMPIJob(mpij *kubeflowv1.MPIJob) error {
mpijUpdateTypeMeta(mpij)
return c.createResource(mpij, mpij.Namespace, mpijRes())
}
// CreatePaddleJob implements Client
func (c *client) CreatePaddleJob(pj *kubeflowv1.PaddleJob) error {
pjUpdateTypeMeta(pj)
return c.createResource(pj, pj.Namespace, pjRes())
}
// CreateTFJob implements Client
func (c *client) CreateTFJob(tfj *kubeflowv1.TFJob) error {
tfjUpdateTypeMeta(tfj)
return c.createResource(tfj, tfj.Namespace, tfjRes())
}
// CreateXGBoostJob implements Client
func (c *client) CreateXGBoostJob(xgbj *kubeflowv1.XGBoostJob) error {
xgbjUpdateTypeMeta(xgbj)
return c.createResource(xgbj, xgbj.Namespace, xgbjRes())
}
// DeleteMPIJob implements Client
func (c *client) DeleteMPIJob(namespace string, name string) error {
return c.deleteResource(namespace, name, mpijRes())
}
// DeletePaddleJob implements Client
func (c *client) DeletePaddleJob(namespace string, name string) error {
return c.deleteResource(namespace, name, pjRes())
}
// DeleteTFJob implements Client
func (c *client) DeleteTFJob(namespace string, name string) error {
return c.deleteResource(namespace, name, tfjRes())
}
// DeleteXGBoostJob implements Client
func (c *client) DeleteXGBoostJob(namespace string, name string) error {
return c.deleteResource(namespace, name, xgbjRes())
}
// GetMPIJob implements Client
func (c *client) GetMPIJob(namespace string, name string) (*kubeflowv1.MPIJob, error) {
var mpij kubeflowv1.MPIJob
resp, err := c.getResource(namespace, name, mpijRes())
if err != nil {
if errors.IsNotFound(err) {
log.Printf("[Warning] MPIJob %s not found (namespace=%s)", name, namespace)
return nil, err
}
msg := fmt.Sprintf("Failed to get MPIJob, with error: %v", err)
log.Printf("[Error] %s", msg)
return nil, fmt.Errorf(msg)
}
unstructured := resp.UnstructuredContent()
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, &mpij); err != nil {
msg := fmt.Sprintf("Failed to translate unstructed to MPIJob, with error: %v", err)
log.Printf("[Error] %s", msg)
return nil, fmt.Errorf(msg)
}
return &mpij, nil
}
// GetPaddleJob implements Client
func (c *client) GetPaddleJob(namespace string, name string) (*kubeflowv1.PaddleJob, error) {
var pj kubeflowv1.PaddleJob
resp, err := c.getResource(namespace, name, pjRes())
if err != nil {
if errors.IsNotFound(err) {
log.Printf("[Warning] PaddleJob %s not found (namespace=%s)", name, namespace)
return nil, err
}
msg := fmt.Sprintf("Failed to get PaddleJob, with error: %v", err)
log.Printf("[Error] %s", msg)
return nil, fmt.Errorf(msg)
}
unstructured := resp.UnstructuredContent()
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, &pj); err != nil {
msg := fmt.Sprintf("Failed to translate unstructed to PaddleJob, with error: %v", err)
log.Printf("[Error] %s", msg)
return nil, fmt.Errorf(msg)
}
return &pj, nil
}
// GetTFJob implements Client
func (c *client) GetTFJob(namespace string, name string) (*kubeflowv1.TFJob, error) {
var tfj kubeflowv1.TFJob
resp, err := c.getResource(namespace, name, tfjRes())
if err != nil {
if errors.IsNotFound(err) {
log.Printf("[Warning] TFJob %s not found (namespace=%s)", name, namespace)
return nil, err
}
msg := fmt.Sprintf("Failed to get TFJob, with error: %v", err)
log.Printf("[Error] %s", msg)
return nil, fmt.Errorf(msg)
}
unstructured := resp.UnstructuredContent()
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, &tfj); err != nil {
msg := fmt.Sprintf("Failed to translate unstructed to TFJob, with error: %v", err)
log.Printf("[Error] %s", msg)
return nil, fmt.Errorf(msg)
}
return &tfj, nil
}
// GetXGBoostJob implements Client
func (c *client) GetXGBoostJob(namespace string, name string) (*kubeflowv1.XGBoostJob, error) {
var xgbj kubeflowv1.XGBoostJob
resp, err := c.getResource(namespace, name, xgbjRes())
if err != nil {
if errors.IsNotFound(err) {
log.Printf("[Warning] XGBoostJob %s not found (namespace=%s)", name, namespace)
return nil, err
}
msg := fmt.Sprintf("Failed to get XGBoostJob, with error: %v", err)
log.Printf("[Error] %s", msg)
return nil, fmt.Errorf(msg)
}
unstructured := resp.UnstructuredContent()
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, &xgbj); err != nil {
msg := fmt.Sprintf("Failed to translate unstructed to XGBoostJob, with error: %v", err)
log.Printf("[Error] %s", msg)
return nil, fmt.Errorf(msg)
}
return &xgbj, nil
}
// UpdateMPIJob implements Client
func (c *client) UpdateMPIJob(namespace string, name string, job *kubeflowv1.MPIJob, data []byte) error {
mpijUpdateTypeMeta(job)
return c.updateResource(namespace, name, mpijRes(), job, data)
}
// UpdatePaddleJob implements Client
func (c *client) UpdatePaddleJob(namespace string, name string, job *kubeflowv1.PaddleJob, data []byte) error {
pjUpdateTypeMeta(job)
return c.updateResource(namespace, name, pjRes(), job, data)
}
// UpdateTFJob implements Client
func (c *client) UpdateTFJob(namespace string, name string, job *kubeflowv1.TFJob, data []byte) error {
tfjUpdateTypeMeta(job)
return c.updateResource(namespace, name, tfjRes(), job, data)
}
// UpdateXGBoostJob implements Client
func (c *client) UpdateXGBoostJob(namespace string, name string, job *kubeflowv1.XGBoostJob, data []byte) error {
xgbjUpdateTypeMeta(job)
return c.updateResource(namespace, name, xgbjRes(), job, data)
}
// CreatePyTorchJob implements Client
func (c *client) CreatePyTorchJob(ptj *kubeflowv1.PyTorchJob) error {
ptjUpdateTypeMeta(ptj)
return c.createResource(ptj, ptj.Namespace, ptjRes())
}
// DeletePyTorchJob implements Client
func (c *client) DeletePyTorchJob(namespace string, name string) error {
return c.deleteResource(namespace, name, ptjRes())
}
// GetPyTorchJob implements Client
func (c *client) GetPyTorchJob(namespace string, name string) (*kubeflowv1.PyTorchJob, error) {
var ptj kubeflowv1.PyTorchJob
resp, err := c.getResource(namespace, name, ptjRes())
if err != nil {
if errors.IsNotFound(err) {
log.Printf("[Warning] PyTorchJob %s not found (namespace=%s)", name, namespace)
return nil, err
}
msg := fmt.Sprintf("Failed to get PyTorchJob, with error: %v", err)
log.Printf("[Error] %s", msg)
return nil, fmt.Errorf(msg)
}
unstructured := resp.UnstructuredContent()
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, &ptj); err != nil {
msg := fmt.Sprintf("Failed to translate unstructed to PyTorchJob, with error: %v", err)
log.Printf("[Error] %s", msg)
return nil, fmt.Errorf(msg)
}
return &ptj, nil
}
// UpdatePyTorchJob implements Client
func (c *client) UpdatePyTorchJob(namespace string, name string, job *kubeflowv1.PyTorchJob, data []byte) error {
ptjUpdateTypeMeta(job)
return c.updateResource(namespace, name, ptjRes(), job, data)
}
func ptjUpdateTypeMeta(job *kubeflowv1.PyTorchJob) {
job.TypeMeta = metav1.TypeMeta{
Kind: "PyTorchJob",
APIVersion: kubeflowv1.GroupVersion.String(),
}
}
func ptjRes() schema.GroupVersionResource {
return schema.GroupVersionResource{
Group: kubeflowv1.GroupVersion.Group,
Version: kubeflowv1.GroupVersion.Version,
Resource: "pytorchjobs",
}
}
func tfjUpdateTypeMeta(job *kubeflowv1.TFJob) {
job.TypeMeta = metav1.TypeMeta{
Kind: "TFJob",
APIVersion: kubeflowv1.GroupVersion.String(),
}
}
func tfjRes() schema.GroupVersionResource {
return schema.GroupVersionResource{
Group: kubeflowv1.GroupVersion.Group,
Version: kubeflowv1.GroupVersion.Version,
Resource: "tfjobs",
}
}
func mpijUpdateTypeMeta(job *kubeflowv1.MPIJob) {
job.TypeMeta = metav1.TypeMeta{
Kind: "MPIJob",
APIVersion: kubeflowv1.GroupVersion.String(),
}
}
func mpijRes() schema.GroupVersionResource {
return schema.GroupVersionResource{
Group: kubeflowv1.GroupVersion.Group,
Version: kubeflowv1.GroupVersion.Version,
Resource: "mpijobs",
}
}
func xgbjUpdateTypeMeta(job *kubeflowv1.XGBoostJob) {
job.TypeMeta = metav1.TypeMeta{
Kind: "XGBoostJob",
APIVersion: kubeflowv1.GroupVersion.String(),
}
}
func xgbjRes() schema.GroupVersionResource {
return schema.GroupVersionResource{
Group: kubeflowv1.GroupVersion.Group,
Version: kubeflowv1.GroupVersion.Version,
Resource: "xgboostjobs",
}
}
func pjUpdateTypeMeta(job *kubeflowv1.PaddleJob) {
job.TypeMeta = metav1.TypeMeta{
Kind: "PaddleJob",
APIVersion: kubeflowv1.GroupVersion.String(),
}
}
func pjRes() schema.GroupVersionResource {
return schema.GroupVersionResource{
Group: kubeflowv1.GroupVersion.Group,
Version: kubeflowv1.GroupVersion.Version,
Resource: "paddlejobs",
}
}
// New creates our client wrapper object for the actual kubeVirt and kubernetes clients we use.
func NewClient(cfg *restclient.Config) (Client, error) {
result := &client{}
c, err := dynamic.NewForConfig(cfg)
if err != nil {
msg := fmt.Sprintf("Failed to create client, with error: %v", err)
log.Printf("[Error] %s", msg)
return nil, fmt.Errorf(msg)
}
result.dynamicClient = c
return result, nil
}
// Generic Resource CRUD operations
func (c *client) createResource(obj interface{}, namespace string, resource schema.GroupVersionResource) error {
resultMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
msg := fmt.Sprintf("Failed to translate %s to Unstructed (for create operation), with error: %v", resource.Resource, err)
log.Printf("[Error] %s", msg)
return fmt.Errorf(msg)
}
input := unstructured.Unstructured{}
input.SetUnstructuredContent(resultMap)
resp, err := c.dynamicClient.Resource(resource).Namespace(namespace).Create(context.Background(), &input, metav1.CreateOptions{})
if err != nil {
msg := fmt.Sprintf("Failed to create %s, with error: %v", resource.Resource, err)
log.Printf("[Error] %s", msg)
return fmt.Errorf(msg)
}
unstructured := resp.UnstructuredContent()
return runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, obj)
}
func (c *client) getResource(namespace string, name string, resource schema.GroupVersionResource) (*unstructured.Unstructured, error) {
return c.dynamicClient.Resource(resource).Namespace(namespace).Get(context.Background(), name, metav1.GetOptions{})
}
func (c *client) updateResource(namespace string, name string, resource schema.GroupVersionResource, obj interface{}, data []byte) error {
resp, err := c.dynamicClient.Resource(resource).Namespace(namespace).Patch(context.Background(), name, pkgApi.JSONPatchType, data, metav1.PatchOptions{})
if err != nil {
msg := fmt.Sprintf("Failed to update %s, with error: %v", resource.Resource, err)
log.Printf("[Error] %s", msg)
return fmt.Errorf(msg)
}
unstructured := resp.UnstructuredContent()
return runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured, obj)
}
func (c *client) deleteResource(namespace string, name string, resource schema.GroupVersionResource) error {
return c.dynamicClient.Resource(resource).Namespace(namespace).Delete(context.Background(), name, metav1.DeleteOptions{})
}