/
config.go
171 lines (142 loc) · 3.91 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
package config
import (
"doppler/iprange"
"errors"
"io/ioutil"
"time"
"encoding/json"
"os"
)
const HeartbeatInterval = 10 * time.Second
type EtcdTLSClientConfig struct {
CertFile string
KeyFile string
CAFile string
}
type TLSListenerConfig struct {
Port uint32
CertFile string
KeyFile string
CAFile string
}
type GRPC struct {
Port uint16
CAFile string
CertFile string
KeyFile string
}
type Config struct {
BlackListIps []iprange.IPRange
ContainerMetricTTLSeconds int
IncomingUDPPort uint32
IncomingTCPPort uint32
EnableTLSTransport bool
TLSListenerConfig TLSListenerConfig
EtcdMaxConcurrentRequests int
EtcdUrls []string
EtcdRequireTLS bool
EtcdTLSClientConfig EtcdTLSClientConfig
Index string
JobName string
LogFilePath string
MaxRetainedLogMessages uint32
MessageDrainBufferSize uint
MetricBatchIntervalMilliseconds uint
MetronAddress string
MonitorIntervalSeconds uint
WebsocketHost string
OutgoingPort uint32
GRPC GRPC
SharedSecret string
SinkDialTimeoutSeconds int
SinkIOTimeoutSeconds int
SinkInactivityTimeoutSeconds int
SinkSkipCertVerify bool
Syslog string
UnmarshallerCount int
WebsocketWriteTimeoutSeconds int
Zone string
PPROFPort uint32
}
func (c *Config) validate() (err error) {
if c.MaxRetainedLogMessages == 0 {
return errors.New("Need max number of log messages to retain per application")
}
if c.BlackListIps != nil {
err = iprange.ValidateIpAddresses(c.BlackListIps)
if err != nil {
return err
}
}
if c.EnableTLSTransport {
if c.TLSListenerConfig.CertFile == "" || c.TLSListenerConfig.KeyFile == "" || c.TLSListenerConfig.Port == 0 {
return errors.New("invalid TLS listener configuration")
}
}
if c.EtcdRequireTLS {
if c.EtcdTLSClientConfig.CertFile == "" || c.EtcdTLSClientConfig.KeyFile == "" || c.EtcdTLSClientConfig.CAFile == "" {
return errors.New("invalid etcd TLS client configuration")
}
}
if len(c.GRPC.CAFile) == 0 {
return errors.New("invalid doppler config, no GRPC.CAFile provided")
}
if len(c.GRPC.CertFile) == 0 {
return errors.New("invalid doppler config, no GRPC.CertFile provided")
}
if len(c.GRPC.KeyFile) == 0 {
return errors.New("invalid doppler config, no GRPC.KeyFile provided")
}
return nil
}
func ParseConfig(configFile string) (*Config, error) {
file, err := os.Open(configFile)
if err != nil {
return nil, err
}
defer file.Close()
b, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
return Parse(b)
}
func Parse(confData []byte) (*Config, error) {
config := &Config{
IncomingUDPPort: 3456,
IncomingTCPPort: 3457,
}
err := json.Unmarshal(confData, config)
if err != nil {
return nil, err
}
err = config.validate()
if err != nil {
return nil, err
}
// TODO: These probably belong in the Config literal, above.
// However, in the interests of not breaking things, we're
// leaving them for further team discussion.
if config.MetricBatchIntervalMilliseconds == 0 {
config.MetricBatchIntervalMilliseconds = 5000
}
if config.MonitorIntervalSeconds == 0 {
config.MonitorIntervalSeconds = 60
}
if config.SinkDialTimeoutSeconds == 0 {
config.SinkDialTimeoutSeconds = 1
}
if config.WebsocketWriteTimeoutSeconds == 0 {
config.WebsocketWriteTimeoutSeconds = 30
}
if config.UnmarshallerCount == 0 {
config.UnmarshallerCount = 1
}
if config.EtcdMaxConcurrentRequests < 1 {
config.EtcdMaxConcurrentRequests = 1
}
if config.GRPC.Port == 0 {
config.GRPC.Port = 8082
}
return config, nil
}