-
Notifications
You must be signed in to change notification settings - Fork 78
/
config.go
200 lines (171 loc) · 7.23 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package v1
const ConfigBackends = "backends"
// Config contains configurations for kusion cli, which stores in ${KUSION_HOME}/config.yaml.
type Config struct {
// Backends contains the configurations for multiple backends.
Backends *BackendConfigs `yaml:"backends,omitempty" json:"backends,omitempty"`
}
const (
DefaultBackendName = "default"
BackendCurrent = "current"
BackendType = "type"
BackendConfigItems = "configs"
BackendLocalPath = "path"
BackendMysqlDBName = "dbName"
BackendMysqlUser = "user"
BackendMysqlPassword = "password"
BackendMysqlHost = "host"
BackendMysqlPort = "port"
BackendGenericOssEndpoint = "endpoint"
BackendGenericOssAK = "accessKeyID"
BackendGenericOssSK = "accessKeySecret"
BackendGenericOssBucket = "bucket"
BackendGenericOssPrefix = "prefix"
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
)
// BackendConfigs contains the configuration of multiple backends and the current backend.
type BackendConfigs struct {
// Current is the name of the current used backend.
Current string `yaml:"current,omitempty" json:"current,omitempty"`
// Backends contains the types and configs of multiple backends, whose key is the backend name.
Backends map[string]*BackendConfig `yaml:",omitempty,inline" json:",omitempty,inline"`
}
// 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 string `yaml:"type,omitempty" json:"type,omitempty"`
// Configs contains config items of the backend, whose keys differ from different backend types.
Configs map[string]any `yaml:"configs,omitempty" json:"configs,omitempty"`
}
// BackendLocalConfig contains the config of using local file system as backend, which can be converted
// from BackendConfig if Type is BackendTypeLocal.
type BackendLocalConfig struct {
// Path of the directory to store the files.
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 {
*GenericBackendObjectStorageConfig `yaml:",inline" json:",inline"` // OSS asks for non-empty endpoint
}
// BackendS3Config contains the config of using S3 as backend, which can be converted from BackendConfig
// if Type is BackendS3Config.
type BackendS3Config struct {
*GenericBackendObjectStorageConfig `yaml:",inline" json:",inline"`
// Region of S3.
Region string `yaml:"region,omitempty" json:"region,omitempty"`
}
// GenericBackendObjectStorageConfig contains generic configs which can be reused by BackendOssConfig and
// BackendS3Config.
type GenericBackendObjectStorageConfig struct {
// Endpoint of the object storage service.
Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty"`
// AccessKeyID of the object storage service.
AccessKeyID string `yaml:"accessKeyID,omitempty" json:"accessKeyID,omitempty"`
// AccessKeySecret of the object storage service.
AccessKeySecret string `yaml:"accessKeySecret,omitempty" json:"accessKeySecret,omitempty"`
// Bucket of the object storage service.
Bucket string `yaml:"bucket" json:"bucket"`
// Prefix of the key to store the files.
Prefix string `yaml:"prefix,omitempty" json:"prefix,omitempty"`
}
// ToLocalBackend converts BackendConfig to structured BackendLocalConfig, works only when the Type
// is BackendTypeLocal, and the Configs are with correct type, or return nil.
func (b *BackendConfig) ToLocalBackend() *BackendLocalConfig {
if b.Type != BackendTypeLocal {
return nil
}
path, _ := b.Configs[BackendLocalPath].(string)
return &BackendLocalConfig{
Path: path,
}
}
// 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 {
if b.Type != BackendTypeOss {
return nil
}
endpoint, _ := b.Configs[BackendGenericOssEndpoint].(string)
accessKeyID, _ := b.Configs[BackendGenericOssAK].(string)
accessKeySecret, _ := b.Configs[BackendGenericOssSK].(string)
bucket, _ := b.Configs[BackendGenericOssBucket].(string)
prefix, _ := b.Configs[BackendGenericOssPrefix].(string)
return &BackendOssConfig{
&GenericBackendObjectStorageConfig{
Endpoint: endpoint,
AccessKeyID: accessKeyID,
AccessKeySecret: accessKeySecret,
Bucket: bucket,
Prefix: prefix,
},
}
}
// ToS3Backend converts BackendConfig to structured BackendS3Config, works only when the Type is
// BackendTypeS3, and the Configs are with correct type, or return nil.
func (b *BackendConfig) ToS3Backend() *BackendS3Config {
if b.Type != BackendTypeS3 {
return nil
}
endpoint, _ := b.Configs[BackendGenericOssEndpoint].(string)
accessKeyID, _ := b.Configs[BackendGenericOssAK].(string)
accessKeySecret, _ := b.Configs[BackendGenericOssSK].(string)
bucket, _ := b.Configs[BackendGenericOssBucket].(string)
prefix, _ := b.Configs[BackendGenericOssPrefix].(string)
region, _ := b.Configs[BackendS3Region].(string)
return &BackendS3Config{
GenericBackendObjectStorageConfig: &GenericBackendObjectStorageConfig{
Endpoint: endpoint,
AccessKeyID: accessKeyID,
AccessKeySecret: accessKeySecret,
Bucket: bucket,
Prefix: prefix,
},
Region: region,
}
}