2020-11-30 20:05:37 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
conn "github.com/cloudflare/cloudflared/connection"
|
2021-11-08 15:43:36 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelstate"
|
2020-11-25 06:55:13 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
2020-11-30 20:05:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ReadyServer serves HTTP 200 if the tunnel can serve traffic. Intended for k8s readiness checks.
|
|
|
|
type ReadyServer struct {
|
2021-11-08 15:43:36 +00:00
|
|
|
tracker *tunnelstate.ConnTracker
|
2020-11-30 20:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewReadyServer initializes a ReadyServer and starts listening for dis/connection events.
|
2021-01-14 22:33:36 +00:00
|
|
|
func NewReadyServer(log *zerolog.Logger) *ReadyServer {
|
|
|
|
return &ReadyServer{
|
2021-11-08 15:43:36 +00:00
|
|
|
tracker: tunnelstate.NewConnTracker(log),
|
2020-11-30 20:05:37 +00:00
|
|
|
}
|
2021-01-14 22:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *ReadyServer) OnTunnelEvent(c conn.Event) {
|
2021-11-08 15:43:36 +00:00
|
|
|
rs.tracker.OnTunnelEvent(c)
|
2020-11-30 20:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type body struct {
|
2021-11-08 15:43:36 +00:00
|
|
|
Status int `json:"status"`
|
|
|
|
ReadyConnections uint `json:"readyConnections"`
|
2020-11-30 20:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP responds with HTTP 200 if the tunnel is connected to the edge.
|
|
|
|
func (rs *ReadyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
statusCode, readyConnections := rs.makeResponse()
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
body := body{
|
|
|
|
Status: statusCode,
|
|
|
|
ReadyConnections: readyConnections,
|
|
|
|
}
|
|
|
|
msg, err := json.Marshal(body)
|
|
|
|
if err != nil {
|
2020-11-25 06:55:13 +00:00
|
|
|
_, _ = fmt.Fprintf(w, `{"error": "%s"}`, err)
|
2020-11-30 20:05:37 +00:00
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
_, _ = w.Write(msg)
|
2020-11-30 20:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is the bulk of the logic for ServeHTTP, broken into its own pure function
|
|
|
|
// to make unit testing easy.
|
2021-11-08 15:43:36 +00:00
|
|
|
func (rs *ReadyServer) makeResponse() (statusCode int, readyConnections uint) {
|
|
|
|
readyConnections = rs.tracker.CountActiveConns()
|
|
|
|
if readyConnections > 0 {
|
|
|
|
return http.StatusOK, readyConnections
|
|
|
|
} else {
|
|
|
|
return http.StatusServiceUnavailable, readyConnections
|
2020-11-30 20:05:37 +00:00
|
|
|
}
|
|
|
|
}
|