Skip to content

Commit 517fb19

Browse files
authored
feat: add single tailnet support to moons (#8587)
1 parent cc8d0af commit 517fb19

36 files changed

+1195
-80
lines changed

coderd/apidoc/docs.go

+71
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

+65
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/coderd.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func New(options *Options) *API {
199199
options.Authorizer,
200200
options.Logger.Named("authz_querier"),
201201
)
202-
experiments := initExperiments(
202+
experiments := ReadExperiments(
203203
options.Logger, options.DeploymentValues.Experiments.Value(),
204204
)
205205
if options.AppHostname != "" && options.AppHostnameRegex == nil || options.AppHostname == "" && options.AppHostnameRegex != nil {
@@ -370,7 +370,9 @@ func New(options *Options) *API {
370370
options.Logger,
371371
options.DERPServer,
372372
options.DERPMap,
373-
&api.TailnetCoordinator,
373+
func(context.Context) (tailnet.MultiAgentConn, error) {
374+
return (*api.TailnetCoordinator.Load()).ServeMultiAgent(uuid.New()), nil
375+
},
374376
wsconncache.New(api._dialWorkspaceAgentTailnet, 0),
375377
)
376378
if err != nil {
@@ -1081,7 +1083,7 @@ func (api *API) CreateInMemoryProvisionerDaemon(ctx context.Context, debounce ti
10811083
}
10821084

10831085
// nolint:revive
1084-
func initExperiments(log slog.Logger, raw []string) codersdk.Experiments {
1086+
func ReadExperiments(log slog.Logger, raw []string) codersdk.Experiments {
10851087
exps := make([]codersdk.Experiment, 0, len(raw))
10861088
for _, v := range raw {
10871089
switch v {

coderd/coderdtest/coderdtest.go

+1
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can
384384
TemplateScheduleStore: &templateScheduleStore,
385385
TLSCertificates: options.TLSCertificates,
386386
TrialGenerator: options.TrialGenerator,
387+
TailnetCoordinator: options.Coordinator,
387388
DERPMap: derpMap,
388389
MetricsCacheRefreshInterval: options.MetricsCacheRefreshInterval,
389390
AgentStatsRefreshInterval: options.AgentStatsRefreshInterval,

coderd/httpapi/websocket.go

+21
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,24 @@ func Heartbeat(ctx context.Context, conn *websocket.Conn) {
2525
}
2626
}
2727
}
28+
29+
// Heartbeat loops to ping a WebSocket to keep it alive. It kills the connection
30+
// on ping failure.
31+
func HeartbeatClose(ctx context.Context, exit func(), conn *websocket.Conn) {
32+
ticker := time.NewTicker(30 * time.Second)
33+
defer ticker.Stop()
34+
35+
for {
36+
select {
37+
case <-ctx.Done():
38+
return
39+
case <-ticker.C:
40+
}
41+
err := conn.Ping(ctx)
42+
if err != nil {
43+
_ = conn.Close(websocket.StatusGoingAway, "Ping failed")
44+
exit()
45+
return
46+
}
47+
}
48+
}

coderd/httpmw/groupparam.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func ExtractGroupParam(db database.Store) func(http.Handler) http.Handler {
6464
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
6565
ctx := r.Context()
6666

67-
groupID, parsed := parseUUID(rw, r, "group")
67+
groupID, parsed := ParseUUIDParam(rw, r, "group")
6868
if !parsed {
6969
return
7070
}

coderd/httpmw/httpmw.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"github.com/coder/coder/codersdk"
1212
)
1313

14-
// parseUUID consumes a url parameter and parses it as a UUID.
15-
func parseUUID(rw http.ResponseWriter, r *http.Request, param string) (uuid.UUID, bool) {
14+
// ParseUUIDParam consumes a url parameter and parses it as a UUID.
15+
func ParseUUIDParam(rw http.ResponseWriter, r *http.Request, param string) (uuid.UUID, bool) {
1616
rawID := chi.URLParam(r, param)
1717
if rawID == "" {
1818
httpapi.Write(r.Context(), rw, http.StatusBadRequest, codersdk.Response{

coderd/httpmw/httpmw_internal_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestParseUUID_Valid(t *testing.T) {
2929
ctx.URLParams.Add(testParam, testWorkspaceAgentID)
3030
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, ctx))
3131

32-
parsed, ok := parseUUID(rw, r, "workspaceagent")
32+
parsed, ok := ParseUUIDParam(rw, r, "workspaceagent")
3333
assert.True(t, ok, "UUID should be parsed")
3434
assert.Equal(t, testWorkspaceAgentID, parsed.String())
3535
}
@@ -44,7 +44,7 @@ func TestParseUUID_Invalid(t *testing.T) {
4444
ctx.URLParams.Add(testParam, "wrong-id")
4545
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, ctx))
4646

47-
_, ok := parseUUID(rw, r, "workspaceagent")
47+
_, ok := ParseUUIDParam(rw, r, "workspaceagent")
4848
assert.False(t, ok, "UUID should not be parsed")
4949
assert.Equal(t, http.StatusBadRequest, rw.Code)
5050

coderd/httpmw/organizationparam.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func ExtractOrganizationParam(db database.Store) func(http.Handler) http.Handler
3939
return func(next http.Handler) http.Handler {
4040
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
4141
ctx := r.Context()
42-
orgID, ok := parseUUID(rw, r, "organization")
42+
orgID, ok := ParseUUIDParam(rw, r, "organization")
4343
if !ok {
4444
return
4545
}

coderd/httpmw/templateparam.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func ExtractTemplateParam(db database.Store) func(http.Handler) http.Handler {
2727
return func(next http.Handler) http.Handler {
2828
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
2929
ctx := r.Context()
30-
templateID, parsed := parseUUID(rw, r, "template")
30+
templateID, parsed := ParseUUIDParam(rw, r, "template")
3131
if !parsed {
3232
return
3333
}

coderd/httpmw/templateversionparam.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func ExtractTemplateVersionParam(db database.Store) func(http.Handler) http.Hand
2929
return func(next http.Handler) http.Handler {
3030
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
3131
ctx := r.Context()
32-
templateVersionID, parsed := parseUUID(rw, r, "templateversion")
32+
templateVersionID, parsed := ParseUUIDParam(rw, r, "templateversion")
3333
if !parsed {
3434
return
3535
}

coderd/httpmw/workspaceagentparam.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func ExtractWorkspaceAgentParam(db database.Store) func(http.Handler) http.Handl
2929
return func(next http.Handler) http.Handler {
3030
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
3131
ctx := r.Context()
32-
agentUUID, parsed := parseUUID(rw, r, "workspaceagent")
32+
agentUUID, parsed := ParseUUIDParam(rw, r, "workspaceagent")
3333
if !parsed {
3434
return
3535
}

coderd/httpmw/workspacebuildparam.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func ExtractWorkspaceBuildParam(db database.Store) func(http.Handler) http.Handl
2727
return func(next http.Handler) http.Handler {
2828
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
2929
ctx := r.Context()
30-
workspaceBuildID, parsed := parseUUID(rw, r, "workspacebuild")
30+
workspaceBuildID, parsed := ParseUUIDParam(rw, r, "workspacebuild")
3131
if !parsed {
3232
return
3333
}

coderd/httpmw/workspaceparam.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func ExtractWorkspaceParam(db database.Store) func(http.Handler) http.Handler {
3030
return func(next http.Handler) http.Handler {
3131
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
3232
ctx := r.Context()
33-
workspaceID, parsed := parseUUID(rw, r, "workspace")
33+
workspaceID, parsed := ParseUUIDParam(rw, r, "workspace")
3434
if !parsed {
3535
return
3636
}

coderd/httpmw/workspaceresourceparam.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func ExtractWorkspaceResourceParam(db database.Store) func(http.Handler) http.Ha
2929
return func(next http.Handler) http.Handler {
3030
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
3131
ctx := r.Context()
32-
resourceUUID, parsed := parseUUID(rw, r, "workspaceresource")
32+
resourceUUID, parsed := ParseUUIDParam(rw, r, "workspaceresource")
3333
if !parsed {
3434
return
3535
}

0 commit comments

Comments
 (0)