Skip to content

Commit e3aad77

Browse files
committed
TUN-6357: Add connector id to ready check endpoint
1 parent cc8aa0e commit e3aad77

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

cmd/cloudflared/tunnel/cmd.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/coreos/go-systemd/daemon"
1616
"github.com/facebookgo/grace/gracenet"
1717
"github.com/getsentry/raven-go"
18+
"github.com/google/uuid"
1819
homedir "github.com/mitchellh/go-homedir"
1920
"github.com/pkg/errors"
2021
"github.com/rs/zerolog"
@@ -348,6 +349,14 @@ func StartServer(
348349
log.Err(err).Msg("Couldn't start tunnel")
349350
return err
350351
}
352+
var clientID uuid.UUID
353+
if tunnelConfig.NamedTunnel != nil {
354+
clientID, err = uuid.FromBytes(tunnelConfig.NamedTunnel.Client.ClientID)
355+
if err != nil {
356+
// set to nil for classic tunnels
357+
clientID = uuid.Nil
358+
}
359+
}
351360

352361
orchestrator, err := orchestration.NewOrchestrator(ctx, orchestratorConfig, tunnelConfig.Tags, tunnelConfig.Log)
353362
if err != nil {
@@ -363,7 +372,7 @@ func StartServer(
363372
wg.Add(1)
364373
go func() {
365374
defer wg.Done()
366-
readinessServer := metrics.NewReadyServer(log)
375+
readinessServer := metrics.NewReadyServer(log, clientID)
367376
observer.RegisterSink(readinessServer)
368377
errC <- metrics.ServeMetrics(metricsListener, ctx.Done(), readinessServer, quickTunnelURL, orchestrator, log)
369378
}()

metrics/readiness.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@ import (
55
"fmt"
66
"net/http"
77

8+
"github.com/google/uuid"
9+
"github.com/rs/zerolog"
10+
811
conn "github.com/cloudflare/cloudflared/connection"
912
"github.com/cloudflare/cloudflared/tunnelstate"
10-
11-
"github.com/rs/zerolog"
1213
)
1314

1415
// ReadyServer serves HTTP 200 if the tunnel can serve traffic. Intended for k8s readiness checks.
1516
type ReadyServer struct {
16-
tracker *tunnelstate.ConnTracker
17+
clientID uuid.UUID
18+
tracker *tunnelstate.ConnTracker
1719
}
1820

1921
// NewReadyServer initializes a ReadyServer and starts listening for dis/connection events.
20-
func NewReadyServer(log *zerolog.Logger) *ReadyServer {
22+
func NewReadyServer(log *zerolog.Logger, clientID uuid.UUID) *ReadyServer {
2123
return &ReadyServer{
22-
tracker: tunnelstate.NewConnTracker(log),
24+
clientID: clientID,
25+
tracker: tunnelstate.NewConnTracker(log),
2326
}
2427
}
2528

@@ -28,8 +31,9 @@ func (rs *ReadyServer) OnTunnelEvent(c conn.Event) {
2831
}
2932

3033
type body struct {
31-
Status int `json:"status"`
32-
ReadyConnections uint `json:"readyConnections"`
34+
Status int `json:"status"`
35+
ReadyConnections uint `json:"readyConnections"`
36+
ConnectorID uuid.UUID `json:"connectorId"`
3337
}
3438

3539
// 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) {
3943
body := body{
4044
Status: statusCode,
4145
ReadyConnections: readyConnections,
46+
ConnectorID: rs.clientID,
4247
}
4348
msg, err := json.Marshal(body)
4449
if err != nil {

metrics/readiness_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"net/http"
55
"testing"
66

7+
"github.com/google/uuid"
78
"github.com/rs/zerolog"
89
"github.com/stretchr/testify/assert"
910

10-
"github.com/cloudflare/cloudflared/tunnelstate"
11-
1211
"github.com/cloudflare/cloudflared/connection"
12+
"github.com/cloudflare/cloudflared/tunnelstate"
1313
)
1414

1515
func TestReadyServer_makeResponse(t *testing.T) {
@@ -66,7 +66,7 @@ func TestReadyServer_makeResponse(t *testing.T) {
6666

6767
func TestReadinessEventHandling(t *testing.T) {
6868
nopLogger := zerolog.Nop()
69-
rs := NewReadyServer(&nopLogger)
69+
rs := NewReadyServer(&nopLogger, uuid.Nil)
7070

7171
// start not ok
7272
code, ready := rs.makeResponse()

0 commit comments

Comments
 (0)