forked from kubeflow/arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kserve_builder.go
353 lines (312 loc) · 9.4 KB
/
kserve_builder.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
package serving
import (
"fmt"
"strings"
"github.com/kubeflow/arena/pkg/apis/types"
"github.com/kubeflow/arena/pkg/argsbuilder"
)
type KServeJobBuilder struct {
args *types.KServeArgs
argValues map[string]interface{}
argsbuilder.ArgsBuilder
}
func NewKServeJobBuilder() *KServeJobBuilder {
args := &types.KServeArgs{
CommonServingArgs: types.CommonServingArgs{
ImagePullPolicy: "IfNotPresent",
Replicas: 1,
Namespace: "default",
Shell: "sh",
},
}
return &KServeJobBuilder{
args: args,
argValues: map[string]interface{}{},
ArgsBuilder: argsbuilder.NewKServeArgsBuilder(args),
}
}
// Name is used to set job name,match option --name
func (b *KServeJobBuilder) Name(name string) *KServeJobBuilder {
if name != "" {
b.args.Name = name
}
return b
}
// Namespace is used to set job namespace,match option --namespace
func (b *KServeJobBuilder) Namespace(namespace string) *KServeJobBuilder {
if namespace != "" {
b.args.Namespace = namespace
}
return b
}
// Shell is used to set bash or sh
func (b *KServeJobBuilder) Shell(shell string) *KServeJobBuilder {
if shell != "" {
b.args.Shell = shell
}
return b
}
// Command is used to set job command
func (b *KServeJobBuilder) Command(args []string) *KServeJobBuilder {
b.args.Command = strings.Join(args, " ")
return b
}
// GPUCount is used to set count of gpu for the job,match the option --gpus
func (b *KServeJobBuilder) GPUCount(count int) *KServeJobBuilder {
if count > 0 {
b.args.GPUCount = count
}
return b
}
// GPUMemory is used to set gpu memory for the job,match the option --gpumemory
func (b *KServeJobBuilder) GPUMemory(memory int) *KServeJobBuilder {
if memory > 0 {
b.args.GPUMemory = memory
}
return b
}
// GPUCore is used to set gpu core for the job, match the option --gpucore
func (b *KServeJobBuilder) GPUCore(core int) *KServeJobBuilder {
if core > 0 {
b.args.GPUCore = core
}
return b
}
// Image is used to set job image,match the option --image
func (b *KServeJobBuilder) Image(image string) *KServeJobBuilder {
if image != "" {
b.args.Image = image
}
return b
}
// ImagePullPolicy is used to set image pull policy,match the option --image-pull-policy
func (b *KServeJobBuilder) ImagePullPolicy(policy string) *KServeJobBuilder {
if policy != "" {
b.args.ImagePullPolicy = policy
}
return b
}
// CPU assign cpu limits,match the option --cpu
func (b *KServeJobBuilder) CPU(cpu string) *KServeJobBuilder {
if cpu != "" {
b.args.Cpu = cpu
}
return b
}
// Memory assign memory limits,match option --memory
func (b *KServeJobBuilder) Memory(memory string) *KServeJobBuilder {
if memory != "" {
b.args.Memory = memory
}
return b
}
// Envs is used to set env of job containers,match option --env
func (b *KServeJobBuilder) Envs(envs map[string]string) *KServeJobBuilder {
if len(envs) != 0 {
envSlice := []string{}
for key, value := range envs {
envSlice = append(envSlice, fmt.Sprintf("%v=%v", key, value))
}
b.argValues["env"] = &envSlice
}
return b
}
// Replicas is used to set serving job replicas,match the option --replicas
func (b *KServeJobBuilder) Replicas(count int) *KServeJobBuilder {
if count > 0 {
b.args.Replicas = count
}
return b
}
// EnableIstio is used to enable istio,match the option --enable-istio
func (b *KServeJobBuilder) EnableIstio() *KServeJobBuilder {
b.args.EnableIstio = true
return b
}
// ExposeService is used to expose service,match the option --expose-service
func (b *KServeJobBuilder) ExposeService() *KServeJobBuilder {
b.args.ExposeService = true
return b
}
// Version is used to set serving job version,match the option --version
func (b *KServeJobBuilder) Version(version string) *KServeJobBuilder {
if version != "" {
b.args.Version = version
}
return b
}
// Tolerations is used to set tolerations for tolerate nodes,match option --toleration
func (b *KServeJobBuilder) Tolerations(tolerations []string) *KServeJobBuilder {
b.argValues["toleration"] = &tolerations
return b
}
// NodeSelectors is used to set node selectors for scheduling job,match option --selector
func (b *KServeJobBuilder) NodeSelectors(selectors map[string]string) *KServeJobBuilder {
if len(selectors) != 0 {
selectorsSlice := []string{}
for key, value := range selectors {
selectorsSlice = append(selectorsSlice, fmt.Sprintf("%v=%v", key, value))
}
b.argValues["selector"] = &selectorsSlice
}
return b
}
// Annotations is used to add annotations for job pods,match option --annotation
func (b *KServeJobBuilder) Annotations(annotations map[string]string) *KServeJobBuilder {
if len(annotations) != 0 {
s := []string{}
for key, value := range annotations {
s = append(s, fmt.Sprintf("%v=%v", key, value))
}
b.argValues["annotation"] = &s
}
return b
}
// Labels is used to add labels for job
func (b *KServeJobBuilder) Labels(labels map[string]string) *KServeJobBuilder {
if len(labels) != 0 {
s := []string{}
for key, value := range labels {
s = append(s, fmt.Sprintf("%v=%v", key, value))
}
b.argValues["label"] = &s
}
return b
}
// Datas is used to mount k8s pvc to job pods,match option --data
func (b *KServeJobBuilder) Datas(volumes map[string]string) *KServeJobBuilder {
if len(volumes) != 0 {
s := []string{}
for key, value := range volumes {
s = append(s, fmt.Sprintf("%v:%v", key, value))
}
b.argValues["data"] = &s
}
return b
}
// DataDirs is used to mount host files to job containers,match option --data-dir
func (b *KServeJobBuilder) DataDirs(volumes map[string]string) *KServeJobBuilder {
if len(volumes) != 0 {
s := []string{}
for key, value := range volumes {
s = append(s, fmt.Sprintf("%v:%v", key, value))
}
b.argValues["data-dir"] = &s
}
return b
}
// ConfigFiles is used to mapping config files form local to job containers,match option --config-file
func (b *KServeJobBuilder) ConfigFiles(files map[string]string) *KServeJobBuilder {
if len(files) != 0 {
filesSlice := []string{}
for localPath, containerPath := range files {
filesSlice = append(filesSlice, fmt.Sprintf("%v:%v", localPath, containerPath))
}
b.argValues["config-file"] = &filesSlice
}
return b
}
// ModelFormat the ModelFormat being served.
func (b *KServeJobBuilder) ModelFormat(modelFormat *types.ModelFormat) *KServeJobBuilder {
if modelFormat != nil {
b.args.ModelFormat = modelFormat
}
return b
}
// Runtime specific ClusterServingRuntime/ServingRuntime name to use for deployment.
func (b *KServeJobBuilder) Runtime(runtime string) *KServeJobBuilder {
if runtime != "" {
b.args.Runtime = runtime
}
return b
}
// StorageUri is used to set storage uri,match the option --storage-uri
func (b *KServeJobBuilder) StorageUri(uri string) *KServeJobBuilder {
if uri != "" {
b.args.StorageUri = uri
}
return b
}
// RuntimeVersion of the predictor docker image
func (b *KServeJobBuilder) RuntimeVersion(runtimeVersion string) *KServeJobBuilder {
if runtimeVersion != "" {
b.args.RuntimeVersion = runtimeVersion
}
return b
}
// ProtocolVersion use by the predictor (i.e. v1 or v2 or grpc-v1 or grpc-v2)
func (b *KServeJobBuilder) ProtocolVersion(protocolVersion string) *KServeJobBuilder {
if protocolVersion != "" {
b.args.ProtocolVersion = protocolVersion
}
return b
}
// MinReplicas number of replicas, defaults to 1 but can be set to 0 to enable scale-to-zero.
func (b *KServeJobBuilder) MinReplicas(minReplicas int) *KServeJobBuilder {
if minReplicas >= 0 {
b.args.MinReplicas = minReplicas
}
return b
}
// MaxReplicas number of replicas for autoscaling.
func (b *KServeJobBuilder) MaxReplicas(maxReplicas int) *KServeJobBuilder {
if maxReplicas > 0 {
b.args.MaxReplicas = maxReplicas
}
return b
}
// ScaleTarget number of replicas for autoscaling.
func (b *KServeJobBuilder) ScaleTarget(scaleTarget int) *KServeJobBuilder {
if scaleTarget > 0 {
b.args.ScaleTarget = scaleTarget
}
return b
}
// ScaleMetric watched by autoscaler. possible values are concurrency, rps, cpu, memory. concurrency, rps are supported via KPA
func (b *KServeJobBuilder) ScaleMetric(scaleMetric string) *KServeJobBuilder {
if scaleMetric != "" {
b.args.ScaleMetric = scaleMetric
}
return b
}
// ContainerConcurrency specifies how many requests can be processed concurrently
func (b *KServeJobBuilder) ContainerConcurrency(containerConcurrency int64) *KServeJobBuilder {
if containerConcurrency > 0 {
b.args.ContainerConcurrency = containerConcurrency
}
return b
}
// TimeoutSeconds specifies the number of seconds to wait before timing out a request to the component.
func (b *KServeJobBuilder) TimeoutSeconds(timeoutSeconds int64) *KServeJobBuilder {
if timeoutSeconds > 0 {
b.args.TimeoutSeconds = timeoutSeconds
}
return b
}
// CanaryTrafficPercent defines the traffic split percentage between the candidate revision and the last ready revision
func (b *KServeJobBuilder) CanaryTrafficPercent(canaryTrafficPercent int64) *KServeJobBuilder {
if canaryTrafficPercent > 0 {
b.args.CanaryTrafficPercent = canaryTrafficPercent
}
return b
}
// Port the port of tcp listening port, default is 8080 in kserve
func (b *KServeJobBuilder) Port(port int) *KServeJobBuilder {
if port > 0 {
b.args.Port = port
}
return b
}
// Build is used to build the job
func (b *KServeJobBuilder) Build() (*Job, error) {
for key, value := range b.argValues {
b.AddArgValue(key, value)
}
if err := b.PreBuild(); err != nil {
return nil, err
}
if err := b.ArgsBuilder.Build(); err != nil {
return nil, err
}
return NewJob(b.args.Name, types.KServeJob, b.args), nil
}