-
Notifications
You must be signed in to change notification settings - Fork 266
/
config.go
579 lines (453 loc) · 15.7 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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*
* Copyright 2020 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package config
import (
"errors"
"fmt"
"net"
"time"
"d7y.io/dragonfly/v2/cmd/dependency/base"
"d7y.io/dragonfly/v2/pkg/net/ip"
"d7y.io/dragonfly/v2/pkg/objectstorage"
"d7y.io/dragonfly/v2/pkg/rpc"
"d7y.io/dragonfly/v2/pkg/slices"
"d7y.io/dragonfly/v2/pkg/types"
)
type Config struct {
// Base options.
base.Options `yaml:",inline" mapstructure:",squash"`
// Server configuration.
Server ServerConfig `yaml:"server" mapstructure:"server"`
// Database configuration.
Database DatabaseConfig `yaml:"database" mapstructure:"database"`
// Cache configuration.
Cache CacheConfig `yaml:"cache" mapstructure:"cache"`
// ObjectStorage configuration.
ObjectStorage ObjectStorageConfig `yaml:"objectStorage" mapstructure:"objectStorage"`
// Metrics configuration.
Metrics MetricsConfig `yaml:"metrics" mapstructure:"metrics"`
// Security configuration.
Security SecurityConfig `yaml:"security" mapstructure:"security"`
// Network configuration.
Network NetworkConfig `yaml:"network" mapstructure:"network"`
}
type ServerConfig struct {
// Server name.
Name string `yaml:"name" mapstructure:"name"`
// Server work directory.
WorkHome string `yaml:"workHome" mapstructure:"workHome"`
// Server dynamic config cache directory.
CacheDir string `yaml:"cacheDir" mapstructure:"cacheDir"`
// Server log directory.
LogDir string `yaml:"logDir" mapstructure:"logDir"`
// Server plugin directory.
PluginDir string `yaml:"pluginDir" mapstructure:"pluginDir"`
// GRPC server configuration.
GRPC GRPCConfig `yaml:"grpc" mapstructure:"grpc"`
// REST server configuration.
REST RestConfig `yaml:"rest" mapstructure:"rest"`
}
type DatabaseConfig struct {
// Database type.
Type string `yaml:"type" mapstructure:"type"`
// Mysql configuration.
Mysql MysqlConfig `yaml:"mysql" mapstructure:"mysql"`
// Postgres configuration.
Postgres PostgresConfig `yaml:"postgres" mapstructure:"postgres"`
// Redis configuration.
Redis RedisConfig `yaml:"redis" mapstructure:"redis"`
}
type MysqlConfig struct {
// Server username.
User string `yaml:"user" mapstructure:"user"`
// Server password.
Password string `yaml:"password" mapstructure:"password"`
// Server host.
Host string `yaml:"host" mapstructure:"host"`
// Server port.
Port int `yaml:"port" mapstructure:"port"`
// Server DB name.
DBName string `yaml:"dbname" mapstructure:"dbname"`
// TLS mode (can be one of "true", "false", "skip-verify", or "preferred").
TLSConfig string `yaml:"tlsConfig" mapstructure:"tlsConfig"`
// Custom TLS configuration (overrides "TLSConfig" setting above).
TLS *TLSConfig `yaml:"tls" mapstructure:"tls"`
// Enable migration.
Migrate bool `yaml:"migrate" mapstructure:"migrate"`
}
type TLSConfig struct {
// Client certificate file path.
Cert string `yaml:"cert" mapstructure:"cert"`
// Client key file path.
Key string `yaml:"key" mapstructure:"key"`
// CA file path.
CA string `yaml:"ca" mapstructure:"ca"`
// InsecureSkipVerify controls whether a client verifies the
// server's certificate chain and host name.
InsecureSkipVerify bool `yaml:"insecureSkipVerify" mapstructure:"insecureSkipVerify"`
}
type PostgresConfig struct {
// Server username.
User string `yaml:"user" mapstructure:"user"`
// Server password.
Password string `yaml:"password" mapstructure:"password"`
// Server host.
Host string `yaml:"host" mapstructure:"host"`
// Server port.
Port int `yaml:"port" mapstructure:"port"`
// Server DB name.
DBName string `yaml:"dbname" mapstructure:"dbname"`
// SSL mode.
SSLMode string `yaml:"sslMode" mapstructure:"sslMode"`
// Server timezone.
Timezone string `yaml:"timezone" mapstructure:"timezone"`
// Enable migration.
Migrate bool `yaml:"migrate" mapstructure:"migrate"`
}
type RedisConfig struct {
// DEPRECATED: Please use the `addrs` field instead.
Host string `yaml:"host" mapstructure:"host"`
// DEPRECATED: Please use the `addrs` field instead.
Port int `yaml:"port" mapstructure:"port"`
// Addrs is server addresses.
Addrs []string `yaml:"addrs" mapstructure:"addrs"`
// MasterName is the sentinel master name.
MasterName string `yaml:"masterName" mapstructure:"masterName"`
// Username is server username.
Username string `yaml:"username" mapstructure:"username"`
// Password is server password.
Password string `yaml:"password" mapstructure:"password"`
// DB is server cache DB name.
DB int `yaml:"db" mapstructure:"db"`
// BrokerDB is server broker DB name.
BrokerDB int `yaml:"brokerDB" mapstructure:"brokerDB"`
// BackendDB is server backend DB name.
BackendDB int `yaml:"backendDB" mapstructure:"backendDB"`
}
type CacheConfig struct {
// Redis cache configuration.
Redis RedisCacheConfig `yaml:"redis" mapstructure:"redis"`
// Local cache configuration.
Local LocalCacheConfig `yaml:"local" mapstructure:"local"`
}
type RedisCacheConfig struct {
// Cache TTL.
TTL time.Duration `yaml:"ttl" mapstructure:"ttl"`
}
type LocalCacheConfig struct {
// Size of LFU cache.
Size int `yaml:"size" mapstructure:"size"`
// Cache TTL.
TTL time.Duration `yaml:"ttl" mapstructure:"ttl"`
}
type RestConfig struct {
// REST server address.
Addr string `yaml:"addr" mapstructure:"addr"`
}
type MetricsConfig struct {
// Enable metrics service.
Enable bool `yaml:"enable" mapstructure:"enable"`
// Metrics service address.
Addr string `yaml:"addr" mapstructure:"addr"`
// Enable peer gauge metrics.
EnablePeerGauge bool `yaml:"enablePeerGauge" mapstructure:"enablePeerGauge"`
}
type GRPCConfig struct {
// DEPRECATED: Please use the `listenIP` field instead.
Listen string `mapstructure:"listen" yaml:"listen"`
// AdvertiseIP is advertise ip.
AdvertiseIP net.IP `yaml:"advertiseIP" mapstructure:"advertiseIP"`
// ListenIP is listen ip, like: 0.0.0.0, 192.168.0.1.
ListenIP net.IP `mapstructure:"listenIP" yaml:"listenIP"`
// Port is listen port.
PortRange TCPListenPortRange `yaml:"port" mapstructure:"port"`
}
type TCPListenPortRange struct {
Start int
End int
}
type ObjectStorageConfig struct {
// Enable object storage.
Enable bool `yaml:"enable" mapstructure:"enable"`
// Name is object storage name of type, it can be s3, oss or obs.
Name string `mapstructure:"name" yaml:"name"`
// Region is storage region.
Region string `mapstructure:"region" yaml:"region"`
// Endpoint is datacenter endpoint.
Endpoint string `mapstructure:"endpoint" yaml:"endpoint"`
// AccessKey is access key ID.
AccessKey string `mapstructure:"accessKey" yaml:"accessKey"`
// SecretKey is access key secret.
SecretKey string `mapstructure:"secretKey" yaml:"secretKey"`
// S3ForcePathStyle sets force path style for s3, true by default.
// Set this to `true` to force the request to use path-style addressing,
// i.e., `http://s3.amazonaws.com/BUCKET/KEY`. By default, the S3 client
// will use virtual hosted bucket addressing when possible
// (`http://BUCKET.s3.amazonaws.com/KEY`).
// Refer to https://github.com/aws/aws-sdk-go/blob/main/aws/config.go#L118.
S3ForcePathStyle bool `mapstructure:"s3ForcePathStyle" yaml:"s3ForcePathStyle"`
}
type SecurityConfig struct {
// AutoIssueCert indicates to issue client certificates for all grpc call.
AutoIssueCert bool `yaml:"autoIssueCert" mapstructure:"autoIssueCert"`
// CACert is the CA certificate for all grpc tls handshake, it can be path or PEM format string.
CACert types.PEMContent `mapstructure:"caCert" yaml:"caCert"`
// CAKey is the CA private key, it can be path or PEM format string.
CAKey types.PEMContent `mapstructure:"caKey" yaml:"caKey"`
// TLSPolicy controls the grpc shandshake behaviors:
// force: both ClientHandshake and ServerHandshake are only support tls
// prefer: ServerHandshake supports tls and insecure (non-tls), ClientHandshake will only support tls
// default: ServerHandshake supports tls and insecure (non-tls), ClientHandshake will only support insecure (non-tls)
TLSPolicy string `mapstructure:"tlsPolicy" yaml:"tlsPolicy"`
// CertSpec is the desired state of certificate.
CertSpec CertSpec `mapstructure:"certSpec" yaml:"certSpec"`
}
type CertSpec struct {
// DNSNames is a list of dns names be set on the certificate.
DNSNames []string `mapstructure:"dnsNames" yaml:"dnsNames"`
// IPAddresses is a list of ip addresses be set on the certificate.
IPAddresses []net.IP `mapstructure:"ipAddresses" yaml:"ipAddresses"`
// ValidityPeriod is the validity period of certificate.
ValidityPeriod time.Duration `mapstructure:"validityPeriod" yaml:"validityPeriod"`
}
type NetworkConfig struct {
// EnableIPv6 enables ipv6 for server.
EnableIPv6 bool `mapstructure:"enableIPv6" yaml:"enableIPv6"`
}
// New config instance.
func New() *Config {
return &Config{
Server: ServerConfig{
Name: DefaultServerName,
GRPC: GRPCConfig{
PortRange: TCPListenPortRange{
Start: DefaultGRPCPort,
End: DefaultGRPCPort,
},
},
REST: RestConfig{
Addr: DefaultRESTAddr,
},
},
Database: DatabaseConfig{
Type: DatabaseTypeMysql,
Mysql: MysqlConfig{
Port: DefaultMysqlPort,
DBName: DefaultMysqlDBName,
Migrate: true,
},
Postgres: PostgresConfig{
Port: DefaultPostgresPort,
DBName: DefaultPostgresDBName,
SSLMode: DefaultPostgresSSLMode,
Timezone: DefaultPostgresTimezone,
Migrate: true,
},
Redis: RedisConfig{
DB: DefaultRedisDB,
BrokerDB: DefaultRedisBrokerDB,
BackendDB: DefaultRedisBackendDB,
},
},
Cache: CacheConfig{
Redis: RedisCacheConfig{
TTL: DefaultRedisCacheTTL,
},
Local: LocalCacheConfig{
Size: DefaultLFUCacheSize,
TTL: DefaultLFUCacheTTL,
},
},
ObjectStorage: ObjectStorageConfig{
Enable: false,
S3ForcePathStyle: true,
},
Security: SecurityConfig{
AutoIssueCert: false,
TLSPolicy: rpc.PreferTLSPolicy,
CertSpec: CertSpec{
DNSNames: DefaultCertDNSNames,
IPAddresses: DefaultCertIPAddresses,
ValidityPeriod: DefaultCertValidityPeriod,
},
},
Metrics: MetricsConfig{
Enable: false,
Addr: DefaultMetricsAddr,
EnablePeerGauge: true,
},
Network: NetworkConfig{
EnableIPv6: DefaultNetworkEnableIPv6,
},
}
}
// Validate config values
func (cfg *Config) Validate() error {
if cfg.Server.Name == "" {
return errors.New("server requires parameter name")
}
if cfg.Server.GRPC.AdvertiseIP == nil {
return errors.New("grpc requires parameter advertiseIP")
}
if cfg.Server.GRPC.ListenIP == nil {
return errors.New("grpc requires parameter listenIP")
}
if cfg.Database.Type == "" {
return errors.New("database requires parameter type")
}
if cfg.Database.Type == DatabaseTypeMysql || cfg.Database.Type == DatabaseTypeMariaDB {
if cfg.Database.Mysql.User == "" {
return errors.New("mysql requires parameter user")
}
if cfg.Database.Mysql.Password == "" {
return errors.New("mysql requires parameter password")
}
if cfg.Database.Mysql.Host == "" {
return errors.New("mysql requires parameter host")
}
if cfg.Database.Mysql.Port <= 0 {
return errors.New("mysql requires parameter port")
}
if cfg.Database.Mysql.DBName == "" {
return errors.New("mysql requires parameter dbname")
}
if cfg.Database.Mysql.TLS != nil {
if cfg.Database.Mysql.TLS.Cert == "" {
return errors.New("tls requires parameter cert")
}
if cfg.Database.Mysql.TLS.Key == "" {
return errors.New("tls requires parameter key")
}
if cfg.Database.Mysql.TLS.CA == "" {
return errors.New("tls requires parameter ca")
}
}
}
if cfg.Database.Type == DatabaseTypePostgres {
if cfg.Database.Postgres.User == "" {
return errors.New("postgres requires parameter user")
}
if cfg.Database.Postgres.Password == "" {
return errors.New("postgres requires parameter password")
}
if cfg.Database.Postgres.Host == "" {
return errors.New("postgres requires parameter host")
}
if cfg.Database.Postgres.Port <= 0 {
return errors.New("postgres requires parameter port")
}
if cfg.Database.Postgres.DBName == "" {
return errors.New("postgres requires parameter dbname")
}
if cfg.Database.Postgres.SSLMode == "" {
return errors.New("postgres requires parameter sslMode")
}
if cfg.Database.Postgres.Timezone == "" {
return errors.New("postgres requires parameter timezone")
}
}
if len(cfg.Database.Redis.Addrs) == 0 {
return errors.New("redis requires parameter addrs")
}
if len(cfg.Database.Redis.Addrs) == 1 {
if cfg.Database.Redis.DB < 0 {
return errors.New("redis requires parameter db")
}
if cfg.Database.Redis.BrokerDB < 0 {
return errors.New("redis requires parameter brokerDB")
}
if cfg.Database.Redis.BackendDB < 0 {
return errors.New("redis requires parameter backendDB")
}
}
if cfg.Database.Redis.BrokerDB < 0 {
return errors.New("redis requires parameter brokerDB")
}
if cfg.Database.Redis.BackendDB < 0 {
return errors.New("redis requires parameter backendDB")
}
if cfg.Cache.Redis.TTL == 0 {
return errors.New("redis requires parameter ttl")
}
if cfg.Cache.Local.Size == 0 {
return errors.New("local requires parameter size")
}
if cfg.Cache.Local.TTL == 0 {
return errors.New("local requires parameter ttl")
}
if cfg.ObjectStorage.Enable {
if cfg.ObjectStorage.Name == "" {
return errors.New("objectStorage requires parameter name")
}
if !slices.Contains([]string{objectstorage.ServiceNameS3, objectstorage.ServiceNameOSS, objectstorage.ServiceNameOBS}, cfg.ObjectStorage.Name) {
return errors.New("objectStorage requires parameter name")
}
if cfg.ObjectStorage.AccessKey == "" {
return errors.New("objectStorage requires parameter accessKey")
}
if cfg.ObjectStorage.SecretKey == "" {
return errors.New("objectStorage requires parameter secretKey")
}
}
if cfg.Security.AutoIssueCert {
if cfg.Security.CACert == "" {
return errors.New("security requires parameter caCert")
}
if cfg.Security.CAKey == "" {
return errors.New("security requires parameter caKey")
}
if !slices.Contains([]string{rpc.DefaultTLSPolicy, rpc.ForceTLSPolicy, rpc.PreferTLSPolicy}, cfg.Security.TLSPolicy) {
return errors.New("security requires parameter tlsPolicy")
}
if len(cfg.Security.CertSpec.IPAddresses) == 0 {
return errors.New("certSpec requires parameter ipAddresses")
}
if len(cfg.Security.CertSpec.DNSNames) == 0 {
return errors.New("certSpec requires parameter dnsNames")
}
}
if cfg.Metrics.Enable {
if cfg.Metrics.Addr == "" {
return errors.New("metrics requires parameter addr")
}
}
return nil
}
func (cfg *Config) Convert() error {
// TODO Compatible with deprecated fields host and port.
if len(cfg.Database.Redis.Addrs) == 0 && cfg.Database.Redis.Host != "" && cfg.Database.Redis.Port > 0 {
cfg.Database.Redis.Addrs = []string{fmt.Sprintf("%s:%d", cfg.Database.Redis.Host, cfg.Database.Redis.Port)}
}
// TODO Compatible with deprecated fields listen.
if cfg.Server.GRPC.Listen != "" && cfg.Server.GRPC.ListenIP == nil {
cfg.Server.GRPC.ListenIP = net.ParseIP(cfg.Server.GRPC.Listen)
}
if cfg.Server.GRPC.AdvertiseIP == nil {
if cfg.Network.EnableIPv6 {
cfg.Server.GRPC.AdvertiseIP = ip.IPv6
} else {
cfg.Server.GRPC.AdvertiseIP = ip.IPv4
}
}
if cfg.Server.GRPC.ListenIP == nil {
if cfg.Network.EnableIPv6 {
cfg.Server.GRPC.ListenIP = net.IPv6zero
} else {
cfg.Server.GRPC.ListenIP = net.IPv4zero
}
}
return nil
}