-
Notifications
You must be signed in to change notification settings - Fork 1
/
clconfig.go
319 lines (281 loc) · 8.59 KB
/
clconfig.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
package clconfig
import (
"crypto/tls"
//"database/sql"
"fmt"
"os"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"github.com/mcuadros/go-defaults"
"github.com/metrico/cloki-config/config"
"github.com/metrico/cloki-config/config/writer"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
)
// Note: ClokiConfig
type ClokiConfig struct {
configPaths []string
logName string
logPath string
typeCloki CLOKI_TYPE
Setting *config.ClokiBaseSettingServer
}
type CLOKI_TYPE int
const (
CLOKI_WRITER CLOKI_TYPE = iota
CLOKI_READER
)
func New(typeCloki CLOKI_TYPE, configPaths []string, logName, logPath string) *ClokiConfig {
c := new(ClokiConfig)
c.Setting = new(config.ClokiBaseSettingServer)
defaults.SetDefaults(c.Setting) //<-- This set the defaults values
c.configPaths = configPaths
c.logPath = logPath
//Type
c.typeCloki = typeCloki
return c
}
func (c *ClokiConfig) readConfigFromFS() {
cnt := 0
for _, p := range c.configPaths {
if len(p) < 5 || p[len(p)-5:] != ".json" {
fmt.Printf("only json extension allowed: %s\n", p)
continue
}
viper.SetConfigFile(p)
if cnt == 0 {
err := viper.ReadInConfig()
if err != nil {
fmt.Println(err)
continue
}
cnt++
continue
}
err := viper.MergeInConfig()
if err != nil {
fmt.Println(err)
}
cnt++
}
}
func (c *ClokiConfig) ReadConfig() {
// Getting constant values
c.readConfigFromFS()
//Env variables
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "[", "_", "]", ""))
viper.SetEnvPrefix(c.Setting.EnvPrefix)
c.setEnvironDataBase()
//Bind Env from Config
c.bindEnvs(*c.Setting)
err := viper.Unmarshal(c.Setting, func(config *mapstructure.DecoderConfig) {
config.TagName = "json"
})
if err != nil {
fmt.Println("couldn't unmarshal viper.")
}
var re = regexp.MustCompile(`database_data\[(\d)\]`)
envParams := []int{}
allSettings := viper.AllSettings()
for key := range allSettings {
if strings.HasPrefix(key, "database_data[") {
key = re.ReplaceAllString(key, "$1")
i, err := strconv.Atoi(key)
if err == nil {
envParams = append(envParams, i)
}
}
}
//Read the data db connection from config. This is fix because defaults doesn't know the size of array
if viper.IsSet("database_data") {
c.Setting.DATABASE_DATA = nil
dataConfig := viper.Get("database_data")
dataVal := dataConfig.([]interface{})
for idx := range dataVal {
val := dataVal[idx].(map[string]interface{})
data := config.ClokiBaseDataBase{}
defaults.SetDefaults(&data) //<-- This set the defaults values
err := mapstructure.Decode(val, &data)
if err != nil {
fmt.Println("ERROR during mapstructure decode[1]:", err)
}
c.Setting.DATABASE_DATA = append(c.Setting.DATABASE_DATA, data)
}
}
//We should do extraction and after sorting 0,1,2,3
sort.Ints(envParams[:])
//Here we do ENV check
for _, idx := range envParams {
value := allSettings[fmt.Sprintf("database_data[%d]", idx)]
val := value.(map[string]interface{})
//If the configuration already exists - we replace only existing params
if len(c.Setting.DATABASE_DATA) > idx {
err := mapstructure.WeakDecode(val, &c.Setting.DATABASE_DATA[idx])
if err != nil {
fmt.Println("ERROR during mapstructure decode[0] sort:", err)
}
} else {
data := config.ClokiBaseDataBase{}
defaults.SetDefaults(&data) //<-- This set the defaults values
err := mapstructure.WeakDecode(val, &data)
if err != nil {
fmt.Println("ERROR during mapstructure decode[1] sort:", err)
}
c.Setting.DATABASE_DATA = append(c.Setting.DATABASE_DATA, data)
}
}
//Prometheus scrapper
re = regexp.MustCompile(`prometheus_scrape\[(\d)\]`)
envParams = []int{}
for key := range allSettings {
if strings.HasPrefix(key, "prometheus_scrape[") {
key = re.ReplaceAllString(key, "$1")
i, err := strconv.Atoi(key)
if err == nil {
envParams = append(envParams, i)
}
}
}
//Read the data prometheus_scrape from config. This is fix because defaults doesn't know the size of array
if viper.IsSet("prometheus_scrape") {
c.Setting.ClokiWriter.PROMETHEUS_SCRAPE = nil
scraperConfig := viper.Get("prometheus_scrape")
dataVal := scraperConfig.([]interface{})
for idx := range dataVal {
val := dataVal[idx].(map[string]interface{})
data := writer.PrometheusScrape{}
defaults.SetDefaults(&data) //<-- This set the defaults values
err := mapstructure.Decode(val, &data)
if err != nil {
fmt.Println("ERROR during mapstructure decode[1]:", err)
}
c.Setting.ClokiWriter.PROMETHEUS_SCRAPE = append(c.Setting.ClokiWriter.PROMETHEUS_SCRAPE, data)
}
}
//We should do extraction and after sorting 0,1,2,3
sort.Ints(envParams[:])
//Here we do ENV check
for _, idx := range envParams {
value := allSettings[fmt.Sprintf("prometheus_scrape[%d]", idx)]
val := value.(map[string]interface{})
//If the configuration already exists - we replace only existing params
if len(c.Setting.ClokiWriter.PROMETHEUS_SCRAPE) > idx {
err := mapstructure.WeakDecode(val, &c.Setting.ClokiWriter.PROMETHEUS_SCRAPE[idx])
if err != nil {
fmt.Println("ERROR during mapstructure scraper decode[0]:", err)
}
} else {
data := writer.PrometheusScrape{}
defaults.SetDefaults(&data) //<-- This set the defaults values
err := mapstructure.WeakDecode(val, &data)
if err != nil {
fmt.Println("ERROR during mapstructure scraper decode[1]:", err)
}
c.Setting.ClokiWriter.PROMETHEUS_SCRAPE = append(c.Setting.ClokiWriter.PROMETHEUS_SCRAPE, data)
}
}
//Check the command line
if c.logName != "" {
c.Setting.LOG_SETTINGS.Name = c.logName
}
if c.logPath != "" {
c.Setting.LOG_SETTINGS.Path = c.logPath
}
//viper.Debug()
c.setFastConfigSettings()
// default table names setting
for i, node := range c.Setting.DATABASE_DATA {
if node.TableSeries == "" {
if node.ClusterName == "" {
c.Setting.DATABASE_DATA[i].TableSeries = "time_series_v2"
} else {
c.Setting.DATABASE_DATA[i].TableSeries = "time_series_v2_dist"
}
}
if node.TableSamples == "" {
if node.ClusterName == "" {
c.Setting.DATABASE_DATA[i].TableSamples = "samples_v4"
} else {
c.Setting.DATABASE_DATA[i].TableSamples = "samples_v4_dist"
}
}
if node.TableMetrics == "" {
if node.ClusterName == "" {
c.Setting.DATABASE_DATA[i].TableMetrics = "samples_v4"
} else {
c.Setting.DATABASE_DATA[i].TableMetrics = "samples_v4_dist"
}
}
}
//c.Setting = &c.Setting
}
//system params for replications, groups
func (c *ClokiConfig) setFastConfigSettings() {
/***********************************/
switch c.Setting.SYSTEM_SETTINGS.HashType {
case "cityhash":
c.Setting.FingerPrintType = writer.FINGERPRINT_CityHash
case "bernstein":
case "default":
c.Setting.FingerPrintType = writer.FINGERPRINT_Bernstein
}
minVersion := c.Setting.HTTPS_SETTINGS.MinTLSVersionString
if minVersion == "TLS1.0" {
c.Setting.HTTPS_SETTINGS.MinTLSVersion = tls.VersionTLS10
} else if minVersion == "TLS1.1" {
c.Setting.HTTPS_SETTINGS.MinTLSVersion = tls.VersionTLS11
} else if minVersion == "TLS1.2" {
c.Setting.HTTPS_SETTINGS.MinTLSVersion = tls.VersionTLS12
} else if minVersion == "TLS1.3" {
c.Setting.HTTPS_SETTINGS.MinTLSVersion = tls.VersionTLS13
}
maxVersion := c.Setting.HTTPS_SETTINGS.MaxTLSVersionString
if maxVersion == "TLS1.0" {
c.Setting.HTTPS_SETTINGS.MaxTLSVersion = tls.VersionTLS10
} else if maxVersion == "TLS1.1" {
c.Setting.HTTPS_SETTINGS.MaxTLSVersion = tls.VersionTLS11
} else if maxVersion == "TLS1.2" {
c.Setting.HTTPS_SETTINGS.MaxTLSVersion = tls.VersionTLS12
} else if maxVersion == "TLS1.3" {
c.Setting.HTTPS_SETTINGS.MaxTLSVersion = tls.VersionTLS13
}
}
//this function will check CLOKI_DATABASE_DATA and set internal bind for viper
//i.e. CLOKI_DATABASE_DATA_0_HOSTNAME -> database_data[0].hostname
func (c *ClokiConfig) setEnvironDataBase() bool {
var re = regexp.MustCompile(`_(\d)_`)
for _, s := range os.Environ() {
if strings.HasPrefix(s, c.Setting.EnvPrefix+"_DATABASE_DATA") {
a := strings.Split(s, "=")
key := strings.TrimPrefix(a[0], c.Setting.EnvPrefix+"_")
key = re.ReplaceAllString(key, "[$1].")
viper.BindEnv(key)
}
}
return true
}
//Now we should bind the ENV params
func (c *ClokiConfig) bindEnvs(iface interface{}, parts ...string) {
ifv := reflect.ValueOf(iface)
ift := reflect.TypeOf(iface)
for i := 0; i < ift.NumField(); i++ {
v := ifv.Field(i)
t := ift.Field(i)
tv, ok := t.Tag.Lookup("mapstructure")
if !ok {
continue
}
switch v.Kind() {
case reflect.Struct:
c.bindEnvs(v.Interface(), append(parts, tv)...)
case reflect.Slice:
continue
default:
viper.BindEnv(strings.Join(append(parts, tv), "."))
}
}
}