@@ -147,11 +147,12 @@ func (t *Tunnel) handleRPC(req *ManagerMessage, msgID uint64) *TunnelMessage {
147
147
state , err := t .conn .CurrentWorkspaceState ()
148
148
if err != nil {
149
149
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
150
154
}
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 )
155
156
resp .Msg = & TunnelMessage_PeerUpdate {
156
157
PeerUpdate : update ,
157
158
}
@@ -240,10 +241,7 @@ func (t *Tunnel) ApplyNetworkSettings(ctx context.Context, ns *NetworkSettingsRe
240
241
}
241
242
242
243
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 )
247
245
msg := & TunnelMessage {
248
246
Msg : & TunnelMessage_PeerUpdate {
249
247
PeerUpdate : peerUpdate ,
@@ -345,7 +343,7 @@ func sinkEntryToPb(e slog.SinkEntry) *Log {
345
343
346
344
// createPeerUpdate creates a PeerUpdate message from a workspace update, populating
347
345
// 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 {
349
347
out := & PeerUpdate {
350
348
UpsertedWorkspaces : make ([]* Workspace , len (update .UpsertedWorkspaces )),
351
349
UpsertedAgents : make ([]* Agent , len (update .UpsertedAgents )),
@@ -362,10 +360,7 @@ func (t *Tunnel) createPeerUpdate(update tailnet.WorkspaceUpdate) (*PeerUpdate,
362
360
Status : Workspace_Status (ws .Status ),
363
361
}
364
362
}
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 )
369
364
out .UpsertedAgents = upsertedAgents
370
365
for i , ws := range update .DeletedWorkspaces {
371
366
out .DeletedWorkspaces [i ] = & Workspace {
@@ -388,15 +383,12 @@ func (t *Tunnel) createPeerUpdate(update tailnet.WorkspaceUpdate) (*PeerUpdate,
388
383
LastHandshake : nil ,
389
384
}
390
385
}
391
- return out , nil
386
+ return out
392
387
}
393
388
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 {
400
392
out := make ([]* Agent , 0 , len (agents ))
401
393
402
394
for _ , agent := range agents {
@@ -411,12 +403,14 @@ func (t *Tunnel) populateAgents(agents []*tailnet.Agent) ([]*Agent, error) {
411
403
Fqdn : fqdn ,
412
404
IpAddrs : hostsToIPStrings (agent .Hosts ),
413
405
}
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
+ }
416
410
out = append (out , protoAgent )
417
411
}
418
412
419
- return out , nil
413
+ return out
420
414
}
421
415
422
416
// saveUpdate saves the workspace update to the tunnel's state, such that it can
@@ -441,12 +435,7 @@ func (t *Tunnel) sendAgentUpdate() {
441
435
t .mu .Lock ()
442
436
defer t .mu .Unlock ()
443
437
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 ))
450
439
if len (upsertedAgents ) == 0 {
451
440
return
452
441
}
@@ -524,6 +513,8 @@ func quote(key string) string {
524
513
return quoted
525
514
}
526
515
516
+ // hostsToIPStrings returns a slice of all unique IP addresses in the values
517
+ // of the given map.
527
518
func hostsToIPStrings (hosts map [dnsname.FQDN ][]netip.Addr ) []string {
528
519
seen := make (map [netip.Addr ]struct {})
529
520
var result []string
0 commit comments