Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(tailnet): track agent names for http debug
These were previously not passed through.
  • Loading branch information
coadler committed Jul 26, 2023
commit 51317bb788a9de2ba0ab29fb71d54231be615acc
4 changes: 2 additions & 2 deletions enterprise/tailnet/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "", 0)
defer tc.Close()

c.addClient(id, tc)
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 15 additions & 11 deletions enterprise/tailnet/pgcoord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, "")
if err := sendCtx(c.ctx, c.newConnections, cIO); err != nil {
// can only be a context error, no need to log here.
return err
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tailnet/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "", 0)
defer tc.Close()

c.core.addClient(id, tc)
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tailnet/trackedconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down