This repository was archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathwatch.go
More file actions
401 lines (346 loc) · 9.52 KB
/
watch.go
File metadata and controls
401 lines (346 loc) · 9.52 KB
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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"math"
"net"
"os"
"os/exec"
"reflect"
"runtime"
"sync"
"text/template"
"time"
"github.com/armon/consul-api"
)
const (
// failSleep controls how long to sleep on a failure
failSleep = 5 * time.Second
// maxFailures controls the maximum number of failures
// before we limit the sleep value
maxFailures = 5
// waitTime is used to control how long we do a blocking
// query for
waitTime = 60 * time.Second
)
type backendData struct {
sync.Mutex
// Client is a shared Consul client
Client *consulapi.Client
// Servers maps each watch path to a list of entries
Servers map[*WatchPath][]*consulapi.ServiceEntry
// Backends maps a backend to a list of watch paths used
// to build up the server list
Backends map[string][]*WatchPath
// ChangeCh is used to inform of an update
ChangeCh chan struct{}
// StopCh is used to trigger a stop
StopCh chan struct{}
// quietTimer is used to wati for quiescence
quietTimer <-chan time.Time
// maxWaitTimer is used to prevent unbounded waiting
// for quiescence
maxWaitTimer <-chan time.Time
}
// watch is used to start a long running watcher to handle updates.
// Returns a stopCh, and a finishCh.
func watch(conf *Config) (chan struct{}, chan struct{}) {
stopCh := make(chan struct{})
finishCh := make(chan struct{})
go runWatch(conf, stopCh, finishCh)
return stopCh, finishCh
}
// runWatch is a long running routine that watches with a
// given configuration
func runWatch(conf *Config, stopCh, doneCh chan struct{}) {
defer close(doneCh)
// Create the consul client
consulConf := consulapi.DefaultConfig()
if conf.Address != "" {
consulConf.Address = conf.Address
}
// Attempt to contact the agent
client, err := consulapi.NewClient(consulConf)
if err != nil {
log.Printf("[ERR] Failed to initialize consul client: %v", err)
return
}
if _, err := client.Agent().NodeName(); err != nil {
log.Printf("[ERR] Failed to contact consul agent: %v", err)
return
}
// Create a backend store
data := &backendData{
Client: client,
Servers: make(map[*WatchPath][]*consulapi.ServiceEntry),
Backends: make(map[string][]*WatchPath),
ChangeCh: make(chan struct{}, 1),
StopCh: stopCh,
}
// Start the watches
data.Lock()
for idx, watch := range conf.watches {
data.Backends[watch.Backend] = append(data.Backends[watch.Backend], watch)
go runSingleWatch(conf, data, idx, watch)
}
data.Unlock()
// Monitor for changes or stop
for {
select {
case <-data.ChangeCh:
if maybeRefresh(conf, data) {
return
}
case <-data.quietTimer:
data.quietTimer = nil
data.maxWaitTimer = nil
if forceRefresh(conf, data) {
return
}
case <-data.maxWaitTimer:
data.quietTimer = nil
data.maxWaitTimer = nil
if forceRefresh(conf, data) {
return
}
case <-stopCh:
return
}
}
}
// maybeRefresh is used to handle a potential config update
func maybeRefresh(conf *Config, data *backendData) (exit bool) {
// Ignore initial updates until all the data is ready
if !allWatchesReturned(conf, data) {
return
}
// If a quiet period is enabled, start the timer
if conf.Quiet != 0 {
data.quietTimer = time.After(conf.Quiet)
if data.maxWaitTimer == nil {
data.maxWaitTimer = time.After(conf.MaxWait)
}
return
}
return forceRefresh(conf, data)
}
// forceRefresh is used to immediately refresh
func forceRefresh(conf *Config, data *backendData) (exit bool) {
// Merge the data for each backend
backendServers := aggregateServers(data)
// Iterate through the list of templates to render
for idx, templatePath := range conf.Templates {
// Build the output template
output, err := buildTemplate(templatePath, backendServers)
if err != nil {
log.Printf("[ERR] %v", err)
return true
}
// Check for a dry run
if conf.DryRun {
fmt.Printf("%s\n", output)
return true
}
// Write out the configuration
if err := ioutil.WriteFile(conf.Paths[idx], output, 0660); err != nil {
log.Printf("[ERR] Failed to write config file at %s: %v", conf.Paths[idx], err)
return true
}
log.Printf("[INFO] Updated configuration file at %s", conf.Paths[idx])
}
// Invoke the reload hook
if err := reload(conf); err != nil {
log.Printf("[ERR] Failed to reload: %v", err)
} else {
log.Printf("[INFO] Completed reload")
}
return
}
// allWatchesReturned checks if all the watches have some
// data registered. Prevents early template generation.
func allWatchesReturned(conf *Config, data *backendData) bool {
data.Lock()
defer data.Unlock()
return len(data.Servers) >= len(conf.watches)
}
// aggregateServers merges the watches belonging to each
// backend together to prepare for template generation
func aggregateServers(data *backendData) map[string][]*consulapi.ServiceEntry {
backendServers := make(map[string][]*consulapi.ServiceEntry)
data.Lock()
defer data.Unlock()
for backend, watches := range data.Backends {
var all []*consulapi.ServiceEntry
for _, watch := range watches {
entries := data.Servers[watch]
all = append(all, entries...)
}
backendServers[backend] = all
}
return backendServers
}
// buildTemplate is used to build the output templates
// from the configuration and server list
func buildTemplate(templatePath string,
servers map[string][]*consulapi.ServiceEntry) ([]byte, error) {
// Format the output
outVars := formatOutput(servers)
// Read the template
raw, err := ioutil.ReadFile(templatePath)
if err != nil {
return nil, fmt.Errorf("Failed to read template: %v", err)
}
// Create the template
templ, err := template.New("output").Parse(string(raw))
if err != nil {
return nil, fmt.Errorf("Failed to parse the template: %v", err)
}
// Generate the output
var output bytes.Buffer
if err := templ.Execute(&output, outVars); err != nil {
return nil, fmt.Errorf("Failed to generate the template: %v", err)
}
return output.Bytes(), nil
}
// runSingleWatch is used to query a single watch path for changes
func runSingleWatch(conf *Config, data *backendData, idx int, watch *WatchPath) {
health := data.Client.Health()
opts := &consulapi.QueryOptions{
WaitTime: waitTime,
}
if watch.Datacenter != "" {
opts.Datacenter = watch.Datacenter
}
failures := 0
for {
if shouldStop(data.StopCh) {
return
}
entries, qm, err := health.Service(watch.Service, watch.Tag, true, opts)
if err != nil {
log.Printf("[ERR] Failed to fetch service nodes: %v", err)
}
// Patch the entries as necessary
for _, entry := range entries {
// Modify the node name to prefix with the watch ID. This
// prevents a name conflict on duplicate names
entry.Node.Node = fmt.Sprintf("%d_%s", idx, entry.Node.Node)
// Patch the port if provided
if watch.Port != 0 {
entry.Service.Port = watch.Port
}
// Clear the health output to prevent reloading due to changes
// in output text since we don't care.
for _, c := range entry.Checks {
c.Notes = ""
c.Output = ""
}
}
// Update the entries. If this is the first read, do it on error
data.Lock()
old, ok := data.Servers[watch]
if !ok || (err == nil && !reflect.DeepEqual(old, entries)) {
data.Servers[watch] = entries
asyncNotify(data.ChangeCh)
if !conf.DryRun {
log.Printf("[DEBUG] Updated nodes for %v", watch.Spec)
}
}
data.Unlock()
// Stop immediately on a dry run
if conf.DryRun {
return
}
// Check for an error
if err != nil {
failures = min(failures+1, maxFailures)
time.Sleep(backoff(failSleep, failures))
} else {
failures = 0
opts.WaitIndex = qm.LastIndex
}
}
}
// reload is used to invoke the reload command
func reload(conf *Config) error {
// Determine the shell invocation based on OS
var shell, flag string
if runtime.GOOS == "windows" {
shell = "cmd"
flag = "/C"
} else {
shell = "/bin/sh"
flag = "-c"
}
// Create and invoke the command
cmd := exec.Command(shell, flag, conf.ReloadCommand)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// shouldStop checks for a closed control channel
func shouldStop(ch chan struct{}) bool {
select {
case <-ch:
return true
default:
return false
}
}
// asyncNotify is used to notify a channel
func asyncNotify(ch chan struct{}) {
select {
case ch <- struct{}{}:
default:
}
}
// min returns the min of two ints
func min(a, b int) int {
if a < b {
return a
}
return b
}
// backoff is used to compute an exponential backoff
func backoff(interval time.Duration, times int) time.Duration {
times--
return interval * time.Duration(math.Pow(2, float64(times)))
}
// ServerEntry is the full data structure exposed to
// the template for each server
type ServerEntry struct {
ID string
Service string
Tags []string
Port int
IP net.IP
Node string
}
// String is the default text representation of a server
func (se *ServerEntry) String() string {
name := fmt.Sprintf("%s_%s", se.Node, se.ID)
addr := &net.TCPAddr{IP: se.IP, Port: se.Port}
return fmt.Sprintf("server %s %s", name, addr)
}
// formatOutput converts the service entries into a format
// suitable for templating into the HAProxy file
func formatOutput(inp map[string][]*consulapi.ServiceEntry) map[string][]*ServerEntry {
out := make(map[string][]*ServerEntry)
for backend, entries := range inp {
servers := make([]*ServerEntry, len(entries))
for idx, entry := range entries {
servers[idx] = &ServerEntry{
ID: entry.Service.ID,
Service: entry.Service.Service,
Tags: entry.Service.Tags,
Port: entry.Service.Port,
IP: net.ParseIP(entry.Node.Address),
Node: entry.Node.Node,
}
}
out[backend] = servers
}
return out
}