-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
144 lines (123 loc) · 5.52 KB
/
serve.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
// 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 commands
import (
"fmt"
"os"
"path/filepath"
"regexp"
validate "github.com/kubeflow/arena/pkg/util"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
modelPathSeparator = ":"
regexp4serviceName = "^[a-z0-9A-Z_-]+$"
)
type ServeArgs struct {
ImagePullPolicy string `yaml:"imagePullPolicy"` // --imagePullPolicy
GPUCount int `yaml:"gpuCount"` // --gpus
Cpu string `yaml:"cpu"` // --cpu
Memory string `yaml:"memory"` // --memory
Envs map[string]string `yaml:"envs"` // --envs
Command string `yaml:"command"` // --command
Replicas int `yaml:"replicas"` // --replicas
Port int `yaml:"port"` // --port
RestfulPort int `yaml:"rest_api_port"` // --restfulPort
ModelName string `yaml:"modelName"` // --modelName
ModelPath string `yaml:"modelPath"` // --modelPath
EnableIstio bool `yaml:"enableIstio"` // --enableIstio
ExposeService bool `yaml:"exposeService"` // --exposeService
ServingName string `yaml:"servingName"` // --servingName
ServingVersion string `yaml:"servingVersion"` // --servingVersion
ModelDirs map[string]string `yaml:"modelDirs"`
}
func (s ServeArgs) validateIstioEnablement() error {
log.Debugf("--enableIstio=%t is specified.", s.EnableIstio)
if !s.EnableIstio {
return nil
}
var reg *regexp.Regexp
reg = regexp.MustCompile(regexp4serviceName)
matched := reg.MatchString(s.ServingName)
if !matched {
return fmt.Errorf("--serviceName should be numbers, letters, dashes, and underscores ONLY")
}
log.Debugf("--serviceVersion=%s is specified.", s.ServingVersion)
if s.ServingVersion == "" {
return fmt.Errorf("--serviceVersion must be specified if enableIstio=true")
}
return nil
}
func (s ServeArgs) validateModelName() error {
if s.ModelName == "" {
return fmt.Errorf("--modelName cannot be blank")
}
var reg *regexp.Regexp
reg = regexp.MustCompile(regexp4serviceName)
matched := reg.MatchString(s.ModelName)
if !matched {
return fmt.Errorf("--modelName should be numbers, letters, dashes, and underscores ONLY")
}
return nil
}
func ParseMountPath(dataset []string) (err error) {
err = validate.ValidateDatasets(dataset)
return err
}
func (serveArgs *ServeArgs) addServeCommonFlags(command *cobra.Command) {
// create subcommands
command.Flags().StringVar(&serveArgs.ImagePullPolicy, "imagePullPolicy", "IfNotPresent", "the policy to pull the image, and the default policy is IfNotPresent")
command.Flags().StringVar(&serveArgs.Command, "command", "", "the command will inject to container's command.")
command.Flags().IntVar(&serveArgs.GPUCount, "gpus", 0, "the limit GPU count of each replica to run the serve.")
command.Flags().StringVar(&serveArgs.Cpu, "cpu", "", "the request cpu of each replica to run the serve.")
command.Flags().StringVar(&serveArgs.Memory, "memory", "", "the request memory of each replica to run the serve.")
command.Flags().IntVar(&serveArgs.Replicas, "replicas", 1, "the replicas number of the serve job.")
command.Flags().StringVar(&serveArgs.ModelPath, "modelPath", "", "the model path for serving in the container")
command.Flags().StringArrayVarP(&envs, "envs", "e", []string{}, "the environment variables")
command.Flags().StringVar(&serveArgs.ModelName, "modelName", "", "the model name for serving")
command.Flags().BoolVar(&serveArgs.EnableIstio, "enableIstio", false, "enable Istio for serving or not (disable Istio by default)")
command.Flags().BoolVar(&serveArgs.ExposeService, "exposeService", false, "expose service using Istio gateway for external access or not (not expose by default)")
command.Flags().StringVar(&serveArgs.ServingName, "servingName", "", "the serving name")
command.Flags().StringVar(&serveArgs.ServingVersion, "servingVersion", "", "the serving version")
command.Flags().StringArrayVarP(&dataset, "data", "d", []string{}, "specify the trained models datasource to mount for serving, like <name_of_datasource>:<mount_point_on_job>")
command.MarkFlagRequired("servingName")
}
func init() {
if os.Getenv(CHART_PKG_LOC) != "" {
standalone_training_chart = filepath.Join(os.Getenv(CHART_PKG_LOC), "training")
}
}
var (
serveLong = `serve a job.
Available Commands:
tensorflow,tf Submit a TensorFlow Serving Job.
`
)
func NewServeCommand() *cobra.Command {
var command = &cobra.Command{
Use: "serve",
Short: "Serve a job.",
Long: serveLong,
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
},
}
command.AddCommand(NewServingTensorRTCommand())
command.AddCommand(NewServingTensorFlowCommand())
command.AddCommand(NewServingListCommand())
command.AddCommand(NewServingDeleteCommand())
command.AddCommand(NewTrafficRouterSplitCommand())
return command
}