Skip to content

Commit f121d6e

Browse files
committed
Use interface to log in Consul package
This will allow to replace log implementation by testing one to include tests into the Consul repository without adding a new dependency
1 parent 556c75e commit f121d6e

File tree

6 files changed

+92
-21
lines changed

6 files changed

+92
-21
lines changed

consul/logger.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package consul
2+
3+
// Logger Allows replacing easily the logger.
4+
type Logger interface {
5+
// Debugf Display debug message
6+
Debugf(format string, args ...interface{})
7+
// Infof Display info message
8+
Infof(format string, args ...interface{})
9+
// Warnf Display warning message
10+
Warnf(format string, args ...interface{})
11+
// Errorf Display error message
12+
Errorf(format string, args ...interface{})
13+
}

consul/logger_testing.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package consul
2+
3+
import "testing"
4+
5+
type testingLogger struct {
6+
t *testing.T
7+
}
8+
9+
// Debugf Display debug message
10+
func (l *testingLogger) Debugf(format string, args ...interface{}) {
11+
l.t.Logf(format, args...)
12+
}
13+
14+
// Infof Display info message
15+
func (l *testingLogger) Infof(format string, args ...interface{}) {
16+
l.t.Logf(format, args...)
17+
}
18+
19+
// Warnf Display warning message
20+
func (l *testingLogger) Warnf(format string, args ...interface{}) {
21+
l.t.Logf(format, args...)
22+
}
23+
24+
// Errorf Display error message
25+
func (l *testingLogger) Errorf(format string, args ...interface{}) {
26+
l.t.Logf(format, args...)
27+
}
28+
29+
// NewTestingLogger creates a Logger for testing.T
30+
func NewTestingLogger(t *testing.T) Logger {
31+
return &testingLogger{t: t}
32+
}

consul/watcher.go

+21-19
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/hashicorp/consul/api"
99
"github.com/hashicorp/consul/command/connect/proxy"
10-
log "github.com/sirupsen/logrus"
1110
)
1211

1312
const (
@@ -58,16 +57,19 @@ type Watcher struct {
5857
leaf *certLeaf
5958

6059
update chan struct{}
60+
log Logger
6161
}
6262

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 {
6465
return &Watcher{
6566
service: service,
6667
consul: consul,
6768

6869
C: make(chan Config),
6970
upstreams: make(map[string]*upstream),
7071
update: make(chan struct{}, 1),
72+
log: log,
7173
}
7274
}
7375

@@ -144,7 +146,7 @@ func (w *Watcher) handleProxyChange(first bool, srv *api.AgentService) {
144146
}
145147

146148
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)
148150

149151
u := &upstream{
150152
LocalBindAddress: up.LocalBindAddress,
@@ -169,7 +171,7 @@ func (w *Watcher) startUpstream(up api.Upstream) {
169171
WaitIndex: index,
170172
})
171173
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)
173175
time.Sleep(errorWaitTime)
174176
index = 0
175177
continue
@@ -188,7 +190,7 @@ func (w *Watcher) startUpstream(up api.Upstream) {
188190
}
189191

190192
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)
192194

193195
w.lock.Lock()
194196
w.upstreams[name].done = true
@@ -197,7 +199,7 @@ func (w *Watcher) removeUpstream(name string) {
197199
}
198200

199201
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)
201203

202204
var lastIndex uint64
203205
first := true
@@ -207,7 +209,7 @@ func (w *Watcher) watchLeaf() {
207209
WaitIndex: lastIndex,
208210
})
209211
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)
211213
time.Sleep(errorWaitTime)
212214
lastIndex = 0
213215
continue
@@ -217,7 +219,7 @@ func (w *Watcher) watchLeaf() {
217219
lastIndex = meta.LastIndex
218220

219221
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)
221223
w.lock.Lock()
222224
if w.leaf == nil {
223225
w.leaf = &certLeaf{}
@@ -229,15 +231,15 @@ func (w *Watcher) watchLeaf() {
229231
}
230232

231233
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)
233235
w.ready.Done()
234236
first = false
235237
}
236238
}
237239
}
238240

239241
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)
241243

242244
hash := ""
243245
first := true
@@ -247,7 +249,7 @@ func (w *Watcher) watchService(service string, handler func(first bool, srv *api
247249
WaitTime: 10 * time.Minute,
248250
})
249251
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)
251253
time.Sleep(errorWaitTime)
252254
hash = ""
253255
continue
@@ -257,7 +259,7 @@ func (w *Watcher) watchService(service string, handler func(first bool, srv *api
257259
hash = meta.LastContentHash
258260

259261
if changed {
260-
log.Debugf("consul: service %s changed", service)
262+
w.log.Debugf("consul: service %s changed", service)
261263
handler(first, srv)
262264
w.notifyChanged()
263265
}
@@ -267,7 +269,7 @@ func (w *Watcher) watchService(service string, handler func(first bool, srv *api
267269
}
268270

269271
func (w *Watcher) watchCA() {
270-
log.Debugf("consul: watching ca certs")
272+
w.log.Debugf("consul: watching ca certs")
271273

272274
first := true
273275
var lastIndex uint64
@@ -277,7 +279,7 @@ func (w *Watcher) watchCA() {
277279
WaitTime: 10 * time.Minute,
278280
})
279281
if err != nil {
280-
log.Errorf("consul: error fetching cas: %s", err)
282+
w.log.Errorf("consul: error fetching cas: %s", err)
281283
time.Sleep(errorWaitTime)
282284
lastIndex = 0
283285
continue
@@ -287,37 +289,37 @@ func (w *Watcher) watchCA() {
287289
lastIndex = meta.LastIndex
288290

289291
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)
291293
w.lock.Lock()
292294
w.certCAs = w.certCAs[:0]
293295
w.certCAPool = x509.NewCertPool()
294296
for _, ca := range caList.Roots {
295297
w.certCAs = append(w.certCAs, []byte(ca.RootCertPEM))
296298
ok := w.certCAPool.AppendCertsFromPEM([]byte(ca.RootCertPEM))
297299
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)
299301
}
300302
}
301303
w.lock.Unlock()
302304
w.notifyChanged()
303305
}
304306

305307
if first {
306-
log.Infof("consul: CA certs ready")
308+
w.log.Infof("consul: CA certs ready")
307309
w.ready.Done()
308310
first = false
309311
}
310312
}
311313
}
312314

313315
func (w *Watcher) genCfg() Config {
314-
log.Debug("generating configuration...")
316+
w.log.Debugf("generating configuration for service %s[%s]...", w.serviceName, w.service)
315317
w.lock.Lock()
316318
serviceInstancesAlive := 0
317319
serviceInstancesTotal := 0
318320
defer func() {
319321
w.lock.Unlock()
320-
log.Debugf("done generating configuration, instances: %d/%d total",
322+
w.log.Debugf("done generating configuration, instances: %d/%d total",
321323
serviceInstancesAlive, serviceInstancesTotal)
322324
}()
323325

haproxy/haproxy_cmd/run.go

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func Start(sd *lib.Shutdown, cfg Config) (*dataplane.Dataplane, error) {
6969

7070
err = dataplaneClient.Ping()
7171
if err != nil {
72+
fmt.Println("*****\n* SOUCHAY: wait for dataplane to be up\n*****")
7273
time.Sleep(100 * time.Millisecond)
7374
continue
7475
}

main.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,28 @@ import (
1414
"github.com/criteo/haproxy-consul-connect/consul"
1515
)
1616

17+
type consulLogger struct{}
18+
19+
// Debugf Display debug message
20+
func (consulLogger) Debugf(format string, args ...interface{}) {
21+
log.Debugf(format, args...)
22+
}
23+
24+
// Infof Display info message
25+
func (consulLogger) Infof(format string, args ...interface{}) {
26+
log.Infof(format, args...)
27+
}
28+
29+
// Warnf Display warning message
30+
func (consulLogger) Warnf(format string, args ...interface{}) {
31+
log.Infof(format, args...)
32+
}
33+
34+
// Errorf Display error message
35+
func (consulLogger) Errorf(format string, args ...interface{}) {
36+
log.Errorf(format, args...)
37+
}
38+
1739
func main() {
1840
logLevel := flag.String("log-level", "INFO", "Log level")
1941
consulAddr := flag.String("http-addr", "127.0.0.1:8500", "Consul agent address")
@@ -73,7 +95,8 @@ func main() {
7395
log.Fatalf("Please specify -sidecar-for or -sidecar-for-tag")
7496
}
7597

76-
watcher := consul.New(serviceID, consulClient)
98+
consulLogger := &consulLogger{}
99+
watcher := consul.New(serviceID, consulClient, consulLogger)
77100
go func() {
78101
if err := watcher.Run(); err != nil {
79102
log.Error(err)

utils_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func startConnectService(t *testing.T, sd *lib.Shutdown, client *api.Client, reg
6464

6565
errs := make(chan error, 2)
6666

67-
watcher := consul.New(reg.ID, client)
67+
watcher := consul.New(reg.ID, client, consul.NewTestingLogger(t))
6868
go func() {
6969
err := watcher.Run()
7070
if err != nil {

0 commit comments

Comments
 (0)