mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
feat: add tailnet to agent RPC service (#11304)
Adds tailnet.DRPCService to the agent API Supports #10531 but we still need to add version negotiation to the websocket endpoint
This commit is contained in:
@@ -26,6 +26,7 @@ import (
|
|||||||
"github.com/coder/coder/v2/coderd/tracing"
|
"github.com/coder/coder/v2/coderd/tracing"
|
||||||
"github.com/coder/coder/v2/codersdk/agentsdk"
|
"github.com/coder/coder/v2/codersdk/agentsdk"
|
||||||
"github.com/coder/coder/v2/tailnet"
|
"github.com/coder/coder/v2/tailnet"
|
||||||
|
tailnetproto "github.com/coder/coder/v2/tailnet/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
const AgentAPIVersionDRPC = "2.0"
|
const AgentAPIVersionDRPC = "2.0"
|
||||||
@@ -42,6 +43,7 @@ type API struct {
|
|||||||
*AppsAPI
|
*AppsAPI
|
||||||
*MetadataAPI
|
*MetadataAPI
|
||||||
*LogsAPI
|
*LogsAPI
|
||||||
|
*tailnet.DRPCService
|
||||||
|
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
cachedWorkspaceID uuid.UUID
|
cachedWorkspaceID uuid.UUID
|
||||||
@@ -145,6 +147,13 @@ func New(opts Options) *API {
|
|||||||
PublishWorkspaceAgentLogsUpdateFn: opts.PublishWorkspaceAgentLogsUpdateFn,
|
PublishWorkspaceAgentLogsUpdateFn: opts.PublishWorkspaceAgentLogsUpdateFn,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
api.DRPCService = &tailnet.DRPCService{
|
||||||
|
CoordPtr: opts.TailnetCoordinator,
|
||||||
|
Logger: opts.Log,
|
||||||
|
DerpMapUpdateFrequency: opts.DerpMapUpdateFrequency,
|
||||||
|
DerpMapFn: opts.DerpMapFn,
|
||||||
|
}
|
||||||
|
|
||||||
return api
|
return api
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,6 +164,11 @@ func (a *API) Server(ctx context.Context) (*drpcserver.Server, error) {
|
|||||||
return nil, xerrors.Errorf("register agent API protocol in DRPC mux: %w", err)
|
return nil, xerrors.Errorf("register agent API protocol in DRPC mux: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = tailnetproto.DRPCRegisterTailnet(mux, a)
|
||||||
|
if err != nil {
|
||||||
|
return nil, xerrors.Errorf("register tailnet API protocol in DRPC mux: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return drpcserver.NewWithOptions(&tracing.DRPCHandler{Handler: mux},
|
return drpcserver.NewWithOptions(&tracing.DRPCHandler{Handler: mux},
|
||||||
drpcserver.Options{
|
drpcserver.Options{
|
||||||
Log: func(err error) {
|
Log: func(err error) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package coderd
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime/pprof"
|
"runtime/pprof"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -22,6 +23,7 @@ import (
|
|||||||
"github.com/coder/coder/v2/coderd/httpmw"
|
"github.com/coder/coder/v2/coderd/httpmw"
|
||||||
"github.com/coder/coder/v2/coderd/util/ptr"
|
"github.com/coder/coder/v2/coderd/util/ptr"
|
||||||
"github.com/coder/coder/v2/codersdk"
|
"github.com/coder/coder/v2/codersdk"
|
||||||
|
"github.com/coder/coder/v2/tailnet"
|
||||||
)
|
)
|
||||||
|
|
||||||
// @Summary Workspace agent RPC API
|
// @Summary Workspace agent RPC API
|
||||||
@@ -128,6 +130,13 @@ func (api *API) workspaceAgentRPC(rw http.ResponseWriter, r *http.Request) {
|
|||||||
UpdateAgentMetricsFn: api.UpdateAgentMetrics,
|
UpdateAgentMetricsFn: api.UpdateAgentMetrics,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
streamID := tailnet.StreamID{
|
||||||
|
Name: fmt.Sprintf("%s-%s-%s", owner.Username, workspace.Name, workspaceAgent.Name),
|
||||||
|
ID: workspaceAgent.ID,
|
||||||
|
Auth: tailnet.AgentTunnelAuth{},
|
||||||
|
}
|
||||||
|
ctx = tailnet.WithStreamID(ctx, streamID)
|
||||||
|
|
||||||
closeCtx, closeCtxCancel := context.WithCancel(ctx)
|
closeCtx, closeCtxCancel := context.WithCancel(ctx)
|
||||||
go func() {
|
go func() {
|
||||||
defer closeCtxCancel()
|
defer closeCtxCancel()
|
||||||
|
|||||||
Reference in New Issue
Block a user