Skip to content

Commit 9b1aafa

Browse files
authored
add http healthcheck (#34)
1 parent a2b03ac commit 9b1aafa

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

Dockerfile

+4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ RUN git clone https://git.zx2c4.com/wireguard-tools && \
2222
make && \
2323
make install
2424

25+
COPY healthcheck.go /go/src/healthcheck.go
26+
RUN go build -o /go/bin/healthcheck /go/src/healthcheck.go
27+
2528
FROM alpine:${ALPINE_VERSION}
2629

2730
RUN apk add --no-cache --update bash libmnl iptables openresolv iproute2
2831

2932
COPY --from=builder /usr/bin/wireguard-go /usr/bin/wg* /usr/bin/
33+
COPY --from=builder /go/bin/healthcheck /usr/bin/healthcheck
3034
COPY entrypoint.sh /entrypoint.sh
3135

3236
CMD ["/entrypoint.sh"]

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ spec:
134134
- containerPort: 51820
135135
protocol: UDP
136136
name: wireguard
137+
- containerPort: 8080
138+
protocol: TCP
139+
name: healthcheck
140+
livenessProbe: &probe
141+
httpGet:
142+
path: /
143+
port: healthcheck
144+
readinessProbe: *probe
137145
env:
138146
- name: LOG_LEVEL
139147
value: info

entrypoint.sh

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ trap finish SIGTERM SIGINT SIGQUIT
88

99
wg-quick up /etc/wireguard/wg0.conf
1010

11-
# Inifinite sleep
11+
# Infinite sleep
1212
sleep infinity &
13+
14+
# Health check
15+
/usr/bin/healthcheck &
1316
wait $!

healthcheck.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"net/http"
7+
"os"
8+
"strings"
9+
)
10+
11+
func isLinkUp(interfaceName string) bool {
12+
content, err := ioutil.ReadFile("/sys/class/net/" + interfaceName + "/carrier")
13+
if err != nil {
14+
return false
15+
}
16+
return strings.TrimSpace(string(content)) == "1"
17+
}
18+
19+
func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
20+
if isLinkUp("wg0") {
21+
w.WriteHeader(http.StatusOK)
22+
fmt.Fprintf(w, "status: OK\n")
23+
} else {
24+
w.WriteHeader(http.StatusServiceUnavailable)
25+
fmt.Fprintf(w, "status: KO\n")
26+
}
27+
}
28+
29+
func main() {
30+
http.HandleFunc("/", healthCheckHandler)
31+
if err := http.ListenAndServe(":8080", nil); err != nil {
32+
fmt.Printf("Failed to start server: %v\n", err)
33+
os.Exit(1)
34+
}
35+
}

0 commit comments

Comments
 (0)