Skip to content

Commit

Permalink
Healthcheck in docker container (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xERR0R committed Feb 10, 2020
1 parent 0061cf0 commit 9bb0682
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ ARG opts
RUN env ${opts} make build

# final stage
FROM scratch
FROM alpine
RUN apk add --no-cache bind-tools
COPY --from=build-env /src/bin/blocky /app/blocky

# the timezone data:
Expand All @@ -35,4 +36,6 @@ COPY --from=build-env /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

WORKDIR /app

HEALTHCHECK --interval=1m --timeout=3s CMD dig @127.0.0.1 -p 53 healthcheck.blocky +tcp || exit 1

ENTRYPOINT ["/app/blocky"]
13 changes: 13 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ func NewServer(cfg *config.Config) (*Server, error) {
server.printConfiguration()

udpHandler.HandleFunc(".", server.OnRequest)
udpHandler.HandleFunc("healthcheck.blocky", server.OnHealthCheck)
tcpHandler.HandleFunc(".", server.OnRequest)
tcpHandler.HandleFunc("healthcheck.blocky", server.OnHealthCheck)

return &server, nil
}
Expand Down Expand Up @@ -192,6 +194,17 @@ func (s *Server) OnRequest(w dns.ResponseWriter, request *dns.Msg) {
}
}

// Handler for docker healthcheck. Just returns OK code without delegating to resolver chain
func (s *Server) OnHealthCheck(w dns.ResponseWriter, request *dns.Msg) {
resp := new(dns.Msg)
resp.SetReply(request)
resp.Rcode = dns.RcodeSuccess

if err := w.WriteMsg(resp); err != nil {
logger().Error("can't write message: ", err)
}
}

func resolveClientIP(addr net.Addr) net.IP {
var clientIP net.IP
if t, ok := addr.(*net.UDPAddr); ok {
Expand Down
10 changes: 10 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ var tests = []struct {
assert.Equal(t, "0.0.0.0", resp.Answer[0].(*dns.A).A.String())
},
},
{
// healthcheck
name: "healthcheck",
mockClientName: "c1",
request: util.NewMsgWithQuestion("healthcheck.blocky.", dns.TypeA),
respValidator: func(t *testing.T, resp *dns.Msg) {
assert.Equal(t, dns.RcodeSuccess, resp.Rcode)
assert.Empty(t, resp.Answer)
},
},
}

//nolint:funlen
Expand Down

0 comments on commit 9bb0682

Please sign in to comment.