MCPcopy Create free account
hub / github.com/coder/coder / createPeerUpdateLocked

Method createPeerUpdateLocked

vpn/tunnel.go:409–483  ·  view source on GitHub ↗

createPeerUpdateLocked creates a PeerUpdate message from a workspace update, populating the network status of the agents.

(update tailnet.WorkspaceUpdate)

Source from the content-addressed store, hash-verified

407// createPeerUpdateLocked creates a PeerUpdate message from a workspace update, populating
408// the network status of the agents.
409func (u *updater) createPeerUpdateLocked(update tailnet.WorkspaceUpdate) *PeerUpdate {
410 // if the update is a snapshot, we need to process the full state
411 if update.Kind == tailnet.Snapshot {
412 processSnapshotUpdate(&update, u.agents, u.workspaces)
413 }
414
415 out := &PeerUpdate{
416 UpsertedWorkspaces: make([]*Workspace, len(update.UpsertedWorkspaces)),
417 UpsertedAgents: make([]*Agent, len(update.UpsertedAgents)),
418 DeletedWorkspaces: make([]*Workspace, len(update.DeletedWorkspaces)),
419 DeletedAgents: make([]*Agent, len(update.DeletedAgents)),
420 }
421
422 var upsertedAgentsWithPing []*agentWithPing
423
424 // save the workspace update to the tunnel's state, such that it can
425 // be used to populate automated peer updates.
426 for _, agent := range update.UpsertedAgents {
427 var lastPing *lastPing
428 if existing, ok := u.agents[agent.ID]; ok {
429 lastPing = existing.lastPing
430 }
431 upsertedAgent := agentWithPing{
432 Agent: agent.Clone(),
433 lastPing: lastPing,
434 }
435 u.agents[agent.ID] = upsertedAgent
436 upsertedAgentsWithPing = append(upsertedAgentsWithPing, &upsertedAgent)
437 }
438 for _, agent := range update.DeletedAgents {
439 delete(u.agents, agent.ID)
440 }
441 for _, workspace := range update.UpsertedWorkspaces {
442 u.workspaces[workspace.ID] = workspace.Clone()
443 }
444 for _, workspace := range update.DeletedWorkspaces {
445 delete(u.workspaces, workspace.ID)
446 }
447
448 for i, ws := range update.UpsertedWorkspaces {
449 out.UpsertedWorkspaces[i] = &Workspace{
450 Id: tailnet.UUIDToByteSlice(ws.ID),
451 Name: ws.Name,
452 Status: Workspace_Status(ws.Status),
453 }
454 }
455
456 upsertedAgents := u.convertAgentsLocked(upsertedAgentsWithPing)
457 out.UpsertedAgents = upsertedAgents
458 for i, ws := range update.DeletedWorkspaces {
459 out.DeletedWorkspaces[i] = &Workspace{
460 Id: tailnet.UUIDToByteSlice(ws.ID),
461 Name: ws.Name,
462 Status: Workspace_Status(ws.Status),
463 }
464 }
465 for i, agent := range update.DeletedAgents {
466 fqdn := make([]string, 0, len(agent.Hosts))

Callers 3

UpdateMethod · 0.95
sendUpdateResponseMethod · 0.95

Calls 6

convertAgentsLockedMethod · 0.95
UUIDToByteSliceFunction · 0.92
processSnapshotUpdateFunction · 0.85
hostsToIPStringsFunction · 0.85
Workspace_StatusTypeAlias · 0.70
CloneMethod · 0.45

Tested by 1