/
worker.go
416 lines (367 loc) · 11.8 KB
/
worker.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
Copyright 2023 KubeAGI.
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 worker
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/kubeagi/arcadia/api/base/v1alpha1"
"github.com/kubeagi/arcadia/apiserver/config"
"github.com/kubeagi/arcadia/apiserver/graph/generated"
"github.com/kubeagi/arcadia/apiserver/pkg/common"
gqlmodel "github.com/kubeagi/arcadia/apiserver/pkg/model"
graphqlutils "github.com/kubeagi/arcadia/apiserver/pkg/utils"
)
const (
NvidiaGPU = "nvidia.com/gpu"
)
func worker2modelConverter(ctx context.Context, c client.Client, showModel bool) func(object client.Object) (generated.PageNode, error) {
return func(object client.Object) (generated.PageNode, error) {
u, ok := object.(*v1alpha1.Worker)
if !ok {
return nil, errors.New("can't convert object to Worker")
}
return Worker2model(ctx, c, u, showModel)
}
}
// Worker2model convert unstructured `CR Worker` to graphql model
func Worker2model(ctx context.Context, c client.Client, worker *v1alpha1.Worker, showModel bool) (*generated.Worker, error) {
id := string(worker.GetUID())
creationtimestamp := worker.GetCreationTimestamp().Time
// conditioned status
condition := worker.Status.GetCondition(v1alpha1.TypeReady)
updateTime := condition.LastTransitionTime.Time
// Unknown,Pending ,Running ,Error
status := common.GetObjStatus(worker)
message := condition.Message
// replicas
var replicas string
if worker.Spec.Replicas != nil {
replicas = fmt.Sprint(*worker.Spec.Replicas)
}
// resources
cpu := worker.Spec.Resources.Limits[v1.ResourceCPU]
cpuStr := cpu.String()
memory := worker.Spec.Resources.Limits[v1.ResourceMemory]
memoryStr := memory.String()
nvidiaGPU := worker.Spec.Resources.Limits[NvidiaGPU]
nvidiaGPUStr := nvidiaGPU.String()
resources := generated.Resources{
CPU: &cpuStr,
Memory: &memoryStr,
NvidiaGpu: &nvidiaGPUStr,
}
matchExpressions := make([]*generated.NodeSelectorRequirement, 0)
if worker.Spec.MatchExpressions != nil {
for _, nodeSelector := range worker.Spec.MatchExpressions {
matchExpressions = append(matchExpressions, &generated.NodeSelectorRequirement{
Key: nodeSelector.Key,
Operator: string(nodeSelector.Operator),
Values: nodeSelector.Values,
})
}
}
// additional envs
additionalEnvs := make(map[string]interface{})
if worker.Spec.AdditionalEnvs != nil {
for _, env := range worker.Spec.AdditionalEnvs {
additionalEnvs[env.Name] = env.Value
}
}
workerType := string(worker.Type())
// wrap Worker
w := generated.Worker{
ID: &id,
Name: worker.Name,
Namespace: worker.Namespace,
Creator: &worker.Spec.Creator,
Labels: graphqlutils.MapStr2Any(worker.GetLabels()),
Annotations: graphqlutils.MapStr2Any(worker.GetAnnotations()),
DisplayName: &worker.Spec.DisplayName,
Description: &worker.Spec.Description,
Type: &workerType,
Status: &status,
Message: &message,
CreationTimestamp: &creationtimestamp,
UpdateTimestamp: &updateTime,
Replicas: &replicas,
Resources: resources,
MatchExpressions: matchExpressions,
AdditionalEnvs: additionalEnvs,
ModelTypes: "unknown",
API: new(string),
}
if r := worker.Labels[v1alpha1.WorkerModelTypesLabel]; r != "" {
w.ModelTypes = strings.ReplaceAll(r, "_", ",")
}
// read worker's models
if worker.Spec.Model != nil && showModel {
typedModel := worker.Model()
model, err := gqlmodel.ReadModel(ctx, c, typedModel.Name, *typedModel.Namespace)
if err != nil {
klog.V(1).ErrorS(err, "worker has no model defined", "worker", worker.Name)
} else {
w.ModelTypes = model.Types
}
w.Model = generated.TypedObjectReference{
APIGroup: typedModel.APIGroup,
Kind: typedModel.Kind,
Name: typedModel.Name,
Namespace: typedModel.Namespace,
}
}
return &w, nil
}
func CreateWorker(ctx context.Context, c client.Client, input generated.CreateWorkerInput) (*generated.Worker, error) {
displayName, description := "", ""
if input.DisplayName != nil {
displayName = *input.DisplayName
}
if input.Description != nil {
description = *input.Description
}
// set the model's namespace
modelNs := input.Namespace
if input.Model.Namespace != nil {
modelNs = *input.Model.Namespace
if modelNs != input.Namespace && modelNs != config.GetConfig().SystemNamespace {
return nil, errors.Errorf("You are trying to use a model in another namespace %s which is not our system namespace: %s", modelNs, config.GetConfig().SystemNamespace)
}
}
// Use `fastchat` as the default worker type
workerType := v1alpha1.DefaultWorkerType()
if input.Type != nil {
workerType = v1alpha1.WorkerType(*input.Type)
}
// set node selectors
var matchExpressions = make([]v1.NodeSelectorRequirement, 0)
if input.MatchExpressions != nil {
for _, nodeSelector := range input.MatchExpressions {
matchExpressions = append(matchExpressions, v1.NodeSelectorRequirement{
Key: nodeSelector.Key,
Operator: v1.NodeSelectorOperator(nodeSelector.Operator),
Values: nodeSelector.Values,
})
}
}
// set additional environment variables
var additionalEnvs = make([]v1.EnvVar, 0)
if input.AdditionalEnvs != nil {
for k, v := range input.AdditionalEnvs {
additionalEnvs = append(additionalEnvs, v1.EnvVar{
Name: k,
Value: fmt.Sprint(v),
})
}
}
worker := &v1alpha1.Worker{
ObjectMeta: metav1.ObjectMeta{
Name: input.Name,
Namespace: input.Namespace,
},
Spec: v1alpha1.WorkerSpec{
CommonSpec: v1alpha1.CommonSpec{
DisplayName: displayName,
Description: description,
},
Type: workerType,
Model: &v1alpha1.TypedObjectReference{
APIGroup: pointer.String(v1alpha1.GroupVersion.String()),
Name: input.Model.Name,
Namespace: &modelNs,
Kind: "Model",
},
AdditionalEnvs: additionalEnvs,
MatchExpressions: matchExpressions,
},
}
common.SetCreator(ctx, &worker.Spec.CommonSpec)
// cpu & memory
resources := v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceCPU: resource.MustParse(input.Resources.CPU),
v1.ResourceMemory: resource.MustParse(input.Resources.Memory),
},
}
// gpu (only nvidia gpu supported now)
if input.Resources.NvidiaGpu != nil {
resources.Limits[NvidiaGPU] = resource.MustParse(*input.Resources.NvidiaGpu)
}
worker.Spec.Resources = resources
err := c.Create(ctx, worker)
if err != nil {
return nil, err
}
api, _ := common.GetAPIServer(ctx, c, true)
w, err := Worker2model(ctx, c, worker, true)
if err != nil {
return nil, err
}
*w.API = api
return w, nil
}
func UpdateWorker(ctx context.Context, c client.Client, input *generated.UpdateWorkerInput) (*generated.Worker, error) {
worker := &v1alpha1.Worker{}
err := c.Get(ctx, types.NamespacedName{Namespace: input.Namespace, Name: input.Name}, worker)
if err != nil {
return nil, err
}
worker.SetLabels(graphqlutils.MapAny2Str(input.Labels))
worker.SetAnnotations(graphqlutils.MapAny2Str(input.Annotations))
if input.DisplayName != nil {
worker.Spec.DisplayName = *input.DisplayName
}
if input.Description != nil {
worker.Spec.Description = *input.Description
}
// worker type
if input.Type != nil {
if worker.Type() != v1alpha1.WorkerType(*input.Type) {
worker.Spec.Type = v1alpha1.WorkerType(*input.Type)
}
}
// replicas
if input.Replicas != nil {
replicas, err := strconv.ParseInt(*input.Replicas, 10, 32)
if err != nil {
return nil, errors.Wrap(err, "Invalid replicas")
}
replicasInt32 := int32(replicas)
worker.Spec.Replicas = &replicasInt32
}
// resources
if input.Resources != nil {
// cpu & memory
resources := v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceCPU: resource.MustParse(input.Resources.CPU),
v1.ResourceMemory: resource.MustParse(input.Resources.Memory),
},
}
// gpu (only nvidia gpu supported now)
if input.Resources.NvidiaGpu != nil {
resources.Limits["nvidia.com/gpu"] = resource.MustParse(*input.Resources.NvidiaGpu)
}
worker.Spec.Resources = resources
}
// set node selectors
if input.MatchExpressions != nil {
var matchExpressions = make([]v1.NodeSelectorRequirement, 0)
for _, nodeSelector := range input.MatchExpressions {
matchExpressions = append(matchExpressions, v1.NodeSelectorRequirement{
Key: nodeSelector.Key,
Operator: v1.NodeSelectorOperator(nodeSelector.Operator),
Values: nodeSelector.Values,
})
}
worker.Spec.MatchExpressions = matchExpressions
}
// set additional environment variables
if input.AdditionalEnvs != nil {
var additionalEnvs = make([]v1.EnvVar, 0)
for k, v := range input.AdditionalEnvs {
additionalEnvs = append(additionalEnvs, v1.EnvVar{
Name: k,
Value: fmt.Sprint(v),
})
}
worker.Spec.AdditionalEnvs = additionalEnvs
}
err = c.Update(ctx, worker)
if err != nil {
return nil, err
}
api, _ := common.GetAPIServer(ctx, c, true)
w, err := Worker2model(ctx, c, worker, true)
if err != nil {
return nil, err
}
*w.API = api
return w, nil
}
func DeleteWorkers(ctx context.Context, c client.Client, input *generated.DeleteCommonInput) (*string, error) {
opts, err := common.DeleteAllOptions(input)
if err != nil {
return nil, err
}
err = c.DeleteAllOf(ctx, &v1alpha1.Worker{}, opts...)
return nil, err
}
func ListWorkers(ctx context.Context, c client.Client, input generated.ListWorkerInput, showWorkerModel bool, listOpts ...common.ListOptionsFunc) (*generated.PaginatedResult, error) {
opts := common.DefaultListOptions()
for _, optFunc := range listOpts {
optFunc(opts)
}
filter := make([]common.ResourceFilter, 0)
page := pointer.IntDeref(input.Page, 1)
pageSize := pointer.IntDeref(input.PageSize, -1)
if input.Keyword != nil {
filter = append(filter, common.FilterWorkerByKeyword(*input.Keyword))
}
if input.ModelTypes != nil {
filter = append(filter, common.FilterWorkerByType(c, input.Namespace, *input.ModelTypes))
}
us := &v1alpha1.WorkerList{}
options, err := common.NewListOptions(generated.ListCommonInput{
Namespace: input.Namespace,
Keyword: input.Keyword,
LabelSelector: input.LabelSelector,
FieldSelector: input.FieldSelector,
Page: input.Page,
PageSize: input.PageSize,
})
if err != nil {
return nil, err
}
err = c.List(ctx, us, options...)
if err != nil {
return nil, err
}
items := make([]client.Object, len(us.Items))
for i := range us.Items {
items[i] = &us.Items[i]
}
list, err := common.ListReources(items, page, pageSize, worker2modelConverter(ctx, c, showWorkerModel), filter...)
if err != nil {
return nil, err
}
api, _ := common.GetAPIServer(ctx, c, true)
for i := range list.Nodes {
tmp := list.Nodes[i].(*generated.Worker)
*tmp.API = api
}
return list, nil
}
func ReadWorker(ctx context.Context, c client.Client, name, namespace string) (*generated.Worker, error) {
u := &v1alpha1.Worker{}
err := c.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, u)
if err != nil {
return nil, err
}
api, _ := common.GetAPIServer(ctx, c, true)
w, err := Worker2model(ctx, c, u, true)
if err != nil {
return nil, err
}
*w.API = api
return w, nil
}