Skip to content

chore: add derpserver to proxy, add proxies to derpmap #7311

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
eae4c3a
chore: add derpserver to proxy, add proxies to derpmap
deansheather Apr 27, 2023
ac99525
progress
deansheather May 1, 2023
4b68a0b
Merge branch 'main' into dean/proxy-derp-map
deansheather May 2, 2023
4ba7af6
progress
deansheather May 3, 2023
dcf072e
derp mesh probably working
deansheather May 4, 2023
2d2f1a3
deregister
deansheather May 4, 2023
28ae155
tests and various fixes
deansheather May 4, 2023
2baa362
Merge branch 'main' into dean/proxy-derp-map
deansheather May 4, 2023
5f5d4ff
more tests
deansheather May 5, 2023
5441dc8
merge main, remove proxy goingaway route
deansheather May 30, 2023
e4a3008
derp tests work
deansheather May 30, 2023
3caa692
Merge branch 'main' into dean/proxy-derp-map
deansheather May 30, 2023
404c3e4
update derp map on new connection
deansheather May 31, 2023
8544882
Merge branch 'main' into dean/proxy-derp-map
deansheather Jun 13, 2023
9b503fa
fixes
deansheather Jun 13, 2023
0e6d39a
tests for derp map changing
deansheather Jun 13, 2023
bb699fb
Merge branch 'main' into dean/proxy-derp-map
deansheather Jun 13, 2023
2943ac2
backwards compatible
deansheather Jun 20, 2023
f0fa578
other comments
deansheather Jun 20, 2023
9d90dc2
Merge branch 'main' into dean/proxy-derp-map
deansheather Jun 25, 2023
b405113
fixup! Merge branch 'main' into dean/proxy-derp-map
deansheather Jun 25, 2023
6a08a59
change derp map updates to be separate websocket
deansheather Jun 28, 2023
403eac5
Merge branch 'main' into dean/proxy-derp-map
deansheather Jul 17, 2023
d220266
Merge branch 'main' into dean/proxy-derp-map
deansheather Jul 24, 2023
9e658d6
fixup! Merge branch 'main' into dean/proxy-derp-map
deansheather Jul 24, 2023
67f2e5c
Working tests
deansheather Jul 25, 2023
3c96149
Merge branch 'main' into dean/proxy-derp-map
deansheather Jul 25, 2023
c26936a
fixup! Merge branch 'main' into dean/proxy-derp-map
deansheather Jul 25, 2023
e59de5a
fixup! Merge branch 'main' into dean/proxy-derp-map
deansheather Jul 25, 2023
2df067f
fixup! Merge branch 'main' into dean/proxy-derp-map
deansheather Jul 25, 2023
dfbfa96
Please
deansheather Jul 26, 2023
8223a35
fixup! Please
deansheather Jul 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore: add derpserver to proxy, add proxies to derpmap
  • Loading branch information
deansheather committed Apr 27, 2023
commit eae4c3a5965cabe673bb9f43e6b231cf6865fee3
2 changes: 1 addition & 1 deletion cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.

if cfg.Prometheus.Enable {
// Agent metrics require reference to the tailnet coordinator, so must be initiated after Coder API.
closeAgentsFunc, err := prometheusmetrics.Agents(ctx, logger, options.PrometheusRegistry, coderAPI.Database, &coderAPI.TailnetCoordinator, options.DERPMap, coderAPI.Options.AgentInactiveDisconnectTimeout, 0)
closeAgentsFunc, err := prometheusmetrics.Agents(ctx, logger, options.PrometheusRegistry, coderAPI.Database, &coderAPI.TailnetCoordinator, coderAPI.DERPMap, coderAPI.Options.AgentInactiveDisconnectTimeout, 0)
if err != nil {
return xerrors.Errorf("register agents prometheus metric: %w", err)
}
Expand Down
35 changes: 22 additions & 13 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ import (
"tailscale.com/types/key"
"tailscale.com/util/singleflight"

"cdr.dev/slog"

"github.com/coder/coder/buildinfo"
"github.com/coder/coder/codersdk/agentsdk"

// Used for swagger docs.
_ "github.com/coder/coder/coderd/apidoc"

"cdr.dev/slog"
"github.com/coder/coder/buildinfo"
"github.com/coder/coder/coderd/audit"
"github.com/coder/coder/coderd/awsidentity"
"github.com/coder/coder/coderd/database"
Expand All @@ -63,6 +61,7 @@ import (
"github.com/coder/coder/coderd/workspaceapps"
"github.com/coder/coder/coderd/wsconncache"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/codersdk/agentsdk"
"github.com/coder/coder/provisionerd/proto"
"github.com/coder/coder/provisionersdk"
"github.com/coder/coder/site"
Expand Down Expand Up @@ -251,14 +250,6 @@ func New(options *Options) *API {
v := schedule.NewAGPLTemplateScheduleStore()
options.TemplateScheduleStore.Store(&v)
}
if options.HealthcheckFunc == nil {
options.HealthcheckFunc = func(ctx context.Context) (*healthcheck.Report, error) {
return healthcheck.Run(ctx, &healthcheck.ReportOptions{
AccessURL: options.AccessURL,
DERPMap: options.DERPMap.Clone(),
})
}
}
if options.HealthcheckTimeout == 0 {
options.HealthcheckTimeout = 30 * time.Second
}
Expand Down Expand Up @@ -325,6 +316,14 @@ func New(options *Options) *API {
Experiments: experiments,
healthCheckGroup: &singleflight.Group[string, *healthcheck.Report]{},
}
if options.HealthcheckFunc == nil {
options.HealthcheckFunc = func(ctx context.Context) (*healthcheck.Report, error) {
return healthcheck.Run(ctx, &healthcheck.ReportOptions{
AccessURL: options.AccessURL,
DERPMap: api.DERPMap().Clone(),
})
}
}
if options.UpdateCheckOptions != nil {
api.updateChecker = updatecheck.New(
options.Database,
Expand Down Expand Up @@ -814,6 +813,7 @@ type API struct {
TailnetCoordinator atomic.Pointer[tailnet.Coordinator]
QuotaCommitter atomic.Pointer[proto.QuotaCommitter]
TemplateScheduleStore *atomic.Pointer[schedule.TemplateScheduleStore]
DERPMapper atomic.Pointer[func(derpMap *tailcfg.DERPMap) *tailcfg.DERPMap]

HTTPAuth *HTTPAuthorizer

Expand Down Expand Up @@ -954,6 +954,15 @@ func (api *API) CreateInMemoryProvisionerDaemon(ctx context.Context, debounce ti
return proto.NewDRPCProvisionerDaemonClient(clientSession), nil
}

func (api *API) DERPMap() *tailcfg.DERPMap {
fn := api.DERPMapper.Load()
if fn != nil {
return (*fn)(api.Options.DERPMap)
}

return api.Options.DERPMap
}

// nolint:revive
func initExperiments(log slog.Logger, raw []string) codersdk.Experiments {
exps := make([]codersdk.Experiment, 0, len(raw))
Expand Down
3 changes: 2 additions & 1 deletion coderd/prometheusmetrics/prometheusmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func Workspaces(ctx context.Context, registerer prometheus.Registerer, db databa
}

// Agents tracks the total number of workspaces with labels on status.
func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Registerer, db database.Store, coordinator *atomic.Pointer[tailnet.Coordinator], derpMap *tailcfg.DERPMap, agentInactiveDisconnectTimeout, duration time.Duration) (func(), error) {
func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Registerer, db database.Store, coordinator *atomic.Pointer[tailnet.Coordinator], derpMapFn func() *tailcfg.DERPMap, agentInactiveDisconnectTimeout, duration time.Duration) (func(), error) {
if duration == 0 {
duration = 1 * time.Minute
}
Expand Down Expand Up @@ -215,6 +215,7 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis

logger.Debug(ctx, "Agent metrics collection is starting")
timer := prometheus.NewTimer(metricsCollectorAgents)
derpMap := derpMapFn()

workspaceRows, err := db.GetWorkspaces(ctx, database.GetWorkspacesParams{
AgentInactiveDisconnectTimeoutSeconds: int64(agentInactiveDisconnectTimeout.Seconds()),
Expand Down
6 changes: 5 additions & 1 deletion coderd/prometheusmetrics/prometheusmetrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"tailscale.com/tailcfg"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogtest"
Expand Down Expand Up @@ -303,6 +304,9 @@ func TestAgents(t *testing.T) {
coordinatorPtr := atomic.Pointer[tailnet.Coordinator]{}
coordinatorPtr.Store(&coordinator)
derpMap := tailnettest.RunDERPAndSTUN(t)
derpMapFn := func() *tailcfg.DERPMap {
return derpMap
}
agentInactiveDisconnectTimeout := 1 * time.Hour // don't need to focus on this value in tests
registry := prometheus.NewRegistry()

Expand All @@ -312,7 +316,7 @@ func TestAgents(t *testing.T) {
// when
closeFunc, err := prometheusmetrics.Agents(ctx, slogtest.Make(t, &slogtest.Options{
IgnoreErrors: true,
}), registry, db, &coordinatorPtr, derpMap, agentInactiveDisconnectTimeout, time.Millisecond)
}), registry, db, &coordinatorPtr, derpMapFn, agentInactiveDisconnectTimeout, time.Millisecond)
require.NoError(t, err)
t.Cleanup(closeFunc)

Expand Down
2 changes: 1 addition & 1 deletion coderd/provisionerjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (api *API) provisionerJobResources(rw http.ResponseWriter, r *http.Request,
}

apiAgent, err := convertWorkspaceAgent(
api.DERPMap, *api.TailnetCoordinator.Load(), agent, convertApps(dbApps), api.AgentInactiveDisconnectTimeout,
api.DERPMap(), *api.TailnetCoordinator.Load(), agent, convertApps(dbApps), api.AgentInactiveDisconnectTimeout,
api.DeploymentValues.AgentFallbackTroubleshootingURL.String(),
)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions coderd/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (api *API) workspaceAgent(rw http.ResponseWriter, r *http.Request) {
return
}
apiAgent, err := convertWorkspaceAgent(
api.DERPMap, *api.TailnetCoordinator.Load(), workspaceAgent, convertApps(dbApps), api.AgentInactiveDisconnectTimeout,
api.DERPMap(), *api.TailnetCoordinator.Load(), workspaceAgent, convertApps(dbApps), api.AgentInactiveDisconnectTimeout,
api.DeploymentValues.AgentFallbackTroubleshootingURL.String(),
)
if err != nil {
Expand All @@ -88,7 +88,7 @@ func (api *API) workspaceAgentManifest(rw http.ResponseWriter, r *http.Request)
ctx := r.Context()
workspaceAgent := httpmw.WorkspaceAgent(r)
apiAgent, err := convertWorkspaceAgent(
api.DERPMap, *api.TailnetCoordinator.Load(), workspaceAgent, nil, api.AgentInactiveDisconnectTimeout,
api.DERPMap(), *api.TailnetCoordinator.Load(), workspaceAgent, nil, api.AgentInactiveDisconnectTimeout,
api.DeploymentValues.AgentFallbackTroubleshootingURL.String(),
)
if err != nil {
Expand Down Expand Up @@ -162,7 +162,7 @@ func (api *API) workspaceAgentManifest(rw http.ResponseWriter, r *http.Request)

httpapi.Write(ctx, rw, http.StatusOK, agentsdk.Manifest{
Apps: convertApps(dbApps),
DERPMap: api.DERPMap,
DERPMap: api.DERPMap(),
GitAuthConfigs: len(api.GitAuthConfigs),
EnvironmentVariables: apiAgent.EnvironmentVariables,
StartupScript: apiAgent.StartupScript,
Expand Down Expand Up @@ -190,7 +190,7 @@ func (api *API) postWorkspaceAgentStartup(rw http.ResponseWriter, r *http.Reques
ctx := r.Context()
workspaceAgent := httpmw.WorkspaceAgent(r)
apiAgent, err := convertWorkspaceAgent(
api.DERPMap, *api.TailnetCoordinator.Load(), workspaceAgent, nil, api.AgentInactiveDisconnectTimeout,
api.DERPMap(), *api.TailnetCoordinator.Load(), workspaceAgent, nil, api.AgentInactiveDisconnectTimeout,
api.DeploymentValues.AgentFallbackTroubleshootingURL.String(),
)
if err != nil {
Expand Down Expand Up @@ -567,7 +567,7 @@ func (api *API) workspaceAgentListeningPorts(rw http.ResponseWriter, r *http.Req
workspaceAgent := httpmw.WorkspaceAgentParam(r)

apiAgent, err := convertWorkspaceAgent(
api.DERPMap, *api.TailnetCoordinator.Load(), workspaceAgent, nil, api.AgentInactiveDisconnectTimeout,
api.DERPMap(), *api.TailnetCoordinator.Load(), workspaceAgent, nil, api.AgentInactiveDisconnectTimeout,
api.DeploymentValues.AgentFallbackTroubleshootingURL.String(),
)
if err != nil {
Expand Down Expand Up @@ -663,7 +663,7 @@ func (api *API) dialWorkspaceAgentTailnet(agentID uuid.UUID) (*codersdk.Workspac
clientConn, serverConn := net.Pipe()
conn, err := tailnet.NewConn(&tailnet.Options{
Addresses: []netip.Prefix{netip.PrefixFrom(tailnet.IP(), 128)},
DERPMap: api.DERPMap,
DERPMap: api.DERPMap(),
Logger: api.Logger.Named("tailnet"),
})
if err != nil {
Expand Down Expand Up @@ -732,7 +732,7 @@ func (api *API) workspaceAgentConnection(rw http.ResponseWriter, r *http.Request
ctx := r.Context()

httpapi.Write(ctx, rw, http.StatusOK, codersdk.WorkspaceAgentConnectionInfo{
DERPMap: api.DERPMap,
DERPMap: api.DERPMap(),
})
}

Expand Down
2 changes: 1 addition & 1 deletion coderd/workspacebuilds.go
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ func (api *API) convertWorkspaceBuild(
for _, agent := range agents {
apps := appsByAgentID[agent.ID]
apiAgent, err := convertWorkspaceAgent(
api.DERPMap, *api.TailnetCoordinator.Load(), agent, convertApps(apps), api.AgentInactiveDisconnectTimeout,
api.DERPMap(), *api.TailnetCoordinator.Load(), agent, convertApps(apps), api.AgentInactiveDisconnectTimeout,
api.DeploymentValues.AgentFallbackTroubleshootingURL.String(),
)
if err != nil {
Expand Down
75 changes: 75 additions & 0 deletions enterprise/coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import (
"crypto/ed25519"
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"net/url"
"strconv"
"sync"
"time"

"golang.org/x/xerrors"
"tailscale.com/tailcfg"

"github.com/cenkalti/backoff/v4"
"github.com/go-chi/chi/v5"
Expand Down Expand Up @@ -429,11 +433,82 @@ func (api *API) updateEntitlements(ctx context.Context) error {
}
}

if changed, enabled := featureChanged(codersdk.FeatureWorkspaceProxy); changed {
if enabled {
fn := derpMapper(api.ProxyHealth)
api.AGPL.DERPMapper.Store(&fn)
} else {
api.AGPL.DERPMapper.Store(nil)
}
}

api.entitlements = entitlements

return nil
}

func derpMapper(proxyHealth *proxyhealth.ProxyHealth) func(*tailcfg.DERPMap) *tailcfg.DERPMap {
return func(derpMap *tailcfg.DERPMap) *tailcfg.DERPMap {
derpMap = derpMap.Clone()

// Add all healthy proxies to the DERP map.
// TODO: @dean proxies should be able to disable DERP and report that
// when they register, and this should respect that.
statusMap := proxyHealth.HealthStatus()
for _, status := range statusMap {
// TODO: @dean region ID should be constant and unique for each
// proxy. Make the proxies report these values (from a flag like the
// primary) when they register.
const (
regionID = -999
regionCode = "proxy"
)

if status.Status != proxyhealth.Healthy {
// Only add healthy proxies to the DERP map.
continue
}

u, err := url.Parse(status.Proxy.Url)
if err != nil {
// Not really any need to log, the proxy should be unreachable
// anyways and filtered out by the above condition.
continue
}
port := u.Port()
if port == "" {
port = "80"
if u.Scheme == "https" {
port = "443"
}
}
portInt, err := strconv.Atoi(port)
if err != nil {
// Not really any need to log, the proxy should be unreachable
// anyways and filtered out by the above condition.
continue
}

derpMap.Regions[regionID] = &tailcfg.DERPRegion{
EmbeddedRelay: true,
RegionID: regionID,
RegionCode: regionCode,
RegionName: status.Proxy.Name,
Nodes: []*tailcfg.DERPNode{{
Name: fmt.Sprintf("%db", regionID),
RegionID: regionID,
HostName: u.Hostname(),
DERPPort: portInt,
STUNPort: -1,
ForceHTTP: u.Scheme == "http",
}},
}
}

return derpMap
}
}

// @Summary Get entitlements
// @ID get-entitlements
// @Security CoderSessionToken
Expand Down
Loading