-
Notifications
You must be signed in to change notification settings - Fork 21
/
config.go
240 lines (210 loc) · 6.05 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
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package caduceus
import (
"fmt"
"os"
"time"
"github.com/goschtalt/goschtalt"
"github.com/xmidt-org/arrange/arrangehttp"
"github.com/xmidt-org/arrange/arrangepprof"
"github.com/xmidt-org/bascule"
"github.com/xmidt-org/caduceus/internal/sink"
"github.com/xmidt-org/candlelight"
"github.com/xmidt-org/clortho"
"github.com/xmidt-org/sallust"
"github.com/xmidt-org/touchstone"
"gopkg.in/dealancer/validate.v2"
)
// Config is the top level configuration for the caduceus service. Everything
// is contained in this structure or it will intentially cause a failure.
type Config struct {
Logging sallust.Config
Tracing candlelight.Config
Prometheus touchstone.Config
Servers Servers
ArgusClientTimeout HttpClientTimeout
JWTValidator JWTValidator
Sender sink.Config
Service Service
AuthHeader []string
Server string
FQDN string
Build string
Flavor string
PreviousVersionSupport bool
Region string
// Webhook ancla.Config //@TODO: need to fix the ancla/argus dependency issue
}
type Service struct {
Consul
}
type Consul struct {
Client ConsulClient
Registrations []ConsulRegistration
DisableGenerateId bool
}
type ConsulClient struct {
Address string
Scheme string
WaitTime string
}
type ConsulRegistration struct {
Id string
Name string
Tags []string
Address string
Port int
Checks []Check
}
type Check struct {
CheckId string
Ttl string
DeregisterCriticalServiceAfter string
}
type Servers struct {
Health HealthServer
Metrics MetricsServer
Pprof PprofServer
Primary PrimaryServer
Alternate PrimaryServer
}
type HealthServer struct {
HTTP arrangehttp.ServerConfig
Path HealthPath `validate:"empty=false"`
}
type HealthPath string
type MetricsServer struct {
HTTP arrangehttp.ServerConfig
MetricsOptions MetricsOption
Path MetricsPath `validate:"empty=false"`
}
type MetricsPath string
type PrimaryServer struct {
HTTP arrangehttp.ServerConfig
}
type PprofServer struct {
HTTP arrangehttp.ServerConfig
Path PprofPathPrefix
}
type PprofPathPrefix string
// httpClientTimeout contains timeouts for an HTTP client and its requests.
type HttpClientTimeout struct {
// ClientTimeout is HTTP Client Timeout.
ClientTimeout time.Duration
// NetDialerTimeout is the net dialer timeout
NetDialerTimeout time.Duration
}
type MetricsOption struct {
Namespace string
Subsystem string
}
// JWTValidator provides a convenient way to define jwt validator through config files
type JWTValidator struct {
// Config is used to create the clortho Resolver & Refresher for JWT verification keys
Config clortho.Config `json:"config"`
// Leeway is used to set the amount of time buffer should be given to JWT
// time values, such as nbf
Leeway bascule.Leeway
}
// Collect and process the configuration files and env vars and
// produce a configuration object.
func provideConfig(cli *CLI) (*goschtalt.Config, error) {
gs, err := goschtalt.New(
goschtalt.StdCfgLayout(applicationName, cli.Files...),
goschtalt.ConfigIs("twoWords"),
goschtalt.DefaultUnmarshalOptions(
goschtalt.WithValidator(
goschtalt.ValidatorFunc(validate.Validate),
),
),
// Seed the program with the default, built-in configuration.
// Mark this as a default so it is ordered correctly.
goschtalt.AddValue("built-in", goschtalt.Root, defaultConfig,
goschtalt.AsDefault()),
)
if err != nil {
return nil, err
}
if cli.Show {
// handleCLIShow handles the -s/--show option where the configuration is
// shown, then the program is exited.
//
// Exit with success because if the configuration is broken it will be
// very hard to debug where the problem originates. This way you can
// see the configuration and then run the service with the same
// configuration to see the error.
fmt.Fprintln(os.Stdout, gs.Explain().String())
out, err := gs.Marshal()
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
fmt.Fprintln(os.Stdout, "## Final Configuration\n---\n"+string(out))
}
os.Exit(0)
}
var tmp Config
err = gs.Unmarshal(goschtalt.Root, &tmp)
if err != nil {
fmt.Fprintln(os.Stderr, "There is a critical error in the configuration.")
fmt.Fprintln(os.Stderr, "Run with -s/--show to see the configuration.")
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
// Exit here to prevent a very difficult to debug error from occurring.
os.Exit(-1)
}
return gs, nil
}
// -----------------------------------------------------------------------------
// Keep the default configuration at the bottom of the file so it is easy to
// see what the default configuration is.
// -----------------------------------------------------------------------------
// TODO: update default values to match what's expected of caduceus
var defaultConfig = Config{
Servers: Servers{
Health: HealthServer{
HTTP: arrangehttp.ServerConfig{
Network: "tcp",
Address: ":80",
},
Path: HealthPath("/health"),
},
Metrics: MetricsServer{
HTTP: arrangehttp.ServerConfig{
Network: "tcp",
Address: "127.0.0.1:9361",
},
Path: MetricsPath("/metrics"),
},
Pprof: PprofServer{
HTTP: arrangehttp.ServerConfig{
Network: "tcp",
Address: "127.0.0.1:9999",
},
Path: arrangepprof.DefaultPathPrefix,
},
Primary: PrimaryServer{
HTTP: arrangehttp.ServerConfig{
Network: "tcp",
Address: ":443",
},
},
Alternate: PrimaryServer{
HTTP: arrangehttp.ServerConfig{
Network: "tcp",
Address: "127.0.0.1:8443",
},
},
},
Prometheus: touchstone.Config{
DefaultNamespace: "xmidt",
DefaultSubsystem: "caduceus",
},
Tracing: candlelight.Config{
ApplicationName: applicationName,
},
Sender: sink.Config{
Linger: 180,
CutOffPeriod: 10,
NumWorkersPerSender: 5000,
},
}