- Notifications
You must be signed in to change notification settings - Fork923
feat: set DNS hostnames in workspace updates controller#15507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
20 changes: 0 additions & 20 deletionstailnet/configmaps.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
39 changes: 4 additions & 35 deletionstailnet/configmaps_internal_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletionstailnet/conn.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
87 changes: 72 additions & 15 deletionstailnet/controllers.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -6,6 +6,7 @@ import ( | ||
"io" | ||
"maps" | ||
"math" | ||
"net/netip" | ||
"strings" | ||
"sync" | ||
"time" | ||
@@ -15,6 +16,7 @@ import ( | ||
"storj.io/drpc" | ||
"storj.io/drpc/drpcerr" | ||
"tailscale.com/tailcfg" | ||
"tailscale.com/util/dnsname" | ||
"cdr.dev/slog" | ||
"github.com/coder/coder/v2/codersdk" | ||
@@ -104,6 +106,12 @@ type WorkspaceUpdatesController interface { | ||
New(WorkspaceUpdatesClient) CloserWaiter | ||
} | ||
// DNSHostsSetter is something that you can set a mapping of DNS names to IPs on. It's the subset | ||
// of the tailnet.Conn that we use to configure DNS records. | ||
type DNSHostsSetter interface { | ||
SetDNSHosts(hosts map[dnsname.FQDN][]netip.Addr) error | ||
spikecurtis marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
// ControlProtocolClients represents an abstract interface to the tailnet control plane via a set | ||
// of protocol clients. The Closer should close all the clients (e.g. by closing the underlying | ||
// connection). | ||
@@ -835,8 +843,9 @@ func (r *basicResumeTokenRefresher) refresh() { | ||
} | ||
type tunnelAllWorkspaceUpdatesController struct { | ||
coordCtrl *TunnelSrcCoordController | ||
dnsHostSetter DNSHostsSetter | ||
logger slog.Logger | ||
} | ||
type workspace struct { | ||
@@ -845,30 +854,48 @@ type workspace struct { | ||
agents map[uuid.UUID]agent | ||
} | ||
// addAllDNSNames adds names for all of its agents to the given map of names | ||
func (w workspace) addAllDNSNames(names map[dnsname.FQDN][]netip.Addr) error { | ||
for _, a := range w.agents { | ||
// TODO: technically, DNS labels cannot start with numbers, but the rules are often not | ||
// strictly enforced. | ||
// TODO: support <agent>.<workspace>.<username>.coder | ||
fqdn, err := dnsname.ToFQDN(fmt.Sprintf("%s.%s.me.coder.", a.name, w.name)) | ||
if err != nil { | ||
return err | ||
} | ||
names[fqdn] = []netip.Addr{CoderServicePrefix.AddrFromUUID(a.id)} | ||
} | ||
// TODO: Possibly support <workspace>.coder. alias if there is only one agent | ||
spikecurtis marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
return nil | ||
} | ||
type agent struct { | ||
id uuid.UUID | ||
name string | ||
} | ||
func (t *tunnelAllWorkspaceUpdatesController) New(client WorkspaceUpdatesClient) CloserWaiter { | ||
updater := &tunnelUpdater{ | ||
client: client, | ||
errChan: make(chan error, 1), | ||
logger: t.logger, | ||
coordCtrl: t.coordCtrl, | ||
dnsHostsSetter: t.dnsHostSetter, | ||
recvLoopDone: make(chan struct{}), | ||
workspaces: make(map[uuid.UUID]*workspace), | ||
} | ||
go updater.recvLoop() | ||
return updater | ||
} | ||
type tunnelUpdater struct { | ||
errChan chan error | ||
logger slog.Logger | ||
client WorkspaceUpdatesClient | ||
coordCtrl *TunnelSrcCoordController | ||
dnsHostsSetter DNSHostsSetter | ||
recvLoopDone chan struct{} | ||
// don't need the mutex since only manipulated by the recvLoop | ||
workspaces map[uuid.UUID]*workspace | ||
@@ -991,6 +1018,16 @@ func (t *tunnelUpdater) handleUpdate(update *proto.WorkspaceUpdate) error { | ||
} | ||
allAgents := t.allAgentIDs() | ||
t.coordCtrl.SyncDestinations(allAgents) | ||
if t.dnsHostsSetter != nil { | ||
t.logger.Debug(context.Background(), "updating dns hosts") | ||
dnsNames := t.allDNSNames() | ||
err := t.dnsHostsSetter.SetDNSHosts(dnsNames) | ||
if err != nil { | ||
return xerrors.Errorf("failed to set DNS hosts: %w", err) | ||
} | ||
} else { | ||
t.logger.Debug(context.Background(), "skipping setting DNS names because we have no setter") | ||
} | ||
return nil | ||
} | ||
@@ -1035,10 +1072,30 @@ func (t *tunnelUpdater) allAgentIDs() []uuid.UUID { | ||
return out | ||
} | ||
func (t *tunnelUpdater) allDNSNames() map[dnsname.FQDN][]netip.Addr { | ||
names := make(map[dnsname.FQDN][]netip.Addr) | ||
for _, w := range t.workspaces { | ||
err := w.addAllDNSNames(names) | ||
if err != nil { | ||
// This should never happen in production, because converting the FQDN only fails | ||
// if names are too long, and we put strict length limits on agent, workspace, and user | ||
// names. | ||
t.logger.Critical(context.Background(), | ||
"failed to include DNS name(s)", | ||
slog.F("workspace_id", w.id), | ||
slog.Error(err)) | ||
} | ||
} | ||
return names | ||
} | ||
// NewTunnelAllWorkspaceUpdatesController creates a WorkspaceUpdatesController that creates tunnels | ||
// (via the TunnelSrcCoordController) to all agents received over the WorkspaceUpdates RPC. If a | ||
// DNSHostSetter is provided, it also programs DNS hosts based on the agent and workspace names. | ||
func NewTunnelAllWorkspaceUpdatesController( | ||
logger slog.Logger, c *TunnelSrcCoordController, d DNSHostsSetter, | ||
) WorkspaceUpdatesController { | ||
return &tunnelAllWorkspaceUpdatesController{logger: logger, coordCtrl: c, dnsHostSetter: d} | ||
} | ||
// NewController creates a new Controller without running it | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.