forked from kubeflow/arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_serving.go
285 lines (255 loc) · 7.92 KB
/
update_serving.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
// Copyright 2018 The Kubeflow 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 argsbuilder
import (
"fmt"
"github.com/kubeflow/arena/pkg/apis/types"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/resource"
"reflect"
"strings"
)
type UpdateServingArgsBuilder struct {
args *types.CommonUpdateServingArgs
argValues map[string]interface{}
subBuilders map[string]ArgsBuilder
}
func NewUpdateServingArgsBuilder(args *types.CommonUpdateServingArgs) ArgsBuilder {
s := &UpdateServingArgsBuilder{
args: args,
argValues: map[string]interface{}{},
subBuilders: map[string]ArgsBuilder{},
}
return s
}
func (s *UpdateServingArgsBuilder) GetName() string {
items := strings.Split(fmt.Sprintf("%v", reflect.TypeOf(*s)), ".")
return items[len(items)-1]
}
func (s *UpdateServingArgsBuilder) AddSubBuilder(builders ...ArgsBuilder) ArgsBuilder {
for _, b := range builders {
s.subBuilders[b.GetName()] = b
}
return s
}
func (s *UpdateServingArgsBuilder) AddArgValue(key string, value interface{}) ArgsBuilder {
for name := range s.subBuilders {
s.subBuilders[name].AddArgValue(key, value)
}
s.argValues[key] = value
return s
}
func (s *UpdateServingArgsBuilder) AddCommandFlags(command *cobra.Command) {
for name := range s.subBuilders {
s.subBuilders[name].AddCommandFlags(command)
}
var (
annotations []string
labels []string
envs []string
selectors []string
tolerations []string
)
command.Flags().StringVar(&s.args.Name, "name", "", "the serving name")
command.Flags().StringVar(&s.args.Version, "version", "", "the serving version")
command.Flags().StringVar(&s.args.Image, "image", "", "the docker image name of serving job")
command.Flags().IntVar(&s.args.GPUCount, "gpus", 0, "the limit GPU count of each replica to run the serve.")
command.Flags().IntVar(&s.args.GPUMemory, "gpumemory", 0, "the limit GPU memory of each replica to run the serve.")
command.Flags().IntVar(&s.args.GPUCore, "gpucore", 0, "the limit GPU core of each replica to run the serve.")
command.Flags().StringVar(&s.args.Cpu, "cpu", "", "the request cpu of each replica to run the serve.")
command.Flags().StringVar(&s.args.Memory, "memory", "", "the request memory of each replica to run the serve.")
command.Flags().IntVar(&s.args.Replicas, "replicas", 0, "the replicas number of the serve job.")
command.Flags().StringArrayVarP(&envs, "env", "e", []string{}, "the environment variables")
command.Flags().StringArrayVarP(&annotations, "annotation", "a", []string{}, `specify the annotations, usage: "--annotation=key=value" or "--annotation key=value"`)
command.Flags().StringArrayVarP(&labels, "label", "l", []string{}, "specify the labels")
command.Flags().StringVar(&s.args.Command, "command", "", "the command will inject to container's command.")
command.Flags().StringArrayVarP(&selectors, "selector", "", []string{}, `assigning jobs to some k8s particular nodes, usage: "--selector=key=value" or "--selector key=value" `)
command.Flags().StringArrayVarP(&tolerations, "toleration", "", []string{}, `tolerate some k8s nodes with taints,usage: "--toleration key=value:effect,operator" or "--toleration all" `)
s.AddArgValue("env", &envs).
AddArgValue("annotation", &annotations).
AddArgValue("selector", &selectors).
AddArgValue("label", &labels).
AddArgValue("toleration", &tolerations)
}
func (s *UpdateServingArgsBuilder) PreBuild() error {
for name := range s.subBuilders {
if err := s.subBuilders[name].PreBuild(); err != nil {
return err
}
}
if err := s.checkNamespace(); err != nil {
return err
}
if err := s.checkName(); err != nil {
return err
}
if err := s.checkReplicas(); err != nil {
return err
}
if err := s.setEnvs(); err != nil {
return err
}
if err := s.setAnnotations(); err != nil {
return err
}
if err := s.setNodeSelectors(); err != nil {
return err
}
if err := s.setTolerations(); err != nil {
return err
}
if err := s.setLabels(); err != nil {
return err
}
if err := s.check(); err != nil {
return err
}
return nil
}
func (s *UpdateServingArgsBuilder) Build() error {
for name := range s.subBuilders {
if err := s.subBuilders[name].Build(); err != nil {
return err
}
}
return nil
}
func (s *UpdateServingArgsBuilder) check() error {
if s.args.GPUCount < 0 {
return fmt.Errorf("--gpus is invalid")
}
if s.args.GPUMemory < 0 {
return fmt.Errorf("--gpumemory is invalid")
}
if s.args.Cpu != "" {
_, err := resource.ParseQuantity(s.args.Cpu)
if err != nil {
return fmt.Errorf("--cpu is invalid")
}
}
if s.args.Memory != "" {
_, err := resource.ParseQuantity(s.args.Memory)
if err != nil {
return fmt.Errorf("--memory is invalid")
}
}
return nil
}
func (s *UpdateServingArgsBuilder) setEnvs() error {
argKey := "env"
var envs *[]string
value, ok := s.argValues[argKey]
if !ok {
return nil
}
envs = value.(*[]string)
s.args.Envs = transformSliceToMap(*envs, "=")
return nil
}
// setNodeSelectors is used to handle option --selector
func (s *UpdateServingArgsBuilder) setNodeSelectors() error {
s.args.NodeSelectors = map[string]string{}
argKey := "selector"
var nodeSelectors *[]string
value, ok := s.argValues[argKey]
if !ok {
return nil
}
nodeSelectors = value.(*[]string)
log.Debugf("node selectors: %v", *nodeSelectors)
s.args.NodeSelectors = transformSliceToMap(*nodeSelectors, "=")
return nil
}
func (s *UpdateServingArgsBuilder) setTolerations() error {
if s.args.Tolerations == nil {
s.args.Tolerations = []types.TolerationArgs{}
}
argKey := "toleration"
var tolerations *[]string
value, ok := s.argValues[argKey]
if !ok {
return nil
}
tolerations = value.(*[]string)
log.Debugf("tolerations: %v", *tolerations)
for _, taintKey := range *tolerations {
if taintKey == "all" {
s.args.Tolerations = append(s.args.Tolerations, types.TolerationArgs{
Operator: "Exists",
})
return nil
}
tolerationArg, err := parseTolerationString(taintKey)
if err != nil {
log.Debugf(err.Error())
continue
}
s.args.Tolerations = append(s.args.Tolerations, *tolerationArg)
}
return nil
}
// setAnnotations is used to handle option --annotation
func (s *UpdateServingArgsBuilder) setAnnotations() error {
s.args.Annotations = map[string]string{}
argKey := "annotation"
var annotations *[]string
item, ok := s.argValues[argKey]
if !ok {
return nil
}
annotations = item.(*[]string)
if len(*annotations) <= 0 {
return nil
}
s.args.Annotations = transformSliceToMap(*annotations, "=")
return nil
}
// setLabels is used to handle option --label
func (s *UpdateServingArgsBuilder) setLabels() error {
s.args.Labels = map[string]string{}
argKey := "label"
var labels *[]string
item, ok := s.argValues[argKey]
if !ok {
return nil
}
labels = item.(*[]string)
if len(*labels) <= 0 {
return nil
}
s.args.Labels = transformSliceToMap(*labels, "=")
return nil
}
func (s *UpdateServingArgsBuilder) checkNamespace() error {
if s.args.Namespace == "" {
return fmt.Errorf("namespace not set, please set it")
}
log.Debugf("namespace is %v", s.args.Namespace)
return nil
}
func (s *UpdateServingArgsBuilder) checkName() error {
if s.args.Name == "" {
return fmt.Errorf("name not set, please set it")
}
log.Debugf("name is %v", s.args.Name)
return nil
}
func (s *UpdateServingArgsBuilder) checkReplicas() error {
if s.args.Replicas < 0 {
return fmt.Errorf("replicas not valid, must be greater than 0")
}
log.Debugf("replicas is %d", s.args.Replicas)
return nil
}