-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
74 lines (57 loc) · 1.6 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
package main
import (
"io/ioutil"
"regexp"
"runtime"
yaml "gopkg.in/yaml.v2"
)
type NetnsExporterConfig struct {
APIServer APIServerConfig `yaml:"api_server"`
InterfaceMetrics []string `yaml:"interface_metrics"`
ProcMetrics map[string]ProcMetric `yaml:"proc_metrics"`
Threads int `yaml:"threads"`
NamespacesFilter NamespacesFilter `yaml:"namespaces_filter"`
}
type ProcMetric struct {
FileName string `yaml:"file"`
}
type APIServerConfig struct {
ServerAddress string `yaml:"server_address"`
ServerPort int `yaml:"server_port"`
RequestTimeout int `yaml:"request_timeout"`
TelemetryPath string `yaml:"telemetry_path"`
}
type NamespacesFilter struct {
BlacklistPattern string `yaml:"blacklist_pattern"`
WhitelistPattern string `yaml:"whitelist_pattern"`
BlacklistRegexp *regexp.Regexp
WhitelistRegexp *regexp.Regexp
}
func LoadConfig(path string) (*NetnsExporterConfig, error) {
cfg := NetnsExporterConfig{}
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, err
}
cfg.Threads = runtime.NumCPU()
return &cfg, nil
}
func (nsFilter *NamespacesFilter) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain NamespacesFilter
err := unmarshal((*plain)(nsFilter))
if err != nil {
return err
}
nsFilter.BlacklistRegexp, err = regexp.Compile(nsFilter.BlacklistPattern)
if err != nil {
return err
}
nsFilter.WhitelistRegexp, err = regexp.Compile(nsFilter.WhitelistPattern)
if err != nil {
return err
}
return nil
}