Skip to content

Only read Cortex config from cortex.yaml #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 28, 2019
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
136 changes: 19 additions & 117 deletions pkg/operator/api/userconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@ limitations under the License.
package userconfig

import (
"fmt"
"io/ioutil"

"github.com/cortexlabs/cortex/pkg/lib/cast"
"github.com/cortexlabs/cortex/pkg/lib/configreader"
cr "github.com/cortexlabs/cortex/pkg/lib/configreader"
"github.com/cortexlabs/cortex/pkg/lib/errors"
"github.com/cortexlabs/cortex/pkg/lib/files"
s "github.com/cortexlabs/cortex/pkg/lib/strings"
"github.com/cortexlabs/cortex/pkg/operator/api/resource"
)

Expand All @@ -39,25 +35,11 @@ var typeFieldValidation = &cr.StructFieldValidation{
Nil: true,
}

func mergeConfigs(target *Config, source *Config) error {
target.APIs = append(target.APIs, source.APIs...)

if source.App != nil {
if target.App != nil {
return ErrorDuplicateConfig(resource.AppType)
}
target.App = source.App
func (config *Config) Validate() error {
if err := config.App.Validate(); err != nil {
return err
}

return nil
}

func (config *Config) ValidatePartial() error {
if config.App != nil {
if err := config.App.Validate(); err != nil {
return err
}
}
if config.APIs != nil {
if err := config.APIs.Validate(); err != nil {
return err
Expand All @@ -67,33 +49,14 @@ func (config *Config) ValidatePartial() error {
return nil
}

func (config *Config) Validate() error {
if config.App == nil {
return ErrorMissingAppDefinition()
}

return nil
}

func (config *Config) MergeBytes(configBytes []byte, filePath string) (*Config, error) {
sliceData, err := cr.ReadYAMLBytes(configBytes)
if err != nil {
return nil, errors.Wrap(err, filePath)
}

subConfig, err := newPartial(sliceData, filePath)
if err != nil {
return nil, err
}
func New(filePath string, configBytes []byte, validate bool) (*Config, error) {
var err error

err = mergeConfigs(config, subConfig)
configData, err := cr.ReadYAMLBytes(configBytes)
if err != nil {
return nil, err
return nil, errors.Wrap(err, filePath, ErrorParseConfig().Error())
}
return config, nil
}

func newPartial(configData interface{}, filePath string) (*Config, error) {
configDataSlice, ok := cast.InterfaceToStrInterfaceMapSlice(configData)
if !ok {
return nil, errors.Wrap(ErrorMalformedConfig(), filePath)
Expand All @@ -115,6 +78,9 @@ func newPartial(configData interface{}, filePath string) (*Config, error) {
var newResource Resource
switch resourceType {
case resource.AppType:
if config.App != nil {
return nil, errors.Wrap(ErrorDuplicateConfig(resource.AppType), filePath)
}
app := &App{}
errs = cr.Struct(app, data, appValidation)
config.App = app
Expand All @@ -139,92 +105,28 @@ func newPartial(configData interface{}, filePath string) (*Config, error) {
}
}

err := config.ValidatePartial()
if err != nil {
return nil, err
}

return config, nil
}

func NewPartialPath(filePath string) (*Config, error) {
configBytes, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, errors.Wrap(err, filePath, ErrorReadConfig().Error())
}

configData, err := cr.ReadYAMLBytes(configBytes)
if err != nil {
return nil, errors.Wrap(err, filePath, ErrorParseConfig().Error())
if config.App == nil {
return nil, ErrorMissingAppDefinition()
}
return newPartial(configData, filePath)
}

func New(configs map[string][]byte) (*Config, error) {
var err error
config := &Config{}
for filePath, configBytes := range configs {
if !files.IsFilePathYAML(filePath) {
continue
}
config, err = config.MergeBytes(configBytes, filePath)
if err != nil {
if validate {
if err := config.Validate(); err != nil {
return nil, err
}
}

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

func ReadAppName(filePath string, relativePath string) (string, error) {
configBytes, err := files.ReadFileBytes(filePath)
if err != nil {
return "", errors.Wrap(err, ErrorReadConfig().Error(), relativePath)
}
configData, err := cr.ReadYAMLBytes(configBytes)
if err != nil {
return "", errors.Wrap(err, ErrorParseConfig().Error(), relativePath)
return "", errors.Wrap(err, relativePath, ErrorReadConfig().Error())
}
configDataSlice, ok := cast.InterfaceToStrInterfaceMapSlice(configData)
if !ok {
return "", errors.Wrap(ErrorMalformedConfig(), relativePath)
}

if len(configDataSlice) == 0 {
return "", errors.Wrap(ErrorMissingAppDefinition(), relativePath)
}

var appName string
for i, configItem := range configDataSlice {
kindStr, _ := configItem[KindKey].(string)
if resource.TypeFromKindString(kindStr) == resource.AppType {
if appName != "" {
return "", errors.Wrap(ErrorDuplicateConfig(resource.AppType), relativePath)
}

wrapStr := fmt.Sprintf("%s at %s", resource.AppType.String(), s.Index(i))

appNameInter, ok := configItem[NameKey]
if !ok {
return "", errors.Wrap(configreader.ErrorMustBeDefined(), relativePath, wrapStr, NameKey)
}

appName, ok = appNameInter.(string)
if !ok {
return "", errors.Wrap(configreader.ErrorInvalidPrimitiveType(appNameInter, configreader.PrimTypeString), relativePath, wrapStr)
}
if appName == "" {
return "", errors.Wrap(configreader.ErrorCannotBeEmpty(), relativePath, wrapStr)
}
}
}

if appName == "" {
return "", errors.Wrap(ErrorMissingAppDefinition(), relativePath)
config, err := New(relativePath, configBytes, false)
if err != nil {
return "", err
}

return appName, nil
return config.App.Name, nil
}
9 changes: 0 additions & 9 deletions pkg/operator/api/userconfig/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const (
ErrSpecifyOnlyOne
ErrOneOfPrerequisitesNotDefined
ErrCannotBeNull
ErrUnsupportedConfigKey
ErrMinReplicasGreaterThanMax
ErrInitReplicasGreaterThanMax
ErrInitReplicasLessThanMin
Expand All @@ -64,7 +63,6 @@ var errorKinds = []string{
"err_specify_only_one",
"err_one_of_prerequisites_not_defined",
"err_cannot_be_null",
"err_unsupported_config_key",
"err_min_replicas_greater_than_max",
"err_init_replicas_greater_than_max",
"err_init_replicas_less_than_min",
Expand Down Expand Up @@ -230,13 +228,6 @@ func ErrorCannotBeNull() error {
}
}

func ErrorUnsupportedConfigKey() error {
return Error{
Kind: ErrUnsupportedConfigKey,
message: "is not supported for this resource",
}
}

func ErrorMinReplicasGreaterThanMax(min int32, max int32) error {
return Error{
Kind: ErrMinReplicasGreaterThanMax,
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/endpoints/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func getContext(r *http.Request, ignoreCache bool) (*context.Context, error) {
return nil, errors.Wrap(err, "form file", "config.zip")
}

config, err := userconfig.New(zipContents)
config, err := userconfig.New("cortex.yaml", zipContents["cortex.yaml"], true)
if err != nil {
return nil, err
}
Expand Down