Skip to content

Commit 89674ea

Browse files
authored
Unzip project to check if file exists and add request_handler file ha… (#438)
1 parent 1c10946 commit 89674ea

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

pkg/operator/api/userconfig/apis.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ func (api *API) UserConfigStr() string {
206206
return sb.String()
207207
}
208208

209-
func (apis APIs) Validate() error {
209+
func (apis APIs) Validate(projectFileMap map[string][]byte) error {
210210
for _, api := range apis {
211-
if err := api.Validate(); err != nil {
211+
if err := api.Validate(projectFileMap); err != nil {
212212
return err
213213
}
214214
}
@@ -226,7 +226,7 @@ func (apis APIs) Validate() error {
226226
return nil
227227
}
228228

229-
func (api *API) Validate() error {
229+
func (api *API) Validate(projectFileMap map[string][]byte) error {
230230
awsClient, err := aws.NewFromS3Path(api.Model, false)
231231
if err != nil {
232232
return err
@@ -269,6 +269,12 @@ func (api *API) Validate() error {
269269
return errors.Wrap(ErrorTFServingOptionsForTFOnly(api.ModelFormat), Identify(api))
270270
}
271271

272+
if api.RequestHandler != nil {
273+
if _, ok := projectFileMap[*api.RequestHandler]; !ok {
274+
return errors.Wrap(ErrorImplDoesNotExist(*api.RequestHandler), Identify(api), RequestHandlerKey)
275+
}
276+
}
277+
272278
if err := api.Compute.Validate(); err != nil {
273279
return errors.Wrap(err, Identify(api), ComputeKey)
274280
}

pkg/operator/api/userconfig/config.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
cr "github.com/cortexlabs/cortex/pkg/lib/configreader"
2323
"github.com/cortexlabs/cortex/pkg/lib/errors"
2424
"github.com/cortexlabs/cortex/pkg/lib/files"
25+
"github.com/cortexlabs/cortex/pkg/lib/zip"
2526
"github.com/cortexlabs/cortex/pkg/operator/api/resource"
2627
)
2728

@@ -35,21 +36,26 @@ var typeFieldValidation = &cr.StructFieldValidation{
3536
Nil: true,
3637
}
3738

38-
func (config *Config) Validate() error {
39+
func (config *Config) Validate(projectBytes []byte) error {
3940
if err := config.App.Validate(); err != nil {
4041
return err
4142
}
4243

44+
projectFileMap, err := zip.UnzipMemToMem(projectBytes)
45+
if err != nil {
46+
return err
47+
}
48+
4349
if config.APIs != nil {
44-
if err := config.APIs.Validate(); err != nil {
50+
if err := config.APIs.Validate(projectFileMap); err != nil {
4551
return err
4652
}
4753
}
4854

4955
return nil
5056
}
5157

52-
func New(filePath string, configBytes []byte, validate bool) (*Config, error) {
58+
func New(filePath string, configBytes []byte) (*Config, error) {
5359
var err error
5460

5561
configData, err := cr.ReadYAMLBytes(configBytes)
@@ -109,11 +115,6 @@ func New(filePath string, configBytes []byte, validate bool) (*Config, error) {
109115
return nil, ErrorMissingAppDefinition()
110116
}
111117

112-
if validate {
113-
if err := config.Validate(); err != nil {
114-
return nil, err
115-
}
116-
}
117118
return config, nil
118119
}
119120

@@ -123,7 +124,7 @@ func ReadAppName(filePath string, relativePath string) (string, error) {
123124
return "", errors.Wrap(err, relativePath, ErrorReadConfig().Error())
124125
}
125126

126-
config, err := New(relativePath, configBytes, false)
127+
config, err := New(relativePath, configBytes)
127128
if err != nil {
128129
return "", err
129130
}

pkg/operator/context/apis.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ func getAPIs(config *userconfig.Config, deploymentVersion string, projectID stri
3636
buf.WriteString(s.Obj(apiConfig.Tracker))
3737
buf.WriteString(apiConfig.ModelFormat.String())
3838
buf.WriteString(deploymentVersion)
39-
buf.WriteString(projectID)
4039
buf.WriteString(strings.TrimSuffix(apiConfig.Model, "/"))
4140

41+
if apiConfig.RequestHandler != nil {
42+
buf.WriteString(projectID)
43+
buf.WriteString(*apiConfig.RequestHandler)
44+
}
45+
4246
id := hash.Bytes(buf.Bytes())
4347

4448
apis[apiConfig.Name] = &context.API{

pkg/operator/context/context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func New(
6060
)
6161

6262
ctx.ProjectID = hash.Bytes(projectBytes)
63+
6364
ctx.ProjectKey = filepath.Join(consts.ProjectsDir, ctx.ProjectID+".zip")
6465
if err = config.AWS.UploadBytesToS3(projectBytes, ctx.ProjectKey); err != nil {
6566
return nil, err

pkg/operator/endpoints/deploy.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,21 @@ func Deploy(w http.ResponseWriter, r *http.Request) {
4747
return
4848
}
4949

50-
userconf, err := userconfig.New("cortex.yaml", configBytes, true)
50+
projectBytes, err := files.ReadReqFile(r, "project.zip")
51+
if err != nil {
52+
RespondError(w, errors.WithStack(err))
53+
return
54+
}
55+
56+
userconf, err := userconfig.New("cortex.yaml", configBytes)
5157
if err != nil {
5258
RespondError(w, err)
5359
return
5460
}
5561

56-
projectBytes, err := files.ReadReqFile(r, "project.zip")
62+
err = userconf.Validate(projectBytes)
5763
if err != nil {
58-
RespondError(w, errors.WithStack(err))
64+
RespondError(w, err)
5965
return
6066
}
6167

0 commit comments

Comments
 (0)