-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
378 lines (321 loc) · 8.64 KB
/
config.go
File metadata and controls
378 lines (321 loc) · 8.64 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package main
import (
"fmt"
"os"
"dario.cat/mergo"
"gopkg.in/yaml.v3"
)
type RuntimeConfig struct {
EnvDir string
EnvName string
}
const (
DefaultEnvDir = "./env"
DefaultEnvName = "default"
)
func RuntimeConfigFromFlags(envDir, envName string) RuntimeConfig {
if envDir == "" {
v := os.Getenv("WORKBENCH_ENV_DIR")
if v != "" {
envDir = v
} else {
envDir = DefaultEnvDir
}
}
if envName == "" {
v := os.Getenv("WORKBENCH_ENV_NAME")
if v != "" {
envName = v
} else {
envName = DefaultEnvName
}
}
return RuntimeConfig{
EnvDir: envDir,
EnvName: envName,
}
}
type EnvironmentConfig struct {
Global GlobalConfig `yaml:"global"`
Features FeatureConfig `yaml:"features"`
Cloudserver CloudserverConfig `yaml:"cloudserver"`
S3Metadata MetadataConfig `yaml:"s3_metadata"`
Backbeat BackbeatConfig `yaml:"backbeat"`
Vault VaultConfig `yaml:"vault"`
Scuba ScubaConfig `yaml:"scuba"`
ScubaMetadata MetadataConfig `yaml:"scuba_metadata"`
Kafka KafkaConfig `yaml:"kafka"`
Zookeeper ZookeeperConfig `yaml:"zookeeper"`
Redis RedisConfig `yaml:"redis"`
Utapi UtapiConfig `yaml:"utapi"`
MigrationTools MigrationToolsConfig `yaml:"migration_tools"`
Clickhouse ClickhouseConfig `yaml:"clickhouse"`
Fluentbit FluentbitConfig `yaml:"fluentbit"`
}
type GlobalConfig struct {
LogLevel string `yaml:"log_level"`
}
type FeatureConfig struct {
Scuba ScubaFeatureConfig `yaml:"scuba"`
BucketNotifications BucketNotificationsFeatureConfig `yaml:"bucket_notifications"`
CrossRegionReplication CrossRegionReplicationFeatureConfig `yaml:"cross_region_replication"`
Utapi UtapiFeatureConfig `yaml:"utapi"`
Migration MigrationFeatureConfig `yaml:"migration"`
AccessLogging AccessLoggingFeatureConfig `yaml:"access_logging"`
}
type ScubaFeatureConfig struct {
Enabled bool `yaml:"enabled"`
EnableServiceUser bool `yaml:"enable_service_user"`
}
type BucketNotificationsFeatureConfig struct {
Enabled bool `yaml:"enabled"`
DestinationAuth struct {
Type string `yaml:"type"`
Username string `yaml:"username"`
Password string `yaml:"password"`
} `yaml:"destinationAuth"`
}
type CrossRegionReplicationFeatureConfig struct {
Enabled bool `yaml:"enabled"`
}
type UtapiFeatureConfig struct {
Enabled bool `yaml:"enabled"`
}
type MigrationFeatureConfig struct {
Enabled bool `yaml:"enabled"`
}
type CloudserverConfig struct {
Image string `yaml:"image"`
EnableNullVersionCompatMode bool `yaml:"enableNullVersionCompatMode"`
LogLevel string `yaml:"log_level"`
}
type BackbeatConfig struct {
Image string `yaml:"image"`
LogLevel string `yaml:"log_level"`
}
type VaultConfig struct {
Image string `yaml:"image"`
LogLevel string `yaml:"log_level"`
}
type UtapiConfig struct {
Image string `yaml:"image"`
LogLevel string `yaml:"log_level"`
}
type MigrationToolsConfig struct {
Image string `yaml:"image"`
LogLevel string `yaml:"log_level"`
}
type VFormat string
func (vf VFormat) String() string {
return string(vf)
}
func (vf VFormat) MarshalJSON() ([]byte, error) {
return []byte(`"` + string(vf) + `"`), nil
}
func (vf *VFormat) UnmarshalJSON(data []byte) error {
str := string(data)
switch str {
case `"v0"`:
*vf = Formatv0
case `"v1"`:
*vf = Formatv1
default:
return fmt.Errorf("unknown version format %s", str)
}
return nil
}
func (vf VFormat) MarshalYAML() (interface{}, error) {
return string(vf), nil
}
func (vf *VFormat) UnmarshalYAML(value *yaml.Node) error {
var str string
if err := value.Decode(&str); err != nil {
return err
}
switch str {
case "v0":
*vf = Formatv0
case "v1":
*vf = Formatv1
default:
return fmt.Errorf("unknown version format %s", str)
}
return nil
}
const (
Formatv0 VFormat = "v0"
Formatv1 VFormat = "v1"
)
type MetadataConfig struct {
Image string `yaml:"image"`
RaftSessions int `yaml:"raft_sessions"`
BasePorts MdPortConfig `yaml:"base_ports"`
LogLevel string `yaml:"log_level"`
VFormat VFormat `yaml:"vformat"`
Migration *MigrationConfig `yaml:"migration"`
}
type MdPortConfig struct {
Bucketd uint16 `yaml:"bucketd"`
Repd uint16 `yaml:"repd"`
RepdAdmin uint16 `yaml:"repdAdmin"`
}
type MigrationConfig struct {
Deploy bool `yaml:"deploy"`
BasePorts MdPortConfig `yaml:"base_ports"`
}
type ScubaConfig struct {
Image string `yaml:"image"`
LogLevel string `yaml:"log_level"`
}
type KafkaConfig struct {
Image string `yaml:"image"`
LogLevel string `yaml:"log_level"`
}
type ZookeeperConfig struct {
Image string `yaml:"image"`
LogLevel string `yaml:"log_level"`
}
type RedisConfig struct {
Image string `yaml:"image"`
LogLevel string `yaml:"log_level"`
}
type ClickhouseConfig struct {
Image string `yaml:"image"`
LogLevel string `yaml:"log_level"`
}
type FluentbitConfig struct {
Image string `yaml:"image"`
LogLevel string `yaml:"log_level"`
}
type AccessLoggingFeatureConfig struct {
Enabled bool `yaml:"enabled"`
}
func DefaultEnvironmentConfig() EnvironmentConfig {
return EnvironmentConfig{
Global: GlobalConfig{
LogLevel: "info",
},
Features: FeatureConfig{
BucketNotifications: BucketNotificationsFeatureConfig{
DestinationAuth: struct {
Type string `yaml:"type"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}{
Type: "none",
},
},
Utapi: UtapiFeatureConfig{
Enabled: false,
},
Migration: MigrationFeatureConfig{
Enabled: false,
},
CrossRegionReplication: CrossRegionReplicationFeatureConfig{
Enabled: false,
},
AccessLogging: AccessLoggingFeatureConfig{
Enabled: false,
},
},
Cloudserver: CloudserverConfig{},
S3Metadata: MetadataConfig{
VFormat: Formatv1,
BasePorts: MdPortConfig{
Bucketd: 9000,
Repd: 4200,
RepdAdmin: 4250,
},
RaftSessions: 3,
Migration: &MigrationConfig{
Deploy: false,
BasePorts: MdPortConfig{
Bucketd: 9001,
Repd: 4700,
RepdAdmin: 4750,
},
},
},
Backbeat: BackbeatConfig{},
Vault: VaultConfig{},
Scuba: ScubaConfig{},
ScubaMetadata: MetadataConfig{
VFormat: Formatv0,
BasePorts: MdPortConfig{
Bucketd: 19000,
Repd: 14200,
RepdAdmin: 14250,
},
RaftSessions: 1,
},
Utapi: UtapiConfig{},
MigrationTools: MigrationToolsConfig{},
Clickhouse: ClickhouseConfig{},
Fluentbit: FluentbitConfig{},
}
}
func LoadEnvironmentConfig(path string) (EnvironmentConfig, error) {
cfg := DefaultEnvironmentConfig()
if path == "" {
return cfg, nil
}
// Read the config file
data, err := os.ReadFile(path)
if err != nil {
return cfg, fmt.Errorf("failed to read config file: %w", err)
}
// Parse the YAML into a temporary config
var fileCfg EnvironmentConfig
if err := yaml.Unmarshal(data, &fileCfg); err != nil {
return cfg, fmt.Errorf("failed to parse config file: %w", err)
}
// Merge the configs, only overriding non-empty fields
if err := mergo.Merge(&cfg, fileCfg, mergo.WithOverride); err != nil {
return cfg, fmt.Errorf("failed to merge configs: %w", err)
}
// Set the log level for each component that doesn't have one already set
if cfg.Cloudserver.LogLevel == "" {
cfg.Cloudserver.LogLevel = cfg.Global.LogLevel
}
if cfg.S3Metadata.LogLevel == "" {
cfg.S3Metadata.LogLevel = cfg.Global.LogLevel
}
// deploy the migration metadata map and bucketds if enabled
if cfg.Features.Migration.Enabled {
cfg.S3Metadata.Migration.Deploy = true
}
if cfg.Backbeat.LogLevel == "" {
cfg.Backbeat.LogLevel = cfg.Global.LogLevel
}
if cfg.Vault.LogLevel == "" {
cfg.Vault.LogLevel = cfg.Global.LogLevel
}
if cfg.Scuba.LogLevel == "" {
cfg.Scuba.LogLevel = cfg.Global.LogLevel
}
if cfg.ScubaMetadata.LogLevel == "" {
cfg.ScubaMetadata.LogLevel = cfg.Global.LogLevel
}
if cfg.Kafka.LogLevel == "" {
cfg.Kafka.LogLevel = cfg.Global.LogLevel
}
if cfg.Zookeeper.LogLevel == "" {
cfg.Zookeeper.LogLevel = cfg.Global.LogLevel
}
if cfg.Redis.LogLevel == "" {
cfg.Redis.LogLevel = cfg.Global.LogLevel
}
if cfg.Utapi.LogLevel == "" {
cfg.Utapi.LogLevel = cfg.Global.LogLevel
}
if cfg.MigrationTools.LogLevel == "" {
cfg.MigrationTools.LogLevel = cfg.Global.LogLevel
}
if cfg.Clickhouse.LogLevel == "" {
cfg.Clickhouse.LogLevel = cfg.Global.LogLevel
}
if cfg.Fluentbit.LogLevel == "" {
cfg.Fluentbit.LogLevel = cfg.Global.LogLevel
}
return cfg, nil
}