Skip to content

Commit d9da2ef

Browse files
committed
refactor(coderd/healthcheck): move derp report to derphealth package
This change helps remove one indirect use of coderd/database in the slim CLI. No size change (yet). Ref: #9380
1 parent b9f604c commit d9da2ef

File tree

7 files changed

+69
-59
lines changed

7 files changed

+69
-59
lines changed

cli/netcheck.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"golang.org/x/xerrors"
1010

1111
"github.com/coder/coder/v2/cli/clibase"
12-
"github.com/coder/coder/v2/coderd/healthcheck"
12+
"github.com/coder/coder/v2/coderd/healthcheck/derphealth"
1313
"github.com/coder/coder/v2/codersdk"
1414
)
1515

@@ -33,8 +33,8 @@ func (r *RootCmd) netcheck() *clibase.Cmd {
3333

3434
_, _ = fmt.Fprint(inv.Stderr, "Gathering a network report. This may take a few seconds...\n\n")
3535

36-
var report healthcheck.DERPReport
37-
report.Run(ctx, &healthcheck.DERPReportOptions{
36+
var report derphealth.Report
37+
report.Run(ctx, &derphealth.ReportOptions{
3838
DERPMap: connInfo.DERPMap,
3939
})
4040

cli/netcheck_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/stretchr/testify/require"
1010

1111
"github.com/coder/coder/v2/cli/clitest"
12-
"github.com/coder/coder/v2/coderd/healthcheck"
12+
"github.com/coder/coder/v2/coderd/healthcheck/derphealth"
1313
"github.com/coder/coder/v2/pty/ptytest"
1414
)
1515

@@ -27,7 +27,7 @@ func TestNetcheck(t *testing.T) {
2727

2828
b := out.Bytes()
2929
t.Log(string(b))
30-
var report healthcheck.DERPReport
30+
var report derphealth.Report
3131
require.NoError(t, json.Unmarshal(b, &report))
3232

3333
assert.True(t, report.Healthy)

coderd/healthcheck/derp.go renamed to coderd/healthcheck/derphealth/derp.go

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package healthcheck
1+
package derphealth
22

33
import (
44
"context"
@@ -24,11 +24,11 @@ import (
2424
"github.com/coder/coder/v2/coderd/util/ptr"
2525
)
2626

27-
// @typescript-generate DERPReport
28-
type DERPReport struct {
27+
// @typescript-generate Report
28+
type Report struct {
2929
Healthy bool `json:"healthy"`
3030

31-
Regions map[int]*DERPRegionReport `json:"regions"`
31+
Regions map[int]*RegionReport `json:"regions"`
3232

3333
Netcheck *netcheck.Report `json:"netcheck"`
3434
NetcheckErr *string `json:"netcheck_err"`
@@ -37,18 +37,18 @@ type DERPReport struct {
3737
Error *string `json:"error"`
3838
}
3939

40-
// @typescript-generate DERPRegionReport
41-
type DERPRegionReport struct {
40+
// @typescript-generate RegionReport
41+
type RegionReport struct {
4242
mu sync.Mutex
4343
Healthy bool `json:"healthy"`
4444

4545
Region *tailcfg.DERPRegion `json:"region"`
46-
NodeReports []*DERPNodeReport `json:"node_reports"`
46+
NodeReports []*NodeReport `json:"node_reports"`
4747
Error *string `json:"error"`
4848
}
4949

50-
// @typescript-generate DERPNodeReport
51-
type DERPNodeReport struct {
50+
// @typescript-generate NodeReport
51+
type NodeReport struct {
5252
mu sync.Mutex
5353
clientCounter int
5454

@@ -64,23 +64,23 @@ type DERPNodeReport struct {
6464
ClientErrs [][]string `json:"client_errs"`
6565
Error *string `json:"error"`
6666

67-
STUN DERPStunReport `json:"stun"`
67+
STUN StunReport `json:"stun"`
6868
}
6969

70-
// @typescript-generate DERPStunReport
71-
type DERPStunReport struct {
70+
// @typescript-generate StunReport
71+
type StunReport struct {
7272
Enabled bool
7373
CanSTUN bool
7474
Error *string
7575
}
7676

77-
type DERPReportOptions struct {
77+
type ReportOptions struct {
7878
DERPMap *tailcfg.DERPMap
7979
}
8080

81-
func (r *DERPReport) Run(ctx context.Context, opts *DERPReportOptions) {
81+
func (r *Report) Run(ctx context.Context, opts *ReportOptions) {
8282
r.Healthy = true
83-
r.Regions = map[int]*DERPRegionReport{}
83+
r.Regions = map[int]*RegionReport{}
8484

8585
wg := &sync.WaitGroup{}
8686
mu := sync.Mutex{}
@@ -89,7 +89,7 @@ func (r *DERPReport) Run(ctx context.Context, opts *DERPReportOptions) {
8989
for _, region := range opts.DERPMap.Regions {
9090
var (
9191
region = region
92-
regionReport = DERPRegionReport{
92+
regionReport = RegionReport{
9393
Region: region,
9494
}
9595
)
@@ -128,17 +128,17 @@ func (r *DERPReport) Run(ctx context.Context, opts *DERPReportOptions) {
128128
wg.Wait()
129129
}
130130

131-
func (r *DERPRegionReport) Run(ctx context.Context) {
131+
func (r *RegionReport) Run(ctx context.Context) {
132132
r.Healthy = true
133-
r.NodeReports = []*DERPNodeReport{}
133+
r.NodeReports = []*NodeReport{}
134134

135135
wg := &sync.WaitGroup{}
136136

137137
wg.Add(len(r.Region.Nodes))
138138
for _, node := range r.Region.Nodes {
139139
var (
140140
node = node
141-
nodeReport = DERPNodeReport{
141+
nodeReport = NodeReport{
142142
Node: node,
143143
Healthy: true,
144144
}
@@ -166,7 +166,7 @@ func (r *DERPRegionReport) Run(ctx context.Context) {
166166
wg.Wait()
167167
}
168168

169-
func (r *DERPNodeReport) derpURL() *url.URL {
169+
func (r *NodeReport) derpURL() *url.URL {
170170
derpURL := &url.URL{
171171
Scheme: "https",
172172
Host: r.Node.HostName,
@@ -185,7 +185,7 @@ func (r *DERPNodeReport) derpURL() *url.URL {
185185
return derpURL
186186
}
187187

188-
func (r *DERPNodeReport) Run(ctx context.Context) {
188+
func (r *NodeReport) Run(ctx context.Context) {
189189
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
190190
defer cancel()
191191

@@ -218,7 +218,7 @@ func (r *DERPNodeReport) Run(ctx context.Context) {
218218
}
219219
}
220220

221-
func (r *DERPNodeReport) doExchangeMessage(ctx context.Context) {
221+
func (r *NodeReport) doExchangeMessage(ctx context.Context) {
222222
if r.Node.STUNOnly {
223223
return
224224
}
@@ -299,7 +299,7 @@ func (r *DERPNodeReport) doExchangeMessage(ctx context.Context) {
299299
wg.Wait()
300300
}
301301

302-
func (r *DERPNodeReport) doSTUNTest(ctx context.Context) {
302+
func (r *NodeReport) doSTUNTest(ctx context.Context) {
303303
if r.Node.STUNPort == -1 {
304304
return
305305
}
@@ -331,7 +331,7 @@ func (r *DERPNodeReport) doSTUNTest(ctx context.Context) {
331331
r.mu.Unlock()
332332
}
333333

334-
func (r *DERPNodeReport) stunAddr(ctx context.Context) (string, int, error) {
334+
func (r *NodeReport) stunAddr(ctx context.Context) (string, int, error) {
335335
port := r.Node.STUNPort
336336
if port == 0 {
337337
port = 3478
@@ -387,13 +387,13 @@ func (r *DERPNodeReport) stunAddr(ctx context.Context) (string, int, error) {
387387
return "", 0, xerrors.New("no stun ips provided")
388388
}
389389

390-
func (r *DERPNodeReport) writeClientErr(clientID int, err error) {
390+
func (r *NodeReport) writeClientErr(clientID int, err error) {
391391
r.mu.Lock()
392392
r.ClientErrs[clientID] = append(r.ClientErrs[clientID], err.Error())
393393
r.mu.Unlock()
394394
}
395395

396-
func (r *DERPNodeReport) derpClient(ctx context.Context, derpURL *url.URL) (*derphttp.Client, int, error) {
396+
func (r *NodeReport) derpClient(ctx context.Context, derpURL *url.URL) (*derphttp.Client, int, error) {
397397
r.mu.Lock()
398398
id := r.clientCounter
399399
r.clientCounter++
@@ -440,7 +440,7 @@ func (r *DERPNodeReport) derpClient(ctx context.Context, derpURL *url.URL) (*der
440440
return client, id, nil
441441
}
442442

443-
func (r *DERPNodeReport) recvData(client *derphttp.Client) (derp.ReceivedPacket, error) {
443+
func (r *NodeReport) recvData(client *derphttp.Client) (derp.ReceivedPacket, error) {
444444
for {
445445
msg, err := client.Recv()
446446
if err != nil {
@@ -459,3 +459,11 @@ func (r *DERPNodeReport) recvData(client *derphttp.Client) (derp.ReceivedPacket,
459459
}
460460
}
461461
}
462+
463+
func convertError(err error) *string {
464+
if err != nil {
465+
return ptr.Ref(err.Error())
466+
}
467+
468+
return nil
469+
}

coderd/healthcheck/derp_test.go renamed to coderd/healthcheck/derphealth/derp_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package healthcheck_test
1+
package derphealth_test
22

33
import (
44
"context"
@@ -17,7 +17,7 @@ import (
1717
"tailscale.com/tailcfg"
1818
"tailscale.com/types/key"
1919

20-
"github.com/coder/coder/v2/coderd/healthcheck"
20+
"github.com/coder/coder/v2/coderd/healthcheck/derphealth"
2121
"github.com/coder/coder/v2/tailnet"
2222
"github.com/coder/coder/v2/testutil"
2323
)
@@ -38,9 +38,9 @@ func TestDERP(t *testing.T) {
3838

3939
var (
4040
ctx = context.Background()
41-
report = healthcheck.DERPReport{}
41+
report = derphealth.Report{}
4242
derpURL, _ = url.Parse(srv.URL)
43-
opts = &healthcheck.DERPReportOptions{
43+
opts = &derphealth.ReportOptions{
4444
DERPMap: &tailcfg.DERPMap{Regions: map[int]*tailcfg.DERPRegion{
4545
1: {
4646
EmbeddedRelay: true,
@@ -95,8 +95,8 @@ func TestDERP(t *testing.T) {
9595

9696
var (
9797
ctx = context.Background()
98-
report = healthcheck.DERPReport{}
99-
opts = &healthcheck.DERPReportOptions{
98+
report = derphealth.Report{}
99+
opts = &derphealth.ReportOptions{
100100
DERPMap: tsDERPMap(ctx, t),
101101
}
102102
)
@@ -145,9 +145,9 @@ func TestDERP(t *testing.T) {
145145

146146
var (
147147
ctx = context.Background()
148-
report = healthcheck.DERPReport{}
148+
report = derphealth.Report{}
149149
derpURL, _ = url.Parse(srv.URL)
150-
opts = &healthcheck.DERPReportOptions{
150+
opts = &derphealth.ReportOptions{
151151
DERPMap: &tailcfg.DERPMap{Regions: map[int]*tailcfg.DERPRegion{
152152
1: {
153153
EmbeddedRelay: true,
@@ -195,8 +195,8 @@ func TestDERP(t *testing.T) {
195195

196196
var (
197197
ctx = context.Background()
198-
report = healthcheck.DERPReport{}
199-
opts = &healthcheck.DERPReportOptions{
198+
report = derphealth.Report{}
199+
opts = &derphealth.ReportOptions{
200200
DERPMap: &tailcfg.DERPMap{Regions: map[int]*tailcfg.DERPRegion{
201201
1: {
202202
EmbeddedRelay: true,

coderd/healthcheck/healthcheck.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/coder/coder/v2/buildinfo"
1414
"github.com/coder/coder/v2/coderd/database"
15+
"github.com/coder/coder/v2/coderd/healthcheck/derphealth"
1516
"github.com/coder/coder/v2/coderd/util/ptr"
1617
)
1718

@@ -23,7 +24,7 @@ const (
2324
)
2425

2526
type Checker interface {
26-
DERP(ctx context.Context, opts *DERPReportOptions) DERPReport
27+
DERP(ctx context.Context, opts *derphealth.ReportOptions) derphealth.Report
2728
AccessURL(ctx context.Context, opts *AccessURLReportOptions) AccessURLReport
2829
Websocket(ctx context.Context, opts *WebsocketReportOptions) WebsocketReport
2930
Database(ctx context.Context, opts *DatabaseReportOptions) DatabaseReport
@@ -38,10 +39,10 @@ type Report struct {
3839
// FailingSections is a list of sections that have failed their healthcheck.
3940
FailingSections []string `json:"failing_sections"`
4041

41-
DERP DERPReport `json:"derp"`
42-
AccessURL AccessURLReport `json:"access_url"`
43-
Websocket WebsocketReport `json:"websocket"`
44-
Database DatabaseReport `json:"database"`
42+
DERP derphealth.Report `json:"derp"`
43+
AccessURL AccessURLReport `json:"access_url"`
44+
Websocket WebsocketReport `json:"websocket"`
45+
Database DatabaseReport `json:"database"`
4546

4647
// The Coder version of the server that the report was generated on.
4748
CoderVersion string `json:"coder_version"`
@@ -60,7 +61,7 @@ type ReportOptions struct {
6061

6162
type defaultChecker struct{}
6263

63-
func (defaultChecker) DERP(ctx context.Context, opts *DERPReportOptions) (report DERPReport) {
64+
func (defaultChecker) DERP(ctx context.Context, opts *derphealth.ReportOptions) (report derphealth.Report) {
6465
report.Run(ctx, opts)
6566
return report
6667
}
@@ -99,7 +100,7 @@ func Run(ctx context.Context, opts *ReportOptions) *Report {
99100
}
100101
}()
101102

102-
report.DERP = opts.Checker.DERP(ctx, &DERPReportOptions{
103+
report.DERP = opts.Checker.DERP(ctx, &derphealth.ReportOptions{
103104
DERPMap: opts.DERPMap,
104105
})
105106
}()

0 commit comments

Comments
 (0)