Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf7aad62

Browse files
committed
review p2
1 parent71a6e59 commitf7aad62

File tree

8 files changed

+333
-151
lines changed

8 files changed

+333
-151
lines changed

‎tailnet/controllers.go

Lines changed: 156 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ type DNSHostsSetter interface {
114114

115115
// UpdatesHandler is anything that expects a stream of workspace update diffs.
116116
typeUpdatesHandlerinterface {
117-
Update(*proto.WorkspaceUpdate)error
117+
Update(WorkspaceUpdate)error
118118
}
119119

120120
// ControlProtocolClients represents an abstract interface to the tailnet control plane via a set
@@ -871,43 +871,51 @@ type TunnelAllWorkspaceUpdatesController struct {
871871
updater*tunnelUpdater
872872
}
873873

874-
typeworkspacestruct {
875-
id uuid.UUID
876-
namestring
877-
agentsmap[uuid.UUID]agent
874+
typeWorkspacestruct {
875+
ID uuid.UUID
876+
Namestring
877+
Status proto.Workspace_Status
878+
879+
ownerUsernamestring
880+
agentsmap[uuid.UUID]*Agent
878881
}
879882

880-
// addAllDNSNames adds names for all of its agents to the given map of names
881-
func (wworkspace)addAllDNSNames(namesmap[dnsname.FQDN][]netip.Addr,ownerstring)error {
882-
for_,a:=rangew.agents {
883+
// updateDNSNames updates the DNS names for all agents in the workspace.
884+
func (w*Workspace)updateDNSNames()error {
885+
forid,a:=rangew.agents {
886+
names:=make(map[dnsname.FQDN][]netip.Addr)
883887
// TODO: technically, DNS labels cannot start with numbers, but the rules are often not
884888
// strictly enforced.
885-
fqdn,err:=dnsname.ToFQDN(fmt.Sprintf("%s.%s.me.coder.",a.name,w.name))
886-
iferr!=nil {
887-
returnerr
888-
}
889-
names[fqdn]= []netip.Addr{CoderServicePrefix.AddrFromUUID(a.id)}
890-
fqdn,err=dnsname.ToFQDN(fmt.Sprintf("%s.%s.%s.coder.",a.name,w.name,owner))
889+
fqdn,err:=dnsname.ToFQDN(fmt.Sprintf("%s.%s.me.coder.",a.Name,w.Name))
891890
iferr!=nil {
892891
returnerr
893892
}
894-
names[fqdn]= []netip.Addr{CoderServicePrefix.AddrFromUUID(a.id)}
895-
}
896-
iflen(w.agents)==1 {
897-
fqdn,err:=dnsname.ToFQDN(fmt.Sprintf("%s.coder.",w.name))
893+
names[fqdn]= []netip.Addr{CoderServicePrefix.AddrFromUUID(a.ID)}
894+
fqdn,err=dnsname.ToFQDN(fmt.Sprintf("%s.%s.%s.coder.",a.Name,w.Name,w.ownerUsername))
898895
iferr!=nil {
899896
returnerr
900897
}
901-
for_,a:=rangew.agents {
902-
names[fqdn]= []netip.Addr{CoderServicePrefix.AddrFromUUID(a.id)}
898+
names[fqdn]= []netip.Addr{CoderServicePrefix.AddrFromUUID(a.ID)}
899+
iflen(w.agents)==1 {
900+
fqdn,err:=dnsname.ToFQDN(fmt.Sprintf("%s.coder.",w.Name))
901+
iferr!=nil {
902+
returnerr
903+
}
904+
for_,a:=rangew.agents {
905+
names[fqdn]= []netip.Addr{CoderServicePrefix.AddrFromUUID(a.ID)}
906+
}
903907
}
908+
a.Hosts=names
909+
w.agents[id]=a
904910
}
905911
returnnil
906912
}
907913

908-
typeagentstruct {
909-
id uuid.UUID
910-
namestring
914+
typeAgentstruct {
915+
ID uuid.UUID
916+
Namestring
917+
WorkspaceID uuid.UUID
918+
Hostsmap[dnsname.FQDN][]netip.Addr
911919
}
912920

913921
func (t*TunnelAllWorkspaceUpdatesController)New(clientWorkspaceUpdatesClient)CloserWaiter {
@@ -922,40 +930,43 @@ func (t *TunnelAllWorkspaceUpdatesController) New(client WorkspaceUpdatesClient)
922930
updateHandler:t.updateHandler,
923931
ownerUsername:t.ownerUsername,
924932
recvLoopDone:make(chanstruct{}),
925-
workspaces:make(map[uuid.UUID]*workspace),
933+
workspaces:make(map[uuid.UUID]*Workspace),
926934
}
927935
t.updater=updater
928936
got.updater.recvLoop()
929937
returnt.updater
930938
}
931939

932-
func (t*TunnelAllWorkspaceUpdatesController)CurrentState()*proto.WorkspaceUpdate {
940+
func (t*TunnelAllWorkspaceUpdatesController)CurrentState()(WorkspaceUpdate,error) {
933941
t.mu.Lock()
934942
defert.mu.Unlock()
935943
ift.updater==nil {
936-
returnnil
944+
returnWorkspaceUpdate{},xerrors.New("no updater")
937945
}
938946
t.updater.Lock()
939947
defert.updater.Unlock()
940-
out:=&proto.WorkspaceUpdate{
941-
UpsertedWorkspaces:make([]*proto.Workspace,0,len(t.updater.workspaces)),
942-
UpsertedAgents:make([]*proto.Agent,0,len(t.updater.workspaces)),
948+
out:=WorkspaceUpdate{
949+
UpsertedWorkspaces:make([]*Workspace,0,len(t.updater.workspaces)),
950+
UpsertedAgents:make([]*Agent,0,len(t.updater.workspaces)),
951+
DeletedWorkspaces:make([]*Workspace,0),
952+
DeletedAgents:make([]*Agent,0),
943953
}
944954
for_,w:=ranget.updater.workspaces {
945-
upw:=&proto.Workspace{
946-
Id:UUIDToByteSlice(w.id),
947-
Name:w.name,
948-
}
949-
out.UpsertedWorkspaces=append(out.UpsertedWorkspaces,upw)
955+
out.UpsertedWorkspaces=append(out.UpsertedWorkspaces,&Workspace{
956+
ID:w.ID,
957+
Name:w.Name,
958+
Status:w.Status,
959+
})
950960
for_,a:=rangew.agents {
951-
out.UpsertedAgents=append(out.UpsertedAgents,&proto.Agent{
952-
Id:UUIDToByteSlice(a.id),
953-
Name:a.name,
954-
WorkspaceId:UUIDToByteSlice(w.id),
961+
out.UpsertedAgents=append(out.UpsertedAgents,&Agent{
962+
ID:a.ID,
963+
Name:a.Name,
964+
WorkspaceID:a.WorkspaceID,
965+
Hosts:maps.Clone(a.Hosts),
955966
})
956967
}
957968
}
958-
returnout
969+
returnout,nil
959970
}
960971

961972
typetunnelUpdaterstruct {
@@ -969,7 +980,7 @@ type tunnelUpdater struct {
969980
recvLoopDonechanstruct{}
970981

971982
sync.Mutex
972-
workspacesmap[uuid.UUID]*workspace
983+
workspacesmap[uuid.UUID]*Workspace
973984
closedbool
974985
}
975986

@@ -1031,20 +1042,78 @@ func (t *tunnelUpdater) recvLoop() {
10311042
}
10321043
}
10331044

1045+
typeWorkspaceUpdatestruct {
1046+
UpsertedWorkspaces []*Workspace
1047+
UpsertedAgents []*Agent
1048+
DeletedWorkspaces []*Workspace
1049+
DeletedAgents []*Agent
1050+
}
1051+
1052+
func (w*WorkspaceUpdate)Clone()WorkspaceUpdate {
1053+
clone:=WorkspaceUpdate{
1054+
UpsertedWorkspaces:make([]*Workspace,len(w.UpsertedWorkspaces)),
1055+
UpsertedAgents:make([]*Agent,len(w.UpsertedAgents)),
1056+
DeletedWorkspaces:make([]*Workspace,len(w.DeletedWorkspaces)),
1057+
DeletedAgents:make([]*Agent,len(w.DeletedAgents)),
1058+
}
1059+
fori,ws:=rangew.UpsertedWorkspaces {
1060+
clone.UpsertedWorkspaces[i]=&Workspace{
1061+
ID:ws.ID,
1062+
Name:ws.Name,
1063+
Status:ws.Status,
1064+
}
1065+
}
1066+
fori,a:=rangew.UpsertedAgents {
1067+
clone.UpsertedAgents[i]=&Agent{
1068+
ID:a.ID,
1069+
Name:a.Name,
1070+
WorkspaceID:a.WorkspaceID,
1071+
Hosts:maps.Clone(a.Hosts),
1072+
}
1073+
}
1074+
fori,ws:=rangew.DeletedWorkspaces {
1075+
clone.DeletedWorkspaces[i]=&Workspace{
1076+
ID:ws.ID,
1077+
Name:ws.Name,
1078+
Status:ws.Status,
1079+
}
1080+
}
1081+
fori,a:=rangew.DeletedAgents {
1082+
clone.DeletedAgents[i]=&Agent{
1083+
ID:a.ID,
1084+
Name:a.Name,
1085+
WorkspaceID:a.WorkspaceID,
1086+
Hosts:maps.Clone(a.Hosts),
1087+
}
1088+
}
1089+
returnclone
1090+
}
1091+
10341092
func (t*tunnelUpdater)handleUpdate(update*proto.WorkspaceUpdate)error {
10351093
t.Lock()
10361094
defert.Unlock()
1095+
1096+
currentUpdate:=WorkspaceUpdate{
1097+
UpsertedWorkspaces: []*Workspace{},
1098+
UpsertedAgents: []*Agent{},
1099+
DeletedWorkspaces: []*Workspace{},
1100+
DeletedAgents: []*Agent{},
1101+
}
1102+
10371103
for_,uw:=rangeupdate.UpsertedWorkspaces {
10381104
workspaceID,err:=uuid.FromBytes(uw.Id)
10391105
iferr!=nil {
10401106
returnxerrors.Errorf("failed to parse workspace ID: %w",err)
10411107
}
1042-
w:=workspace{
1043-
id:workspaceID,
1044-
name:uw.Name,
1045-
agents:make(map[uuid.UUID]agent),
1108+
w:=&Workspace{
1109+
ID:workspaceID,
1110+
Name:uw.Name,
1111+
Status:uw.Status,
1112+
ownerUsername:t.ownerUsername,
1113+
agents:make(map[uuid.UUID]*Agent),
10461114
}
10471115
t.upsertWorkspace(w)
1116+
currentUpdate.UpsertedWorkspaces=append(currentUpdate.UpsertedWorkspaces,w)
10481117
}
10491118

10501119
// delete agents before deleting workspaces, since the agents have workspace ID references
@@ -1057,17 +1126,22 @@ func (t *tunnelUpdater) handleUpdate(update *proto.WorkspaceUpdate) error {
10571126
iferr!=nil {
10581127
returnxerrors.Errorf("failed to parse workspace ID: %w",err)
10591128
}
1060-
err=t.deleteAgent(workspaceID,agentID)
1129+
deletedAgent,err:=t.deleteAgent(workspaceID,agentID)
10611130
iferr!=nil {
10621131
returnxerrors.Errorf("failed to delete agent: %w",err)
10631132
}
1133+
currentUpdate.DeletedAgents=append(currentUpdate.DeletedAgents,deletedAgent)
10641134
}
10651135
for_,dw:=rangeupdate.DeletedWorkspaces {
10661136
workspaceID,err:=uuid.FromBytes(dw.Id)
10671137
iferr!=nil {
10681138
returnxerrors.Errorf("failed to parse workspace ID: %w",err)
10691139
}
1070-
t.deleteWorkspace(workspaceID)
1140+
deletedWorkspace,err:=t.deleteWorkspace(workspaceID)
1141+
iferr!=nil {
1142+
returnxerrors.Errorf("failed to delete workspace: %w",err)
1143+
}
1144+
currentUpdate.DeletedWorkspaces=append(currentUpdate.DeletedWorkspaces,deletedWorkspace)
10711145
}
10721146

10731147
// upsert agents last, after all workspaces have been added and deleted, since agents reference
@@ -1081,17 +1155,18 @@ func (t *tunnelUpdater) handleUpdate(update *proto.WorkspaceUpdate) error {
10811155
iferr!=nil {
10821156
returnxerrors.Errorf("failed to parse workspace ID: %w",err)
10831157
}
1084-
a:=agent{name:ua.Name,id:agentID}
1158+
a:=&Agent{Name:ua.Name,ID:agentID,WorkspaceID:workspaceID}
10851159
err=t.upsertAgent(workspaceID,a)
10861160
iferr!=nil {
10871161
returnxerrors.Errorf("failed to upsert agent: %w",err)
10881162
}
1163+
currentUpdate.UpsertedAgents=append(currentUpdate.UpsertedAgents,a)
10891164
}
10901165
allAgents:=t.allAgentIDs()
10911166
t.coordCtrl.SyncDestinations(allAgents)
1167+
dnsNames:=t.updateDNSNames()
10921168
ift.dnsHostsSetter!=nil {
10931169
t.logger.Debug(context.Background(),"updating dns hosts")
1094-
dnsNames:=t.allDNSNames()
10951170
err:=t.dnsHostsSetter.SetDNSHosts(dnsNames)
10961171
iferr!=nil {
10971172
returnxerrors.Errorf("failed to set DNS hosts: %w",err)
@@ -1101,43 +1176,55 @@ func (t *tunnelUpdater) handleUpdate(update *proto.WorkspaceUpdate) error {
11011176
}
11021177
ift.updateHandler!=nil {
11031178
t.logger.Debug(context.Background(),"calling update handler")
1104-
err:=t.updateHandler.Update(update)
1179+
err:=t.updateHandler.Update(currentUpdate.Clone())
11051180
iferr!=nil {
11061181
t.logger.Error(context.Background(),"failed to call update handler",slog.Error(err))
11071182
}
11081183
}
11091184
returnnil
11101185
}
11111186

1112-
func (t*tunnelUpdater)upsertWorkspace(wworkspace) {
1113-
old,ok:=t.workspaces[w.id]
1187+
func (t*tunnelUpdater)upsertWorkspace(w*Workspace)*Workspace {
1188+
old,ok:=t.workspaces[w.ID]
11141189
if!ok {
1115-
t.workspaces[w.id]=&w
1116-
return
1190+
t.workspaces[w.ID]=w
1191+
returnw
11171192
}
1118-
old.name=w.name
1193+
old.Name=w.Name
1194+
old.Status=w.Status
1195+
old.ownerUsername=w.ownerUsername
1196+
returnw
11191197
}
11201198

1121-
func (t*tunnelUpdater)deleteWorkspace(id uuid.UUID) {
1199+
func (t*tunnelUpdater)deleteWorkspace(id uuid.UUID) (*Workspace,error) {
1200+
w,ok:=t.workspaces[id]
1201+
if!ok {
1202+
returnnil,xerrors.Errorf("workspace %s not found",id)
1203+
}
11221204
delete(t.workspaces,id)
1205+
returnw,nil
11231206
}
11241207

1125-
func (t*tunnelUpdater)upsertAgent(workspaceID uuid.UUID,aagent)error {
1208+
func (t*tunnelUpdater)upsertAgent(workspaceID uuid.UUID,a*Agent)error {
11261209
w,ok:=t.workspaces[workspaceID]
11271210
if!ok {
11281211
returnxerrors.Errorf("workspace %s not found",workspaceID)
11291212
}
1130-
w.agents[a.id]=a
1213+
w.agents[a.ID]=a
11311214
returnnil
11321215
}
11331216

1134-
func (t*tunnelUpdater)deleteAgent(workspaceID,id uuid.UUID)error {
1217+
func (t*tunnelUpdater)deleteAgent(workspaceID,id uuid.UUID)(*Agent,error) {
11351218
w,ok:=t.workspaces[workspaceID]
11361219
if!ok {
1137-
returnxerrors.Errorf("workspace %s not found",workspaceID)
1220+
returnnil,xerrors.Errorf("workspace %s not found",workspaceID)
1221+
}
1222+
a,ok:=w.agents[id]
1223+
if!ok {
1224+
returnnil,xerrors.Errorf("agent %s not found in workspace %s",id,workspaceID)
11381225
}
11391226
delete(w.agents,id)
1140-
returnnil
1227+
returna,nil
11411228
}
11421229

11431230
func (t*tunnelUpdater)allAgentIDs() []uuid.UUID {
@@ -1150,19 +1237,25 @@ func (t *tunnelUpdater) allAgentIDs() []uuid.UUID {
11501237
returnout
11511238
}
11521239

1153-
func (t*tunnelUpdater)allDNSNames()map[dnsname.FQDN][]netip.Addr {
1240+
// updateDNSNames updates the DNS names for all workspaces in the tunnelUpdater.
1241+
func (t*tunnelUpdater)updateDNSNames()map[dnsname.FQDN][]netip.Addr {
11541242
names:=make(map[dnsname.FQDN][]netip.Addr)
11551243
for_,w:=ranget.workspaces {
1156-
err:=w.addAllDNSNames(names,t.ownerUsername)
1244+
err:=w.updateDNSNames()
11571245
iferr!=nil {
11581246
// This should never happen in production, because converting the FQDN only fails
11591247
// if names are too long, and we put strict length limits on agent, workspace, and user
11601248
// names.
11611249
t.logger.Critical(context.Background(),
11621250
"failed to include DNS name(s)",
1163-
slog.F("workspace_id",w.id),
1251+
slog.F("workspace_id",w.ID),
11641252
slog.Error(err))
11651253
}
1254+
for_,a:=rangew.agents {
1255+
forname,addrs:=rangea.Hosts {
1256+
names[name]=addrs
1257+
}
1258+
}
11661259
}
11671260
returnnames
11681261
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp