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

View File

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

View File

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