TUN-6357: Add connector id to ready check endpoint

This commit is contained in:
Devin Carr 2022-06-07 23:09:26 -07:00
parent cc8aa0efb5
commit e3aad7799e
3 changed files with 25 additions and 11 deletions

View File

@ -15,6 +15,7 @@ import (
"github.com/coreos/go-systemd/daemon"
"github.com/facebookgo/grace/gracenet"
"github.com/getsentry/raven-go"
"github.com/google/uuid"
homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/rs/zerolog"
@ -348,6 +349,14 @@ func StartServer(
log.Err(err).Msg("Couldn't start tunnel")
return err
}
var clientID uuid.UUID
if tunnelConfig.NamedTunnel != nil {
clientID, err = uuid.FromBytes(tunnelConfig.NamedTunnel.Client.ClientID)
if err != nil {
// set to nil for classic tunnels
clientID = uuid.Nil
}
}
orchestrator, err := orchestration.NewOrchestrator(ctx, orchestratorConfig, tunnelConfig.Tags, tunnelConfig.Log)
if err != nil {
@ -363,7 +372,7 @@ func StartServer(
wg.Add(1)
go func() {
defer wg.Done()
readinessServer := metrics.NewReadyServer(log)
readinessServer := metrics.NewReadyServer(log, clientID)
observer.RegisterSink(readinessServer)
errC <- metrics.ServeMetrics(metricsListener, ctx.Done(), readinessServer, quickTunnelURL, orchestrator, log)
}()

View File

@ -5,20 +5,23 @@ import (
"fmt"
"net/http"
"github.com/google/uuid"
"github.com/rs/zerolog"
conn "github.com/cloudflare/cloudflared/connection"
"github.com/cloudflare/cloudflared/tunnelstate"
"github.com/rs/zerolog"
)
// ReadyServer serves HTTP 200 if the tunnel can serve traffic. Intended for k8s readiness checks.
type ReadyServer struct {
clientID uuid.UUID
tracker *tunnelstate.ConnTracker
}
// NewReadyServer initializes a ReadyServer and starts listening for dis/connection events.
func NewReadyServer(log *zerolog.Logger) *ReadyServer {
func NewReadyServer(log *zerolog.Logger, clientID uuid.UUID) *ReadyServer {
return &ReadyServer{
clientID: clientID,
tracker: tunnelstate.NewConnTracker(log),
}
}
@ -30,6 +33,7 @@ func (rs *ReadyServer) OnTunnelEvent(c conn.Event) {
type body struct {
Status int `json:"status"`
ReadyConnections uint `json:"readyConnections"`
ConnectorID uuid.UUID `json:"connectorId"`
}
// ServeHTTP responds with HTTP 200 if the tunnel is connected to the edge.
@ -39,6 +43,7 @@ func (rs *ReadyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body := body{
Status: statusCode,
ReadyConnections: readyConnections,
ConnectorID: rs.clientID,
}
msg, err := json.Marshal(body)
if err != nil {

View File

@ -4,12 +4,12 @@ import (
"net/http"
"testing"
"github.com/google/uuid"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/cloudflare/cloudflared/tunnelstate"
"github.com/cloudflare/cloudflared/connection"
"github.com/cloudflare/cloudflared/tunnelstate"
)
func TestReadyServer_makeResponse(t *testing.T) {
@ -66,7 +66,7 @@ func TestReadyServer_makeResponse(t *testing.T) {
func TestReadinessEventHandling(t *testing.T) {
nopLogger := zerolog.Nop()
rs := NewReadyServer(&nopLogger)
rs := NewReadyServer(&nopLogger, uuid.Nil)
// start not ok
code, ready := rs.makeResponse()