diff --git a/pkg/config/operator.go b/pkg/config/operator.go index 3c5e15d1..f962f96a 100644 --- a/pkg/config/operator.go +++ b/pkg/config/operator.go @@ -13,7 +13,6 @@ import ( "gopkg.in/yaml.v3" v1 "kusionstack.io/kusion/pkg/apis/core/v1" - "kusionstack.io/kusion/pkg/config/validation" "kusionstack.io/kusion/pkg/util/kfile" ) @@ -267,11 +266,6 @@ func setItemInConfig(config *v1.Config, info *itemInfo, key string, value any) ( return convertFromCfgMap(cfg) } -// validateConfig checks the config is valid or not. -func validateConfig(config *v1.Config) error { - return validation.ValidateConfig(config) -} - // tidyConfig is used to clean dirty empty block. func tidyConfig(configAddr **v1.Config) { config := *configAddr @@ -328,12 +322,12 @@ func parseStructuredConfigItem(info *itemInfo, strValue string) (any, error) { case int: value, err = strconv.Atoi(strValue) if err != nil { - return nil, validation.ErrNotInt + return nil, ErrNotInt } case bool: value, err = strconv.ParseBool(strValue) if err != nil { - return nil, validation.ErrNotBool + return nil, ErrNotBool } default: if reflect.TypeOf(value).Kind() == reflect.Pointer { diff --git a/pkg/config/operator_test.go b/pkg/config/operator_test.go index 44c676b1..310b245f 100644 --- a/pkg/config/operator_test.go +++ b/pkg/config/operator_test.go @@ -28,10 +28,10 @@ var ( "pre": { Type: v1.BackendTypeMysql, Configs: map[string]any{ - v1.BackendMysqlDBName: "kk", - v1.BackendMysqlUser: "kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, "prod": { @@ -52,10 +52,10 @@ var ( "pre": map[string]any{ v1.BackendType: v1.DeprecatedBackendMysql, v1.BackendConfigItems: map[string]any{ - v1.BackendMysqlDBName: "kk", - v1.BackendMysqlUser: "kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, "prod": map[string]any{ @@ -154,7 +154,7 @@ func TestOperator_GetConfigItem(t *testing.T) { success: true, o: mockOperator(existValidConfigPath, validConfig), key: "backends.pre.configs.port", - expectedVal: 6443, + expectedVal: 3306, }, { name: "get structured config item successfully type pointer of struct", @@ -164,10 +164,10 @@ func TestOperator_GetConfigItem(t *testing.T) { expectedVal: &v1.BackendConfig{ Type: v1.BackendTypeMysql, Configs: map[string]any{ - v1.BackendMysqlDBName: "kk", - v1.BackendMysqlUser: "kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, }, @@ -211,14 +211,14 @@ func TestOperator_GetEncodedConfigItem(t *testing.T) { success: true, o: mockOperator(existValidConfigPath, validConfig), key: "backends.pre.configs.port", - expectedVal: "6443", + expectedVal: "3306", }, { name: "get encoding config item successfully type map", success: true, o: mockOperator(existValidConfigPath, validConfig), key: "backends.pre", - expectedVal: `{"configs":{"dbName":"kk","host":"127.0.0.1","port":6443,"user":"kusion"},"type":"mysql"}`, + expectedVal: `{"configs":{"dbName":"kusion","host":"127.0.0.1","port":3306,"user":"kk"},"type":"mysql"}`, }, } @@ -265,14 +265,14 @@ func TestOperator_SetConfigItem(t *testing.T) { }, }), key: "backends.pre.configs.port", - val: 6443, + val: 3306, expectedConfig: &v1.Config{ Backends: &v1.BackendConfigs{ Backends: map[string]*v1.BackendConfig{ "pre": { Type: v1.BackendTypeMysql, Configs: map[string]any{ - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, }, @@ -287,10 +287,10 @@ func TestOperator_SetConfigItem(t *testing.T) { val: &v1.BackendConfig{ Type: v1.BackendTypeMysql, Configs: map[string]any{ - v1.BackendMysqlDBName: "kk-is-tired", - v1.BackendMysqlUser: "kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk-tired", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, expectedConfig: &v1.Config{ @@ -303,10 +303,10 @@ func TestOperator_SetConfigItem(t *testing.T) { "pre": { Type: v1.BackendTypeMysql, Configs: map[string]any{ - v1.BackendMysqlDBName: "kk-is-tired", - v1.BackendMysqlUser: "kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk-tired", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, "prod": { @@ -325,7 +325,7 @@ func TestOperator_SetConfigItem(t *testing.T) { o: mockOperator(existValidConfigPath, validConfig), key: "backends.prod.configs", val: map[string]any{ - v1.BackendGenericOssBucket: "kk-is-so-tired", + v1.BackendGenericOssBucket: "kk-so-tired", }, expectedConfig: &v1.Config{ Backends: &v1.BackendConfigs{ @@ -337,16 +337,16 @@ func TestOperator_SetConfigItem(t *testing.T) { "pre": { Type: v1.BackendTypeMysql, Configs: map[string]any{ - v1.BackendMysqlDBName: "kk", - v1.BackendMysqlUser: "kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, "prod": { Type: v1.BackendTypeS3, Configs: map[string]any{ - v1.BackendGenericOssBucket: "kk-is-so-tired", + v1.BackendGenericOssBucket: "kk-so-tired", }, }, }, @@ -424,14 +424,14 @@ func TestOperator_setEncodedConfigItem(t *testing.T) { }, }), key: "backends.pre.configs.port", - val: "6443", + val: "3306", expectedConfig: &v1.Config{ Backends: &v1.BackendConfigs{ Backends: map[string]*v1.BackendConfig{ "pre": { Type: v1.BackendTypeMysql, Configs: map[string]any{ - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, }, @@ -443,7 +443,7 @@ func TestOperator_setEncodedConfigItem(t *testing.T) { success: true, o: mockOperator(existValidConfigPath, validConfig), key: "backends.pre", - val: `{"configs":{"dbName":"kk-is-tired","host":"127.0.0.1","port":6443,"user":"kusion"},"type":"mysql"}`, + val: `{"configs":{"dbName":"kusion","host":"127.0.0.1","port":3306,"user":"kk-tired"},"type":"mysql"}`, expectedConfig: &v1.Config{ Backends: &v1.BackendConfigs{ Current: "dev", @@ -454,10 +454,10 @@ func TestOperator_setEncodedConfigItem(t *testing.T) { "pre": { Type: v1.BackendTypeMysql, Configs: map[string]any{ - v1.BackendMysqlDBName: "kk-is-tired", - v1.BackendMysqlUser: "kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk-tired", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, "prod": { @@ -475,7 +475,7 @@ func TestOperator_setEncodedConfigItem(t *testing.T) { success: true, o: mockOperator(existValidConfigPath, validConfig), key: "backends.prod.configs", - val: `{"bucket": "kk-is-so-tired"}`, + val: `{"bucket":"kusion","region":"us-east-1"}`, expectedConfig: &v1.Config{ Backends: &v1.BackendConfigs{ Current: "dev", @@ -486,16 +486,17 @@ func TestOperator_setEncodedConfigItem(t *testing.T) { "pre": { Type: v1.BackendTypeMysql, Configs: map[string]any{ - v1.BackendMysqlDBName: "kk", - v1.BackendMysqlUser: "kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, "prod": { Type: v1.BackendTypeS3, Configs: map[string]any{ - v1.BackendGenericOssBucket: "kk-is-so-tired", + v1.BackendGenericOssBucket: "kusion", + v1.BackendS3Region: "us-east-1", }, }, }, @@ -687,8 +688,8 @@ func TestParseStructuredConfigItem(t *testing.T) { name: "parse structured config item successfully int", success: true, info: registeredItems["backends.*.configs.port"], - strVal: "6443", - val: 6443, + strVal: "3306", + val: 3306, }, { name: "parse structured config item successfully bool", @@ -701,14 +702,14 @@ func TestParseStructuredConfigItem(t *testing.T) { name: "parse structured config item successfully struct ptr", success: true, info: registeredItems["backends.*"], - strVal: `{"configs":{"dbName":"kk","host":"127.0.0.1","port":6443,"user":"kusion"},"type":"mysql"}`, + strVal: `{"configs":{"dbName":"kusion","host":"127.0.0.1","port":3306,"user":"kk"},"type":"mysql"}`, val: &v1.BackendConfig{ Type: v1.BackendTypeMysql, Configs: map[string]any{ - v1.BackendMysqlDBName: "kk", - v1.BackendMysqlUser: "kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, }, @@ -716,14 +717,14 @@ func TestParseStructuredConfigItem(t *testing.T) { name: "parse structured config item successfully struct", success: true, info: &itemInfo{v1.BackendConfig{}, nil, nil}, - strVal: `{"configs":{"dbName":"kk","host":"127.0.0.1","port":6443,"user":"kusion"},"type":"mysql"}`, + strVal: `{"configs":{"dbName":"kusion","host":"127.0.0.1","port":3306,"user":"kk"},"type":"mysql"}`, val: v1.BackendConfig{ Type: v1.BackendTypeMysql, Configs: map[string]any{ - v1.BackendMysqlDBName: "kk", - v1.BackendMysqlUser: "kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, }, @@ -856,7 +857,7 @@ func TestGetItemFromCfgMap(t *testing.T) { success: true, cfg: validCfg, key: "backends.pre.configs.port", - expectedVal: 6443, + expectedVal: 3306, }, { name: "get item from config map successfully type map", @@ -981,13 +982,13 @@ func TestSetItemFromCfgMap(t *testing.T) { }, }, key: "backends.pre.configs.port", - val: 6443, + val: 3306, expectedCfg: map[string]any{ "backends": map[string]any{ "pre": map[string]any{ "type": "mysql", "configs": map[string]any{ - "port": 6443, + "port": 3306, "dbName": "kusion", }, }, @@ -1002,10 +1003,10 @@ func TestSetItemFromCfgMap(t *testing.T) { val: &v1.BackendConfig{ Type: v1.BackendTypeMysql, Configs: map[string]any{ - v1.BackendMysqlDBName: "kk", - v1.BackendMysqlUser: "kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, expectedCfg: map[string]any{ @@ -1013,10 +1014,10 @@ func TestSetItemFromCfgMap(t *testing.T) { "pre": &v1.BackendConfig{ Type: v1.BackendTypeMysql, Configs: map[string]any{ - v1.BackendMysqlDBName: "kk", - v1.BackendMysqlUser: "kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, }, diff --git a/pkg/config/registry.go b/pkg/config/registry.go index 8efd9cef..6042b934 100644 --- a/pkg/config/registry.go +++ b/pkg/config/registry.go @@ -2,7 +2,6 @@ package config import ( v1 "kusionstack.io/kusion/pkg/apis/core/v1" - "kusionstack.io/kusion/pkg/config/validation" ) const ( @@ -26,22 +25,22 @@ const ( func newRegisteredItems() map[string]*itemInfo { return map[string]*itemInfo{ - backendCurrent: {"", validation.ValidateCurrentBackend, nil}, - backendConfig: {&v1.BackendConfig{}, validation.ValidateBackendConfig, validation.ValidateUnsetBackendConfig}, - backendConfigType: {"", validation.ValidateBackendType, validation.ValidateUnsetBackendType}, - backendConfigItems: {map[string]any{}, validation.ValidateBackendConfigItems, nil}, - backendLocalPath: {"", validation.ValidateLocalBackendItem, nil}, - backendMysqlDBName: {"", validation.ValidateMysqlBackendItem, nil}, - backendMysqlUser: {"", validation.ValidateMysqlBackendItem, nil}, - backendMysqlPassword: {"", validation.ValidateMysqlBackendItem, nil}, - backendMysqlHost: {"", validation.ValidateMysqlBackendItem, nil}, - backendMysqlPort: {0, validation.ValidateMysqlBackendPort, nil}, - backendGenericOssEndpoint: {"", validation.ValidateGenericOssBackendItem, nil}, - backendGenericOssAK: {"", validation.ValidateGenericOssBackendItem, nil}, - backendGenericOssSK: {"", validation.ValidateGenericOssBackendItem, nil}, - backendGenericOssBucket: {"", validation.ValidateGenericOssBackendItem, nil}, - backendGenericOssPrefix: {"", validation.ValidateGenericOssBackendItem, nil}, - backendS3Region: {"", validation.ValidateS3BackendItem, nil}, + backendCurrent: {"", validateCurrentBackend, nil}, + backendConfig: {&v1.BackendConfig{}, validateBackendConfig, validateUnsetBackendConfig}, + backendConfigType: {"", validateBackendType, validateUnsetBackendType}, + backendConfigItems: {map[string]any{}, validateBackendConfigItems, nil}, + backendLocalPath: {"", validateLocalBackendItem, nil}, + backendMysqlDBName: {"", validateMysqlBackendItem, nil}, + backendMysqlUser: {"", validateMysqlBackendItem, nil}, + backendMysqlPassword: {"", validateMysqlBackendItem, nil}, + backendMysqlHost: {"", validateMysqlBackendItem, nil}, + backendMysqlPort: {0, validateMysqlBackendPort, nil}, + backendGenericOssEndpoint: {"", validateGenericOssBackendItem, nil}, + backendGenericOssAK: {"", validateGenericOssBackendItem, nil}, + backendGenericOssSK: {"", validateGenericOssBackendItem, nil}, + backendGenericOssBucket: {"", validateGenericOssBackendItem, nil}, + backendGenericOssPrefix: {"", validateGenericOssBackendItem, nil}, + backendS3Region: {"", validateS3BackendItem, nil}, } } @@ -65,10 +64,10 @@ type itemInfo struct { // config setting. The unregistered config item, empty item value and invalid item value type is forbidden // by config operator by default, which are unnecessary to check in the validateFunc. // Please do not do any real setting job in the validateFunc. - validateFunc validation.ValidateFunc + validateFunc validateFunc // validateDeleteFunc is used to check the config item is valid or not to unset, calling before executing // real config unsetting. // Please do not do any real unsetting job in the validateDeleteFunc. - validateDeleteFunc validation.ValidateUnsetFunc + validateDeleteFunc validateDeleteFunc } diff --git a/pkg/config/testdata/config_for_read.yaml b/pkg/config/testdata/config_for_read.yaml index 2b01897c..6c11082d 100644 --- a/pkg/config/testdata/config_for_read.yaml +++ b/pkg/config/testdata/config_for_read.yaml @@ -5,10 +5,10 @@ backends: pre: type: mysql configs: - dbName: kk + dbName: kusion host: 127.0.0.1 - port: 6443 - user: kusion + port: 3306 + user: kk prod: type: s3 configs: diff --git a/pkg/config/util_test.go b/pkg/config/util_test.go index fdcf2e33..27ce6cbf 100644 --- a/pkg/config/util_test.go +++ b/pkg/config/util_test.go @@ -77,7 +77,7 @@ func TestGetEncodedConfigItem(t *testing.T) { success: true, configFilePath: existValidConfigPath, configItemKey: "backends.pre.configs.port", - expectedConfigItem: "6443", + expectedConfigItem: "3306", }, { name: "get encoded config item successfully type struct", @@ -142,17 +142,17 @@ func TestSetEncodedConfigItem(t *testing.T) { success: true, configFilePath: emptyValidConfigPath, configItemKey: "backends.pre", - configItem: `{"configs":{"dbName":"kk","host":"127.0.0.1","port":6443,"user":"kusion"},"type":"mysql"}`, + configItem: `{"configs":{"dbName":"kusion","host":"127.0.0.1","port":3306,"user":"kk"},"type":"mysql"}`, expectedConfig: &v1.Config{ Backends: &v1.BackendConfigs{ Backends: map[string]*v1.BackendConfig{ "pre": { Type: v1.BackendTypeMysql, Configs: map[string]any{ - v1.BackendMysqlDBName: "kk", - v1.BackendMysqlUser: "kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, }, @@ -238,10 +238,10 @@ func TestDeleteConfigItem(t *testing.T) { "pre": { Type: v1.BackendTypeMysql, Configs: map[string]any{ - v1.BackendMysqlDBName: "kk", - v1.BackendMysqlUser: "kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, }, diff --git a/pkg/config/validation/validation.go b/pkg/config/validation.go similarity index 65% rename from pkg/config/validation/validation.go rename to pkg/config/validation.go index 8941352e..56f837e7 100644 --- a/pkg/config/validation/validation.go +++ b/pkg/config/validation.go @@ -1,4 +1,4 @@ -package validation +package config import ( "errors" @@ -6,11 +6,12 @@ import ( "strings" v1 "kusionstack.io/kusion/pkg/apis/core/v1" + "kusionstack.io/kusion/pkg/backend/storages" ) type ( - ValidateFunc func(config *v1.Config, key string, val any) error - ValidateUnsetFunc func(config *v1.Config, key string) error + validateFunc func(config *v1.Config, key string, val any) error + validateDeleteFunc func(config *v1.Config, key string) error ) var ( @@ -24,9 +25,9 @@ var ( ErrInvalidBackendMysqlPort = errors.New("backend mysql port must be between 1 and 65535") ) -// ValidateConfig is used to check the config is valid or not, where the invalidation comes from the unexpected +// validateConfig is used to check the config is valid or not, where the invalidation comes from the unexpected // manual modification. -func ValidateConfig(config *v1.Config) error { +func validateConfig(config *v1.Config) error { if config == nil { return nil } @@ -39,25 +40,25 @@ func ValidateConfig(config *v1.Config) error { if backends.Current != "" && len(backends.Backends) == 0 { return fmt.Errorf("%w, non-empty current backend name %s but empty backends", ErrUnexpectedInvalidConfig, backends.Current) } - for name, backendConfig := range backends.Backends { + for name, bkConfig := range backends.Backends { if name == "" || name == "current" { return fmt.Errorf("%w, invalid backend name %s", ErrUnexpectedInvalidConfig, name) } - if backendConfig == nil { + if bkConfig == nil { return fmt.Errorf("%w, empty backend config with name %s", ErrUnexpectedInvalidConfig, name) } - if backendConfig.Type == "" && len(backendConfig.Configs) != 0 { + if bkConfig.Type == "" && len(bkConfig.Configs) != 0 { return fmt.Errorf("%w, empty backend config item but non-empty type with name %s", ErrUnexpectedInvalidConfig, name) } - if err := validateBasalBackendConfig(backendConfig); err != nil { + if err := checkBasalBackendConfig(bkConfig); err != nil { return fmt.Errorf("%w, %v", ErrUnexpectedInvalidConfig, err) } } return nil } -// ValidateCurrentBackend is used to check that setting the current backend is valid or not. -func ValidateCurrentBackend(config *v1.Config, _ string, val any) error { +// validateCurrentBackend is used to check that setting the current backend is valid or not. +func validateCurrentBackend(config *v1.Config, _ string, val any) error { current, _ := val.(string) if config != nil && config.Backends != nil && config.Backends.Backends != nil { _, ok := config.Backends.Backends[current] @@ -68,14 +69,14 @@ func ValidateCurrentBackend(config *v1.Config, _ string, val any) error { return ErrNotExistCurrentBackend } -// ValidateBackendConfig is used to check that setting the backend config is valid or not. -func ValidateBackendConfig(_ *v1.Config, _ string, val any) error { - backendConfig, _ := val.(*v1.BackendConfig) - return validateBackendConfig(backendConfig) +// validateBackendConfig is used to check that setting the backend config is valid or not. +func validateBackendConfig(_ *v1.Config, _ string, val any) error { + config, _ := val.(*v1.BackendConfig) + return checkBackendConfig(config) } -// ValidateUnsetBackendConfig is used to check that unsetting the backend config is valid or not. -func ValidateUnsetBackendConfig(config *v1.Config, key string) error { +// validateUnsetBackendConfig is used to check that unsetting the backend config is valid or not. +func validateUnsetBackendConfig(config *v1.Config, key string) error { if config == nil || config.Backends == nil { return nil } @@ -85,8 +86,8 @@ func ValidateUnsetBackendConfig(config *v1.Config, key string) error { return nil } -// ValidateBackendType is used to check that setting the backend type is valid or not. -func ValidateBackendType(config *v1.Config, key string, val any) error { +// validateBackendType is used to check that setting the backend type is valid or not. +func validateBackendType(config *v1.Config, key string, val any) error { backendType, _ := val.(string) if backendType != v1.BackendTypeLocal && backendType != v1.BackendTypeMysql && backendType != v1.BackendTypeOss && backendType != v1.BackendTypeS3 { return ErrUnsupportedBackendType @@ -102,8 +103,8 @@ func ValidateBackendType(config *v1.Config, key string, val any) error { return nil } -// ValidateUnsetBackendType is used to check that unsetting the backend config is valid or not. -func ValidateUnsetBackendType(config *v1.Config, key string) error { +// validateUnsetBackendType is used to check that unsetting the backend config is valid or not. +func validateUnsetBackendType(config *v1.Config, key string) error { backendName := parseBackendName(key) if config == nil || config.Backends == nil || config.Backends.Backends == nil || config.Backends.Backends[backendName] == nil { return nil @@ -117,33 +118,33 @@ func ValidateUnsetBackendType(config *v1.Config, key string) error { return nil } -// ValidateBackendConfigItems is used to check that setting the backend config items is valid or not. -func ValidateBackendConfigItems(config *v1.Config, key string, val any) error { +// validateBackendConfigItems is used to check that setting the backend config items is valid or not. +func validateBackendConfigItems(config *v1.Config, key string, val any) error { configItems, _ := val.(map[string]any) backendName := parseBackendName(key) if config == nil || config.Backends == nil || config.Backends.Backends == nil || config.Backends.Backends[backendName] == nil || config.Backends.Backends[backendName].Type == "" { return ErrEmptyBackendType } - backendConfig := &v1.BackendConfig{ + bkConfig := &v1.BackendConfig{ Type: config.Backends.Backends[backendName].Type, Configs: configItems, } - return validateBackendConfig(backendConfig) + return checkBackendConfig(bkConfig) } -// ValidateLocalBackendItem is used to check that setting the config item for local-type backend is valid or not. -func ValidateLocalBackendItem(config *v1.Config, key string, _ any) error { +// validateLocalBackendItem is used to check that setting the config item for local-type backend is valid or not. +func validateLocalBackendItem(config *v1.Config, key string, _ any) error { return checkBackendTypeForBackendItem(config, key, v1.BackendTypeLocal) } -// ValidateMysqlBackendItem is used to check that setting the config item for mysql-type backend is valid or not. -func ValidateMysqlBackendItem(config *v1.Config, key string, _ any) error { +// validateMysqlBackendItem is used to check that setting the config item for mysql-type backend is valid or not. +func validateMysqlBackendItem(config *v1.Config, key string, _ any) error { return checkBackendTypeForBackendItem(config, key, v1.BackendTypeMysql) } -// ValidateMysqlBackendPort is used to check that setting the port of mysql-type backend is valid or not. -func ValidateMysqlBackendPort(config *v1.Config, key string, val any) error { - if err := ValidateMysqlBackendItem(config, key, val); err != nil { +// validateMysqlBackendPort is used to check that setting the port of mysql-type backend is valid or not. +func validateMysqlBackendPort(config *v1.Config, key string, val any) error { + if err := validateMysqlBackendItem(config, key, val); err != nil { return err } port, _ := val.(int) @@ -153,52 +154,52 @@ func ValidateMysqlBackendPort(config *v1.Config, key string, val any) error { return nil } -// ValidateGenericOssBackendItem is used to check that setting the generic config of oss/s3-type backend is valid or not. -func ValidateGenericOssBackendItem(config *v1.Config, key string, _ any) error { +// validateGenericOssBackendItem is used to check that setting the generic config of oss/s3-type backend is valid or not. +func validateGenericOssBackendItem(config *v1.Config, key string, _ any) error { return checkBackendTypeForBackendItem(config, key, v1.BackendTypeOss, v1.BackendTypeS3) } -// ValidateS3BackendItem is used to check that setting the bucket of s3-type backend is valid or not. -func ValidateS3BackendItem(config *v1.Config, key string, _ any) error { +// validateS3BackendItem is used to check that setting the bucket of s3-type backend is valid or not. +func validateS3BackendItem(config *v1.Config, key string, _ any) error { return checkBackendTypeForBackendItem(config, key, v1.BackendTypeS3) } -// validateBackendConfig is used to check that setting the backend config is valid or not, which is called -// ValidateBackendConfig and ValidateBackendConfigItems. -func validateBackendConfig(backendConfig *v1.BackendConfig) error { - if err := validateBasalBackendConfig(backendConfig); err != nil { +// checkBackendConfig is used to check that setting the backend config is valid or not, which is called +// validateBackendConfig and validateBackendConfigItems. +func checkBackendConfig(config *v1.BackendConfig) error { + if err := checkBasalBackendConfig(config); err != nil { return err } - switch backendConfig.Type { + switch config.Type { case v1.BackendTypeMysql: - mysqlBackend := backendConfig.ToMysqlBackend() - if err := ValidateMysqlConfig(mysqlBackend); err != nil { + mysqlBackend := config.ToMysqlBackend() + if err := storages.ValidateMysqlConfigFromFile(mysqlBackend); err != nil { return err } case v1.BackendTypeOss: - ossBackend := backendConfig.ToOssBackend() - if err := ValidateGenericObjectStorageConfig(ossBackend.GenericBackendObjectStorageConfig); err != nil { + ossBackend := config.ToOssBackend() + if err := storages.ValidateOssConfigFromFile(ossBackend); err != nil { return err } case v1.BackendTypeS3: - ossBackend := backendConfig.ToS3Backend() - if err := ValidateGenericObjectStorageConfig(ossBackend.GenericBackendObjectStorageConfig); err != nil { + s3Backend := config.ToS3Backend() + if err := storages.ValidateS3ConfigFromFile(s3Backend); err != nil { return err } } return nil } -// validateBasalBackendConfig does basal validation of the backend config. Besides used when setting backend +// checkBasalBackendConfig does basal validation of the backend config. Besides used when setting backend // config, it's also used to check the validation of the current backend config -func validateBasalBackendConfig(backendConfig *v1.BackendConfig) error { - switch backendConfig.Type { +func checkBasalBackendConfig(config *v1.BackendConfig) error { + switch config.Type { case v1.BackendTypeLocal: items := map[string]checkTypeFunc{ v1.BackendLocalPath: checkString, } - if err := checkBasalBackendConfigItems(backendConfig, items); err != nil { + if err := checkBasalBackendConfigItems(config, items); err != nil { return err } case v1.BackendTypeMysql: @@ -209,7 +210,7 @@ func validateBasalBackendConfig(backendConfig *v1.BackendConfig) error { v1.BackendMysqlHost: checkString, v1.BackendMysqlPort: checkInt, } - if err := checkBasalBackendConfigItems(backendConfig, items); err != nil { + if err := checkBasalBackendConfigItems(config, items); err != nil { return err } case v1.BackendTypeOss: @@ -220,7 +221,7 @@ func validateBasalBackendConfig(backendConfig *v1.BackendConfig) error { v1.BackendGenericOssBucket: checkString, v1.BackendGenericOssPrefix: checkString, } - if err := checkBasalBackendConfigItems(backendConfig, items); err != nil { + if err := checkBasalBackendConfigItems(config, items); err != nil { return err } case v1.BackendTypeS3: @@ -232,7 +233,7 @@ func validateBasalBackendConfig(backendConfig *v1.BackendConfig) error { v1.BackendGenericOssPrefix: checkString, v1.BackendS3Region: checkString, } - if err := checkBasalBackendConfigItems(backendConfig, items); err != nil { + if err := checkBasalBackendConfigItems(config, items); err != nil { return err } default: @@ -316,39 +317,3 @@ func checkInt(val any) error { } return nil } - -// todo: the following funcs should be moved to package backend later - -var ( - ErrEmptyMysqlDBName = errors.New("empty db name") - ErrEmptyMysqlUser = errors.New("empty mysql db user") - ErrEmptyMysqlHost = errors.New("empty mysql host") - ErrInvalidMysqlPort = errors.New("mysql port must be between 1 and 65535") - ErrEmptyBucket = errors.New("empty bucket") -) - -// ValidateMysqlConfig is used to validate v1.BackendMysqlConfig is valid or not. -func ValidateMysqlConfig(config *v1.BackendMysqlConfig) error { - if config.DBName == "" { - return ErrEmptyMysqlDBName - } - if config.User == "" { - return ErrEmptyMysqlUser - } - if config.Host == "" { - return ErrEmptyMysqlHost - } - if config.Port != 0 && (config.Port < 1 || config.Port > 65535) { - return ErrInvalidMysqlPort - } - return nil -} - -// ValidateGenericObjectStorageConfig is used to validate v1.BackendOssConfig and v1.BackendS3Config is -// valid or not, where the sensitive data items set as environment variables are not included. -func ValidateGenericObjectStorageConfig(config *v1.GenericBackendObjectStorageConfig) error { - if config.Bucket == "" { - return ErrEmptyBucket - } - return nil -} diff --git a/pkg/config/validation/validation_test.go b/pkg/config/validation_test.go similarity index 90% rename from pkg/config/validation/validation_test.go rename to pkg/config/validation_test.go index 3504e299..ee9eac3a 100644 --- a/pkg/config/validation/validation_test.go +++ b/pkg/config/validation_test.go @@ -1,4 +1,4 @@ -package validation +package config import ( "testing" @@ -66,7 +66,7 @@ func TestValidateConfig(t *testing.T) { for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { - err := ValidateConfig(tc.config) + err := validateConfig(tc.config) assert.Equal(t, tc.success, err == nil) }) } @@ -113,7 +113,7 @@ func TestValidateCurrentBackend(t *testing.T) { for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { - err := ValidateCurrentBackend(tc.config, "", tc.val) + err := validateCurrentBackend(tc.config, "", tc.val) assert.Equal(t, tc.success, err == nil) }) } @@ -141,10 +141,10 @@ func TestValidateBackendConfig(t *testing.T) { val: &v1.BackendConfig{ Type: v1.BackendTypeMysql, Configs: map[string]any{ - v1.BackendMysqlDBName: "db-name-kusion", - v1.BackendMysqlUser: "pwd-kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, }, @@ -154,7 +154,8 @@ func TestValidateBackendConfig(t *testing.T) { val: &v1.BackendConfig{ Type: v1.BackendTypeOss, Configs: map[string]any{ - v1.BackendGenericOssBucket: "bucket-kusion", + v1.BackendGenericOssBucket: "kusion", + v1.BackendGenericOssEndpoint: "http://oss-cn-hangzhou.aliyuncs.com", }, }, }, @@ -164,7 +165,8 @@ func TestValidateBackendConfig(t *testing.T) { val: &v1.BackendConfig{ Type: v1.BackendTypeS3, Configs: map[string]any{ - v1.BackendGenericOssBucket: "bucket-kusion", + v1.BackendGenericOssBucket: "kusion", + v1.BackendGenericOssEndpoint: "http://oss-cn-hangzhou.aliyuncs.com", }, }, }, @@ -181,10 +183,10 @@ func TestValidateBackendConfig(t *testing.T) { val: &v1.BackendConfig{ Type: v1.DeprecatedBackendMysql, Configs: map[string]any{ - v1.BackendMysqlDBName: "db-name-kusion", - v1.BackendMysqlUser: "pwd-kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: "6443", + v1.BackendMysqlPort: "3306", }, }, }, @@ -202,7 +204,7 @@ func TestValidateBackendConfig(t *testing.T) { for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { - err := ValidateBackendConfig(nil, "", tc.val) + err := validateBackendConfig(nil, "", tc.val) assert.Equal(t, tc.success, err == nil) }) } @@ -244,7 +246,7 @@ func TestValidateUnsetBackendConfig(t *testing.T) { for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { - err := ValidateUnsetBackendConfig(tc.config, tc.key) + err := validateUnsetBackendConfig(tc.config, tc.key) assert.Equal(t, tc.success, err == nil) }) } @@ -296,7 +298,7 @@ func TestValidateBackendType(t *testing.T) { for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { - err := ValidateBackendType(tc.config, tc.key, tc.val) + err := validateBackendType(tc.config, tc.key, tc.val) assert.Equal(t, tc.success, err == nil) }) } @@ -353,7 +355,7 @@ func TestValidateUnsetBackendType(t *testing.T) { for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { - err := ValidateUnsetBackendType(tc.config, tc.key) + err := validateUnsetBackendType(tc.config, tc.key) assert.Equal(t, tc.success, err == nil) }) } @@ -379,10 +381,10 @@ func TestValidateBackendConfigItems(t *testing.T) { }, key: "backends.dev.configs", val: map[string]any{ - v1.BackendMysqlDBName: "db-name-kusion", - v1.BackendMysqlUser: "pwd-kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, { @@ -397,17 +399,17 @@ func TestValidateBackendConfigItems(t *testing.T) { }, key: "backends.dev.configs", val: map[string]any{ - v1.BackendMysqlDBName: "db-name-kusion", - v1.BackendMysqlUser: "pwd-kusion", + v1.BackendMysqlDBName: "kusion", + v1.BackendMysqlUser: "kk", v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 6443, + v1.BackendMysqlPort: 3306, }, }, } for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { - err := ValidateBackendConfigItems(tc.config, tc.key, tc.val) + err := validateBackendConfigItems(tc.config, tc.key, tc.val) assert.Equal(t, tc.success, err == nil) }) } @@ -432,7 +434,7 @@ func TestValidateMysqlBackendPort(t *testing.T) { }, }, key: "backends.dev.configs.port", - val: 6443, + val: 3306, }, { name: "invalid mysql port", @@ -451,7 +453,7 @@ func TestValidateMysqlBackendPort(t *testing.T) { for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { - err := ValidateMysqlBackendPort(tc.config, tc.key, tc.val) + err := validateMysqlBackendPort(tc.config, tc.key, tc.val) assert.Equal(t, tc.success, err == nil) }) }