-
Notifications
You must be signed in to change notification settings - Fork 136
/
config.go
445 lines (399 loc) · 13 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
package jobs
import (
"fmt"
"os"
"strconv"
"time"
"github.com/joyent/containerpilot/commands"
"github.com/joyent/containerpilot/config/decode"
"github.com/joyent/containerpilot/config/services"
"github.com/joyent/containerpilot/config/timing"
"github.com/joyent/containerpilot/discovery"
"github.com/joyent/containerpilot/events"
log "github.com/sirupsen/logrus"
)
const taskMinDuration = time.Millisecond
// Config holds the configuration for service discovery data
type Config struct {
Name string `mapstructure:"name"`
Exec interface{} `mapstructure:"exec"`
// service discovery
Port int `mapstructure:"port"`
InitialStatus string `mapstructure:"initial_status"`
Interfaces interface{} `mapstructure:"interfaces"`
Tags []string `mapstructure:"tags"`
ConsulExtras *ConsulExtras `mapstructure:"consul"`
serviceDefinition *discovery.ServiceDefinition
// health checking
Health *HealthConfig `mapstructure:"health"`
healthCheckExec *commands.Command
heartbeatInterval time.Duration
ttl int
// timeouts and restarts
ExecTimeout string `mapstructure:"timeout"`
Restarts interface{} `mapstructure:"restarts"`
StopTimeout string `mapstructure:"stopTimeout"`
execTimeout time.Duration
exec *commands.Command
stoppingTimeout time.Duration
restartLimit int
freqInterval time.Duration
// related jobs and frequency
When *WhenConfig `mapstructure:"when"`
whenEvent events.Event
whenTimeout time.Duration
whenStartsLimit int
stoppingWaitEvent events.Event
// logging
Logging *LoggingConfig `mapstructure:"logging"`
}
// WhenConfig determines when a Job runs (dependencies on other Jobs,
// Watches, or frequency timers)
type WhenConfig struct {
Frequency string `mapstructure:"interval"`
Source string `mapstructure:"source"`
Once string `mapstructure:"once"`
Each string `mapstructure:"each"`
Timeout string `mapstructure:"timeout"`
}
// HealthConfig configures the Job's health checks
type HealthConfig struct {
CheckExec interface{} `mapstructure:"exec"`
CheckTimeout string `mapstructure:"timeout"`
Heartbeat int `mapstructure:"interval"` // time in seconds
TTL int `mapstructure:"ttl"` // time in seconds
Logging *LoggingConfig `mapstructure:"logging"`
}
// ConsulExtras handles additional Consul configuration.
type ConsulExtras struct {
EnableTagOverride bool `mapstructure:"enableTagOverride"`
DeregisterCriticalServiceAfter string `mapstructure:"deregisterCriticalServiceAfter"`
}
// LoggingConfig handles job-specific logging fields
type LoggingConfig struct {
Raw bool `mapstructure:"raw"`
}
// NewConfigs parses json config into a validated slice of Configs
func NewConfigs(raw []interface{}, disc discovery.Backend) ([]*Config, error) {
var jobs []*Config
if raw == nil {
return jobs, nil
}
if err := decode.ToStruct(raw, &jobs); err != nil {
return nil, fmt.Errorf("job configuration error: %v", err)
}
stopDependencies := make(map[string]string)
for _, job := range jobs {
if err := job.Validate(disc); err != nil {
return nil, err
}
if job.whenEvent.Code == events.Stopping {
stopDependencies[job.whenEvent.Source] = job.Name
}
}
// set up any dependencies on "stopping" events
for _, job := range jobs {
if dependent, ok := stopDependencies[job.Name]; ok {
job.setStopping(dependent)
}
}
return jobs, nil
}
// Validate ensures that a Config meets all constraints
func (cfg *Config) Validate(disc discovery.Backend) error {
if err := cfg.validateDiscovery(disc); err != nil {
return err
}
if err := cfg.validateWhen(); err != nil {
return err
}
if err := cfg.validateStoppingTimeout(); err != nil {
return err
}
if err := cfg.validateRestarts(); err != nil {
return err
}
return cfg.validateExec()
}
func (cfg *Config) setStopping(name string) {
cfg.stoppingWaitEvent = events.Event{events.Stopped, name}
}
func (cfg *Config) validateDiscovery(disc discovery.Backend) error {
// setting up discovery requires the TTL from the health check first
if err := cfg.validateHealthCheck(); err != nil {
return err
}
// if port isn't set then we won't do any discovery for this job
if (cfg.Port == 0 || disc == nil) && cfg.Name != "" {
return nil
}
// we only need to validate initialStatus if we're doing discovery.
if err := cfg.valdiateInitialStatus(); err != nil {
return err
}
// we only need to validate the name if we're doing discovery;
// we'll just take the name of the exec otherwise
if err := services.ValidateName(cfg.Name); err != nil {
return err
}
return cfg.addDiscoveryConfig(disc)
}
func (cfg *Config) valdiateInitialStatus() error {
// initial status is optional
if cfg.InitialStatus == "" {
return nil
}
if cfg.InitialStatus != "passing" &&
cfg.InitialStatus != "warning" &&
cfg.InitialStatus != "critical" {
return fmt.Errorf("job[%s].initialStatus must be one of 'passing', 'warning' or 'critical'",
cfg.Name)
}
return nil
}
func (cfg *Config) validateWhen() error {
if cfg.When == nil {
// set defaults (frequencyInterval will be zero-value)
cfg.When = &WhenConfig{} // give us a safe zero-value
cfg.whenTimeout = time.Duration(0)
cfg.whenEvent = events.GlobalStartup
cfg.whenStartsLimit = 1
return nil
}
if (cfg.When.Frequency != "" && cfg.When.Once != "") ||
(cfg.When.Frequency != "" && cfg.When.Each != "") ||
(cfg.When.Once != "" && cfg.When.Each != "") {
return fmt.Errorf("job[%s].when can have only one of 'interval', 'once', or 'each'",
cfg.Name)
}
if cfg.When.Frequency != "" {
return cfg.validateFrequency()
}
return cfg.validateWhenEvent()
}
func (cfg *Config) validateFrequency() error {
freq, err := timing.ParseDuration(cfg.When.Frequency)
if err != nil {
return fmt.Errorf("unable to parse job[%s].when.interval '%s': %v",
cfg.Name, cfg.When.Frequency, err)
}
if freq < taskMinDuration {
return fmt.Errorf("job[%s].when.interval '%s' cannot be less than %v",
cfg.Name, cfg.When.Frequency, taskMinDuration)
}
cfg.freqInterval = freq
cfg.whenTimeout = time.Duration(0)
cfg.whenEvent = events.GlobalStartup
cfg.whenStartsLimit = 1
return nil
}
func (cfg *Config) validateWhenEvent() error {
whenTimeout, err := timing.GetTimeout(cfg.When.Timeout)
if err != nil {
return fmt.Errorf("unable to parse job[%s].when.timeout: %v",
cfg.Name, err)
}
cfg.whenTimeout = whenTimeout
var eventCode events.EventCode
if cfg.When.Once != "" {
eventCode, err = events.FromString(cfg.When.Once)
cfg.whenStartsLimit = 1
}
if cfg.When.Each != "" && cfg.When.Once == "" {
eventCode, err = events.FromString(cfg.When.Each)
cfg.whenStartsLimit = unlimited
}
if err != nil {
return fmt.Errorf("unable to parse job[%s].when.event: %v",
cfg.Name, err)
}
if cfg.When.Source == "SIGHUP" || cfg.When.Source == "SIGUSR2" {
eventCode = events.Signal
cfg.whenStartsLimit = unlimited
}
cfg.whenEvent = events.Event{eventCode, cfg.When.Source}
return nil
}
func (cfg *Config) validateStoppingTimeout() error {
stoppingTimeout, err := timing.GetTimeout(cfg.StopTimeout)
if err != nil {
return fmt.Errorf("unable to parse job[%s].stopTimeout '%s': %v",
cfg.Name, cfg.StopTimeout, err)
}
cfg.stoppingTimeout = stoppingTimeout
cfg.stoppingWaitEvent = events.NonEvent
return nil
}
func (cfg *Config) validateExec() error {
if cfg.ExecTimeout == "" && cfg.freqInterval != 0 {
// periodic tasks require a timeout
cfg.execTimeout = cfg.freqInterval
}
if cfg.ExecTimeout != "" {
execTimeout, err := timing.GetTimeout(cfg.ExecTimeout)
if err != nil {
return fmt.Errorf("unable to parse job[%s].timeout '%s': %v",
cfg.Name, cfg.ExecTimeout, err)
}
if execTimeout < time.Duration(1*time.Millisecond) {
// if there's no timeout set, that's ok, but if we have a timeout
// set we need to make sure it's functional
return fmt.Errorf("job[%s].timeout '%v' cannot be less than 1ms",
cfg.Name, cfg.ExecTimeout)
}
cfg.execTimeout = execTimeout
}
if cfg.Exec != nil {
fields := log.Fields{"job": cfg.Name}
if cfg.Logging != nil && cfg.Logging.Raw {
fields = nil
}
cmd, err := commands.NewCommand(cfg.Exec, cfg.execTimeout, fields)
if err != nil {
return fmt.Errorf("unable to create job[%s].exec: %v", cfg.Name, err)
}
if cfg.Name == "" {
cfg.Name = cmd.Exec
}
cmd.Name = cfg.Name
cfg.exec = cmd
}
return nil
}
func (cfg *Config) validateHealthCheck() error {
if cfg.Port != 0 && cfg.Health == nil && cfg.Name != "containerpilot" {
return fmt.Errorf("job[%s].health must be set if 'port' is set", cfg.Name)
}
if cfg.Health == nil {
return nil // non-advertised jobs don't need health checks
}
if cfg.Health.Heartbeat < 1 {
return fmt.Errorf("job[%s].health.interval must be > 0", cfg.Name)
}
if cfg.Health.TTL < 1 {
return fmt.Errorf("job[%s].health.ttl must be > 0", cfg.Name)
}
cfg.ttl = cfg.Health.TTL
cfg.heartbeatInterval = time.Duration(cfg.Health.Heartbeat) * time.Second
var checkTimeout time.Duration
if cfg.Health.CheckTimeout != "" {
parsedTimeout, err := timing.GetTimeout(cfg.Health.CheckTimeout)
if err != nil {
return fmt.Errorf("could not parse job[%s].health.timeout '%s': %v",
cfg.Name, cfg.Health.CheckTimeout, err)
}
checkTimeout = parsedTimeout
} else {
checkTimeout = cfg.heartbeatInterval
}
if cfg.Health.CheckExec != nil {
// the telemetry service won't have a health check
checkName := "check." + cfg.Name
fields := log.Fields{"check": checkName}
if cfg.Health.Logging != nil && cfg.Health.Logging.Raw {
fields = nil
}
log.Debugf("job[%s].health.exec fields: %v", cfg.Name, fields)
cmd, err := commands.NewCommand(cfg.Health.CheckExec, checkTimeout, fields)
if err != nil {
return fmt.Errorf("unable to create job[%s].health.exec: %v",
cfg.Name, err)
}
cmd.Name = checkName
cfg.healthCheckExec = cmd
}
return nil
}
func (cfg *Config) validateRestarts() error {
// defaults if omitted
if cfg.Restarts == nil {
if cfg.freqInterval != time.Duration(0) {
cfg.restartLimit = unlimited
} else {
cfg.restartLimit = 0
}
return nil
}
const msg = `job[%s].restarts field '%v' invalid: %v`
switch t := cfg.Restarts.(type) {
case string:
if t == "unlimited" {
if cfg.When.Each != "" {
return fmt.Errorf(msg, cfg.Name, cfg.Restarts,
`may not be used when 'job.when.each' is set because it may result in infinite processes`)
}
cfg.restartLimit = unlimited
} else if t == "never" {
cfg.restartLimit = 0
} else if i, err := strconv.Atoi(t); err == nil && i >= 0 {
cfg.restartLimit = i
} else {
return fmt.Errorf(msg, cfg.Name, cfg.Restarts,
`accepts positive integers, "unlimited", or "never"`)
}
case float64, int:
// mapstructure can figure out how to decode strings into int fields
// but doesn't try to guess and just gives us a float64 if it's got
// a number that it's decoding to an interface{}. We'll only return
// an error if we can't cast it to an int. This means that an end-user
// can pass in `restarts: 1.2` and have undocumented truncation but the
// wtf would be all on them at that point.
if i, ok := t.(int); ok && i >= 0 {
cfg.restartLimit = i
} else if i, ok := t.(float64); ok && i >= 0 {
cfg.restartLimit = int(i)
} else {
return fmt.Errorf(msg, cfg.Name, cfg.Restarts,
`number must be positive integer`)
}
default:
return fmt.Errorf(msg, cfg.Name, cfg.Restarts,
`accepts positive integers, "unlimited", or "never"`)
}
return nil
}
// addDiscoveryConfig validates the configuration for service discovery
// and attaches the discovery.ServiceDefinition to the Config
func (cfg *Config) addDiscoveryConfig(disc discovery.Backend) error {
interfaces, ifaceErr := decode.ToStrings(cfg.Interfaces)
if ifaceErr != nil {
return ifaceErr
}
ipAddress, err := services.GetIP(interfaces)
if err != nil {
return err
}
hostname, _ := os.Hostname()
id := fmt.Sprintf("%s-%s", cfg.Name, hostname)
var (
enableTagOverride bool
deregAfter string
)
if cfg.ConsulExtras != nil {
deregAfter = cfg.ConsulExtras.DeregisterCriticalServiceAfter
_, err := time.ParseDuration(deregAfter)
if err != nil {
return fmt.Errorf(
"unable to parse job[%s].consul.deregisterCriticalServiceAfter: %s",
cfg.Name, err)
}
enableTagOverride = cfg.ConsulExtras.EnableTagOverride
}
cfg.serviceDefinition = &discovery.ServiceDefinition{
ID: id,
Name: cfg.Name,
Port: cfg.Port,
TTL: cfg.ttl,
Tags: cfg.Tags,
InitialStatus: cfg.InitialStatus,
IPAddress: ipAddress,
DeregisterCriticalServiceAfter: deregAfter,
EnableTagOverride: enableTagOverride,
Consul: disc,
}
return nil
}
// String implements the stdlib fmt.Stringer interface for pretty-printing
func (cfg *Config) String() string {
return "jobs.Config[" + cfg.Name + "]"
}