Skip to content

Commit

Permalink
feat: rename backend related configs in workspace to deprecated (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
healthjyk committed Feb 29, 2024
1 parent 6d6f840 commit fd0915c
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 122 deletions.
46 changes: 26 additions & 20 deletions pkg/apis/core/v1/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const (
DefaultBlock = "default"
ProjectSelectorField = "projectSelector"

BackendLocal = "local"
BackendMysql = "mysql"
BackendOss = "oss"
BackendS3 = "s3"
DeprecatedBackendLocal = "local"
DeprecatedBackendMysql = "mysql"
DeprecatedBackendOss = "oss"
DeprecatedBackendS3 = "s3"
EnvBackendMysqlPassword = "KUSION_BACKEND_MYSQL_PASSWORD"
EnvAwsAccessKeyID = "AWS_ACCESS_KEY_ID"
EnvAwsSecretAccessKey = "AWS_SECRET_ACCESS_KEY"
Expand All @@ -34,7 +34,8 @@ type Workspace struct {
Runtimes *RuntimeConfigs `yaml:"runtimes,omitempty" json:"runtimes,omitempty"`

// Backends are the configs of a set of backends.
Backends *BackendConfigs `yaml:"backends,omitempty" json:"backends,omitempty"`
// Deprecated: do not support backend configs in workspace anymore
Backends *DeprecatedBackendConfigs `yaml:"backends,omitempty" json:"backends,omitempty"`

// SecretStore represents a secure external location for storing secrets.
SecretStore *SecretStoreSpec `yaml:"secretStore,omitempty" json:"secretStore,omitempty"`
Expand Down Expand Up @@ -121,28 +122,31 @@ type ProviderConfig struct {
GenericConfig `yaml:",inline,omitempty" json:",inline,omitempty"`
}

// BackendConfigs contains config of the backend, which is used to store state, etc. Only one kind
// DeprecatedBackendConfigs contains config of the backend, which is used to store state, etc. Only one kind
// backend can be configured.
type BackendConfigs struct {
// Deprecated: do not support backend configs in workspace anymore
type DeprecatedBackendConfigs struct {
// Local is the backend using local file system.
Local *LocalFileConfig `yaml:"local,omitempty" json:"local,omitempty"`
Local *DeprecatedLocalFileConfig `yaml:"local,omitempty" json:"local,omitempty"`

// Mysql is the backend using mysql database.
Mysql *MysqlConfig `yaml:"mysql,omitempty" json:"mysql,omitempty"`
Mysql *DeprecatedMysqlConfig `yaml:"mysql,omitempty" json:"mysql,omitempty"`

// Oss is the backend using OSS.
Oss *OssConfig `yaml:"oss,omitempty" json:"oss,omitempty"`
Oss *DeprecatedOssConfig `yaml:"oss,omitempty" json:"oss,omitempty"`

// S3 is the backend using S3.
S3 *S3Config `yaml:"s3,omitempty" json:"s3,omitempty"`
S3 *DeprecatedS3Config `yaml:"s3,omitempty" json:"s3,omitempty"`
}

// LocalFileConfig contains the config of using local file system as backend. Now there is no configuration
// DeprecatedLocalFileConfig contains the config of using local file system as backend. Now there is no configuration
// item for local file.
type LocalFileConfig struct{}
// Deprecated: do not support backend configs in workspace anymore
type DeprecatedLocalFileConfig struct{}

// MysqlConfig contains the config of using mysql database as backend.
type MysqlConfig struct {
// DeprecatedMysqlConfig contains the config of using mysql database as backend.
// Deprecated: do not support backend configs in workspace anymore
type DeprecatedMysqlConfig struct {
// DBName is the database name.
DBName string `yaml:"dbName" json:"dbName"`

Expand All @@ -159,20 +163,22 @@ type MysqlConfig struct {
Port *int `yaml:"port,omitempty" json:"port,omitempty"`
}

// OssConfig contains the config of using OSS as backend.
type OssConfig struct {
// 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
}

// S3Config contains the config of using S3 as backend.
type S3Config struct {
// 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"`

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

// GenericObjectStorageConfig contains generic configs which can be reused by OssConfig and S3Config.
// GenericObjectStorageConfig contains generic configs which can be reused by DeprecatedOssConfig and DeprecatedS3Config.
type GenericObjectStorageConfig struct {
// Endpoint of the object storage service.
Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/build/builders/appconfig_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/stretchr/testify/assert"

"kusionstack.io/kusion/pkg/apis/core/v1"
v1 "kusionstack.io/kusion/pkg/apis/core/v1"
"kusionstack.io/kusion/pkg/apis/core/v1/workload/network"

"kusionstack.io/kusion/pkg/apis/core/v1/workload"
Expand Down Expand Up @@ -76,8 +76,8 @@ func buildMockWorkspace() *v1.Workspace {
KubeConfig: "/etc/kubeconfig.yaml",
},
},
Backends: &v1.BackendConfigs{
Local: &v1.LocalFileConfig{},
Backends: &v1.DeprecatedBackendConfigs{
Local: &v1.DeprecatedLocalFileConfig{},
},
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/build/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/require"
kclgo "kcl-lang.io/kcl-go"

"kusionstack.io/kusion/pkg/apis/core/v1"
v1 "kusionstack.io/kusion/pkg/apis/core/v1"
"kusionstack.io/kusion/pkg/cmd/build/builders"
"kusionstack.io/kusion/pkg/cmd/build/builders/kcl"
"kusionstack.io/kusion/pkg/workspace"
Expand Down Expand Up @@ -146,8 +146,8 @@ resources:
KubeConfig: "/etc/kubeconfig.yaml",
},
},
Backends: &v1.BackendConfigs{
Local: &v1.LocalFileConfig{},
Backends: &v1.DeprecatedBackendConfigs{
Local: &v1.DeprecatedLocalFileConfig{},
},
}
)
Expand Down
22 changes: 11 additions & 11 deletions pkg/engine/backend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type StateStorageConfig struct {
Config map[string]any
}

// NewConfig news a StateStorageConfig from workspace BackendConfigs, BackendOptions and environment variables.
func NewConfig(workDir string, configs *v1.BackendConfigs, opts *BackendOptions) (*StateStorageConfig, error) {
// NewConfig news a StateStorageConfig from workspace DeprecatedBackendConfigs, BackendOptions and environment variables.
func NewConfig(workDir string, configs *v1.DeprecatedBackendConfigs, opts *BackendOptions) (*StateStorageConfig, error) {
var config, overrideConfig *StateStorageConfig
config = convertWorkspaceBackendConfig(workDir, configs)
if opts != nil && !opts.IsEmpty() {
Expand All @@ -41,7 +41,7 @@ func NewConfig(workDir string, configs *v1.BackendConfigs, opts *BackendOptions)
// NewDefaultStateStorageConfig news the default state storage which uses local backend.
func NewDefaultStateStorageConfig(workDir string) *StateStorageConfig {
return &StateStorageConfig{
Type: v1.BackendLocal,
Type: v1.DeprecatedBackendLocal,
Config: map[string]any{
"path": filepath.Join(workDir, local.KusionStateFileFile),
},
Expand All @@ -68,28 +68,28 @@ func (c *StateStorageConfig) NewStateStorage() (states.StateStorage, error) {
}

// convertWorkspaceBackendConfig converts workspace backend config to StateStorageConfig.
func convertWorkspaceBackendConfig(workDir string, configs *v1.BackendConfigs) *StateStorageConfig {
func convertWorkspaceBackendConfig(workDir string, configs *v1.DeprecatedBackendConfigs) *StateStorageConfig {
name := workspace.GetBackendName(configs)
var config map[string]any
switch name {
case v1.BackendLocal:
case v1.DeprecatedBackendLocal:
config = NewDefaultStateStorageConfig(workDir).Config
case v1.BackendMysql:
case v1.DeprecatedBackendMysql:
config = map[string]any{
"dbName": configs.Mysql.DBName,
"user": configs.Mysql.User,
"password": configs.Mysql.Password,
"host": configs.Mysql.Host,
"port": *configs.Mysql.Port,
}
case v1.BackendOss:
case v1.DeprecatedBackendOss:
config = map[string]any{
"endpoint": configs.Oss.Endpoint,
"bucket": configs.Oss.Bucket,
"accessKeyID": configs.Oss.AccessKeyID,
"accessKeySecret": configs.Oss.AccessKeySecret,
}
case v1.BackendS3:
case v1.DeprecatedBackendS3:
config = map[string]any{
"endpoint": configs.S3.Endpoint,
"bucket": configs.S3.Bucket,
Expand All @@ -108,20 +108,20 @@ func convertWorkspaceBackendConfig(workDir string, configs *v1.BackendConfigs) *
func getEnvBackendConfig(backendType string) map[string]any {
config := make(map[string]any)
switch backendType {
case v1.BackendMysql:
case v1.DeprecatedBackendMysql:
password := workspace.GetMysqlPasswordFromEnv()
if password != "" {
config["password"] = password
}
case v1.BackendOss:
case v1.DeprecatedBackendOss:
accessKeyID, accessKeySecret := workspace.GetOssSensitiveDataFromEnv()
if accessKeyID != "" {
config["accessKeyID"] = accessKeyID
}
if accessKeySecret != "" {
config["accessKeySecret"] = accessKeySecret
}
case v1.BackendS3:
case v1.DeprecatedBackendS3:
accessKeyID, accessKeySecret, region := workspace.GetS3SensitiveDataFromEnv()
if accessKeyID != "" {
config["accessKeyID"] = accessKeyID
Expand Down
44 changes: 22 additions & 22 deletions pkg/engine/backend/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/stretchr/testify/assert"

"kusionstack.io/kusion/pkg/apis/core/v1"
v1 "kusionstack.io/kusion/pkg/apis/core/v1"
"kusionstack.io/kusion/pkg/engine/states"
"kusionstack.io/kusion/pkg/engine/states/local"
)
Expand All @@ -18,7 +18,7 @@ func TestNewConfig(t *testing.T) {
name string
success bool
workDir string
configs *v1.BackendConfigs
configs *v1.DeprecatedBackendConfigs
opts *BackendOptions
setEnvFunc, unSetEnvFunc func()
expectedConfig *StateStorageConfig
Expand All @@ -32,7 +32,7 @@ func TestNewConfig(t *testing.T) {
setEnvFunc: nil,
unSetEnvFunc: nil,
expectedConfig: &StateStorageConfig{
Type: v1.BackendLocal,
Type: v1.DeprecatedBackendLocal,
Config: map[string]any{
"path": "/test_project/test_stack/kusion_state.yaml",
},
Expand All @@ -42,8 +42,8 @@ func TestNewConfig(t *testing.T) {
name: "empty backend options",
success: true,
workDir: "/testProject/testStack",
configs: &v1.BackendConfigs{
Mysql: &v1.MysqlConfig{
configs: &v1.DeprecatedBackendConfigs{
Mysql: &v1.DeprecatedMysqlConfig{
DBName: "kusion_db",
User: "kusion",
Password: "do_not_recommend",
Expand All @@ -59,7 +59,7 @@ func TestNewConfig(t *testing.T) {
_ = os.Unsetenv(v1.EnvBackendMysqlPassword)
},
expectedConfig: &StateStorageConfig{
Type: v1.BackendMysql,
Type: v1.DeprecatedBackendMysql,
Config: map[string]any{
"dbName": "kusion_db",
"user": "kusion",
Expand All @@ -73,16 +73,16 @@ func TestNewConfig(t *testing.T) {
name: "backend options override",
success: true,
workDir: "/testProject/testStack",
configs: &v1.BackendConfigs{
Mysql: &v1.MysqlConfig{
configs: &v1.DeprecatedBackendConfigs{
Mysql: &v1.DeprecatedMysqlConfig{
DBName: "kusion_db",
User: "kusion",
Host: "127.0.0.1",
Port: &mysqlPort,
},
},
opts: &BackendOptions{
Type: v1.BackendS3,
Type: v1.DeprecatedBackendS3,
Config: []string{"region=ua-east-2", "bucket=kusion_bucket"},
},
setEnvFunc: func() {
Expand All @@ -96,7 +96,7 @@ func TestNewConfig(t *testing.T) {
_ = os.Unsetenv(v1.EnvAwsSecretAccessKey)
},
expectedConfig: &StateStorageConfig{
Type: v1.BackendS3,
Type: v1.DeprecatedBackendS3,
Config: map[string]any{
"region": "ua-east-2",
"accessKeyID": "aws_ak_id",
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestStateStorageConfig_NewStateStorage(t *testing.T) {
name: "local state storage",
success: true,
config: &StateStorageConfig{
Type: v1.BackendLocal,
Type: v1.DeprecatedBackendLocal,
Config: map[string]any{
"path": "/test_project/test_stack/kusion_state.yaml",
},
Expand Down Expand Up @@ -163,27 +163,27 @@ func TestMergeConfig(t *testing.T) {
}{
{
name: "empty override config",
backendType: v1.BackendLocal,
backendType: v1.DeprecatedBackendLocal,
config: &StateStorageConfig{
Type: v1.BackendLocal,
Type: v1.DeprecatedBackendLocal,
Config: map[string]any{
"path": "/test_project/test_stack/kusion_state.yaml",
},
},
overrideConfig: nil,
envConfig: nil,
mergedConfig: &StateStorageConfig{
Type: v1.BackendLocal,
Type: v1.DeprecatedBackendLocal,
Config: map[string]any{
"path": "/test_project/test_stack/kusion_state.yaml",
},
},
},
{
name: "same type override config",
backendType: v1.BackendMysql,
backendType: v1.DeprecatedBackendMysql,
config: &StateStorageConfig{
Type: v1.BackendMysql,
Type: v1.DeprecatedBackendMysql,
Config: map[string]any{
"dbName": "kusion_db",
"user": "kusion",
Expand All @@ -192,7 +192,7 @@ func TestMergeConfig(t *testing.T) {
},
},
overrideConfig: &StateStorageConfig{
Type: v1.BackendMysql,
Type: v1.DeprecatedBackendMysql,
Config: map[string]any{
"dbName": "new_kusion_db",
"user": "new_kusion",
Expand All @@ -202,7 +202,7 @@ func TestMergeConfig(t *testing.T) {
"password": "new_kusion_password",
},
mergedConfig: &StateStorageConfig{
Type: v1.BackendMysql,
Type: v1.DeprecatedBackendMysql,
Config: map[string]any{
"dbName": "new_kusion_db",
"user": "new_kusion",
Expand All @@ -214,9 +214,9 @@ func TestMergeConfig(t *testing.T) {
},
{
name: "different type override config",
backendType: v1.BackendOss,
backendType: v1.DeprecatedBackendOss,
config: &StateStorageConfig{
Type: v1.BackendMysql,
Type: v1.DeprecatedBackendMysql,
Config: map[string]any{
"dbName": "kusion_db",
"user": "kusion",
Expand All @@ -225,7 +225,7 @@ func TestMergeConfig(t *testing.T) {
},
},
overrideConfig: &StateStorageConfig{
Type: v1.BackendOss,
Type: v1.DeprecatedBackendOss,
Config: map[string]any{
"endpoint": "oss-cn-hangzhou.aliyuncs.com",
"bucket": "kusion_test",
Expand All @@ -238,7 +238,7 @@ func TestMergeConfig(t *testing.T) {
"accessKeySecret": "kusion_test_env",
},
mergedConfig: &StateStorageConfig{
Type: v1.BackendOss,
Type: v1.DeprecatedBackendOss,
Config: map[string]any{
"endpoint": "oss-cn-hangzhou.aliyuncs.com",
"bucket": "kusion_test",
Expand Down
Loading

0 comments on commit fd0915c

Please sign in to comment.