Skip to content

Commit

Permalink
add http health check
Browse files Browse the repository at this point in the history
  • Loading branch information
sholdee committed Jul 5, 2024
1 parent a2b03ac commit 20feaa3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@ ARG wg_tools_tag=v1.0.20210914

RUN apk add --update git build-base libmnl-dev iptables

RUN git clone https://git.zx2c4.com/wireguard-go && \
RUN git clone https://github.com/WireGuard/wireguard-go && \
cd wireguard-go && \
git checkout $wg_go_tag && \
make && \
make install

ENV WITH_WGQUICK=yes
RUN git clone https://git.zx2c4.com/wireguard-tools && \
RUN git clone https://github.com/WireGuard/wireguard-tools && \
cd wireguard-tools && \
git checkout $wg_tools_tag && \
cd src && \
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"]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: buildx_container build_and_push

DOCKER_REGISTRY ?= masipcat/wireguard-go
DOCKER_REGISTRY ?= sholdee/wireguard-go-docker

buildx_container:
docker buildx create \
Expand Down
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 20feaa3

Please sign in to comment.