| 1049 | } |
| 1050 | |
| 1051 | func (t *TunnelAllWorkspaceUpdatesController) New(client WorkspaceUpdatesClient) CloserWaiter { |
| 1052 | t.mu.Lock() |
| 1053 | defer t.mu.Unlock() |
| 1054 | |
| 1055 | // Preserve workspace state from the previous updater so that DNS |
| 1056 | // hosts remain programmed while we wait for the new server |
| 1057 | // snapshot. Without this, a control-plane reconnection would |
| 1058 | // leave the internal DNS resolver empty until the first |
| 1059 | // workspace update arrives, breaking .coder resolution. |
| 1060 | var previousWorkspaces map[uuid.UUID]*Workspace |
| 1061 | if t.updater != nil { |
| 1062 | t.updater.Lock() |
| 1063 | if len(t.updater.workspaces) > 0 { |
| 1064 | previousWorkspaces = make(map[uuid.UUID]*Workspace, len(t.updater.workspaces)) |
| 1065 | for id, w := range t.updater.workspaces { |
| 1066 | clone := w.Clone() |
| 1067 | previousWorkspaces[id] = &clone |
| 1068 | } |
| 1069 | } |
| 1070 | t.updater.Unlock() |
| 1071 | } |
| 1072 | |
| 1073 | workspaces := make(map[uuid.UUID]*Workspace) |
| 1074 | if previousWorkspaces != nil { |
| 1075 | workspaces = previousWorkspaces |
| 1076 | } |
| 1077 | |
| 1078 | updater := &tunnelUpdater{ |
| 1079 | client: client, |
| 1080 | errChan: make(chan error, 1), |
| 1081 | logger: t.logger, |
| 1082 | coordCtrl: t.coordCtrl, |
| 1083 | dnsHostsSetter: t.dnsHostSetter, |
| 1084 | dnsNameOptions: t.dnsNameOptions, |
| 1085 | updateHandler: t.updateHandler, |
| 1086 | ownerUsername: t.ownerUsername, |
| 1087 | recvLoopDone: make(chan struct{}), |
| 1088 | workspaces: workspaces, |
| 1089 | } |
| 1090 | |
| 1091 | // If we inherited workspace state, immediately re-program DNS |
| 1092 | // hosts so the resolver stays populated during the reconnection |
| 1093 | // window. |
| 1094 | if len(previousWorkspaces) > 0 { |
| 1095 | dnsNames := updater.updateDNSNamesLocked() |
| 1096 | if updater.dnsHostsSetter != nil { |
| 1097 | t.logger.Debug(context.Background(), "re-applying DNS hosts from previous session", |
| 1098 | slog.F("num_hosts", len(dnsNames)), |
| 1099 | ) |
| 1100 | if err := updater.dnsHostsSetter.SetDNSHosts(dnsNames); err != nil { |
| 1101 | t.logger.Warn(context.Background(), "failed to re-apply DNS hosts from previous session", |
| 1102 | slog.Error(err), |
| 1103 | ) |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | t.updater = updater |