Skip to content

Commit 7db8655

Browse files
committed
fixup! tests
1 parent ac0d625 commit 7db8655

File tree

9 files changed

+37
-37
lines changed

9 files changed

+37
-37
lines changed

agent/agent.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ func New(options Options) Agent {
115115

116116
if len(options.Addresses) == 0 {
117117
options.Addresses = []netip.Prefix{
118+
// This is the IP that should be used primarily.
118119
netip.PrefixFrom(tailnet.IP(), 128),
120+
// We also listen on the legacy codersdk.WorkspaceAgentIP. This
121+
// allows for a transition away from wsconncache.
119122
netip.PrefixFrom(codersdk.WorkspaceAgentIP, 128),
120123
}
121124
}

coderd/coderd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ func New(options *Options) *API {
357357
options.DERPServer,
358358
options.DERPMap,
359359
&api.TailnetCoordinator,
360-
wsconncache.New(api.dialWorkspaceAgentTailnet, 0),
360+
wsconncache.New(api._dialWorkspaceAgentTailnet, 0),
361361
)
362362
if err != nil {
363363
panic("failed to setup server tailnet: " + err.Error())

coderd/tailnet.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ func (s *ServerTailnet) ReverseProxy(targetURL, dashboardURL *url.URL, agentID u
157157

158158
type agentIDKey struct{}
159159

160+
// director makes sure agentIDKey is set on the context in the reverse proxy.
161+
// This allows the transport to correctly identify which agent to dial to.
160162
func (*ServerTailnet) director(agentID uuid.UUID, prev func(req *http.Request)) func(req *http.Request) {
161163
return func(req *http.Request) {
162164
ctx := context.WithValue(req.Context(), agentIDKey{}, agentID)
@@ -278,7 +280,7 @@ func (s *ServerTailnet) AgentConn(ctx context.Context, agentID uuid.UUID) (*code
278280
conn = codersdk.NewWorkspaceAgentConn(s.conn, codersdk.WorkspaceAgentConnOptions{
279281
AgentID: agentID,
280282
GetNode: s.getNode,
281-
CloseFunc: func() {},
283+
CloseFunc: func() error { return codersdk.ErrSkipClose },
282284
})
283285
}
284286

coderd/tailnet_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func setupAgent(t *testing.T, agentAddresses []netip.Prefix) (uuid.UUID, agent.A
187187
}
188188
return node, nil
189189
},
190-
CloseFunc: func() {},
190+
CloseFunc: func() error { return codersdk.ErrSkipClose },
191191
}), nil
192192
}, 0)
193193

coderd/workspaceagents.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,8 @@ func (api *API) workspaceAgentListeningPorts(rw http.ResponseWriter, r *http.Req
729729
httpapi.Write(ctx, rw, http.StatusOK, portsResponse)
730730
}
731731

732-
func (api *API) dialWorkspaceAgentTailnet(agentID uuid.UUID) (*codersdk.WorkspaceAgentConn, error) {
732+
// Deprecated: use api.tailnet.AgentConn instead.
733+
func (api *API) _dialWorkspaceAgentTailnet(agentID uuid.UUID) (*codersdk.WorkspaceAgentConn, error) {
733734
clientConn, serverConn := net.Pipe()
734735
conn, err := tailnet.NewConn(&tailnet.Options{
735736
Addresses: []netip.Prefix{netip.PrefixFrom(tailnet.IP(), 128)},
@@ -769,13 +770,16 @@ func (api *API) dialWorkspaceAgentTailnet(agentID uuid.UUID) (*codersdk.Workspac
769770
AgentID: agentID,
770771
GetNode: func(agentID uuid.UUID) (*tailnet.Node, error) {
771772
return &tailnet.Node{
773+
// Since this is a legacy function only used by wsconncache as a
774+
// fallback, we hardcode the node to use the wsconncache IP.
772775
Addresses: []netip.Prefix{netip.PrefixFrom(codersdk.WorkspaceAgentIP, 128)},
773776
}, nil
774777
},
775-
CloseFunc: func() {
778+
CloseFunc: func() error {
776779
cancel()
777780
_ = clientConn.Close()
778781
_ = serverConn.Close()
782+
return nil
779783
},
780784
})
781785
go func() {

codersdk/workspaceagentconn.go

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"time"
1616

1717
"github.com/google/uuid"
18+
"github.com/hashicorp/go-multierror"
1819
"golang.org/x/crypto/ssh"
1920
"golang.org/x/xerrors"
2021
"tailscale.com/ipn/ipnstate"
@@ -27,8 +28,13 @@ import (
2728
// WorkspaceAgentIP is a static IPv6 address with the Tailscale prefix that is used to route
2829
// connections from clients to this node. A dynamic address is not required because a Tailnet
2930
// client only dials a single agent at a time.
31+
//
32+
// Deprecated: use tailnet.IP() instead. This is kept for backwards
33+
// compatibility with wsconncache.
3034
var WorkspaceAgentIP = netip.MustParseAddr("fd7a:115c:a1e0:49d6:b259:b7ac:b1b2:48f4")
3135

36+
var ErrSkipClose = xerrors.New("skip tailnet close")
37+
3238
const (
3339
WorkspaceAgentSSHPort = tailnet.WorkspaceAgentSSHPort
3440
WorkspaceAgentReconnectingPTYPort = tailnet.WorkspaceAgentReconnectingPTYPort
@@ -120,33 +126,10 @@ func init() {
120126
}
121127
}
122128

123-
type AgentConn interface {
124-
// AwaitReachable waits for the agent to be reachable.
125-
AwaitReachable(ctx context.Context) bool
126-
// Ping pings the agent and returns the round-trip time.
127-
// The bool returns true if the ping was made P2P.
128-
Ping(ctx context.Context) (time.Duration, bool, *ipnstate.PingResult, error)
129-
// Close ends the connection to the workspace agent.
130-
Close() error
131-
// ReconnectingPTY spawns a new reconnecting terminal session.
132-
// `ReconnectingPTYRequest` should be JSON marshaled and written to the returned net.Conn.
133-
// Raw terminal output will be read from the returned net.Conn.
134-
ReconnectingPTY(ctx context.Context, id uuid.UUID, height, width uint16, command string) (net.Conn, error)
135-
// SSH pipes the SSH protocol over the returned net.Conn.
136-
// This connects to the built-in SSH server in the workspace agent.
137-
SSH(ctx context.Context) (net.Conn, error)
138-
// SSHClient calls SSH to create a client that uses a weak cipher
139-
// to improve throughput.
140-
SSHClient(ctx context.Context) (*ssh.Client, error)
141-
// Speedtest runs a speedtest against the workspace agent.
142-
Speedtest(ctx context.Context, direction speedtest.Direction, duration time.Duration) ([]speedtest.Result, error)
143-
// DialContext dials the address provided in the workspace agent.
144-
// The network must be "tcp" or "udp".
145-
DialContext(ctx context.Context, network string, addr string) (net.Conn, error)
146-
// ListeningPorts lists the ports that are currently in use by the workspace.
147-
ListeningPorts(ctx context.Context) (WorkspaceAgentListeningPortsResponse, error)
148-
}
149-
129+
// NewWorkspaceAgentConn creates a new WorkspaceAgentConn. `conn` may be unique
130+
// to the WorkspaceAgentConn, or it may be shared in the case of coderd. If the
131+
// conn is shared and closing it is undesirable, you may return ErrNoClose from
132+
// opts.CloseFunc. This will ensure the underlying conn is not closed.
150133
func NewWorkspaceAgentConn(conn *tailnet.Conn, opts WorkspaceAgentConnOptions) *WorkspaceAgentConn {
151134
return &WorkspaceAgentConn{
152135
Conn: conn,
@@ -165,7 +148,7 @@ type WorkspaceAgentConn struct {
165148
type WorkspaceAgentConnOptions struct {
166149
AgentID uuid.UUID
167150
GetNode func(agentID uuid.UUID) (*tailnet.Node, error)
168-
CloseFunc func()
151+
CloseFunc func() error
169152
}
170153

171154
func (c *WorkspaceAgentConn) getAgentAddress() (netip.Addr, error) {
@@ -219,8 +202,15 @@ func (c *WorkspaceAgentConn) Ping(ctx context.Context) (time.Duration, bool, *ip
219202

220203
// Close ends the connection to the workspace agent.
221204
func (c *WorkspaceAgentConn) Close() error {
205+
var cerr error
222206
if c.opts.CloseFunc != nil {
223-
c.opts.CloseFunc()
207+
cerr = c.opts.CloseFunc()
208+
if xerrors.Is(cerr, ErrSkipClose) {
209+
return nil
210+
}
211+
}
212+
if cerr != nil {
213+
return multierror.Append(cerr, c.Conn.Close())
224214
}
225215
return c.Conn.Close()
226216
}

codersdk/workspaceagents.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,10 @@ func (c *Client) DialWorkspaceAgent(ctx context.Context, agentID uuid.UUID, opti
328328
}
329329
return node, nil
330330
},
331-
CloseFunc: func() {
331+
CloseFunc: func() error {
332332
cancel()
333333
<-closed
334+
return conn.Close()
334335
},
335336
})
336337

enterprise/tailnet/coordinator.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ func NewCoordinator(logger slog.Logger, ps pubsub.Pubsub) (agpl.Coordinator, err
5151
}
5252

5353
func (c *haCoordinator) SubscribeAgent(agentID uuid.UUID, cb func(agentID uuid.UUID, node *agpl.Node)) func() {
54-
_, _ = agentID, cb
55-
5654
c.mutex.Lock()
5755
defer c.mutex.Unlock()
5856

scaletest/agentconn/run.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,8 @@ func agentHTTPClient(conn *codersdk.WorkspaceAgentConn) *http.Client {
377377
return nil, xerrors.Errorf("parse port %q: %w", port, err)
378378
}
379379

380+
// Addr doesn't matter here, besides the port. DialContext will
381+
// automatically choose the right IP to dial.
380382
return conn.DialContext(ctx, "tcp", fmt.Sprintf("127.0.0.1:%d", portUint))
381383
},
382384
},

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy