Skip to content

Commit 1e0df13

Browse files
authored
Only read Cortex config from cortex.yaml (#396)
1 parent 8eb372c commit 1e0df13

File tree

3 files changed

+20
-127
lines changed

3 files changed

+20
-127
lines changed

pkg/operator/api/userconfig/config.go

Lines changed: 19 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ limitations under the License.
1717
package userconfig
1818

1919
import (
20-
"fmt"
21-
"io/ioutil"
22-
2320
"github.com/cortexlabs/cortex/pkg/lib/cast"
2421
"github.com/cortexlabs/cortex/pkg/lib/configreader"
2522
cr "github.com/cortexlabs/cortex/pkg/lib/configreader"
2623
"github.com/cortexlabs/cortex/pkg/lib/errors"
2724
"github.com/cortexlabs/cortex/pkg/lib/files"
28-
s "github.com/cortexlabs/cortex/pkg/lib/strings"
2925
"github.com/cortexlabs/cortex/pkg/operator/api/resource"
3026
)
3127

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

42-
func mergeConfigs(target *Config, source *Config) error {
43-
target.APIs = append(target.APIs, source.APIs...)
44-
45-
if source.App != nil {
46-
if target.App != nil {
47-
return ErrorDuplicateConfig(resource.AppType)
48-
}
49-
target.App = source.App
38+
func (config *Config) Validate() error {
39+
if err := config.App.Validate(); err != nil {
40+
return err
5041
}
5142

52-
return nil
53-
}
54-
55-
func (config *Config) ValidatePartial() error {
56-
if config.App != nil {
57-
if err := config.App.Validate(); err != nil {
58-
return err
59-
}
60-
}
6143
if config.APIs != nil {
6244
if err := config.APIs.Validate(); err != nil {
6345
return err
@@ -67,33 +49,14 @@ func (config *Config) ValidatePartial() error {
6749
return nil
6850
}
6951

70-
func (config *Config) Validate() error {
71-
if config.App == nil {
72-
return ErrorMissingAppDefinition()
73-
}
74-
75-
return nil
76-
}
77-
78-
func (config *Config) MergeBytes(configBytes []byte, filePath string) (*Config, error) {
79-
sliceData, err := cr.ReadYAMLBytes(configBytes)
80-
if err != nil {
81-
return nil, errors.Wrap(err, filePath)
82-
}
83-
84-
subConfig, err := newPartial(sliceData, filePath)
85-
if err != nil {
86-
return nil, err
87-
}
52+
func New(filePath string, configBytes []byte, validate bool) (*Config, error) {
53+
var err error
8854

89-
err = mergeConfigs(config, subConfig)
55+
configData, err := cr.ReadYAMLBytes(configBytes)
9056
if err != nil {
91-
return nil, err
57+
return nil, errors.Wrap(err, filePath, ErrorParseConfig().Error())
9258
}
93-
return config, nil
94-
}
9559

96-
func newPartial(configData interface{}, filePath string) (*Config, error) {
9760
configDataSlice, ok := cast.InterfaceToStrInterfaceMapSlice(configData)
9861
if !ok {
9962
return nil, errors.Wrap(ErrorMalformedConfig(), filePath)
@@ -115,6 +78,9 @@ func newPartial(configData interface{}, filePath string) (*Config, error) {
11578
var newResource Resource
11679
switch resourceType {
11780
case resource.AppType:
81+
if config.App != nil {
82+
return nil, errors.Wrap(ErrorDuplicateConfig(resource.AppType), filePath)
83+
}
11884
app := &App{}
11985
errs = cr.Struct(app, data, appValidation)
12086
config.App = app
@@ -139,92 +105,28 @@ func newPartial(configData interface{}, filePath string) (*Config, error) {
139105
}
140106
}
141107

142-
err := config.ValidatePartial()
143-
if err != nil {
144-
return nil, err
145-
}
146-
147-
return config, nil
148-
}
149-
150-
func NewPartialPath(filePath string) (*Config, error) {
151-
configBytes, err := ioutil.ReadFile(filePath)
152-
if err != nil {
153-
return nil, errors.Wrap(err, filePath, ErrorReadConfig().Error())
154-
}
155-
156-
configData, err := cr.ReadYAMLBytes(configBytes)
157-
if err != nil {
158-
return nil, errors.Wrap(err, filePath, ErrorParseConfig().Error())
108+
if config.App == nil {
109+
return nil, ErrorMissingAppDefinition()
159110
}
160-
return newPartial(configData, filePath)
161-
}
162111

163-
func New(configs map[string][]byte) (*Config, error) {
164-
var err error
165-
config := &Config{}
166-
for filePath, configBytes := range configs {
167-
if !files.IsFilePathYAML(filePath) {
168-
continue
169-
}
170-
config, err = config.MergeBytes(configBytes, filePath)
171-
if err != nil {
112+
if validate {
113+
if err := config.Validate(); err != nil {
172114
return nil, err
173115
}
174116
}
175-
176-
if err := config.Validate(); err != nil {
177-
return nil, err
178-
}
179117
return config, nil
180118
}
181119

182120
func ReadAppName(filePath string, relativePath string) (string, error) {
183121
configBytes, err := files.ReadFileBytes(filePath)
184122
if err != nil {
185-
return "", errors.Wrap(err, ErrorReadConfig().Error(), relativePath)
186-
}
187-
configData, err := cr.ReadYAMLBytes(configBytes)
188-
if err != nil {
189-
return "", errors.Wrap(err, ErrorParseConfig().Error(), relativePath)
123+
return "", errors.Wrap(err, relativePath, ErrorReadConfig().Error())
190124
}
191-
configDataSlice, ok := cast.InterfaceToStrInterfaceMapSlice(configData)
192-
if !ok {
193-
return "", errors.Wrap(ErrorMalformedConfig(), relativePath)
194-
}
195-
196-
if len(configDataSlice) == 0 {
197-
return "", errors.Wrap(ErrorMissingAppDefinition(), relativePath)
198-
}
199-
200-
var appName string
201-
for i, configItem := range configDataSlice {
202-
kindStr, _ := configItem[KindKey].(string)
203-
if resource.TypeFromKindString(kindStr) == resource.AppType {
204-
if appName != "" {
205-
return "", errors.Wrap(ErrorDuplicateConfig(resource.AppType), relativePath)
206-
}
207-
208-
wrapStr := fmt.Sprintf("%s at %s", resource.AppType.String(), s.Index(i))
209125

210-
appNameInter, ok := configItem[NameKey]
211-
if !ok {
212-
return "", errors.Wrap(configreader.ErrorMustBeDefined(), relativePath, wrapStr, NameKey)
213-
}
214-
215-
appName, ok = appNameInter.(string)
216-
if !ok {
217-
return "", errors.Wrap(configreader.ErrorInvalidPrimitiveType(appNameInter, configreader.PrimTypeString), relativePath, wrapStr)
218-
}
219-
if appName == "" {
220-
return "", errors.Wrap(configreader.ErrorCannotBeEmpty(), relativePath, wrapStr)
221-
}
222-
}
223-
}
224-
225-
if appName == "" {
226-
return "", errors.Wrap(ErrorMissingAppDefinition(), relativePath)
126+
config, err := New(relativePath, configBytes, false)
127+
if err != nil {
128+
return "", err
227129
}
228130

229-
return appName, nil
131+
return config.App.Name, nil
230132
}

pkg/operator/api/userconfig/errors.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const (
3939
ErrSpecifyOnlyOne
4040
ErrOneOfPrerequisitesNotDefined
4141
ErrCannotBeNull
42-
ErrUnsupportedConfigKey
4342
ErrMinReplicasGreaterThanMax
4443
ErrInitReplicasGreaterThanMax
4544
ErrInitReplicasLessThanMin
@@ -64,7 +63,6 @@ var errorKinds = []string{
6463
"err_specify_only_one",
6564
"err_one_of_prerequisites_not_defined",
6665
"err_cannot_be_null",
67-
"err_unsupported_config_key",
6866
"err_min_replicas_greater_than_max",
6967
"err_init_replicas_greater_than_max",
7068
"err_init_replicas_less_than_min",
@@ -230,13 +228,6 @@ func ErrorCannotBeNull() error {
230228
}
231229
}
232230

233-
func ErrorUnsupportedConfigKey() error {
234-
return Error{
235-
Kind: ErrUnsupportedConfigKey,
236-
message: "is not supported for this resource",
237-
}
238-
}
239-
240231
func ErrorMinReplicasGreaterThanMax(min int32, max int32) error {
241232
return Error{
242233
Kind: ErrMinReplicasGreaterThanMax,

pkg/operator/endpoints/deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func getContext(r *http.Request, ignoreCache bool) (*context.Context, error) {
124124
return nil, errors.Wrap(err, "form file", "config.zip")
125125
}
126126

127-
config, err := userconfig.New(zipContents)
127+
config, err := userconfig.New("cortex.yaml", zipContents["cortex.yaml"], true)
128128
if err != nil {
129129
return nil, err
130130
}

0 commit comments

Comments
 (0)