Skip to content

Commit

Permalink
add http healthcheck (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
sholdee authored Jul 6, 2024
1 parent a2b03ac commit 9b1aafa
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ RUN git clone https://git.zx2c4.com/wireguard-tools && \
make && \
make install

COPY healthcheck.go /go/src/healthcheck.go
RUN go build -o /go/bin/healthcheck /go/src/healthcheck.go

FROM alpine:${ALPINE_VERSION}

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

COPY --from=builder /usr/bin/wireguard-go /usr/bin/wg* /usr/bin/
COPY --from=builder /go/bin/healthcheck /usr/bin/healthcheck
COPY entrypoint.sh /entrypoint.sh

CMD ["/entrypoint.sh"]
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ spec:
- containerPort: 51820
protocol: UDP
name: wireguard
- containerPort: 8080
protocol: TCP
name: healthcheck
livenessProbe: &probe
httpGet:
path: /
port: healthcheck
readinessProbe: *probe
env:
- name: LOG_LEVEL
value: info
Expand Down
5 changes: 4 additions & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ trap finish SIGTERM SIGINT SIGQUIT

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

# Inifinite sleep
# Infinite sleep
sleep infinity &

# Health check
/usr/bin/healthcheck &
wait $!
35 changes: 35 additions & 0 deletions healthcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)

func isLinkUp(interfaceName string) bool {
content, err := ioutil.ReadFile("/sys/class/net/" + interfaceName + "/carrier")
if err != nil {
return false
}
return strings.TrimSpace(string(content)) == "1"
}

func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
if isLinkUp("wg0") {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "status: OK\n")
} else {
w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintf(w, "status: KO\n")
}
}

func main() {
http.HandleFunc("/", healthCheckHandler)
if err := http.ListenAndServe(":8080", nil); err != nil {
fmt.Printf("Failed to start server: %v\n", err)
os.Exit(1)
}
}

0 comments on commit 9b1aafa

Please sign in to comment.