fix(tailnet): track agent names for http debug (#8744)

This commit is contained in:
Colin Adler 2023-07-26 13:44:10 -05:00 committed by GitHub
parent b5dec61cd5
commit 6b92abebb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 18 deletions

View File

@ -158,7 +158,7 @@ func (c *haCoordinator) ServeClient(conn net.Conn, id, agentID uuid.UUID) error
defer cancel()
logger := c.clientLogger(id, agentID)
tc := agpl.NewTrackedConn(ctx, cancel, conn, id, logger, 0)
tc := agpl.NewTrackedConn(ctx, cancel, conn, id, logger, id.String(), 0)
defer tc.Close()
c.addClient(id, tc)
@ -301,7 +301,7 @@ func (c *haCoordinator) ServeAgent(conn net.Conn, id uuid.UUID, name string) err
}
// This uniquely identifies a connection that belongs to this goroutine.
unique := uuid.New()
tc := agpl.NewTrackedConn(ctx, cancel, conn, unique, logger, overwrites)
tc := agpl.NewTrackedConn(ctx, cancel, conn, unique, logger, name, overwrites)
// Publish all nodes on this instance that want to connect to this agent.
nodes := c.nodesSubscribedToAgent(id)

View File

@ -131,12 +131,6 @@ func (c *pgCoord) ServeMultiAgent(id uuid.UUID) agpl.MultiAgentConn {
panic("not implemented") // TODO: Implement
}
func (*pgCoord) ServeHTTPDebug(w http.ResponseWriter, _ *http.Request) {
// TODO(spikecurtis) I'd like to hold off implementing this until after the rest of this is code reviewed.
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("Coder Enterprise PostgreSQL distributed tailnet coordinator"))
}
func (c *pgCoord) Node(id uuid.UUID) *agpl.Node {
// In production, we only ever get this request for an agent.
// We're going to directly query the database, since we would only have the agent mapping stored locally if we had
@ -167,7 +161,7 @@ func (c *pgCoord) ServeClient(conn net.Conn, id uuid.UUID, agent uuid.UUID) erro
slog.Error(err))
}
}()
cIO := newConnIO(c.ctx, c.logger, c.bindings, conn, id, agent)
cIO := newConnIO(c.ctx, c.logger, c.bindings, conn, id, agent, id.String())
if err := sendCtx(c.ctx, c.newConnections, cIO); err != nil {
// can only be a context error, no need to log here.
return err
@ -186,7 +180,7 @@ func (c *pgCoord) ServeAgent(conn net.Conn, id uuid.UUID, name string) error {
}
}()
logger := c.logger.With(slog.F("name", name))
cIO := newConnIO(c.ctx, logger, c.bindings, conn, uuid.Nil, id)
cIO := newConnIO(c.ctx, logger, c.bindings, conn, uuid.Nil, id, name)
if err := sendCtx(c.ctx, c.newConnections, cIO); err != nil {
// can only be a context error, no need to log here.
return err
@ -217,8 +211,12 @@ type connIO struct {
bindings chan<- binding
}
func newConnIO(
pCtx context.Context, logger slog.Logger, bindings chan<- binding, conn net.Conn, client, agent uuid.UUID,
func newConnIO(pCtx context.Context,
logger slog.Logger,
bindings chan<- binding,
conn net.Conn,
client, agent uuid.UUID,
name string,
) *connIO {
ctx, cancel := context.WithCancel(pCtx)
id := agent
@ -235,7 +233,7 @@ func newConnIO(
client: client,
agent: agent,
decoder: json.NewDecoder(conn),
updates: agpl.NewTrackedConn(ctx, cancel, conn, id, logger, 0),
updates: agpl.NewTrackedConn(ctx, cancel, conn, id, logger, name, 0),
bindings: bindings,
}
go c.recvLoop()
@ -1291,3 +1289,9 @@ func (h *heartbeats) cleanup() {
}
h.logger.Debug(h.ctx, "cleaned up old coordinators")
}
func (*pgCoord) ServeHTTPDebug(w http.ResponseWriter, _ *http.Request) {
// TODO(spikecurtis) I'd like to hold off implementing this until after the rest of this is code reviewed.
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("Coder Enterprise PostgreSQL distributed tailnet coordinator"))
}

4
go.mod
View File

@ -91,6 +91,7 @@ require (
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
github.com/creack/pty v1.1.18
github.com/dave/dst v0.27.2
github.com/davecgh/go-spew v1.1.1
github.com/elastic/go-sysinfo v1.11.0
github.com/fatih/color v1.15.0
github.com/fatih/structs v1.1.0
@ -187,6 +188,7 @@ require (
nhooyr.io/websocket v1.8.7
storj.io/drpc v0.0.33-0.20230420154621-9716137f6037
tailscale.com v1.32.3
golang.org/x/net v0.12.0
)
require (
@ -222,7 +224,6 @@ require (
github.com/containerd/console v1.0.3 // indirect
github.com/containerd/continuity v0.3.0 // indirect
github.com/coreos/go-iptables v0.6.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/docker/cli v20.10.17+incompatible // indirect
github.com/docker/docker v23.0.3+incompatible // indirect
@ -355,7 +356,6 @@ require (
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go4.org/mem v0.0.0-20210711025021-927187094b94 // indirect
golang.org/x/net v0.12.0
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 // indirect

View File

@ -262,7 +262,7 @@ func (c *coordinator) ServeClient(conn net.Conn, id, agentID uuid.UUID) error {
logger := c.core.clientLogger(id, agentID)
logger.Debug(ctx, "coordinating client")
tc := NewTrackedConn(ctx, cancel, conn, id, logger, 0)
tc := NewTrackedConn(ctx, cancel, conn, id, logger, id.String(), 0)
defer tc.Close()
c.core.addClient(id, tc)
@ -507,7 +507,7 @@ func (c *core) initAndTrackAgent(ctx context.Context, cancel func(), conn net.Co
overwrites = oldAgentSocket.Overwrites() + 1
_ = oldAgentSocket.Close()
}
tc := NewTrackedConn(ctx, cancel, conn, unique, logger, overwrites)
tc := NewTrackedConn(ctx, cancel, conn, unique, logger, name, overwrites)
c.agentNameCache.Add(id, name)
sockets, ok := c.agentToConnectionSockets[id]

View File

@ -35,7 +35,7 @@ type TrackedConn struct {
overwrites int64
}
func NewTrackedConn(ctx context.Context, cancel func(), conn net.Conn, id uuid.UUID, logger slog.Logger, overwrites int64) *TrackedConn {
func NewTrackedConn(ctx context.Context, cancel func(), conn net.Conn, id uuid.UUID, logger slog.Logger, name string, overwrites int64) *TrackedConn {
// buffer updates so they don't block, since we hold the
// coordinator mutex while queuing. Node updates don't
// come quickly, so 512 should be plenty for all but
@ -51,6 +51,7 @@ func NewTrackedConn(ctx context.Context, cancel func(), conn net.Conn, id uuid.U
id: id,
start: now,
lastWrite: now,
name: name,
overwrites: overwrites,
}
}