Skip to content

Commit f89505c

Browse files
authored
Merge pull request #26 from skoef/cleanup
some linting and small fixes
2 parents a6f0fc7 + 881669d commit f89505c

File tree

6 files changed

+68
-54
lines changed

6 files changed

+68
-54
lines changed

.goreleaser.yaml

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
2+
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
3+
4+
version: 2
5+
16
before:
27
hooks:
38
- go mod tidy
9+
410
dist: build
11+
512
builds:
613
- env:
714
- CGO_ENABLED=0
@@ -10,12 +17,20 @@ builds:
1017
goarch:
1118
- amd64
1219
- arm64
20+
1321
archives:
14-
- format: binary
15-
checksum:
16-
name_template: 'checksums.txt'
17-
snapshot:
18-
name_template: "{{ incpatch .Version }}-next"
22+
- formats:
23+
- binary
24+
25+
changelog:
26+
sort: asc
27+
filters:
28+
exclude:
29+
- "^docs:"
30+
- "^test:"
31+
- '^Merge pull request'
32+
- '^Merge branch'
33+
1934
nfpms:
2035
- package_name: birdwatcher
2136
homepage: https://github.com/skoef/birdwatcher
@@ -55,11 +70,3 @@ nfpms:
5570
rpm:
5671
dependencies:
5772
- glibc
58-
changelog:
59-
sort: asc
60-
filters:
61-
exclude:
62-
- '^docs:'
63-
- '^test:'
64-
- '^Merge pull request'
65-
- '^Merge branch'

birdwatcher/bird_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestWriteBirdConfig(t *testing.T) {
1717
t.Parallel()
1818

1919
// open tempfile
20-
tmpFile, err := os.CreateTemp("", "bird_test")
20+
tmpFile, err := os.CreateTemp(t.TempDir(), "bird_test")
2121
require.NoError(t, err)
2222
defer os.Remove(tmpFile.Name())
2323

@@ -42,7 +42,7 @@ func TestWriteBirdConfig(t *testing.T) {
4242
t.Parallel()
4343

4444
// open tempfile
45-
tmpFile, err := os.CreateTemp("", "bird_test")
45+
tmpFile, err := os.CreateTemp(t.TempDir(), "bird_test")
4646
require.NoError(t, err)
4747
defer os.Remove(tmpFile.Name())
4848

@@ -72,7 +72,7 @@ func TestWriteBirdConfig(t *testing.T) {
7272
t.Parallel()
7373

7474
// open tempfile
75-
tmpFile, err := os.CreateTemp("", "bird_test")
75+
tmpFile, err := os.CreateTemp(t.TempDir(), "bird_test")
7676
require.NoError(t, err)
7777
defer os.Remove(tmpFile.Name())
7878

@@ -117,11 +117,11 @@ func TestBirdCompareFiles(t *testing.T) {
117117
t.Parallel()
118118

119119
// open 2 tempfiles
120-
tmpFileA, err := os.CreateTemp("", "bird_test")
120+
tmpFileA, err := os.CreateTemp(t.TempDir(), "bird_test")
121121
require.NoError(t, err)
122122
defer os.Remove(tmpFileA.Name())
123123

124-
tmpFileB, err := os.CreateTemp("", "bird_test")
124+
tmpFileB, err := os.CreateTemp(t.TempDir(), "bird_test")
125125
require.NoError(t, err)
126126
defer os.Remove(tmpFileB.Name())
127127

birdwatcher/healthcheck.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func NewHealthCheck(c Config) HealthCheck {
4949

5050
// Start starts the process of health checking the services and handling
5151
// Actions that come from them
52-
func (h *HealthCheck) Start(services []*ServiceCheck, ready chan<- bool, status *chan string) {
52+
func (h *HealthCheck) Start(services []*ServiceCheck, ready chan<- bool, status chan string) {
5353
// copy reference to services
5454
h.services = services
5555
// create channel for service check to push there events on
@@ -91,7 +91,7 @@ func (h *HealthCheck) didReloadBefore() bool {
9191
return h.reloadedBefore
9292
}
9393

94-
func (h *HealthCheck) handleAction(action *Action, status *chan string) {
94+
func (h *HealthCheck) handleAction(action *Action, status chan string) {
9595
for _, p := range action.Prefixes {
9696
switch action.State {
9797
case ServiceStateUp:
@@ -111,10 +111,8 @@ func (h *HealthCheck) handleAction(action *Action, status *chan string) {
111111
// gather data for a status update
112112
su := h.statusUpdate()
113113
log.WithField("status", su).Debug("status update")
114-
// if status channel is given, send update on it
115-
if status != nil {
116-
*status <- su
117-
}
114+
// send update over channel
115+
status <- su
118116

119117
if err := h.applyConfig(h.Config, h.prefixes); err != nil {
120118
log.WithError(err).Error("could not apply BIRD config")

birdwatcher/healthcheck_test.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,15 @@ func TestHealthCheck_handleAction(t *testing.T) {
101101
action.Service = &ServiceCheck{
102102
FunctionName: "test",
103103
}
104+
105+
sc := make(chan string)
106+
go func() {
107+
// make sure to read the status channel to prevent blocking handleAction
108+
<-sc
109+
}()
110+
104111
// handle service state up
105-
hc.handleAction(action, nil)
112+
hc.handleAction(action, sc)
106113

107114
if assert.Contains(t, hc.prefixes, "test") {
108115
assert.Len(t, hc.prefixes["test"].prefixes, 2)
@@ -111,7 +118,11 @@ func TestHealthCheck_handleAction(t *testing.T) {
111118
// action switches to down for one of the prefixes
112119
action.State = ServiceStateDown
113120
action.Prefixes = action.Prefixes[1:]
114-
hc.handleAction(action, nil)
121+
go func() {
122+
// make sure to read the status channel to prevent blocking handleAction
123+
<-sc
124+
}()
125+
hc.handleAction(action, sc)
115126

116127
if assert.Contains(t, hc.prefixes, "test") {
117128
assert.Len(t, hc.prefixes["test"].prefixes, 1)

dist/birdwatcher.conf

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ enabled = false
1212
# TCP port to expose the prometheus exporter on
1313
port = 9091
1414
# HTTP path to expose the prometheus exporter on
15-
path = /metrics
15+
path = "/metrics"
1616

1717
[services]
1818
# example service
@@ -21,7 +21,7 @@ path = /metrics
2121
# command = "/usr/bin/my_check.sh"
2222
# functionname = "match_route"
2323
# interval = 1
24-
# timeout = 10s
24+
# timeout = "10s"
2525
# fail = 1
2626
# rise = 1
2727
# prefixes = ["192.168.0.0/24", "fc00::/7"]

main.go

+24-26
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ package main
33

44
import (
55
"encoding/json"
6-
"errors"
76
"flag"
87
"fmt"
98
"net/http"
109
"os"
1110
"os/signal"
12-
"runtime/debug"
1311
"syscall"
1412
"time"
1513

@@ -24,6 +22,13 @@ const (
2422
systemdStatusBufferSize = 32
2523
)
2624

25+
// variables filled in by goreleaser during release
26+
var (
27+
version = "devel"
28+
commit = "none"
29+
// date = "unknown"
30+
)
31+
2732
//nolint:funlen // we should refactor this a bit
2833
func main() {
2934
// initialize logging
@@ -35,23 +40,18 @@ func main() {
3540
checkConfig = flag.Bool("check-config", false, "check config file and exit")
3641
debugFlag = flag.Bool("debug", false, "increase loglevel to debug")
3742
useSystemd = flag.Bool("systemd", false, "optimize behavior for running under systemd")
38-
version = flag.Bool("version", false, "show version and exit")
43+
versionFlag = flag.Bool("version", false, "show version and exit")
3944
)
4045

4146
flag.Parse()
4247

43-
versionString := "(devel)"
44-
if vcs, ok := debug.ReadBuildInfo(); ok {
45-
versionString = vcs.Main.Version
46-
}
47-
48-
if *version {
49-
fmt.Printf("birdwatcher, %s\n", versionString)
48+
if *versionFlag {
49+
fmt.Printf("birdwatcher, %s (%s)\n", version, commit)
5050

5151
return
5252
}
5353

54-
log.Infof("starting birdwatcher, %s", versionString)
54+
log.Infof("starting birdwatcher, %s (%s)", version, commit)
5555

5656
if *debugFlag {
5757
log.SetLevel(log.DebugLevel)
@@ -71,7 +71,7 @@ func main() {
7171
if err := birdwatcher.ReadConfig(&config, *configFile); err != nil {
7272
// return slightly different message when birdwatcher was invoked with -check-config
7373
if *checkConfig {
74-
fmt.Printf("Configuration file %s not OK: %s\n", *configFile, errors.Unwrap(err))
74+
fmt.Printf("Configuration file %s not OK: %s\n", *configFile, err)
7575
os.Exit(1)
7676
}
7777

@@ -107,24 +107,22 @@ func main() {
107107
hc := birdwatcher.NewHealthCheck(config)
108108
ready := make(chan bool)
109109

110-
var status *chan string
111-
112-
if *useSystemd {
113-
// create status update channel for systemd
114-
// give it a little buffer so the chances of it blocking the health check
115-
// is low
116-
s := make(chan string, systemdStatusBufferSize)
117-
status = &s
118-
119-
go func() {
120-
for update := range *status {
110+
// create status update channel for systemd
111+
// give it a little buffer so the chances of it blocking the health check
112+
// is low
113+
sdStatus := make(chan string, systemdStatusBufferSize)
114+
go func() {
115+
// make sure we read from the sdStatus channel, regardless if we use
116+
// systemd integration or not to prevent the channel from blocking
117+
for update := range sdStatus {
118+
if *useSystemd {
121119
log.Debug("notifying systemd of new status")
122120
sdnotify("STATUS=" + update)
123121
}
124-
}()
125-
}
122+
}
123+
}()
126124

127-
go hc.Start(config.GetServices(), ready, status)
125+
go hc.Start(config.GetServices(), ready, sdStatus)
128126
// wait for all health services to have started
129127
<-ready
130128

0 commit comments

Comments
 (0)