7
7
8
8
"github.com/hashicorp/consul/api"
9
9
"github.com/hashicorp/consul/command/connect/proxy"
10
- log "github.com/sirupsen/logrus"
11
10
)
12
11
13
12
const (
@@ -58,16 +57,19 @@ type Watcher struct {
58
57
leaf * certLeaf
59
58
60
59
update chan struct {}
60
+ log Logger
61
61
}
62
62
63
- func New (service string , consul * api.Client ) * Watcher {
63
+ // New builds a new watcher
64
+ func New (service string , consul * api.Client , log Logger ) * Watcher {
64
65
return & Watcher {
65
66
service : service ,
66
67
consul : consul ,
67
68
68
69
C : make (chan Config ),
69
70
upstreams : make (map [string ]* upstream ),
70
71
update : make (chan struct {}, 1 ),
72
+ log : log ,
71
73
}
72
74
}
73
75
@@ -144,7 +146,7 @@ func (w *Watcher) handleProxyChange(first bool, srv *api.AgentService) {
144
146
}
145
147
146
148
func (w * Watcher ) startUpstream (up api.Upstream ) {
147
- log .Infof ("consul: watching upstream for service %s" , up .DestinationName )
149
+ w . log .Infof ("consul: watching upstream for service %s" , up .DestinationName )
148
150
149
151
u := & upstream {
150
152
LocalBindAddress : up .LocalBindAddress ,
@@ -169,7 +171,7 @@ func (w *Watcher) startUpstream(up api.Upstream) {
169
171
WaitIndex : index ,
170
172
})
171
173
if err != nil {
172
- log .Errorf ("consul: error fetching service definition for service %s: %s" , up .DestinationName , err )
174
+ w . log .Errorf ("consul: error fetching service definition for service %s: %s" , up .DestinationName , err )
173
175
time .Sleep (errorWaitTime )
174
176
index = 0
175
177
continue
@@ -188,7 +190,7 @@ func (w *Watcher) startUpstream(up api.Upstream) {
188
190
}
189
191
190
192
func (w * Watcher ) removeUpstream (name string ) {
191
- log .Infof ("consul: removing upstream for service %s" , name )
193
+ w . log .Infof ("consul: removing upstream for service %s" , name )
192
194
193
195
w .lock .Lock ()
194
196
w .upstreams [name ].done = true
@@ -197,7 +199,7 @@ func (w *Watcher) removeUpstream(name string) {
197
199
}
198
200
199
201
func (w * Watcher ) watchLeaf () {
200
- log .Debugf ("consul: watching leaf cert for %s" , w .serviceName )
202
+ w . log .Debugf ("consul: watching leaf cert for %s" , w .serviceName )
201
203
202
204
var lastIndex uint64
203
205
first := true
@@ -207,7 +209,7 @@ func (w *Watcher) watchLeaf() {
207
209
WaitIndex : lastIndex ,
208
210
})
209
211
if err != nil {
210
- log .Errorf ("consul error fetching leaf cert for service %s: %s" , w .serviceName , err )
212
+ w . log .Errorf ("consul error fetching leaf cert for service %s: %s" , w .serviceName , err )
211
213
time .Sleep (errorWaitTime )
212
214
lastIndex = 0
213
215
continue
@@ -217,7 +219,7 @@ func (w *Watcher) watchLeaf() {
217
219
lastIndex = meta .LastIndex
218
220
219
221
if changed {
220
- log .Infof ("consul: leaf cert for service %s changed, serial: %s, valid before: %s, valid after: %s" , w .serviceName , cert .SerialNumber , cert .ValidBefore , cert .ValidAfter )
222
+ w . log .Infof ("consul: leaf cert for service %s changed, serial: %s, valid before: %s, valid after: %s" , w .serviceName , cert .SerialNumber , cert .ValidBefore , cert .ValidAfter )
221
223
w .lock .Lock ()
222
224
if w .leaf == nil {
223
225
w .leaf = & certLeaf {}
@@ -229,15 +231,15 @@ func (w *Watcher) watchLeaf() {
229
231
}
230
232
231
233
if first {
232
- log .Infof ("consul: leaf cert for %s ready" , w .serviceName )
234
+ w . log .Infof ("consul: leaf cert for %s ready" , w .serviceName )
233
235
w .ready .Done ()
234
236
first = false
235
237
}
236
238
}
237
239
}
238
240
239
241
func (w * Watcher ) watchService (service string , handler func (first bool , srv * api.AgentService )) {
240
- log .Infof ("consul: watching service %s" , service )
242
+ w . log .Infof ("consul: watching service %s" , service )
241
243
242
244
hash := ""
243
245
first := true
@@ -247,7 +249,7 @@ func (w *Watcher) watchService(service string, handler func(first bool, srv *api
247
249
WaitTime : 10 * time .Minute ,
248
250
})
249
251
if err != nil {
250
- log .Errorf ("consul: error fetching service %s definition: %s" , service , err )
252
+ w . log .Errorf ("consul: error fetching service %s definition: %s" , service , err )
251
253
time .Sleep (errorWaitTime )
252
254
hash = ""
253
255
continue
@@ -257,7 +259,7 @@ func (w *Watcher) watchService(service string, handler func(first bool, srv *api
257
259
hash = meta .LastContentHash
258
260
259
261
if changed {
260
- log .Debugf ("consul: service %s changed" , service )
262
+ w . log .Debugf ("consul: service %s changed" , service )
261
263
handler (first , srv )
262
264
w .notifyChanged ()
263
265
}
@@ -267,7 +269,7 @@ func (w *Watcher) watchService(service string, handler func(first bool, srv *api
267
269
}
268
270
269
271
func (w * Watcher ) watchCA () {
270
- log .Debugf ("consul: watching ca certs" )
272
+ w . log .Debugf ("consul: watching ca certs" )
271
273
272
274
first := true
273
275
var lastIndex uint64
@@ -277,7 +279,7 @@ func (w *Watcher) watchCA() {
277
279
WaitTime : 10 * time .Minute ,
278
280
})
279
281
if err != nil {
280
- log .Errorf ("consul: error fetching cas: %s" , err )
282
+ w . log .Errorf ("consul: error fetching cas: %s" , err )
281
283
time .Sleep (errorWaitTime )
282
284
lastIndex = 0
283
285
continue
@@ -287,37 +289,37 @@ func (w *Watcher) watchCA() {
287
289
lastIndex = meta .LastIndex
288
290
289
291
if changed {
290
- log .Infof ("consul: CA certs changed, active root id: %s" , caList .ActiveRootID )
292
+ w . log .Infof ("consul: CA certs changed, active root id: %s" , caList .ActiveRootID )
291
293
w .lock .Lock ()
292
294
w .certCAs = w .certCAs [:0 ]
293
295
w .certCAPool = x509 .NewCertPool ()
294
296
for _ , ca := range caList .Roots {
295
297
w .certCAs = append (w .certCAs , []byte (ca .RootCertPEM ))
296
298
ok := w .certCAPool .AppendCertsFromPEM ([]byte (ca .RootCertPEM ))
297
299
if ! ok {
298
- log .Warn ("consul: unable to add CA certificate to pool" )
300
+ w . log .Warnf ("consul: unable to add CA certificate to pool for root id: %s" , caList . ActiveRootID )
299
301
}
300
302
}
301
303
w .lock .Unlock ()
302
304
w .notifyChanged ()
303
305
}
304
306
305
307
if first {
306
- log .Infof ("consul: CA certs ready" )
308
+ w . log .Infof ("consul: CA certs ready" )
307
309
w .ready .Done ()
308
310
first = false
309
311
}
310
312
}
311
313
}
312
314
313
315
func (w * Watcher ) genCfg () Config {
314
- log .Debug ("generating configuration..." )
316
+ w . log .Debugf ("generating configuration for service %s[%s] ..." , w . serviceName , w . service )
315
317
w .lock .Lock ()
316
318
serviceInstancesAlive := 0
317
319
serviceInstancesTotal := 0
318
320
defer func () {
319
321
w .lock .Unlock ()
320
- log .Debugf ("done generating configuration, instances: %d/%d total" ,
322
+ w . log .Debugf ("done generating configuration, instances: %d/%d total" ,
321
323
serviceInstancesAlive , serviceInstancesTotal )
322
324
}()
323
325
0 commit comments