Skip to content

Commit 021544d

Browse files
committed
review
1 parent 74bf2a8 commit 021544d

File tree

2 files changed

+21
-31
lines changed

2 files changed

+21
-31
lines changed

vpn/tunnel.go

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

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

347345
// createPeerUpdate creates a PeerUpdate message from a workspace update, populating
348346
// the network status of the agents.
349-
func (t *Tunnel) createPeerUpdate(update tailnet.WorkspaceUpdate) (*PeerUpdate, error) {
347+
func (t *Tunnel) createPeerUpdate(update tailnet.WorkspaceUpdate) *PeerUpdate {
350348
out := &PeerUpdate{
351349
UpsertedWorkspaces: make([]*Workspace, len(update.UpsertedWorkspaces)),
352350
UpsertedAgents: make([]*Agent, len(update.UpsertedAgents)),
@@ -363,10 +361,7 @@ func (t *Tunnel) createPeerUpdate(update tailnet.WorkspaceUpdate) (*PeerUpdate,
363361
Status: Workspace_Status(ws.Status),
364362
}
365363
}
366-
upsertedAgents, err := t.populateAgents(update.UpsertedAgents)
367-
if err != nil {
368-
return nil, xerrors.Errorf("failed to populate agent network info: %w", err)
369-
}
364+
upsertedAgents := t.convertAgents(update.UpsertedAgents)
370365
out.UpsertedAgents = upsertedAgents
371366
for i, ws := range update.DeletedWorkspaces {
372367
out.DeletedWorkspaces[i] = &Workspace{
@@ -389,15 +384,12 @@ func (t *Tunnel) createPeerUpdate(update tailnet.WorkspaceUpdate) (*PeerUpdate,
389384
LastHandshake: nil,
390385
}
391386
}
392-
return out, nil
387+
return out
393388
}
394389

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

403395
for _, agent := range agents {
@@ -412,12 +404,14 @@ func (t *Tunnel) populateAgents(agents []*tailnet.Agent) ([]*Agent, error) {
412404
Fqdn: fqdn,
413405
IpAddrs: hostsToIPStrings(agent.Hosts),
414406
}
415-
diags := t.conn.GetPeerDiagnostics(agent.ID)
416-
protoAgent.LastHandshake = timestamppb.New(diags.LastWireguardHandshake)
407+
if t.conn != nil {
408+
diags := t.conn.GetPeerDiagnostics(agent.ID)
409+
protoAgent.LastHandshake = timestamppb.New(diags.LastWireguardHandshake)
410+
}
417411
out = append(out, protoAgent)
418412
}
419413

420-
return out, nil
414+
return out
421415
}
422416

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

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

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

vpn/tunnel_internal_test.go

Lines changed: 1 addition & 2 deletions
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)