Skip to content

Commit d150a25

Browse files
committed
review
1 parent 8c551a7 commit d150a25

File tree

2 files changed

+21
-31
lines changed

2 files changed

+21
-31
lines changed

vpn/tunnel.go

+20-29
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,12 @@ func (t *Tunnel) handleRPC(req *ManagerMessage, msgID uint64) *TunnelMessage {
147147
state, err := t.conn.CurrentWorkspaceState()
148148
if err != nil {
149149
t.logger.Critical(t.ctx, "failed to get current workspace state", slog.Error(err))
150+
resp.Msg = &TunnelMessage_PeerUpdate{
151+
PeerUpdate: nil,
152+
}
153+
return resp
150154
}
151-
update, err := t.createPeerUpdate(state)
152-
if err != nil {
153-
t.logger.Error(t.ctx, "failed to populate agent network info", slog.Error(err))
154-
}
155+
update := t.createPeerUpdate(state)
155156
resp.Msg = &TunnelMessage_PeerUpdate{
156157
PeerUpdate: update,
157158
}
@@ -240,10 +241,7 @@ func (t *Tunnel) ApplyNetworkSettings(ctx context.Context, ns *NetworkSettingsRe
240241
}
241242

242243
func (t *Tunnel) Update(update tailnet.WorkspaceUpdate) error {
243-
peerUpdate, err := t.createPeerUpdate(update)
244-
if err != nil {
245-
t.logger.Error(t.ctx, "failed to populate agent network info", slog.Error(err))
246-
}
244+
peerUpdate := t.createPeerUpdate(update)
247245
msg := &TunnelMessage{
248246
Msg: &TunnelMessage_PeerUpdate{
249247
PeerUpdate: peerUpdate,
@@ -345,7 +343,7 @@ func sinkEntryToPb(e slog.SinkEntry) *Log {
345343

346344
// createPeerUpdate creates a PeerUpdate message from a workspace update, populating
347345
// the network status of the agents.
348-
func (t *Tunnel) createPeerUpdate(update tailnet.WorkspaceUpdate) (*PeerUpdate, error) {
346+
func (t *Tunnel) createPeerUpdate(update tailnet.WorkspaceUpdate) *PeerUpdate {
349347
out := &PeerUpdate{
350348
UpsertedWorkspaces: make([]*Workspace, len(update.UpsertedWorkspaces)),
351349
UpsertedAgents: make([]*Agent, len(update.UpsertedAgents)),
@@ -362,10 +360,7 @@ func (t *Tunnel) createPeerUpdate(update tailnet.WorkspaceUpdate) (*PeerUpdate,
362360
Status: Workspace_Status(ws.Status),
363361
}
364362
}
365-
upsertedAgents, err := t.populateAgents(update.UpsertedAgents)
366-
if err != nil {
367-
return nil, xerrors.Errorf("failed to populate agent network info: %w", err)
368-
}
363+
upsertedAgents := t.convertAgents(update.UpsertedAgents)
369364
out.UpsertedAgents = upsertedAgents
370365
for i, ws := range update.DeletedWorkspaces {
371366
out.DeletedWorkspaces[i] = &Workspace{
@@ -388,15 +383,12 @@ func (t *Tunnel) createPeerUpdate(update tailnet.WorkspaceUpdate) (*PeerUpdate,
388383
LastHandshake: nil,
389384
}
390385
}
391-
return out, nil
386+
return out
392387
}
393388

394-
// Given a list of `tailnet.Agent`, populate their network info, and convert them to proto agents.
395-
func (t *Tunnel) populateAgents(agents []*tailnet.Agent) ([]*Agent, error) {
396-
if t.conn == nil {
397-
return nil, xerrors.New("no active connection")
398-
}
399-
389+
// convertAgents takes a list of `tailnet.Agent` and converts them to proto agents.
390+
// If there is an active connection, the last handshake time is populated.
391+
func (t *Tunnel) convertAgents(agents []*tailnet.Agent) []*Agent {
400392
out := make([]*Agent, 0, len(agents))
401393

402394
for _, agent := range agents {
@@ -411,12 +403,14 @@ func (t *Tunnel) populateAgents(agents []*tailnet.Agent) ([]*Agent, error) {
411403
Fqdn: fqdn,
412404
IpAddrs: hostsToIPStrings(agent.Hosts),
413405
}
414-
diags := t.conn.GetPeerDiagnostics(agent.ID)
415-
protoAgent.LastHandshake = timestamppb.New(diags.LastWireguardHandshake)
406+
if t.conn != nil {
407+
diags := t.conn.GetPeerDiagnostics(agent.ID)
408+
protoAgent.LastHandshake = timestamppb.New(diags.LastWireguardHandshake)
409+
}
416410
out = append(out, protoAgent)
417411
}
418412

419-
return out, nil
413+
return out
420414
}
421415

422416
// saveUpdate saves the workspace update to the tunnel's state, such that it can
@@ -441,12 +435,7 @@ func (t *Tunnel) sendAgentUpdate() {
441435
t.mu.Lock()
442436
defer t.mu.Unlock()
443437

444-
upsertedAgents, err := t.populateAgents(maps.Values(t.agents))
445-
if err != nil {
446-
t.logger.Error(t.ctx, "failed to produce agent network status update", slog.Error(err))
447-
return
448-
}
449-
438+
upsertedAgents := t.convertAgents(maps.Values(t.agents))
450439
if len(upsertedAgents) == 0 {
451440
return
452441
}
@@ -524,6 +513,8 @@ func quote(key string) string {
524513
return quoted
525514
}
526515

516+
// hostsToIPStrings returns a slice of all unique IP addresses in the values
517+
// of the given map.
527518
func hostsToIPStrings(hosts map[dnsname.FQDN][]netip.Addr) []string {
528519
seen := make(map[netip.Addr]struct{})
529520
var result []string

vpn/tunnel_internal_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func TestTunnel_createPeerUpdate(t *testing.T) {
286286
hsTime := time.Now().Add(-time.Minute).UTC()
287287
tun.conn = newFakeConn(tailnet.WorkspaceUpdate{}, hsTime)
288288

289-
update, err := tun.createPeerUpdate(tailnet.WorkspaceUpdate{
289+
update := tun.createPeerUpdate(tailnet.WorkspaceUpdate{
290290
UpsertedWorkspaces: []*tailnet.Workspace{
291291
{ID: w1ID, Name: "w1", Status: proto.Workspace_STARTING},
292292
},
@@ -314,7 +314,6 @@ func TestTunnel_createPeerUpdate(t *testing.T) {
314314
},
315315
},
316316
})
317-
require.NoError(t, err)
318317
require.Len(t, update.UpsertedAgents, 1)
319318
slices.SortFunc(update.UpsertedAgents[0].Fqdn, func(a, b string) int {
320319
return strings.Compare(a, b)

0 commit comments

Comments
 (0)