Skip to content

Commit 27e4008

Browse files
committed
[!] improve logging and add verbose support for all components
[+] use `go.uber.org/zap` instead of standard `log` package [*] move net related code from `main` to `ipmanager` [*] user TriggerKey and TriggerValue names instead of Key and NodeName
1 parent 7ecae13 commit 27e4008

File tree

10 files changed

+184
-145
lines changed

10 files changed

+184
-145
lines changed

checker/consul_leader_checker.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package checker
22

33
import (
44
"context"
5-
"log"
65
"net/url"
76
"time"
87

@@ -17,15 +16,15 @@ type ConsulLeaderChecker struct {
1716
apiClient *api.Client
1817
}
1918

20-
//naming this cConf to avoid conflict with conf in etcd_leader_checker.go
19+
// naming this cConf to avoid conflict with conf in etcd_leader_checker.go
2120
var cConf *vipconfig.Config
2221

2322
// NewConsulLeaderChecker returns a new instance
2423
func NewConsulLeaderChecker(con *vipconfig.Config) (*ConsulLeaderChecker, error) {
2524
cConf = con
2625
lc := &ConsulLeaderChecker{
27-
key: cConf.Key,
28-
nodename: cConf.Nodename,
26+
key: cConf.TriggerKey,
27+
nodename: cConf.TriggerValue,
2928
}
3029

3130
url, err := url.Parse(cConf.Endpoints[0])
@@ -69,13 +68,13 @@ checkLoop:
6968
if ctx.Err() != nil {
7069
break checkLoop
7170
}
72-
log.Printf("consul error: %s", err)
71+
cConf.Logger.Sugar().Error("consul error: ", err)
7372
out <- false
7473
time.Sleep(time.Duration(cConf.Interval) * time.Millisecond)
7574
continue
7675
}
7776
if resp == nil {
78-
log.Printf("Cannot get variable for key %s. Will try again in a second.", c.key)
77+
cConf.Logger.Sugar().Errorf("Cannot get variable for key %s. Will try again in a second.", c.key)
7978
out <- false
8079
time.Sleep(time.Duration(cConf.Interval) * time.Millisecond)
8180
continue

checker/etcd_leader_checker.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55
"crypto/tls"
66
"crypto/x509"
77
"fmt"
8-
"log"
98
"os"
109
"time"
1110

1211
"github.com/cybertec-postgresql/vip-manager/vipconfig"
1312
clientv3 "go.etcd.io/etcd/client/v3"
13+
"go.uber.org/zap"
1414
)
1515

1616
// EtcdLeaderChecker is used to check state of the leader key in Etcd
@@ -32,6 +32,7 @@ func NewEtcdLeaderChecker(conf *vipconfig.Config) (*EtcdLeaderChecker, error) {
3232
DialKeepAliveTime: time.Second,
3333
Username: conf.EtcdUser,
3434
Password: conf.EtcdPassword,
35+
Logger: conf.Logger,
3536
}
3637
c, err := clientv3.New(cfg)
3738
return &EtcdLeaderChecker{conf, c}, err
@@ -71,39 +72,39 @@ func getTransport(conf *vipconfig.Config) (*tls.Config, error) {
7172

7273
// get gets the current leader from etcd
7374
func (elc *EtcdLeaderChecker) get(ctx context.Context, out chan<- bool) {
74-
resp, err := elc.Get(ctx, elc.Key)
75+
resp, err := elc.Get(ctx, elc.TriggerKey)
7576
if err != nil {
76-
log.Printf("etcd error: %s", err)
77+
elc.Logger.Error("etcd error:", zap.Error(err))
7778
out <- false
7879
return
7980
}
8081
for _, kv := range resp.Kvs {
81-
log.Printf("current leader from DCS: %s", kv.Value)
82-
out <- string(kv.Value) == elc.Nodename
82+
elc.Logger.Sugar().Info("current leader from DCS:", kv.Value)
83+
out <- string(kv.Value) == elc.TriggerValue
8384
}
8485
}
8586

8687
// watch monitors the leader change from etcd
8788
func (elc *EtcdLeaderChecker) watch(ctx context.Context, out chan<- bool) error {
88-
watchChan := elc.Watch(ctx, elc.Key)
89-
log.Println("set WATCH on " + elc.Key)
89+
watchChan := elc.Watch(ctx, elc.TriggerKey)
90+
elc.Logger.Sugar().Info("set WATCH on ", elc.TriggerKey)
9091
for {
9192
select {
9293
case <-ctx.Done():
9394
return ctx.Err()
9495
case watchResp := <-watchChan:
9596
if watchResp.Canceled {
96-
watchChan = elc.Watch(ctx, elc.Key)
97-
log.Println("reset cancelled WATCH on " + elc.Key)
97+
watchChan = elc.Watch(ctx, elc.TriggerKey)
98+
elc.Logger.Sugar().Info("reset cancelled WATCH on ", elc.TriggerKey)
9899
continue
99100
}
100101
if err := watchResp.Err(); err != nil {
101102
elc.get(ctx, out) // RPC failed, try to get the key directly to be on the safe side
102103
continue
103104
}
104105
for _, event := range watchResp.Events {
105-
out <- string(event.Kv.Value) == elc.Nodename
106-
log.Printf("current leader from DCS: %s", event.Kv.Value)
106+
out <- string(event.Kv.Value) == elc.TriggerValue
107+
elc.Logger.Sugar().Info("current leader from DCS:", event.Kv.Value)
107108
}
108109
}
109110
}

checker/patroni_leader_checker.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package checker
22

33
import (
44
"context"
5-
"log"
65
"strconv"
76
"time"
87

@@ -30,12 +29,12 @@ func (c *PatroniLeaderChecker) GetChangeNotificationStream(ctx context.Context,
3029
case <-ctx.Done():
3130
return nil
3231
case <-time.After(time.Duration(c.Interval) * time.Millisecond):
33-
r, err := http.Get(c.Endpoints[0] + c.Key)
32+
r, err := http.Get(c.Endpoints[0] + c.TriggerKey)
3433
if err != nil {
35-
log.Printf("patroni REST API error: %s", err)
34+
c.Logger.Sugar().Error("patroni REST API error:", err)
3635
continue
3736
}
38-
out <- strconv.Itoa(r.StatusCode) == c.Nodename
37+
out <- strconv.Itoa(r.StatusCode) == c.TriggerValue
3938
}
4039
}
4140
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/spf13/pflag v1.0.5
99
github.com/spf13/viper v1.19.0
1010
go.etcd.io/etcd/client/v3 v3.5.17
11+
go.uber.org/zap v1.27.0
1112
golang.org/x/sys v0.27.0
1213
)
1314

@@ -48,7 +49,6 @@ require (
4849
go.etcd.io/etcd/api/v3 v3.5.17 // indirect
4950
go.etcd.io/etcd/client/pkg/v3 v3.5.17 // indirect
5051
go.uber.org/multierr v1.11.0 // indirect
51-
go.uber.org/zap v1.27.0 // indirect
5252
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 // indirect
5353
golang.org/x/net v0.28.0 // indirect
5454
golang.org/x/sync v0.8.0 // indirect

ipmanager/basicConfigurer_linux.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ipmanager
22

33
import (
4-
"log"
54
"net"
65
"os/exec"
76
"time"
@@ -22,11 +21,11 @@ var (
2221
func (c *BasicConfigurer) configureAddress() bool {
2322
if c.arpClient == nil {
2423
if err := c.createArpClient(); err != nil {
25-
log.Printf("Couldn't create an Arp client: %s", err)
24+
log.Error("Couldn't create an Arp client:", err)
2625
}
2726
}
2827

29-
log.Printf("Configuring address %s on %s", c.getCIDR(), c.Iface.Name)
28+
log.Infof("Configuring address %s on %s", c.getCIDR(), c.Iface.Name)
3029

3130
result := c.runAddressConfiguration("add")
3231

@@ -42,7 +41,7 @@ func (c *BasicConfigurer) configureAddress() bool {
4241

4342
// deconfigureAddress drops virtual IP address
4443
func (c *BasicConfigurer) deconfigureAddress() bool {
45-
log.Printf("Removing address %s on %s", c.getCIDR(), c.Iface.Name)
44+
log.Infof("Removing address %s on %s", c.getCIDR(), c.Iface.Name)
4645
return c.runAddressConfiguration("delete")
4746
}
4847

@@ -54,12 +53,12 @@ func (c *BasicConfigurer) runAddressConfiguration(action string) bool {
5453

5554
switch err.(type) {
5655
case *exec.ExitError:
57-
log.Printf("Got error %s", output)
56+
log.Infof("Got error %s", output)
5857

5958
return false
6059
}
6160
if err != nil {
62-
log.Printf("Error running ip address %s %s on %s: %s",
61+
log.Infof("Error running ip address %s %s on %s: %s",
6362
action, c.VIP, c.Iface.Name, err)
6463
return false
6564
}
@@ -71,7 +70,7 @@ func (c *BasicConfigurer) createArpClient() (err error) {
7170
if c.arpClient, err = arp.Dial(&c.Iface); err == nil {
7271
return
7372
}
74-
log.Printf("Problems with producing the arp client: %s", err)
73+
log.Infof("Problems with producing the arp client: %s", err)
7574
time.Sleep(time.Duration(c.RetryAfter) * time.Millisecond)
7675
}
7776
return
@@ -86,7 +85,7 @@ func (c *BasicConfigurer) arpSendGratuitous() error {
8685
* https://support.citrix.com/article/CTX112701
8786
*/
8887
if c.arpClient == nil {
89-
log.Println("No arp client available, skip send gratuitous ARP")
88+
log.Info("No arp client available, skip send gratuitous ARP")
9089
return nil
9190
}
9291
gratuitousReplyPackage, err := arp.NewPacket(
@@ -97,7 +96,7 @@ func (c *BasicConfigurer) arpSendGratuitous() error {
9796
c.VIP,
9897
)
9998
if err != nil {
100-
log.Printf("Gratuitous arp reply package is malformed: %s", err)
99+
log.Infof("Gratuitous arp reply package is malformed: %s", err)
101100
return err
102101
}
103102

@@ -121,23 +120,23 @@ func (c *BasicConfigurer) arpSendGratuitous() error {
121120
c.VIP,
122121
)
123122
if err != nil {
124-
log.Printf("Gratuitous arp request package is malformed: %s", err)
123+
log.Infof("Gratuitous arp request package is malformed: %s", err)
125124
return err
126125
}
127126

128127
for i := 0; i < c.RetryNum; i++ {
129128
errReply := c.arpClient.WriteTo(gratuitousReplyPackage, ethernetBroadcast)
130129
if err != nil {
131-
log.Printf("Couldn't write to the arpClient: %s", errReply)
130+
log.Error("Couldn't write to the arpClient:", errReply)
132131
} else {
133-
log.Println("Sent gratuitous ARP reply")
132+
log.Info("Sent gratuitous ARP reply")
134133
}
135134

136135
errRequest := c.arpClient.WriteTo(gratuitousRequestPackage, ethernetBroadcast)
137136
if err != nil {
138-
log.Printf("Couldn't write to the arpClient: %s", errRequest)
137+
log.Error("Couldn't write to the arpClient:", errRequest)
139138
} else {
140-
log.Println("Sent gratuitous ARP request")
139+
log.Info("Sent gratuitous ARP request")
141140
}
142141

143142
if errReply != nil || errRequest != nil {
@@ -152,7 +151,7 @@ func (c *BasicConfigurer) arpSendGratuitous() error {
152151
time.Sleep(time.Duration(c.RetryAfter) * time.Millisecond)
153152
}
154153
if err != nil {
155-
log.Print("too many retries")
154+
log.Error("too many retries", err)
156155
return err
157156
}
158157

ipmanager/basicConfigurer_windows.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,27 @@ package ipmanager
22

33
import (
44
"encoding/binary"
5-
"log"
65
"net"
76

87
"github.com/cybertec-postgresql/vip-manager/iphlpapi"
98
)
109

1110
// configureAddress assigns virtual IP address
1211
func (c *BasicConfigurer) configureAddress() bool {
13-
log.Printf("Configuring address %s on %s", c.getCIDR(), c.Iface.Name)
12+
log.Infof("Configuring address %s on %s", c.getCIDR(), c.Iface.Name)
1413
var (
1514
ip = binary.LittleEndian.Uint32(c.VIP.AsSlice())
1615
mask = binary.LittleEndian.Uint32(c.Netmask)
1716
nteinstance uint32
1817
)
1918
iface, err := net.InterfaceByName(c.Iface.Name)
2019
if err != nil {
21-
log.Printf("Got error: %v", err)
20+
log.Infof("Got error: %v", err)
2221
return false
2322
}
2423
err = iphlpapi.AddIPAddress(ip, mask, uint32(iface.Index), &c.ntecontext, &nteinstance)
2524
if err != nil {
26-
log.Printf("Got error: %v", err)
25+
log.Infof("Got error: %v", err)
2726
return false
2827
}
2928
// For now it is save to say that also working even if a
@@ -35,10 +34,10 @@ func (c *BasicConfigurer) configureAddress() bool {
3534

3635
// deconfigureAddress drops virtual IP address
3736
func (c *BasicConfigurer) deconfigureAddress() bool {
38-
log.Printf("Removing address %s on %s", c.getCIDR(), c.Iface.Name)
37+
log.Infof("Removing address %s on %s", c.getCIDR(), c.Iface.Name)
3938
err := iphlpapi.DeleteIPAddress(c.ntecontext)
4039
if err != nil {
41-
log.Printf("Got error: %v", err)
40+
log.Error(err)
4241
return false
4342
}
4443
return true

0 commit comments

Comments
 (0)