Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

chore: set backend related const in workspace to deprecated #889

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/apis/core/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type BackendMysqlConfig struct {
// Host of the database.
Host string `yaml:"host" json:"host"`

// Port of the database. If not set, then it will be set to DefaultMysqlPort.
// Port of the database. If not set, then it will be set to DeprecatedDefaultMysqlPort.
Port int `yaml:"port,omitempty" json:"port,omitempty"`
}

Expand Down
34 changes: 17 additions & 17 deletions pkg/apis/core/v1/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ const (
DefaultBlock = "default"
ProjectSelectorField = "projectSelector"

DeprecatedBackendLocal = "local"
DeprecatedBackendMysql = "mysql"
DeprecatedBackendOss = "oss"
DeprecatedBackendS3 = "s3"
EnvBackendMysqlPassword = "KUSION_BACKEND_MYSQL_PASSWORD"
EnvAwsAccessKeyID = "AWS_ACCESS_KEY_ID"
EnvAwsSecretAccessKey = "AWS_SECRET_ACCESS_KEY"
EnvAwsDefaultRegion = "AWS_DEFAULT_REGION"
EnvAwsRegion = "AWS_REGION"
EnvOssAccessKeyID = "OSS_ACCESS_KEY_ID"
EnvOssAccessKeySecret = "OSS_ACCESS_KEY_SECRET"
DefaultMysqlPort = 3306
DeprecatedBackendLocal = "local"
DeprecatedBackendMysql = "mysql"
DeprecatedBackendOss = "oss"
DeprecatedBackendS3 = "s3"
DeprecatedEnvBackendMysqlPassword = "KUSION_BACKEND_MYSQL_PASSWORD"
DeprecatedEnvAwsAccessKeyID = "AWS_ACCESS_KEY_ID"
DeprecatedEnvAwsSecretAccessKey = "AWS_SECRET_ACCESS_KEY"
DeprecatedEnvAwsDefaultRegion = "AWS_DEFAULT_REGION"
DeprecatedEnvAwsRegion = "AWS_REGION"
DeprecatedEnvOssAccessKeyID = "OSS_ACCESS_KEY_ID"
DeprecatedEnvOssAccessKeySecret = "OSS_ACCESS_KEY_SECRET"
DeprecatedDefaultMysqlPort = 3306
)

// Workspace is a logical concept representing a target that stacks will be deployed to.
Expand Down Expand Up @@ -159,27 +159,27 @@ type DeprecatedMysqlConfig struct {
// Host of the database.
Host string `yaml:"host" json:"host"`

// Port of the database. If not set, then it will be set to DefaultMysqlPort.
// Port of the database. If not set, then it will be set to DeprecatedDefaultMysqlPort.
Port *int `yaml:"port,omitempty" json:"port,omitempty"`
}

// DeprecatedOssConfig contains the config of using OSS as backend.
// Deprecated: do not support backend configs in workspace anymore
type DeprecatedOssConfig struct {
GenericObjectStorageConfig `yaml:",inline" json:",inline"` // OSS asks for non-empty endpoint
DeprecatedGenericObjectStorageConfig `yaml:",inline" json:",inline"` // OSS asks for non-empty endpoint
}

// DeprecatedS3Config contains the config of using S3 as backend.
// Deprecated: do not support backend configs in workspace anymore
type DeprecatedS3Config struct {
GenericObjectStorageConfig `yaml:",inline" json:",inline"`
DeprecatedGenericObjectStorageConfig `yaml:",inline" json:",inline"`

// Region of S3.
Region string `yaml:"region,omitempty" json:"region,omitempty"`
}

// GenericObjectStorageConfig contains generic configs which can be reused by DeprecatedOssConfig and DeprecatedS3Config.
type GenericObjectStorageConfig struct {
// DeprecatedGenericObjectStorageConfig contains generic configs which can be reused by DeprecatedOssConfig and DeprecatedS3Config.
type DeprecatedGenericObjectStorageConfig struct {
// Endpoint of the object storage service.
Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty"`

Expand Down
16 changes: 8 additions & 8 deletions pkg/engine/backend/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ func TestNewConfig(t *testing.T) {
},
opts: &BackendOptions{},
setEnvFunc: func() {
_ = os.Setenv(v1.EnvBackendMysqlPassword, "kusion_password")
_ = os.Setenv(v1.DeprecatedEnvBackendMysqlPassword, "kusion_password")
},
unSetEnvFunc: func() {
_ = os.Unsetenv(v1.EnvBackendMysqlPassword)
_ = os.Unsetenv(v1.DeprecatedEnvBackendMysqlPassword)
},
expectedConfig: &StateStorageConfig{
Type: v1.DeprecatedBackendMysql,
Expand Down Expand Up @@ -86,14 +86,14 @@ func TestNewConfig(t *testing.T) {
Config: []string{"region=ua-east-2", "bucket=kusion_bucket"},
},
setEnvFunc: func() {
_ = os.Setenv(v1.EnvAwsRegion, "ua-east-1")
_ = os.Setenv(v1.EnvAwsAccessKeyID, "aws_ak_id")
_ = os.Setenv(v1.EnvAwsSecretAccessKey, "aws_ak_secret")
_ = os.Setenv(v1.DeprecatedEnvAwsRegion, "ua-east-1")
_ = os.Setenv(v1.DeprecatedEnvAwsAccessKeyID, "aws_ak_id")
_ = os.Setenv(v1.DeprecatedEnvAwsSecretAccessKey, "aws_ak_secret")
},
unSetEnvFunc: func() {
_ = os.Unsetenv(v1.EnvAwsDefaultRegion)
_ = os.Unsetenv(v1.EnvOssAccessKeyID)
_ = os.Unsetenv(v1.EnvAwsSecretAccessKey)
_ = os.Unsetenv(v1.DeprecatedEnvAwsDefaultRegion)
_ = os.Unsetenv(v1.DeprecatedEnvOssAccessKeyID)
_ = os.Unsetenv(v1.DeprecatedEnvAwsSecretAccessKey)
},
expectedConfig: &StateStorageConfig{
Type: v1.DeprecatedBackendS3,
Expand Down
12 changes: 6 additions & 6 deletions pkg/engine/backend/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ func Test_NewStateStorage(t *testing.T) {
stack: mockStack("s3_backend_ws"),
opts: &BackendOptions{},
setEnvFunc: func() {
_ = os.Setenv(v1.EnvAwsRegion, "ua-east-2")
_ = os.Setenv(v1.EnvAwsAccessKeyID, "aws_ak_id")
_ = os.Setenv(v1.EnvAwsSecretAccessKey, "aws_ak_secret")
_ = os.Setenv(v1.DeprecatedEnvAwsRegion, "ua-east-2")
_ = os.Setenv(v1.DeprecatedEnvAwsAccessKeyID, "aws_ak_id")
_ = os.Setenv(v1.DeprecatedEnvAwsSecretAccessKey, "aws_ak_secret")
},
unsetEnvFunc: func() {
_ = os.Unsetenv(v1.EnvAwsDefaultRegion)
_ = os.Unsetenv(v1.EnvOssAccessKeyID)
_ = os.Unsetenv(v1.EnvAwsSecretAccessKey)
_ = os.Unsetenv(v1.DeprecatedEnvAwsDefaultRegion)
_ = os.Unsetenv(v1.DeprecatedEnvOssAccessKeyID)
_ = os.Unsetenv(v1.DeprecatedEnvAwsSecretAccessKey)
},
},
{
Expand Down
12 changes: 6 additions & 6 deletions pkg/workspace/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,27 +151,27 @@ func GetBackendName(configs *v1.DeprecatedBackendConfigs) string {

// GetMysqlPasswordFromEnv returns mysql password set by environment variables.
func GetMysqlPasswordFromEnv() string {
return os.Getenv(v1.EnvBackendMysqlPassword)
return os.Getenv(v1.DeprecatedEnvBackendMysqlPassword)
}

// GetOssSensitiveDataFromEnv returns oss accessKeyID, accessKeySecret set by environment variables.
func GetOssSensitiveDataFromEnv() (string, string) {
return os.Getenv(v1.EnvOssAccessKeyID), os.Getenv(v1.EnvOssAccessKeySecret)
return os.Getenv(v1.DeprecatedEnvOssAccessKeyID), os.Getenv(v1.DeprecatedEnvOssAccessKeySecret)
}

// GetS3SensitiveDataFromEnv returns s3 accessKeyID, accessKeySecret, region set by environment variables.
func GetS3SensitiveDataFromEnv() (string, string, string) {
region := os.Getenv(v1.EnvAwsRegion)
region := os.Getenv(v1.DeprecatedEnvAwsRegion)
if region == "" {
region = os.Getenv(v1.EnvAwsDefaultRegion)
region = os.Getenv(v1.DeprecatedEnvAwsDefaultRegion)
}
return os.Getenv(v1.EnvAwsAccessKeyID), os.Getenv(v1.EnvAwsSecretAccessKey), region
return os.Getenv(v1.DeprecatedEnvAwsAccessKeyID), os.Getenv(v1.DeprecatedEnvAwsSecretAccessKey), region
}

// CompleteMysqlConfig sets default value of mysql config if not set.
func CompleteMysqlConfig(config *v1.DeprecatedMysqlConfig) {
if config.Port == nil {
port := v1.DefaultMysqlPort
port := v1.DeprecatedDefaultMysqlPort
config.Port = &port
}
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/workspace/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,13 @@ func ValidateBackendConfigs(configs *v1.DeprecatedBackendConfigs) error {
return ValidateMysqlConfig(configs.Mysql)
}
if configs.Oss != nil {
if err := ValidateGenericObjectStorageConfig(&configs.Oss.GenericObjectStorageConfig); err != nil {
if err := ValidateGenericObjectStorageConfig(&configs.Oss.DeprecatedGenericObjectStorageConfig); err != nil {
return fmt.Errorf("%w of %s", err, v1.DeprecatedBackendOss)
}
return nil
}
if configs.S3 != nil {
if err := ValidateGenericObjectStorageConfig(&configs.S3.GenericObjectStorageConfig); err != nil {
if err := ValidateGenericObjectStorageConfig(&configs.S3.DeprecatedGenericObjectStorageConfig); err != nil {
return fmt.Errorf("%w of %s", err, v1.DeprecatedBackendS3)
}
return nil
Expand Down Expand Up @@ -292,7 +292,7 @@ func ValidateMysqlConfig(config *v1.DeprecatedMysqlConfig) error {

// ValidateGenericObjectStorageConfig is used to validate ossConfig and s3Config is valid or not, where the
// sensitive data items set as environment variables are not included.
func ValidateGenericObjectStorageConfig(config *v1.GenericObjectStorageConfig) error {
func ValidateGenericObjectStorageConfig(config *v1.DeprecatedGenericObjectStorageConfig) error {
if config.Bucket == "" {
return ErrEmptyBucket
}
Expand All @@ -302,7 +302,7 @@ func ValidateGenericObjectStorageConfig(config *v1.GenericObjectStorageConfig) e
// ValidateWholeOssConfig is used to validate ossConfig is valid or not, where all the items are included.
// If valid, the config contains all valid items to new an oss client.
func ValidateWholeOssConfig(config *v1.DeprecatedOssConfig) error {
if err := validateWholeGenericObjectStorageConfig(&config.GenericObjectStorageConfig); err != nil {
if err := validateWholeGenericObjectStorageConfig(&config.DeprecatedGenericObjectStorageConfig); err != nil {
return fmt.Errorf("%w of %s", err, v1.DeprecatedBackendOss)
}
if config.Endpoint == "" {
Expand All @@ -314,7 +314,7 @@ func ValidateWholeOssConfig(config *v1.DeprecatedOssConfig) error {
// ValidateWholeS3Config is used to validate s3Config is valid or not, where all the items are included.
// If valid, the config contains all valid items to new a s3 client.
func ValidateWholeS3Config(config *v1.DeprecatedS3Config) error {
if err := validateWholeGenericObjectStorageConfig(&config.GenericObjectStorageConfig); err != nil {
if err := validateWholeGenericObjectStorageConfig(&config.DeprecatedGenericObjectStorageConfig); err != nil {
return fmt.Errorf("%w of %s", err, v1.DeprecatedBackendS3)
}
if config.Region == "" {
Expand All @@ -323,7 +323,7 @@ func ValidateWholeS3Config(config *v1.DeprecatedS3Config) error {
return nil
}

func validateWholeGenericObjectStorageConfig(config *v1.GenericObjectStorageConfig) error {
func validateWholeGenericObjectStorageConfig(config *v1.DeprecatedGenericObjectStorageConfig) error {
if err := ValidateGenericObjectStorageConfig(config); err != nil {
return err
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/workspace/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,15 @@ func mockValidMysqlConfig() *v1.DeprecatedMysqlConfig {
}
}

func mockValidGenericObjectStorageConfig() *v1.GenericObjectStorageConfig {
return &v1.GenericObjectStorageConfig{
func mockValidGenericObjectStorageConfig() *v1.DeprecatedGenericObjectStorageConfig {
return &v1.DeprecatedGenericObjectStorageConfig{
Bucket: "kusion_bucket",
}
}

func mockValidCompletedOssConfig() *v1.DeprecatedOssConfig {
return &v1.DeprecatedOssConfig{
GenericObjectStorageConfig: v1.GenericObjectStorageConfig{
DeprecatedGenericObjectStorageConfig: v1.DeprecatedGenericObjectStorageConfig{
Endpoint: "http://oss-cn-hangzhou.aliyuncs.com",
AccessKeyID: "fake-access-key-id",
AccessKeySecret: "fake-access-key-secret",
Expand All @@ -218,7 +218,7 @@ func mockValidCompletedOssConfig() *v1.DeprecatedOssConfig {

func mockValidCompletedS3Config() *v1.DeprecatedS3Config {
return &v1.DeprecatedS3Config{
GenericObjectStorageConfig: v1.GenericObjectStorageConfig{
DeprecatedGenericObjectStorageConfig: v1.DeprecatedGenericObjectStorageConfig{
AccessKeyID: "fake-access-key-id",
AccessKeySecret: "fake-access-key-secret",
Bucket: "kusion_bucket",
Expand Down Expand Up @@ -554,7 +554,7 @@ func TestValidateValidateGenericObjectStorageConfig(t *testing.T) {
testcases := []struct {
name string
success bool
config *v1.GenericObjectStorageConfig
config *v1.DeprecatedGenericObjectStorageConfig
}{
{
name: "valid generic object storage config",
Expand All @@ -564,7 +564,7 @@ func TestValidateValidateGenericObjectStorageConfig(t *testing.T) {
{
name: "invalid generic object storage config empty bucket",
success: false,
config: &v1.GenericObjectStorageConfig{},
config: &v1.DeprecatedGenericObjectStorageConfig{},
},
}

Expand All @@ -591,7 +591,7 @@ func TestValidateWholeOssConfig(t *testing.T) {
name: "invalid oss config empty endpoint",
success: false,
ossConfig: &v1.DeprecatedOssConfig{
GenericObjectStorageConfig: v1.GenericObjectStorageConfig{
DeprecatedGenericObjectStorageConfig: v1.DeprecatedGenericObjectStorageConfig{
Endpoint: "",
AccessKeyID: "fake-access-key-id",
AccessKeySecret: "fake-access-key-secret",
Expand All @@ -603,7 +603,7 @@ func TestValidateWholeOssConfig(t *testing.T) {
name: "invalid oss config empty access key id",
success: false,
ossConfig: &v1.DeprecatedOssConfig{
GenericObjectStorageConfig: v1.GenericObjectStorageConfig{
DeprecatedGenericObjectStorageConfig: v1.DeprecatedGenericObjectStorageConfig{
Endpoint: "http://oss-cn-hangzhou.aliyuncs.com",
AccessKeyID: "",
AccessKeySecret: "fake-access-key-secret",
Expand All @@ -615,7 +615,7 @@ func TestValidateWholeOssConfig(t *testing.T) {
name: "invalid oss config empty access key secret",
success: false,
ossConfig: &v1.DeprecatedOssConfig{
GenericObjectStorageConfig: v1.GenericObjectStorageConfig{
DeprecatedGenericObjectStorageConfig: v1.DeprecatedGenericObjectStorageConfig{
Endpoint: "http://oss-cn-hangzhou.aliyuncs.com",
AccessKeyID: "fake-access-key-id",
AccessKeySecret: "",
Expand Down Expand Up @@ -648,7 +648,7 @@ func TestValidateWholeS3Config(t *testing.T) {
name: "invalid s3 config empty region",
success: false,
s3Config: &v1.DeprecatedS3Config{
GenericObjectStorageConfig: v1.GenericObjectStorageConfig{
DeprecatedGenericObjectStorageConfig: v1.DeprecatedGenericObjectStorageConfig{
AccessKeyID: "fake-access-key-id",
AccessKeySecret: "fake-access-key-secret",
Bucket: "kusion_bucket",
Expand Down
Loading