diff --git a/pkg/apis/internal.kusion.io/v1/types.go b/pkg/apis/internal.kusion.io/v1/types.go index 44d442bb..89fe7af4 100644 --- a/pkg/apis/internal.kusion.io/v1/types.go +++ b/pkg/apis/internal.kusion.io/v1/types.go @@ -332,11 +332,6 @@ const ( BackendType = "type" BackendConfigItems = "configs" BackendLocalPath = "path" - BackendMysqlDBName = "dbName" - BackendMysqlUser = "user" - BackendMysqlPassword = "password" - BackendMysqlHost = "host" - BackendMysqlPort = "port" BackendGenericOssEndpoint = "endpoint" BackendGenericOssAK = "accessKeyID" BackendGenericOssSK = "accessKeySecret" @@ -345,19 +340,15 @@ const ( BackendS3Region = "region" BackendTypeLocal = "local" - BackendTypeMysql = "mysql" BackendTypeOss = "oss" BackendTypeS3 = "s3" - EnvBackendMysqlPassword = "KUSION_BACKEND_MYSQL_PASSWORD" - EnvOssAccessKeyID = "OSS_ACCESS_KEY_ID" - EnvOssAccessKeySecret = "OSS_ACCESS_KEY_SECRET" - EnvAwsAccessKeyID = "AWS_ACCESS_KEY_ID" - EnvAwsSecretAccessKey = "AWS_SECRET_ACCESS_KEY" - EnvAwsDefaultRegion = "AWS_DEFAULT_REGION" - EnvAwsRegion = "AWS_REGION" - - DefaultMysqlPort = 3306 + EnvOssAccessKeyID = "OSS_ACCESS_KEY_ID" + EnvOssAccessKeySecret = "OSS_ACCESS_KEY_SECRET" + EnvAwsAccessKeyID = "AWS_ACCESS_KEY_ID" + EnvAwsSecretAccessKey = "AWS_SECRET_ACCESS_KEY" + EnvAwsDefaultRegion = "AWS_DEFAULT_REGION" + EnvAwsRegion = "AWS_REGION" ) // BackendConfigs contains the configuration of multiple backends and the current backend. @@ -371,7 +362,7 @@ type BackendConfigs struct { // BackendConfig contains the type and configs of a backend, which is used to store Spec, State and Workspace. type BackendConfig struct { - // Type is the backend type, supports BackendTypeLocal, BackendTypeMysql, BackendTypeOss, BackendTypeS3. + // Type is the backend type, supports BackendTypeLocal, BackendTypeOss, BackendTypeS3. Type string `yaml:"type,omitempty" json:"type,omitempty"` // Configs contains config items of the backend, whose keys differ from different backend types. @@ -385,25 +376,6 @@ type BackendLocalConfig struct { Path string `yaml:"path,omitempty" json:"path,omitempty"` } -// BackendMysqlConfig contains the config of using mysql database as backend, which can be converted -// from BackendConfig if Type is BackendMysqlConfig. -type BackendMysqlConfig struct { - // DBName is the database name. - DBName string `yaml:"dbName" json:"dbName"` - - // User of the database. - User string `yaml:"user" json:"user"` - - // Password of the database. - Password string `yaml:"password,omitempty" json:"password,omitempty"` - - // Host of the database. - Host string `yaml:"host" json:"host"` - - // Port of the database. If not set, then it will be set to DeprecatedDefaultMysqlPort. - Port int `yaml:"port,omitempty" json:"port,omitempty"` -} - // BackendOssConfig contains the config of using OSS as backend, which can be converted from BackendConfig // if Type is BackendOssConfig. type BackendOssConfig struct { @@ -450,26 +422,6 @@ func (b *BackendConfig) ToLocalBackend() *BackendLocalConfig { } } -// ToMysqlBackend converts BackendConfig to structured BackendMysqlConfig, works only when the Type -// is BackendTypeMysql, and the Configs are with correct type, or return nil. -func (b *BackendConfig) ToMysqlBackend() *BackendMysqlConfig { - if b.Type != BackendTypeMysql { - return nil - } - dbName, _ := b.Configs[BackendMysqlDBName].(string) - user, _ := b.Configs[BackendMysqlUser].(string) - password, _ := b.Configs[BackendMysqlPassword].(string) - host, _ := b.Configs[BackendMysqlHost].(string) - port, _ := b.Configs[BackendMysqlPort].(int) - return &BackendMysqlConfig{ - DBName: dbName, - User: user, - Password: password, - Host: host, - Port: port, - } -} - // ToOssBackend converts BackendConfig to structured BackendOssConfig, works only when the Type is // BackendTypeOss, and the Configs are with correct type, or return nil. func (b *BackendConfig) ToOssBackend() *BackendOssConfig { diff --git a/pkg/backend/backend.go b/pkg/backend/backend.go index b3f55fa5..a176d871 100644 --- a/pkg/backend/backend.go +++ b/pkg/backend/backend.go @@ -46,16 +46,6 @@ func NewBackend(name string) (Backend, error) { return nil, fmt.Errorf("complete local config failed, %w", err) } return storages.NewLocalStorage(bkConfig), nil - case v1.BackendTypeMysql: - bkConfig := bkCfg.ToMysqlBackend() - storages.CompleteMysqlConfig(bkConfig) - if err = storages.ValidateMysqlConfig(bkConfig); err != nil { - return nil, fmt.Errorf("invalid config of backend %s, %w", name, err) - } - storage, err = storages.NewMysqlStorage(bkConfig) - if err != nil { - return nil, fmt.Errorf("new mysql storage of backend %s failed, %w", name, err) - } case v1.BackendTypeOss: bkConfig := bkCfg.ToOssBackend() storages.CompleteOssConfig(bkConfig) diff --git a/pkg/backend/backend_test.go b/pkg/backend/backend_test.go index 3bba047d..5209089c 100644 --- a/pkg/backend/backend_test.go +++ b/pkg/backend/backend_test.go @@ -27,15 +27,6 @@ func mockConfig() *v1.Config { v1.BackendLocalPath: "/etc", }, }, - "pre": { - Type: v1.BackendTypeMysql, - Configs: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, - }, - }, "staging": { Type: v1.BackendTypeOss, Configs: map[string]any{ @@ -60,7 +51,6 @@ func mockCompleteLocalStorage() { func mockNewStorage() { mockey.Mock(storages.NewLocalStorage).Return(&storages.LocalStorage{}).Build() - mockey.Mock(storages.NewMysqlStorage).Return(&storages.MysqlStorage{}, nil).Build() mockey.Mock(storages.NewOssStorage).Return(&storages.OssStorage{}, nil).Build() mockey.Mock(storages.NewS3Storage).Return(&storages.S3Storage{}, nil).Build() } @@ -86,14 +76,6 @@ func TestNewBackend(t *testing.T) { bkName: "", storage: &storages.LocalStorage{}, }, - { - name: "new current backend", - success: true, - cfg: mockConfig(), - envs: nil, - bkName: "", - storage: &storages.MysqlStorage{}, - }, { name: "new local backend", success: true, @@ -110,16 +92,6 @@ func TestNewBackend(t *testing.T) { bkName: "dev", storage: &storages.LocalStorage{}, }, - { - name: "new mysql backend", - success: true, - cfg: mockConfig(), - envs: map[string]string{ - v1.EnvBackendMysqlPassword: "fake-password", - }, - bkName: "pre", - storage: &storages.MysqlStorage{}, - }, { name: "new oss backend", success: true, diff --git a/pkg/backend/storages/completion.go b/pkg/backend/storages/completion.go index 64a69129..9c9e8ece 100644 --- a/pkg/backend/storages/completion.go +++ b/pkg/backend/storages/completion.go @@ -19,18 +19,6 @@ func CompleteLocalConfig(config *v1.BackendLocalConfig) error { return nil } -// CompleteMysqlConfig sets default value of port if not set, which is 3306, and fulfills password from environment -// variables if set. -func CompleteMysqlConfig(config *v1.BackendMysqlConfig) { - if config.Port == 0 { - config.Port = v1.DefaultMysqlPort - } - password := os.Getenv(v1.EnvBackendMysqlPassword) - if password != "" { - config.Password = password - } -} - // CompleteOssConfig fulfills the whole oss config from environment variables if set. func CompleteOssConfig(config *v1.BackendOssConfig) { accessKeyID := os.Getenv(v1.EnvOssAccessKeyID) diff --git a/pkg/backend/storages/completion_test.go b/pkg/backend/storages/completion_test.go index 9eb9ac8a..f395d8ac 100644 --- a/pkg/backend/storages/completion_test.go +++ b/pkg/backend/storages/completion_test.go @@ -42,44 +42,6 @@ func TestCompleteLocalConfig(t *testing.T) { } } -func TestCompleteMysqlConfig(t *testing.T) { - testcases := []struct { - name string - config *v1.BackendMysqlConfig - envs map[string]string - completeConfig *v1.BackendMysqlConfig - }{ - { - name: "complete mysql config", - config: &v1.BackendMysqlConfig{ - DBName: "kusion", - User: "kk", - Host: "127.0.0.1", - }, - envs: map[string]string{ - v1.EnvBackendMysqlPassword: "fake-password", - }, - completeConfig: &v1.BackendMysqlConfig{ - DBName: "kusion", - User: "kk", - Host: "127.0.0.1", - Port: 3306, - Password: "fake-password", - }, - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - for k, v := range tc.envs { - _ = os.Setenv(k, v) - } - CompleteMysqlConfig(tc.config) - assert.Equal(t, tc.completeConfig, tc.config) - }) - } -} - func TestCompleteOssConfig(t *testing.T) { testcases := []struct { name string diff --git a/pkg/backend/storages/mysql.go b/pkg/backend/storages/mysql.go deleted file mode 100644 index a261434a..00000000 --- a/pkg/backend/storages/mysql.go +++ /dev/null @@ -1,49 +0,0 @@ -package storages - -import ( - "strconv" - - gomysql "github.com/go-sql-driver/mysql" - "gorm.io/driver/mysql" - "gorm.io/gorm" - - v1 "kusionstack.io/kusion/pkg/apis/internal.kusion.io/v1" - "kusionstack.io/kusion/pkg/engine/state" - statestorages "kusionstack.io/kusion/pkg/engine/state/storages" - "kusionstack.io/kusion/pkg/workspace" - workspacestorages "kusionstack.io/kusion/pkg/workspace/storages" -) - -// MysqlStorage is an implementation of backend.Backend which uses mysql as storage. -type MysqlStorage struct { - db *gorm.DB -} - -func NewMysqlStorage(config *v1.BackendMysqlConfig) (*MysqlStorage, error) { - c := gomysql.NewConfig() - c.User = config.User - c.Passwd = config.Password - c.Addr = config.Host + ":" + strconv.Itoa(config.Port) - c.DBName = config.DBName - c.Net = "tcp" - c.ParseTime = true - c.InterpolateParams = true - c.Params = map[string]string{ - "charset": "utf8", - "loc": "Asia/Shanghai", - } - db, err := gorm.Open(mysql.Open(c.FormatDSN()), &gorm.Config{}) - if err != nil { - return nil, err - } - - return &MysqlStorage{db: db}, nil -} - -func (s *MysqlStorage) StateStorage(project, workspace string) state.Storage { - return statestorages.NewMysqlStorage(s.db, project, workspace) -} - -func (s *MysqlStorage) WorkspaceStorage() (workspace.Storage, error) { - return workspacestorages.NewMysqlStorage(s.db) -} diff --git a/pkg/backend/storages/mysql_test.go b/pkg/backend/storages/mysql_test.go deleted file mode 100644 index 2d76fb04..00000000 --- a/pkg/backend/storages/mysql_test.go +++ /dev/null @@ -1,99 +0,0 @@ -package storages - -import ( - "testing" - - "github.com/bytedance/mockey" - "github.com/stretchr/testify/assert" - "gorm.io/gorm" - - v1 "kusionstack.io/kusion/pkg/apis/internal.kusion.io/v1" - "kusionstack.io/kusion/pkg/engine/state" - statestorages "kusionstack.io/kusion/pkg/engine/state/storages" - workspacestorages "kusionstack.io/kusion/pkg/workspace/storages" -) - -func TestNewMysqlStorage(t *testing.T) { - testcases := []struct { - name string - success bool - config *v1.BackendMysqlConfig - }{ - { - name: "new mysql storage successfully", - success: true, - config: &v1.BackendMysqlConfig{ - DBName: "kusion", - User: "kk", - Host: "127.0.0.1", - Port: 3306, - }, - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - mockey.PatchConvey("mock gorm db", t, func() { - mockey.Mock(gorm.Open).Return(&gorm.DB{}, nil).Build() - _, err := NewMysqlStorage(tc.config) - assert.Equal(t, tc.success, err == nil) - }) - }) - } -} - -func TestMysqlStorage_StateStorage(t *testing.T) { - testcases := []struct { - name string - mysqlStorage *MysqlStorage - project, workspace string - stateStorage state.Storage - }{ - { - name: "state storage from mysql", - mysqlStorage: &MysqlStorage{ - db: &gorm.DB{}, - }, - project: "wordpress", - workspace: "dev", - stateStorage: statestorages.NewMysqlStorage( - &gorm.DB{}, - "wordpress", - "dev", - ), - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - stateStorage := tc.mysqlStorage.StateStorage(tc.project, tc.workspace) - assert.Equal(t, tc.stateStorage, stateStorage) - }) - } -} - -func TestMysqlStorage_WorkspaceStorage(t *testing.T) { - testcases := []struct { - name string - success bool - mysqlStorage *MysqlStorage - }{ - { - name: "workspace storage from mysql backend", - success: true, - mysqlStorage: &MysqlStorage{ - db: &gorm.DB{}, - }, - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - mockey.PatchConvey("mock new mysql workspace storage", t, func() { - mockey.Mock(workspacestorages.NewMysqlStorage).Return(&workspacestorages.MysqlStorage{}, nil).Build() - _, err := tc.mysqlStorage.WorkspaceStorage() - assert.Equal(t, tc.success, err == nil) - }) - }) - } -} diff --git a/pkg/backend/storages/validation.go b/pkg/backend/storages/validation.go index 40abd275..02e0c525 100644 --- a/pkg/backend/storages/validation.go +++ b/pkg/backend/storages/validation.go @@ -8,10 +8,6 @@ import ( ) 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") ErrEmptyAccessKeyID = errors.New("empty access key id") ErrEmptyAccessKeySecret = errors.New("empty access key secret") @@ -19,36 +15,6 @@ var ( ErrEmptyS3Region = errors.New("empty s3 region") ) -// ValidateMysqlConfig is used to validate the v1.BackendMysqlConfig is valid or not. -// If valid, the config contains all valid items to new a mysql DB. -func ValidateMysqlConfig(config *v1.BackendMysqlConfig) error { - if err := ValidateMysqlConfigFromFile(config); err != nil { - return err - } - if config.Port < 1 || config.Port > 65535 { - return ErrInvalidMysqlPort - } - return nil -} - -// ValidateMysqlConfigFromFile is used to validate the v1.BackendMysqlConfig parsed from config file is valid -// or not, where the sensitive data items set as environment variables are not included. -func ValidateMysqlConfigFromFile(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 -} - // ValidateOssConfig is used to validate v1.BackendOssConfig is valid or not, where all the items are included. // If valid, the config contains all valid items to new an oss client. func ValidateOssConfig(config *v1.BackendOssConfig) error { diff --git a/pkg/backend/storages/validation_test.go b/pkg/backend/storages/validation_test.go index e0080729..4e7aec4b 100644 --- a/pkg/backend/storages/validation_test.go +++ b/pkg/backend/storages/validation_test.go @@ -8,103 +8,6 @@ import ( v1 "kusionstack.io/kusion/pkg/apis/internal.kusion.io/v1" ) -func TestValidateMysqlConfig(t *testing.T) { - testcases := []struct { - name string - success bool - config *v1.BackendMysqlConfig - }{ - { - name: "valid mysql config", - success: true, - config: &v1.BackendMysqlConfig{ - DBName: "kusion", - User: "kk", - Host: "127.0.0.1", - Port: 3306, - }, - }, - { - name: "invalid mysql config empty port", - success: false, - config: &v1.BackendMysqlConfig{ - DBName: "kusion", - User: "kk", - Host: "127.0.0.1", - }, - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - err := ValidateMysqlConfig(tc.config) - assert.Equal(t, tc.success, err == nil) - }) - } -} - -func TestValidateMysqlConfigFromFile(t *testing.T) { - testcases := []struct { - name string - success bool - config *v1.BackendMysqlConfig - }{ - { - name: "valid mysql config", - success: true, - config: &v1.BackendMysqlConfig{ - DBName: "kusion", - User: "kk", - Host: "127.0.0.1", - }, - }, - { - name: "invalid mysql config empty dbName", - success: false, - config: &v1.BackendMysqlConfig{ - DBName: "", - User: "kk", - Host: "127.0.0.1", - }, - }, - { - name: "invalid mysql config empty user", - success: false, - config: &v1.BackendMysqlConfig{ - DBName: "kusion", - User: "", - Host: "127.0.0.1", - }, - }, - { - name: "invalid mysql config empty host", - success: false, - config: &v1.BackendMysqlConfig{ - DBName: "kusion", - User: "kk", - Host: "", - }, - }, - { - name: "invalid mysql config invalid port", - success: false, - config: &v1.BackendMysqlConfig{ - DBName: "kusion", - User: "kk", - Host: "127.0.0.1", - Port: -1, - }, - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - err := ValidateMysqlConfigFromFile(tc.config) - assert.Equal(t, tc.success, err == nil) - }) - } -} - func TestValidateOssConfig(t *testing.T) { testcases := []struct { name string diff --git a/pkg/cmd/config/set/cmd.go b/pkg/cmd/config/set/cmd.go index 61b7a78b..64339371 100644 --- a/pkg/cmd/config/set/cmd.go +++ b/pkg/cmd/config/set/cmd.go @@ -17,13 +17,10 @@ func NewCmd() *cobra.Command { example = i18n.T(` # Set a config item with string type value - kusion config set backends.current mysql-pre - - # Set a config item with int type value - kusion config set backends.mysql-pre.configs.port 3306 + kusion config set backends.current s3-pre # Set a config item with struct or map type value - kusion config set backends.mysql-pre.configs '{"dbName":"kusion","user":"kk","host":"127.0.0.1","port":3306}'`) + kusion config set backends.s3-pre.configs '{"bucket":"kusion"}'`) ) o := NewOptions() diff --git a/pkg/cmd/config/set/cmd_test.go b/pkg/cmd/config/set/cmd_test.go index c0bdf3fa..fa81dfc6 100644 --- a/pkg/cmd/config/set/cmd_test.go +++ b/pkg/cmd/config/set/cmd_test.go @@ -12,7 +12,7 @@ func TestNewCmd(t *testing.T) { mockey.PatchConvey("mock cmd", t, func() { mockey.Mock((*Options).Complete).To(func(o *Options, args []string) error { o.Item = "backends.current" - o.Value = "mysql-pre" + o.Value = "s3-pre" return nil }).Build() mockey.Mock((*Options).Run).Return(nil).Build() diff --git a/pkg/cmd/config/set/options_test.go b/pkg/cmd/config/set/options_test.go index 001628b8..9cd33a63 100644 --- a/pkg/cmd/config/set/options_test.go +++ b/pkg/cmd/config/set/options_test.go @@ -18,11 +18,11 @@ func TestOptions_Complete(t *testing.T) { }{ { name: "successfully complete options", - args: []string{"backends.current", "mysql-pre"}, + args: []string{"backends.current", "s3-pre"}, success: true, expectedOpts: &Options{ Item: "backends.current", - Value: "mysql-pre", + Value: "s3-pre", }, }, { @@ -55,14 +55,14 @@ func TestOptions_Validate(t *testing.T) { name: "valid options", opts: &Options{ Item: "backends.current", - Value: "mysql-pre", + Value: "s3-pre", }, success: true, }, { name: "invalid options empty config item", opts: &Options{ - Value: "mysql-pre", + Value: "s3-pre", }, success: false, }, @@ -93,7 +93,7 @@ func TestOptions_Run(t *testing.T) { name: "successfully run", opts: &Options{ Item: "backends.current", - Value: "mysql-pre", + Value: "s3-pre", }, success: true, }, diff --git a/pkg/cmd/config/unset/cmd.go b/pkg/cmd/config/unset/cmd.go index 6d6cfcb6..b6c99dfb 100644 --- a/pkg/cmd/config/unset/cmd.go +++ b/pkg/cmd/config/unset/cmd.go @@ -17,7 +17,7 @@ func NewCmd() *cobra.Command { example = i18n.T(` # Unset a config item - kusion config unset backends.mysql-pre.configs.port`) + kusion config unset backends.s3-pre.configs.bucket`) ) o := NewOptions() diff --git a/pkg/cmd/config/unset/cmd_test.go b/pkg/cmd/config/unset/cmd_test.go index 7d73c566..96f4db13 100644 --- a/pkg/cmd/config/unset/cmd_test.go +++ b/pkg/cmd/config/unset/cmd_test.go @@ -11,7 +11,7 @@ func TestNewCmd(t *testing.T) { t.Run("successfully unset config item", func(t *testing.T) { mockey.PatchConvey("mock cmd", t, func() { mockey.Mock((*Options).Complete).To(func(o *Options, args []string) error { - o.Item = "backends.mysql-pre.configs.port" + o.Item = "backends.s3-pre.configs.bucket" return nil }).Build() mockey.Mock((*Options).Run).Return(nil).Build() diff --git a/pkg/cmd/config/unset/options_test.go b/pkg/cmd/config/unset/options_test.go index 403fe680..bf5899c5 100644 --- a/pkg/cmd/config/unset/options_test.go +++ b/pkg/cmd/config/unset/options_test.go @@ -18,10 +18,10 @@ func TestOptions_Complete(t *testing.T) { }{ { name: "successfully complete options", - args: []string{"backends.mysql-pre.configs.port"}, + args: []string{"backends.s3-pre.configs.bucket"}, success: true, expectedOpts: &Options{ - Item: "backends.mysql-pre.configs.port", + Item: "backends.s3-pre.configs.bucket", }, }, { @@ -53,7 +53,7 @@ func TestOptions_Validate(t *testing.T) { { name: "valid options", opts: &Options{ - Item: "backends.mysql-pre.configs.port", + Item: "backends.s3-pre.configs.bucket", }, success: true, }, @@ -81,7 +81,7 @@ func TestOptions_Run(t *testing.T) { { name: "successfully run", opts: &Options{ - Item: "backends.mysql-pre.configs.port", + Item: "backends.s3-pre.configs.bucket", }, success: true, }, diff --git a/pkg/config/operator_test.go b/pkg/config/operator_test.go index 33bebb30..a4b1e167 100644 --- a/pkg/config/operator_test.go +++ b/pkg/config/operator_test.go @@ -31,15 +31,6 @@ func mockValidConfig() *v1.Config { "dev": { Type: v1.BackendTypeLocal, }, - "pre": { - Type: v1.BackendTypeMysql, - Configs: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, - }, - }, "prod": { Type: v1.BackendTypeS3, Configs: map[string]any{ @@ -61,15 +52,6 @@ func mockValidCfgMap() map[string]any { "dev": map[string]any{ v1.BackendType: v1.BackendTypeLocal, }, - "pre": map[string]any{ - v1.BackendType: v1.BackendTypeMysql, - v1.BackendConfigItems: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, - }, - }, "prod": map[string]any{ v1.BackendType: v1.BackendTypeS3, v1.BackendConfigItems: map[string]any{ @@ -258,28 +240,18 @@ func TestOperator_GetConfigItem(t *testing.T) { name: "get structured config item successfully type string", success: true, o: mockOperator(mockConfigPath, mockValidConfig()), - key: "backends.pre.configs.host", - expectedVal: "127.0.0.1", - }, - { - name: "get structured config item successfully type int", - success: true, - o: mockOperator(mockConfigPath, mockValidConfig()), - key: "backends.pre.configs.port", - expectedVal: 3306, + key: "backends.prod.configs.bucket", + expectedVal: "kusion", }, { name: "get structured config item successfully type pointer of struct", success: true, o: mockOperator(mockConfigPath, mockValidConfig()), - key: "backends.pre", + key: "backends.prod", expectedVal: &v1.BackendConfig{ - Type: v1.BackendTypeMysql, + Type: v1.BackendTypeS3, Configs: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, + v1.BackendGenericOssBucket: "kusion", }, }, }, @@ -315,22 +287,15 @@ func TestOperator_GetEncodedConfigItem(t *testing.T) { name: "get encoding config item successfully type string", success: true, o: mockOperator(mockConfigPath, mockValidConfig()), - key: "backends.pre.configs.host", - expectedVal: "127.0.0.1", - }, - { - name: "get encoding config item successfully type int", - success: true, - o: mockOperator(mockConfigPath, mockValidConfig()), - key: "backends.pre.configs.port", - expectedVal: "3306", + key: "backends.prod.configs.bucket", + expectedVal: "kusion", }, { name: "get encoding config item successfully type map", success: true, o: mockOperator(mockConfigPath, mockValidConfig()), - key: "backends.pre", - expectedVal: `{"configs":{"dbName":"kusion","host":"127.0.0.1","port":3306,"user":"kk"},"type":"mysql"}`, + key: "backends.prod", + expectedVal: `{"configs":{"bucket":"kusion"},"type":"s3"}`, }, } @@ -370,43 +335,15 @@ func TestOperator_SetConfigItem(t *testing.T) { }, }, }, - { - name: "set config item successfully type int", - success: true, - o: mockOperator(mockConfigPath, &v1.Config{ - Backends: &v1.BackendConfigs{ - Backends: map[string]*v1.BackendConfig{ - "pre": {Type: v1.BackendTypeMysql}, - }, - }, - }), - key: "backends.pre.configs.port", - val: 3306, - expectedConfig: &v1.Config{ - Backends: &v1.BackendConfigs{ - Backends: map[string]*v1.BackendConfig{ - "pre": { - Type: v1.BackendTypeMysql, - Configs: map[string]any{ - v1.BackendMysqlPort: 3306, - }, - }, - }, - }, - }, - }, { name: "set config item successfully type struct", success: true, o: mockOperator(mockConfigPath, mockValidConfig()), - key: "backends.pre", + key: "backends.prod", val: &v1.BackendConfig{ - Type: v1.BackendTypeMysql, + Type: v1.BackendTypeS3, Configs: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk-tired", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, + v1.BackendGenericOssBucket: "kusion-s3", }, }, expectedConfig: &v1.Config{ @@ -419,19 +356,10 @@ func TestOperator_SetConfigItem(t *testing.T) { "dev": { Type: v1.BackendTypeLocal, }, - "pre": { - Type: v1.BackendTypeMysql, - Configs: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk-tired", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, - }, - }, "prod": { Type: v1.BackendTypeS3, Configs: map[string]any{ - v1.BackendGenericOssBucket: "kusion", + v1.BackendGenericOssBucket: "kusion-s3", }, }, }, @@ -456,15 +384,6 @@ func TestOperator_SetConfigItem(t *testing.T) { "dev": { Type: v1.BackendTypeLocal, }, - "pre": { - Type: v1.BackendTypeMysql, - Configs: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, - }, - }, "prod": { Type: v1.BackendTypeS3, Configs: map[string]any{ @@ -551,37 +470,12 @@ func TestOperator_setEncodedConfigItem(t *testing.T) { }, }, }, - { - name: "set config item successfully type int", - success: true, - o: mockOperator(mockConfigPath, &v1.Config{ - Backends: &v1.BackendConfigs{ - Backends: map[string]*v1.BackendConfig{ - "pre": {Type: v1.BackendTypeMysql}, - }, - }, - }), - key: "backends.pre.configs.port", - val: "3306", - expectedConfig: &v1.Config{ - Backends: &v1.BackendConfigs{ - Backends: map[string]*v1.BackendConfig{ - "pre": { - Type: v1.BackendTypeMysql, - Configs: map[string]any{ - v1.BackendMysqlPort: 3306, - }, - }, - }, - }, - }, - }, { name: "set config item successfully type struct", success: true, o: mockOperator(mockConfigPath, mockValidConfig()), - key: "backends.pre", - val: `{"configs":{"dbName":"kusion","host":"127.0.0.1","port":3306,"user":"kk-tired"},"type":"mysql"}`, + key: "backends.prod", + val: `{"configs":{"bucket":"kusion"},"type":"s3"}`, expectedConfig: &v1.Config{ Backends: &v1.BackendConfigs{ Current: "dev", @@ -592,15 +486,6 @@ func TestOperator_setEncodedConfigItem(t *testing.T) { "dev": { Type: v1.BackendTypeLocal, }, - "pre": { - Type: v1.BackendTypeMysql, - Configs: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk-tired", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, - }, - }, "prod": { Type: v1.BackendTypeS3, Configs: map[string]any{ @@ -627,15 +512,6 @@ func TestOperator_setEncodedConfigItem(t *testing.T) { "dev": { Type: v1.BackendTypeLocal, }, - "pre": { - Type: v1.BackendTypeMysql, - Configs: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, - }, - }, "prod": { Type: v1.BackendTypeS3, Configs: map[string]any{ @@ -836,13 +712,6 @@ func TestParseStructuredConfigItem(t *testing.T) { strVal: "dev", val: "dev", }, - { - name: "parse structured config item successfully int", - success: true, - info: newRegisteredItems()["backends.*.configs.port"], - strVal: "3306", - val: 3306, - }, { name: "parse structured config item successfully bool", success: true, @@ -854,14 +723,11 @@ func TestParseStructuredConfigItem(t *testing.T) { name: "parse structured config item successfully struct ptr", success: true, info: newRegisteredItems()["backends.*"], - strVal: `{"configs":{"dbName":"kusion","host":"127.0.0.1","port":3306,"user":"kk"},"type":"mysql"}`, + strVal: `{"configs":{"bucket":"kusion"},"type":"s3"}`, val: &v1.BackendConfig{ - Type: v1.BackendTypeMysql, + Type: v1.BackendTypeS3, Configs: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, + v1.BackendGenericOssBucket: "kusion", }, }, }, @@ -869,14 +735,11 @@ func TestParseStructuredConfigItem(t *testing.T) { name: "parse structured config item successfully struct", success: true, info: &itemInfo{v1.BackendConfig{}, nil, nil}, - strVal: `{"configs":{"dbName":"kusion","host":"127.0.0.1","port":3306,"user":"kk"},"type":"mysql"}`, + strVal: `{"configs":{"bucket":"kusion"},"type":"s3"}`, val: v1.BackendConfig{ - Type: v1.BackendTypeMysql, + Type: v1.BackendTypeS3, Configs: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, + v1.BackendGenericOssBucket: "kusion", }, }, }, @@ -889,13 +752,6 @@ func TestParseStructuredConfigItem(t *testing.T) { v1.BackendGenericOssBucket: "kusion", }, }, - { - name: "failed to parse structured config item int", - success: false, - info: newRegisteredItems()["backends.*.configs.port"], - strVal: "not_valid_int", - val: nil, - }, { name: "failed to parse structured config item bool", success: false, @@ -1004,13 +860,6 @@ func TestGetItemFromCfgMap(t *testing.T) { key: "backends.current", expectedVal: "dev", }, - { - name: "get item from config map successfully type int", - success: true, - cfg: mockValidCfgMap(), - key: "backends.pre.configs.port", - expectedVal: 3306, - }, { name: "get item from config map successfully type map", success: true, @@ -1075,22 +924,22 @@ func TestSetItemFromCfgMap(t *testing.T) { cfg: map[string]any{ "backends": map[string]any{ "pre": map[string]any{ - "type": "mysql", + "type": "s3", "configs": map[string]any{ - "user": "kusion", + "bucket": "kusion", }, }, }, }, - key: "backends.pre.configs.dbName", + key: "backends.pre.configs.prefix", val: "kusion", expectedCfg: map[string]any{ "backends": map[string]any{ "pre": map[string]any{ - "type": "mysql", + "type": "s3", "configs": map[string]any{ - "dbName": "kusion", - "user": "kusion", + "bucket": "kusion", + "prefix": "kusion", }, }, }, @@ -1102,46 +951,18 @@ func TestSetItemFromCfgMap(t *testing.T) { cfg: map[string]any{ "backends": map[string]any{ "pre": map[string]any{ - "type": "mysql", + "type": "s3", }, }, }, - key: "backends.pre.configs.dbName", + key: "backends.pre.configs.bucket", val: "kusion", expectedCfg: map[string]any{ "backends": map[string]any{ "pre": map[string]any{ - "type": "mysql", - "configs": map[string]any{ - "dbName": "kusion", - }, - }, - }, - }, - }, - { - name: "set item in config map successfully int", - success: true, - cfg: map[string]any{ - "backends": map[string]any{ - "pre": map[string]any{ - "type": "mysql", - "configs": map[string]any{ - "dbName": "kusion", - "port": 8222, - }, - }, - }, - }, - key: "backends.pre.configs.port", - val: 3306, - expectedCfg: map[string]any{ - "backends": map[string]any{ - "pre": map[string]any{ - "type": "mysql", + "type": "s3", "configs": map[string]any{ - "port": 3306, - "dbName": "kusion", + "bucket": "kusion", }, }, }, @@ -1153,23 +974,17 @@ func TestSetItemFromCfgMap(t *testing.T) { cfg: map[string]any{}, key: "backends.pre", val: &v1.BackendConfig{ - Type: v1.BackendTypeMysql, + Type: v1.BackendTypeS3, Configs: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, + v1.BackendGenericOssBucket: "kusion", }, }, expectedCfg: map[string]any{ "backends": map[string]any{ "pre": &v1.BackendConfig{ - Type: v1.BackendTypeMysql, + Type: v1.BackendTypeS3, Configs: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, + v1.BackendGenericOssBucket: "kusion", }, }, }, diff --git a/pkg/config/registry.go b/pkg/config/registry.go index 7ad33baa..b08477fb 100644 --- a/pkg/config/registry.go +++ b/pkg/config/registry.go @@ -10,11 +10,6 @@ const ( backendConfigType = backendConfig + "." + v1.BackendType backendConfigItems = backendConfig + "." + v1.BackendConfigItems backendLocalPath = backendConfigItems + "." + v1.BackendLocalPath - backendMysqlDBName = backendConfigItems + "." + v1.BackendMysqlDBName - backendMysqlUser = backendConfigItems + "." + v1.BackendMysqlUser - backendMysqlPassword = backendConfigItems + "." + v1.BackendMysqlPassword - backendMysqlHost = backendConfigItems + "." + v1.BackendMysqlHost - backendMysqlPort = backendConfigItems + "." + v1.BackendMysqlPort backendGenericOssEndpoint = backendConfigItems + "." + v1.BackendGenericOssEndpoint backendGenericOssAK = backendConfigItems + "." + v1.BackendGenericOssAK backendGenericOssSK = backendConfigItems + "." + v1.BackendGenericOssSK @@ -30,11 +25,6 @@ func newRegisteredItems() map[string]*itemInfo { backendConfigType: {"", validateSetBackendType, validateUnsetBackendType}, backendConfigItems: {map[string]any{}, validateSetBackendConfigItems, validateUnsetBackendConfigItems}, backendLocalPath: {"", validateSetLocalBackendItem, validateUnsetLocalBackendItem}, - backendMysqlDBName: {"", validateSetMysqlBackendItem, nil}, - backendMysqlUser: {"", validateSetMysqlBackendItem, nil}, - backendMysqlPassword: {"", validateSetMysqlBackendItem, nil}, - backendMysqlHost: {"", validateSetMysqlBackendItem, nil}, - backendMysqlPort: {0, validateSetMysqlBackendPort, nil}, backendGenericOssEndpoint: {"", validateSetGenericOssBackendItem, nil}, backendGenericOssAK: {"", validateSetGenericOssBackendItem, nil}, backendGenericOssSK: {"", validateSetGenericOssBackendItem, nil}, diff --git a/pkg/config/testdata/config.yaml b/pkg/config/testdata/config.yaml index 864ede76..f1e1db5f 100644 --- a/pkg/config/testdata/config.yaml +++ b/pkg/config/testdata/config.yaml @@ -4,13 +4,6 @@ backends: type: local dev: type: local - pre: - type: mysql - configs: - dbName: kusion - host: 127.0.0.1 - port: 3306 - user: kk prod: type: s3 configs: diff --git a/pkg/config/util_test.go b/pkg/config/util_test.go index 991bead0..8aa6c4b7 100644 --- a/pkg/config/util_test.go +++ b/pkg/config/util_test.go @@ -58,13 +58,6 @@ func TestGetEncodedConfigItem(t *testing.T) { configItemKey: "backends.dev.type", expectedConfigItem: "local", }, - { - name: "get encoded config item successfully type int", - success: true, - config: mockValidConfig(), - configItemKey: "backends.pre.configs.port", - expectedConfigItem: "3306", - }, { name: "get encoded config item successfully type struct", success: true, @@ -131,7 +124,7 @@ func TestSetEncodedConfigItem(t *testing.T) { name: "set encoded config item successfully type struct", success: true, configItemKey: "backends.pre", - configItem: `{"configs":{"dbName":"kusion","host":"127.0.0.1","port":3306,"user":"kk"},"type":"mysql"}`, + configItem: `{"configs":{"bucket":"kusion"},"type":"s3"}`, config: &v1.Config{ Backends: &v1.BackendConfigs{ Backends: map[string]*v1.BackendConfig{}, @@ -141,12 +134,9 @@ func TestSetEncodedConfigItem(t *testing.T) { Backends: &v1.BackendConfigs{ Backends: map[string]*v1.BackendConfig{ "pre": { - Type: v1.BackendTypeMysql, + Type: v1.BackendTypeS3, Configs: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, + v1.BackendGenericOssBucket: "kusion", }, }, }, @@ -232,12 +222,9 @@ func TestDeleteConfigItem(t *testing.T) { Backends: &v1.BackendConfigs{ Backends: map[string]*v1.BackendConfig{ "pre": { - Type: v1.BackendTypeMysql, + Type: v1.BackendTypeOss, Configs: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, + v1.BackendGenericOssBucket: "kusion", }, }, }, diff --git a/pkg/config/validation.go b/pkg/config/validation.go index bf8029f4..88bc9b1a 100644 --- a/pkg/config/validation.go +++ b/pkg/config/validation.go @@ -22,7 +22,6 @@ var ( ErrNonEmptyBackendConfigItems = errors.New("non-empty backend config items") ErrEmptyBackendType = errors.New("empty backend type") ErrConflictBackendType = errors.New("conflict backend type") - ErrInvalidBackendMysqlPort = errors.New("backend mysql port must be between 1 and 65535") ErrInvalidBackNameDefault = errors.New("backend name should not be default") ) @@ -68,7 +67,7 @@ func validateUnsetBackendConfig(config *v1.Config, key string) error { // validateSetBackendType is used to check that setting the backend type is valid or not. func validateSetBackendType(config *v1.Config, key string, val any) error { backendType, _ := val.(string) - if backendType != v1.BackendTypeLocal && backendType != v1.BackendTypeMysql && backendType != v1.BackendTypeOss && backendType != v1.BackendTypeS3 { + if backendType != v1.BackendTypeLocal && backendType != v1.BackendTypeOss && backendType != v1.BackendTypeS3 { return ErrUnsupportedBackendType } @@ -138,23 +137,6 @@ func validateUnsetLocalBackendItem(_ *v1.Config, key string) error { return checkNotDefaultBackendName(parseBackendName(key)) } -// validateSetMysqlBackendItem is used to check that setting the config item for mysql-type backend is valid or not. -func validateSetMysqlBackendItem(config *v1.Config, key string, _ any) error { - return checkBackendTypeForBackendItem(config, key, v1.BackendTypeMysql) -} - -// validateSetMysqlBackendPort is used to check that setting the port of mysql-type backend is valid or not. -func validateSetMysqlBackendPort(config *v1.Config, key string, val any) error { - if err := validateSetMysqlBackendItem(config, key, val); err != nil { - return err - } - port, _ := val.(int) - if port < 1 || port > 65535 { - return ErrInvalidBackendMysqlPort - } - return nil -} - // validateSetGenericOssBackendItem is used to check that setting the generic config of oss/s3-type backend is valid or not. func validateSetGenericOssBackendItem(config *v1.Config, key string, _ any) error { return checkBackendTypeForBackendItem(config, key, v1.BackendTypeOss, v1.BackendTypeS3) @@ -173,11 +155,6 @@ func checkBackendConfig(config *v1.BackendConfig) error { } switch config.Type { - case v1.BackendTypeMysql: - mysqlBackend := config.ToMysqlBackend() - if err := storages.ValidateMysqlConfigFromFile(mysqlBackend); err != nil { - return err - } case v1.BackendTypeOss: ossBackend := config.ToOssBackend() if err := storages.ValidateOssConfigFromFile(ossBackend); err != nil { @@ -203,17 +180,6 @@ func checkBasalBackendConfig(config *v1.BackendConfig) error { if err := checkBasalBackendConfigItems(config, items); err != nil { return err } - case v1.BackendTypeMysql: - items := map[string]checkTypeFunc{ - v1.BackendMysqlDBName: checkString, - v1.BackendMysqlUser: checkString, - v1.BackendMysqlPassword: checkString, - v1.BackendMysqlHost: checkString, - v1.BackendMysqlPort: checkInt, - } - if err := checkBasalBackendConfigItems(config, items); err != nil { - return err - } case v1.BackendTypeOss: items := map[string]checkTypeFunc{ v1.BackendGenericOssEndpoint: checkString, @@ -286,7 +252,7 @@ func checkNotDefaultBackendName(name string) error { return nil } -// parseBackendName parses the backend name from the config key, the key is like "backends.dev.configs.dbName", +// parseBackendName parses the backend name from the config key, the key is like "backends.dev.configs.bucket", // "backends.dev.type", "backends.dev" func parseBackendName(key string) string { fields := strings.Split(key, ".") @@ -296,7 +262,7 @@ func parseBackendName(key string) string { return fields[1] } -// parseBackendItem parses the backend config item from the config key, the key is like "backends.dev.configs.dbName" +// parseBackendItem parses the backend config item from the config key, the key is like "backends.dev.configs.bucket" func parseBackendItem(key string) string { fields := strings.Split(key, ".") if len(fields) != 4 { @@ -319,10 +285,3 @@ func checkString(val any) error { } return nil } - -func checkInt(val any) error { - if _, ok := val.(int); !ok { - return ErrNotInt - } - return nil -} diff --git a/pkg/config/validation_test.go b/pkg/config/validation_test.go index c751fdc4..29ad3aa3 100644 --- a/pkg/config/validation_test.go +++ b/pkg/config/validation_test.go @@ -105,19 +105,6 @@ func TestValidateSetBackendConfig(t *testing.T) { }, }, }, - { - name: "valid database backend", - success: true, - val: &v1.BackendConfig{ - Type: v1.BackendTypeMysql, - Configs: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, - }, - }, - }, { name: "valid oss backend", success: true, @@ -147,29 +134,6 @@ func TestValidateSetBackendConfig(t *testing.T) { Type: "not-support-type", }, }, - { - name: "invalid backend config invalid config item type", - success: false, - val: &v1.BackendConfig{ - Type: v1.BackendTypeMysql, - Configs: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: "3306", - }, - }, - }, - { - name: "invalid backend config unsupported config item", - success: false, - val: &v1.BackendConfig{ - Type: v1.BackendTypeMysql, - Configs: map[string]any{ - "not-support": "mock-not-support-value", - }, - }, - }, } for _, tc := range testcases { @@ -239,7 +203,7 @@ func TestValidateSetBackendType(t *testing.T) { }, }, key: "backends.dev.type", - val: "mysql", + val: "s3", }, { name: "invalid backend type unsupported type", @@ -266,7 +230,7 @@ func TestValidateSetBackendType(t *testing.T) { }, }, key: "backends.dev.type", - val: "mysql", + val: "s3", }, } @@ -349,16 +313,13 @@ func TestValidateSetBackendConfigItems(t *testing.T) { config: &v1.Config{ Backends: &v1.BackendConfigs{ Backends: map[string]*v1.BackendConfig{ - "dev": {Type: v1.BackendTypeMysql}, + "dev": {Type: v1.BackendTypeS3}, }, }, }, key: "backends.dev.configs", val: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, + v1.BackendGenericOssBucket: "kusion", }, }, { @@ -373,10 +334,7 @@ func TestValidateSetBackendConfigItems(t *testing.T) { }, key: "backends.dev.configs", val: map[string]any{ - v1.BackendMysqlDBName: "kusion", - v1.BackendMysqlUser: "kk", - v1.BackendMysqlHost: "127.0.0.1", - v1.BackendMysqlPort: 3306, + v1.BackendGenericOssBucket: "kusion", }, }, } @@ -415,50 +373,6 @@ func TestValidateUnsetBackendConfigItems(t *testing.T) { } } -func TestValidateSetMysqlBackendPort(t *testing.T) { - testcases := []struct { - name string - success bool - config *v1.Config - key string - val int - }{ - { - name: "valid mysql port", - success: true, - config: &v1.Config{ - Backends: &v1.BackendConfigs{ - Backends: map[string]*v1.BackendConfig{ - "dev": {Type: v1.BackendTypeMysql}, - }, - }, - }, - key: "backends.dev.configs.port", - val: 3306, - }, - { - name: "invalid mysql port", - success: false, - config: &v1.Config{ - Backends: &v1.BackendConfigs{ - Backends: map[string]*v1.BackendConfig{ - "dev": {Type: v1.BackendTypeMysql}, - }, - }, - }, - key: "backends.dev.configs.port", - val: -1, - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - err := validateSetMysqlBackendPort(tc.config, tc.key, tc.val) - assert.Equal(t, tc.success, err == nil) - }) - } -} - func TestCheckBackendTypeForBackendItem(t *testing.T) { testcases := []struct { name string @@ -473,12 +387,12 @@ func TestCheckBackendTypeForBackendItem(t *testing.T) { config: &v1.Config{ Backends: &v1.BackendConfigs{ Backends: map[string]*v1.BackendConfig{ - "dev": {Type: v1.BackendTypeMysql}, + "dev": {Type: v1.BackendTypeOss}, }, }, }, - key: "backends.dev.configs.dbName", - backendTypes: []string{v1.BackendTypeMysql}, + key: "backends.dev.configs.bucket", + backendTypes: []string{v1.BackendTypeOss}, }, { name: "invalid backend config item empty backend type", @@ -490,8 +404,8 @@ func TestCheckBackendTypeForBackendItem(t *testing.T) { }, }, }, - key: "backends.dev.configs.dbName", - backendTypes: []string{v1.BackendTypeMysql}, + key: "backends.dev.configs.bucket", + backendTypes: []string{v1.BackendTypeOss}, }, { name: "invalid backend config item conflict backend type", @@ -499,12 +413,12 @@ func TestCheckBackendTypeForBackendItem(t *testing.T) { config: &v1.Config{ Backends: &v1.BackendConfigs{ Backends: map[string]*v1.BackendConfig{ - "dev": {Type: v1.BackendTypeOss}, + "dev": {Type: v1.BackendTypeLocal}, }, }, }, - key: "backends.dev.configs.dbName", - backendTypes: []string{v1.BackendTypeMysql}, + key: "backends.dev.configs.bucket", + backendTypes: []string{v1.BackendTypeOss}, }, } diff --git a/pkg/domain/constant/organization.go b/pkg/domain/constant/organization.go index 99d07d40..495e9975 100644 --- a/pkg/domain/constant/organization.go +++ b/pkg/domain/constant/organization.go @@ -8,7 +8,6 @@ import "errors" // const ( // // SourceProviderTypeGithub represents github source provider type. // BackendTypeOss BackendType = "oss" -// BackendTypeMysql BackendType = "mysql" // BackendTypeLocal BackendType = "local" // ) diff --git a/pkg/engine/state/storages/mysql.go b/pkg/engine/state/storages/mysql.go deleted file mode 100644 index 6d3d70b8..00000000 --- a/pkg/engine/state/storages/mysql.go +++ /dev/null @@ -1,122 +0,0 @@ -package storages - -import ( - "errors" - - "gopkg.in/yaml.v3" - "gorm.io/gorm" - - v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1" -) - -// MysqlStorage is an implementation of state.Storage which uses mysql as storage. -type MysqlStorage struct { - db *gorm.DB - project, workspace string -} - -func NewMysqlStorage(db *gorm.DB, project, workspace string) *MysqlStorage { - return &MysqlStorage{ - db: db, - project: project, - workspace: workspace, - } -} - -func (s *MysqlStorage) Get() (*v1.DeprecatedState, error) { - stateDO, err := getStateFromMysql(s.db, s.project, s.workspace) - if err != nil { - return nil, err - } - if stateDO == nil { - return nil, nil - } - return convertFromMysqlDO(stateDO) -} - -func (s *MysqlStorage) Apply(state *v1.DeprecatedState) error { - exist, err := checkStateExistenceInMysql(s.db, s.project, s.workspace) - if err != nil { - return err - } - - stateDO, err := convertToMysqlDO(state) - if err != nil { - return err - } - if exist { - return updateStateInMysql(s.db, stateDO) - } else { - return createStateInMysql(s.db, stateDO) - } -} - -// StateMysqlDO is the data object stored in the mysql db. -type StateMysqlDO struct { - Project string - Workspace string - Content string -} - -func (s StateMysqlDO) TableName() string { - return stateTable -} - -func getStateFromMysql(db *gorm.DB, project, workspace string) (*StateMysqlDO, error) { - q := &StateMysqlDO{ - Project: project, - Workspace: workspace, - } - s := &StateMysqlDO{} - result := db.Where(q).First(s) - // if no record, return nil state and nil error - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, nil - } - return s, result.Error -} - -func checkStateExistenceInMysql(db *gorm.DB, project, workspace string) (bool, error) { - q := &StateMysqlDO{ - Project: project, - Workspace: workspace, - } - s := &StateMysqlDO{} - result := db.Select("project").Where(q).First(s) - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return false, nil - } - return result.Error == nil, result.Error -} - -func createStateInMysql(db *gorm.DB, s *StateMysqlDO) error { - return db.Create(s).Error -} - -func updateStateInMysql(db *gorm.DB, s *StateMysqlDO) error { - q := &StateMysqlDO{ - Project: s.Project, - Workspace: s.Workspace, - } - return db.Where(q).Updates(s).Error -} - -func convertToMysqlDO(state *v1.DeprecatedState) (*StateMysqlDO, error) { - content, err := yaml.Marshal(state) - if err != nil { - return nil, err - } - return &StateMysqlDO{ - Project: state.Project, - Workspace: state.Workspace, - Content: string(content), - }, nil -} - -func convertFromMysqlDO(s *StateMysqlDO) (*v1.DeprecatedState, error) { - state := &v1.DeprecatedState{} - if err := yaml.Unmarshal([]byte(s.Content), state); err != nil { - return nil, err - } - return state, nil -} diff --git a/pkg/engine/state/storages/mysql_test.go b/pkg/engine/state/storages/mysql_test.go deleted file mode 100644 index dbd99f1c..00000000 --- a/pkg/engine/state/storages/mysql_test.go +++ /dev/null @@ -1,89 +0,0 @@ -package storages - -import ( - "testing" - - "github.com/bytedance/mockey" - "github.com/stretchr/testify/assert" - "gorm.io/gorm" - - v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1" -) - -func mockStateMysqlDO() *StateMysqlDO { - return &StateMysqlDO{ - Project: "wordpress", - Workspace: "dev", - Content: mockStateContent(), - } -} - -func mockMysqlStorage() *MysqlStorage { - return &MysqlStorage{db: &gorm.DB{}} -} - -func TestMysqlStorage_Get(t *testing.T) { - testcases := []struct { - name string - success bool - stateDO *StateMysqlDO - state *v1.DeprecatedState - }{ - { - name: "get mysql state successfully", - success: true, - stateDO: mockStateMysqlDO(), - state: mockState(), - }, - { - name: "get empty mysql state successfully", - success: true, - stateDO: nil, - state: nil, - }, - } - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - mockey.PatchConvey("mock mysql get", t, func() { - mockey.Mock(getStateFromMysql).Return(tc.stateDO, nil).Build() - state, err := mockMysqlStorage().Get() - assert.Equal(t, tc.success, err == nil) - assert.Equal(t, tc.state, state) - }) - }) - } -} - -func TestMysqlStorage_Apply(t *testing.T) { - testcases := []struct { - name string - success bool - lastStateDO *StateMysqlDO - state *v1.DeprecatedState - }{ - { - name: "update mysql state successfully", - success: true, - lastStateDO: mockStateMysqlDO(), - state: mockState(), - }, - { - name: "create mysql state successfully", - success: true, - lastStateDO: nil, - state: mockState(), - }, - } - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - mockey.PatchConvey("mock mysql create and update", t, func() { - mockey.Mock(getStateFromMysql).Return(tc.lastStateDO, nil).Build() - mockey.Mock(checkStateExistenceInMysql).Return(tc.lastStateDO != nil, nil).Build() - mockey.Mock(createStateInMysql).Return(nil).Build() - mockey.Mock(updateStateInMysql).Return(nil).Build() - err := mockMysqlStorage().Apply(tc.state) - assert.Equal(t, tc.success, err == nil) - }) - }) - } -} diff --git a/pkg/server/manager/stack/util.go b/pkg/server/manager/stack/util.go index d3c26aea..c057e8d8 100644 --- a/pkg/server/manager/stack/util.go +++ b/pkg/server/manager/stack/util.go @@ -9,6 +9,7 @@ import ( "path/filepath" "gorm.io/gorm" + apiv1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1" v1 "kusionstack.io/kusion/pkg/apis/internal.kusion.io/v1" "kusionstack.io/kusion/pkg/backend" @@ -68,16 +69,6 @@ func NewBackendFromEntity(backendEntity entity.Backend) (backend.Backend, error) return nil, fmt.Errorf("complete local config failed, %w", err) } return storages.NewLocalStorage(bkConfig), nil - case v1.BackendTypeMysql: - bkConfig := backendEntity.BackendConfig.ToMysqlBackend() - storages.CompleteMysqlConfig(bkConfig) - if err = storages.ValidateMysqlConfig(bkConfig); err != nil { - return nil, fmt.Errorf("invalid config of backend %s, %w", backendEntity.Name, err) - } - storage, err = storages.NewMysqlStorage(bkConfig) - if err != nil { - return nil, fmt.Errorf("new mysql storage of backend %s failed, %w", backendEntity.Name, err) - } case v1.BackendTypeOss: bkConfig := backendEntity.BackendConfig.ToOssBackend() storages.CompleteOssConfig(bkConfig) diff --git a/pkg/workspace/storages/mysql.go b/pkg/workspace/storages/mysql.go deleted file mode 100644 index 126744fd..00000000 --- a/pkg/workspace/storages/mysql.go +++ /dev/null @@ -1,244 +0,0 @@ -package storages - -import ( - "errors" - "fmt" - - "gopkg.in/yaml.v3" - "gorm.io/gorm" - - v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1" -) - -// MysqlStorage is an implementation of workspace.Storage which uses mysql as storage. -type MysqlStorage struct { - db *gorm.DB -} - -// NewMysqlStorage news mysql workspace storage and init default workspace. -func NewMysqlStorage(db *gorm.DB) (*MysqlStorage, error) { - s := &MysqlStorage{db: db} - - return s, s.initDefaultWorkspaceIf() -} - -func (s *MysqlStorage) Get(name string) (*v1.Workspace, error) { - w, err := getWorkspaceFromMysql(s.db, name) - if err != nil { - return nil, fmt.Errorf("get workspace from mysql database failed: %w", err) - } - if w == nil { - return nil, ErrWorkspaceNotExist - } - - ws, err := convertFromMysqlDO(w) - if err != nil { - return nil, err - } - ws.Name = name - return ws, nil -} - -func (s *MysqlStorage) Create(ws *v1.Workspace) error { - exist, err := checkWorkspaceExistenceInMysql(s.db, ws.Name) - if err != nil { - return err - } - if exist { - return ErrWorkspaceAlreadyExist - } - - w, err := convertToMysqlDO(ws) - if err != nil { - return err - } - return createWorkspaceInMysql(s.db, w) -} - -func (s *MysqlStorage) Update(ws *v1.Workspace) error { - if ws.Name == "" { - name, err := getCurrentWorkspaceNameFromMysql(s.db) - if err != nil { - return err - } - ws.Name = name - } - exist, err := checkWorkspaceExistenceInMysql(s.db, ws.Name) - if err != nil { - return err - } - if !exist { - return ErrWorkspaceNotExist - } - - w, err := convertToMysqlDO(ws) - if err != nil { - return err - } - return updateWorkspaceInMysql(s.db, w) -} - -func (s *MysqlStorage) Delete(name string) error { - if name == "" { - var err error - name, err = getCurrentWorkspaceNameFromMysql(s.db) - if err != nil { - return err - } - } - return deleteWorkspaceInMysql(s.db, name) -} - -func (s *MysqlStorage) GetNames() ([]string, error) { - return getWorkspaceNamesFromMysql(s.db) -} - -func (s *MysqlStorage) GetCurrent() (string, error) { - return getCurrentWorkspaceNameFromMysql(s.db) -} - -func (s *MysqlStorage) SetCurrent(name string) error { - exist, err := checkWorkspaceExistenceInMysql(s.db, name) - if err != nil { - return err - } - if !exist { - return ErrWorkspaceNotExist - } - - return alterCurrentWorkspaceInMysql(s.db, name) -} - -func (s *MysqlStorage) initDefaultWorkspaceIf() error { - exist, err := checkWorkspaceExistenceInMysql(s.db, DefaultWorkspace) - if err != nil { - return err - } - if exist { - return nil - } - - w := &WorkspaceMysqlDO{Name: DefaultWorkspace} - currentName, err := getCurrentWorkspaceNameFromMysql(s.db) - if err != nil { - return err - } - if currentName == "" { - isCurrent := true - w.IsCurrent = &isCurrent - } - - return createWorkspaceInMysql(s.db, w) -} - -// WorkspaceMysqlDO is the data object stored in the mysql db. -type WorkspaceMysqlDO struct { - Name string - Content string - IsCurrent *bool -} - -func (s WorkspaceMysqlDO) TableName() string { - return workspaceTable -} - -func getWorkspaceFromMysql(db *gorm.DB, name string) (*WorkspaceMysqlDO, error) { - q := &WorkspaceMysqlDO{Name: name} - if name == "" { - isCurrent := true - q.IsCurrent = &isCurrent - } - w := &WorkspaceMysqlDO{} - result := db.Where(q).First(w) - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return nil, nil - } - return w, result.Error -} - -func getCurrentWorkspaceNameFromMysql(db *gorm.DB) (string, error) { - isCurrent := true - q := &WorkspaceMysqlDO{IsCurrent: &isCurrent} - w := &WorkspaceMysqlDO{} - result := db.Select("name").Where(q).First(w) - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return "", nil - } - return w.Name, result.Error -} - -func getWorkspaceNamesFromMysql(db *gorm.DB) ([]string, error) { - var wList []*WorkspaceMysqlDO - result := db.Select("name").Find(wList) - if result.Error != nil { - return nil, result.Error - } - names := make([]string, len(wList)) - for i, w := range wList { - names[i] = w.Name - } - return names, nil -} - -func checkWorkspaceExistenceInMysql(db *gorm.DB, name string) (bool, error) { - q := &WorkspaceMysqlDO{Name: name} - w := &WorkspaceMysqlDO{} - result := db.Select("name").Where(q).First(w) - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return false, nil - } - return result.Error == nil, result.Error -} - -func createWorkspaceInMysql(db *gorm.DB, w *WorkspaceMysqlDO) error { - return db.Create(w).Error -} - -func updateWorkspaceInMysql(db *gorm.DB, w *WorkspaceMysqlDO) error { - q := &WorkspaceMysqlDO{Name: w.Name} - return db.Where(q).Updates(w).Error -} - -func deleteWorkspaceInMysql(db *gorm.DB, name string) error { - q := &WorkspaceMysqlDO{Name: name} - return db.Where(q).Delete(&WorkspaceMysqlDO{}).Error -} - -func alterCurrentWorkspaceInMysql(db *gorm.DB, name string) error { - return db.Transaction(func(tx *gorm.DB) error { - // set the current workspace now to not current - isCurrent := true - q := &WorkspaceMysqlDO{IsCurrent: &isCurrent} - notCurrent := false - w := &WorkspaceMysqlDO{IsCurrent: ¬Current} - result := tx.Where(q).Updates(w) - if result.Error != nil { - return result.Error - } - - // set current of the specified workspace - q = &WorkspaceMysqlDO{Name: name} - w = &WorkspaceMysqlDO{IsCurrent: &isCurrent} - result = tx.Where(q).Updates(w) - return result.Error - }) -} - -func convertToMysqlDO(ws *v1.Workspace) (*WorkspaceMysqlDO, error) { - content, err := yaml.Marshal(ws) - if err != nil { - return nil, fmt.Errorf("yaml marshal workspace failed: %w", err) - } - return &WorkspaceMysqlDO{ - Name: ws.Name, - Content: string(content), - }, nil -} - -func convertFromMysqlDO(w *WorkspaceMysqlDO) (*v1.Workspace, error) { - ws := &v1.Workspace{} - if err := yaml.Unmarshal([]byte(w.Content), ws); err != nil { - return nil, fmt.Errorf("yaml unmarshal workspace failed: %w", err) - } - return ws, nil -} diff --git a/pkg/workspace/storages/mysql_test.go b/pkg/workspace/storages/mysql_test.go deleted file mode 100644 index ae7e2c9d..00000000 --- a/pkg/workspace/storages/mysql_test.go +++ /dev/null @@ -1,120 +0,0 @@ -package storages - -import ( - "testing" - - "github.com/bytedance/mockey" - "github.com/stretchr/testify/assert" - "gorm.io/gorm" - - v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1" -) - -func mockMysqlStorage() *MysqlStorage { - return &MysqlStorage{db: &gorm.DB{}} -} - -func TestMysqlStorage_Get(t *testing.T) { - testcases := []struct { - name string - success bool - wsName string - content string - expectedWorkspace *v1.Workspace - }{ - { - name: "get workspace successfully", - success: true, - wsName: "dev", - content: mockWorkspaceContent(), - expectedWorkspace: mockWorkspace("dev"), - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - mockey.PatchConvey("mock gorm operation", t, func() { - mockey.Mock(checkWorkspaceExistenceInMysql).Return(true, nil).Build() - mockey.Mock(getWorkspaceFromMysql).Return(&WorkspaceMysqlDO{Content: tc.content}, nil).Build() - workspace, err := mockMysqlStorage().Get(tc.wsName) - assert.Equal(t, tc.success, err == nil) - assert.Equal(t, tc.expectedWorkspace, workspace) - }) - }) - } -} - -func TestMysqlStorage_Create(t *testing.T) { - testcases := []struct { - name string - success bool - workspace *v1.Workspace - }{ - { - name: "create workspace successfully", - success: true, - workspace: mockWorkspace("pre"), - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - mockey.PatchConvey("mock gorm operation", t, func() { - mockey.Mock(checkWorkspaceExistenceInMysql).Return(false, nil).Build() - mockey.Mock(createWorkspaceInMysql).Return(nil).Build() - err := mockMysqlStorage().Create(tc.workspace) - assert.Equal(t, tc.success, err == nil) - }) - }) - } -} - -func TestMysqlStorage_Update(t *testing.T) { - testcases := []struct { - name string - success bool - workspace *v1.Workspace - }{ - { - name: "update workspace successfully", - success: true, - workspace: mockWorkspace("dev"), - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - mockey.PatchConvey("mock gorm operation", t, func() { - mockey.Mock(checkWorkspaceExistenceInMysql).Return(true, nil).Build() - mockey.Mock(updateWorkspaceInMysql).Return(nil).Build() - err := mockMysqlStorage().Update(tc.workspace) - assert.Equal(t, tc.success, err == nil) - }) - }) - } -} - -func TestMysqlStorage_SetCurrent(t *testing.T) { - testcases := []struct { - name string - success bool - current string - }{ - { - name: "set current workspace successfully", - success: true, - current: "prod", - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - mockey.PatchConvey("mock gorm operation", t, func() { - mockey.Mock(checkWorkspaceExistenceInMysql).Return(true, nil).Build() - mockey.Mock(alterCurrentWorkspaceInMysql).Return(nil).Build() - err := mockMysqlStorage().SetCurrent(tc.current) - assert.Equal(t, tc.success, err == nil) - }) - }) - } -}