From e3aad7799eecdd7249d13b72b6f967e2d57e09b9 Mon Sep 17 00:00:00 2001 From: Devin Carr Date: Tue, 7 Jun 2022 23:09:26 -0700 Subject: [PATCH] TUN-6357: Add connector id to ready check endpoint --- cmd/cloudflared/tunnel/cmd.go | 11 ++++++++++- metrics/readiness.go | 19 ++++++++++++------- metrics/readiness_test.go | 6 +++--- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/cmd/cloudflared/tunnel/cmd.go b/cmd/cloudflared/tunnel/cmd.go index 4cedff50..ae1e5f55 100644 --- a/cmd/cloudflared/tunnel/cmd.go +++ b/cmd/cloudflared/tunnel/cmd.go @@ -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) }() diff --git a/metrics/readiness.go b/metrics/readiness.go index 8524bae8..b4e2025d 100644 --- a/metrics/readiness.go +++ b/metrics/readiness.go @@ -5,21 +5,24 @@ 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 { - tracker *tunnelstate.ConnTracker + 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{ - tracker: tunnelstate.NewConnTracker(log), + clientID: clientID, + tracker: tunnelstate.NewConnTracker(log), } } @@ -28,8 +31,9 @@ func (rs *ReadyServer) OnTunnelEvent(c conn.Event) { } type body struct { - Status int `json:"status"` - ReadyConnections uint `json:"readyConnections"` + 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 { diff --git a/metrics/readiness_test.go b/metrics/readiness_test.go index e6578962..62e42f63 100644 --- a/metrics/readiness_test.go +++ b/metrics/readiness_test.go @@ -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()