Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pkg/operator/api/userconfig/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ func (api *API) UserConfigStr() string {
return sb.String()
}

func (apis APIs) Validate() error {
func (apis APIs) Validate(projectFileMap map[string][]byte) error {
for _, api := range apis {
if err := api.Validate(); err != nil {
if err := api.Validate(projectFileMap); err != nil {
return err
}
}
Expand All @@ -226,7 +226,7 @@ func (apis APIs) Validate() error {
return nil
}

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

if api.RequestHandler != nil {
if _, ok := projectFileMap[*api.RequestHandler]; !ok {
return errors.Wrap(ErrorImplDoesNotExist(*api.RequestHandler), Identify(api), RequestHandlerKey)
}
}

if err := api.Compute.Validate(); err != nil {
return errors.Wrap(err, Identify(api), ComputeKey)
}
Expand Down
19 changes: 10 additions & 9 deletions pkg/operator/api/userconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
cr "github.com/cortexlabs/cortex/pkg/lib/configreader"
"github.com/cortexlabs/cortex/pkg/lib/errors"
"github.com/cortexlabs/cortex/pkg/lib/files"
"github.com/cortexlabs/cortex/pkg/lib/zip"
"github.com/cortexlabs/cortex/pkg/operator/api/resource"
)

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

func (config *Config) Validate() error {
func (config *Config) Validate(projectBytes []byte) error {
if err := config.App.Validate(); err != nil {
return err
}

projectFileMap, err := zip.UnzipMemToMem(projectBytes)
if err != nil {
return err
}

if config.APIs != nil {
if err := config.APIs.Validate(); err != nil {
if err := config.APIs.Validate(projectFileMap); err != nil {
return err
}
}

return nil
}

func New(filePath string, configBytes []byte, validate bool) (*Config, error) {
func New(filePath string, configBytes []byte) (*Config, error) {
var err error

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

if validate {
if err := config.Validate(); err != nil {
return nil, err
}
}
return config, nil
}

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

config, err := New(relativePath, configBytes, false)
config, err := New(relativePath, configBytes)
if err != nil {
return "", err
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/operator/context/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ func getAPIs(config *userconfig.Config, deploymentVersion string, projectID stri
buf.WriteString(s.Obj(apiConfig.Tracker))
buf.WriteString(apiConfig.ModelFormat.String())
buf.WriteString(deploymentVersion)
buf.WriteString(projectID)
buf.WriteString(strings.TrimSuffix(apiConfig.Model, "/"))

if apiConfig.RequestHandler != nil {
buf.WriteString(projectID)
buf.WriteString(*apiConfig.RequestHandler)
}

id := hash.Bytes(buf.Bytes())

apis[apiConfig.Name] = &context.API{
Expand Down
1 change: 1 addition & 0 deletions pkg/operator/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func New(
)

ctx.ProjectID = hash.Bytes(projectBytes)

ctx.ProjectKey = filepath.Join(consts.ProjectsDir, ctx.ProjectID+".zip")
if err = config.AWS.UploadBytesToS3(projectBytes, ctx.ProjectKey); err != nil {
return nil, err
Expand Down
12 changes: 9 additions & 3 deletions pkg/operator/endpoints/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,21 @@ func Deploy(w http.ResponseWriter, r *http.Request) {
return
}

userconf, err := userconfig.New("cortex.yaml", configBytes, true)
projectBytes, err := files.ReadReqFile(r, "project.zip")
if err != nil {
RespondError(w, errors.WithStack(err))
return
}

userconf, err := userconfig.New("cortex.yaml", configBytes)
if err != nil {
RespondError(w, err)
return
}

projectBytes, err := files.ReadReqFile(r, "project.zip")
err = userconf.Validate(projectBytes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how you split this out! Makes much more sense than the bool param to New()

if err != nil {
RespondError(w, errors.WithStack(err))
RespondError(w, err)
return
}

Expand Down