Skip to content

Commit 0de5df2

Browse files
committed
additional tests
1 parent 288c33e commit 0de5df2

File tree

2 files changed

+341
-37
lines changed

2 files changed

+341
-37
lines changed

vpn/tunnel.go

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -394,48 +394,12 @@ func (u *updater) sendUpdateResponse(req *request[*TunnelMessage, *ManagerMessag
394394
return nil
395395
}
396396

397-
// processFreshState handles the logic for when a fresh state update is received.
398-
func (u *updater) processFreshState(update *tailnet.WorkspaceUpdate) {
399-
// ignoredWorkspaces is initially populated with the workspaces that are
400-
// in the current update. Later on we populate it with the deleted workspaces too
401-
// so that we don't send duplicate updates. Same applies to ignoredAgents.
402-
ignoredWorkspaces := make(map[uuid.UUID]struct{}, len(update.UpsertedWorkspaces))
403-
ignoredAgents := make(map[uuid.UUID]struct{}, len(update.UpsertedAgents))
404-
405-
for _, workspace := range update.UpsertedWorkspaces {
406-
ignoredWorkspaces[workspace.ID] = struct{}{}
407-
}
408-
for _, agent := range update.UpsertedAgents {
409-
ignoredAgents[agent.ID] = struct{}{}
410-
}
411-
for _, agent := range u.agents {
412-
if _, ok := ignoredAgents[agent.ID]; !ok {
413-
// delete any current agents that are not in the new update
414-
update.DeletedAgents = append(update.DeletedAgents, &tailnet.Agent{
415-
ID: agent.ID,
416-
Name: agent.Name,
417-
WorkspaceID: agent.WorkspaceID,
418-
})
419-
// if the workspace connected to an agent we're deleting,
420-
// is not present in the fresh state, add it to the deleted workspaces
421-
if _, ok := ignoredWorkspaces[agent.WorkspaceID]; !ok {
422-
update.DeletedWorkspaces = append(update.DeletedWorkspaces, &tailnet.Workspace{
423-
// other fields cannot be populated because the tunnel
424-
// only stores agents and corresponding workspaceIDs
425-
ID: agent.WorkspaceID,
426-
})
427-
ignoredWorkspaces[agent.WorkspaceID] = struct{}{}
428-
}
429-
}
430-
}
431-
}
432-
433397
// createPeerUpdateLocked creates a PeerUpdate message from a workspace update, populating
434398
// the network status of the agents.
435399
func (u *updater) createPeerUpdateLocked(update tailnet.WorkspaceUpdate) *PeerUpdate {
436400
// this flag is true on the first update after a reconnect
437401
if update.FreshState {
438-
u.processFreshState(&update)
402+
processFreshState(&update, u.agents)
439403
}
440404

441405
out := &PeerUpdate{
@@ -590,6 +554,42 @@ func (u *updater) netStatusLoop() {
590554
}
591555
}
592556

557+
// processFreshState handles the logic for when a fresh state update is received.
558+
func processFreshState(update *tailnet.WorkspaceUpdate, agents map[uuid.UUID]tailnet.Agent) {
559+
// ignoredWorkspaces is initially populated with the workspaces that are
560+
// in the current update. Later on we populate it with the deleted workspaces too
561+
// so that we don't send duplicate updates. Same applies to ignoredAgents.
562+
ignoredWorkspaces := make(map[uuid.UUID]struct{}, len(update.UpsertedWorkspaces))
563+
ignoredAgents := make(map[uuid.UUID]struct{}, len(update.UpsertedAgents))
564+
565+
for _, workspace := range update.UpsertedWorkspaces {
566+
ignoredWorkspaces[workspace.ID] = struct{}{}
567+
}
568+
for _, agent := range update.UpsertedAgents {
569+
ignoredAgents[agent.ID] = struct{}{}
570+
}
571+
for _, agent := range agents {
572+
if _, ok := ignoredAgents[agent.ID]; !ok {
573+
// delete any current agents that are not in the new update
574+
update.DeletedAgents = append(update.DeletedAgents, &tailnet.Agent{
575+
ID: agent.ID,
576+
Name: agent.Name,
577+
WorkspaceID: agent.WorkspaceID,
578+
})
579+
// if the workspace connected to an agent we're deleting,
580+
// is not present in the fresh state, add it to the deleted workspaces
581+
if _, ok := ignoredWorkspaces[agent.WorkspaceID]; !ok {
582+
update.DeletedWorkspaces = append(update.DeletedWorkspaces, &tailnet.Workspace{
583+
// other fields cannot be populated because the tunnel
584+
// only stores agents and corresponding workspaceIDs
585+
ID: agent.WorkspaceID,
586+
})
587+
ignoredWorkspaces[agent.WorkspaceID] = struct{}{}
588+
}
589+
}
590+
}
591+
}
592+
593593
// hostsToIPStrings returns a slice of all unique IP addresses in the values
594594
// of the given map.
595595
func hostsToIPStrings(hosts map[dnsname.FQDN][]netip.Addr) []string {

0 commit comments

Comments
 (0)