- Notifications
You must be signed in to change notification settings - Fork1k
feat: add support for multiple tunnel destinations in tailnet#15409
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
+530 −23
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
6 changes: 4 additions & 2 deletionsagent/agent_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
4 changes: 3 additions & 1 deletioncodersdk/workspacesdk/workspacesdk.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
157 changes: 141 additions & 16 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 |
---|---|---|
@@ -4,6 +4,7 @@ import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"maps" | ||
"math" | ||
"strings" | ||
"sync" | ||
@@ -239,15 +240,17 @@ func (c *BasicCoordination) respLoop() { | ||
defer func() { | ||
cErr := c.Client.Close() | ||
if cErr != nil { | ||
c.logger.Debug(context.Background(), | ||
"failed to close coordinate client after respLoop exit", slog.Error(cErr)) | ||
} | ||
c.coordinatee.SetAllPeersLost() | ||
close(c.respLoopDone) | ||
}() | ||
for { | ||
resp, err := c.Client.Recv() | ||
if err != nil { | ||
c.logger.Debug(context.Background(), | ||
"failed to read from protocol", slog.Error(err)) | ||
c.SendErr(xerrors.Errorf("read: %w", err)) | ||
return | ||
} | ||
@@ -278,7 +281,8 @@ func (c *BasicCoordination) respLoop() { | ||
ReadyForHandshake: rfh, | ||
}) | ||
if err != nil { | ||
c.logger.Debug(context.Background(), | ||
"failed to send ready for handshake", slog.Error(err)) | ||
c.SendErr(xerrors.Errorf("send: %w", err)) | ||
return | ||
} | ||
@@ -287,37 +291,158 @@ func (c *BasicCoordination) respLoop() { | ||
} | ||
} | ||
typeTunnelSrcCoordController struct { | ||
*BasicCoordinationController | ||
mu sync.Mutex | ||
dests map[uuid.UUID]struct{} | ||
coordination *BasicCoordination | ||
} | ||
// NewTunnelSrcCoordController creates a CoordinationController for peers that are exclusively | ||
// tunnel sources (that is, they create tunnel --- Coder clients not workspaces). | ||
func NewTunnelSrcCoordController( | ||
logger slog.Logger, coordinatee Coordinatee, | ||
) *TunnelSrcCoordController { | ||
return &TunnelSrcCoordController{ | ||
BasicCoordinationController: &BasicCoordinationController{ | ||
Logger: logger, | ||
Coordinatee: coordinatee, | ||
SendAcks: false, | ||
}, | ||
dests: make(map[uuid.UUID]struct{}), | ||
} | ||
} | ||
func (c *TunnelSrcCoordController) New(client CoordinatorClient) CloserWaiter { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
b := c.BasicCoordinationController.NewCoordination(client) | ||
c.coordination = b | ||
// resync destinations on reconnect | ||
for dest := range c.dests { | ||
err := client.Send(&proto.CoordinateRequest{ | ||
AddTunnel: &proto.CoordinateRequest_Tunnel{Id: UUIDToByteSlice(dest)}, | ||
}) | ||
if err != nil { | ||
b.SendErr(err) | ||
c.coordination = nil | ||
cErr := client.Close() | ||
if cErr != nil { | ||
c.Logger.Debug( | ||
context.Background(), | ||
"failed to close coordinator client after add tunnel failure", | ||
slog.Error(cErr), | ||
) | ||
} | ||
break | ||
} | ||
} | ||
return b | ||
} | ||
func (c *TunnelSrcCoordController) AddDestination(dest uuid.UUID) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
c.Coordinatee.SetTunnelDestination(dest) // this prepares us for an ack | ||
c.dests[dest] = struct{}{} | ||
if c.coordination == nil { | ||
return | ||
} | ||
err := c.coordination.Client.Send( | ||
&proto.CoordinateRequest{ | ||
AddTunnel: &proto.CoordinateRequest_Tunnel{Id: UUIDToByteSlice(dest)}, | ||
}) | ||
if err != nil { | ||
c.coordination.SendErr(err) | ||
cErr := c.coordination.Client.Close() // close the client so we don't gracefully disconnect | ||
if cErr != nil { | ||
c.Logger.Debug(context.Background(), | ||
"failed to close coordinator client after add tunnel failure", | ||
slog.Error(cErr)) | ||
} | ||
c.coordination = nil | ||
} | ||
} | ||
func (c *TunnelSrcCoordController) RemoveDestination(dest uuid.UUID) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
delete(c.dests, dest) | ||
if c.coordination == nil { | ||
return | ||
} | ||
err := c.coordination.Client.Send( | ||
&proto.CoordinateRequest{ | ||
RemoveTunnel: &proto.CoordinateRequest_Tunnel{Id: UUIDToByteSlice(dest)}, | ||
}) | ||
if err != nil { | ||
c.coordination.SendErr(err) | ||
cErr := c.coordination.Client.Close() // close the client so we don't gracefully disconnect | ||
if cErr != nil { | ||
c.Logger.Debug(context.Background(), | ||
"failed to close coordinator client after remove tunnel failure", | ||
slog.Error(cErr)) | ||
} | ||
c.coordination = nil | ||
} | ||
} | ||
func (c *TunnelSrcCoordController) SyncDestinations(destinations []uuid.UUID) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
toAdd := make(map[uuid.UUID]struct{}) | ||
toRemove := maps.Clone(c.dests) | ||
spikecurtis marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
all := make(map[uuid.UUID]struct{}) | ||
for _, dest := range destinations { | ||
all[dest] = struct{}{} | ||
delete(toRemove, dest) | ||
if _, ok := c.dests[dest]; !ok { | ||
toAdd[dest] = struct{}{} | ||
} | ||
} | ||
c.dests = all | ||
if c.coordination == nil { | ||
return | ||
} | ||
var err error | ||
defer func() { | ||
if err != nil { | ||
c.coordination.SendErr(err) | ||
cErr := c.coordination.Client.Close() // don't gracefully disconnect | ||
if cErr != nil { | ||
c.Logger.Debug(context.Background(), | ||
"failed to close coordinator client during sync destinations", | ||
slog.Error(cErr)) | ||
} | ||
c.coordination = nil | ||
} | ||
}() | ||
for dest := range toAdd { | ||
err = c.coordination.Client.Send( | ||
&proto.CoordinateRequest{ | ||
AddTunnel: &proto.CoordinateRequest_Tunnel{Id: UUIDToByteSlice(dest)}, | ||
}) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
for dest := range toRemove { | ||
err = c.coordination.Client.Send( | ||
&proto.CoordinateRequest{ | ||
RemoveTunnel: &proto.CoordinateRequest_Tunnel{Id: UUIDToByteSlice(dest)}, | ||
}) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
} | ||
// NewAgentCoordinationController creates a CoordinationController for Coder Agents, which never | ||
// create tunnels and always send ReadyToHandshake acknowledgements. | ||
func NewAgentCoordinationController( | ||
logger slog.Logger, coordinatee Coordinatee, | ||
) CoordinationController { | ||
return &BasicCoordinationController{ | ||
Logger: logger, | ||
Coordinatee: coordinatee, | ||
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.