Skip to content

feat: add support package #12289

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 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
address PR comments
  • Loading branch information
johnstcn committed Feb 29, 2024
commit cd41a8479061fdaf37b2cf4b72b6e85dbbcd7b3f
2 changes: 1 addition & 1 deletion codersdk/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type UpdateHealthSettings struct {
DismissedHealthchecks []HealthSection `json:"dismissed_healthchecks"`
}

func (c *Client) GetDebugHealth(ctx context.Context) (HealthcheckReport, error) {
func (c *Client) DebugHealth(ctx context.Context) (HealthcheckReport, error) {
res, err := c.Request(ctx, http.MethodGet, "/api/v2/debug/health", nil)
if err != nil {
return HealthcheckReport{}, err
Expand Down
20 changes: 10 additions & 10 deletions support/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Deps struct {

func DeploymentInfo(ctx context.Context, client *codersdk.Client, log slog.Logger) Deployment {
var d Deployment

bi, err := client.BuildInfo(ctx)
if err != nil {
log.Error(ctx, "fetch build info", slog.Error(err))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the approach you implemented here. It is appropriate, but maybe we could keep a field for bundling errors within the bundle? It would make debugging easier when coderd returns HTTP 500, and the client error is saved.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll see if there's a nice generic solution to this in a follow-up.

Copy link
Member

@mtojek mtojek Feb 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely 👍 This is something that can be done in a follow-up.

Expand All @@ -76,7 +77,7 @@ func DeploymentInfo(ctx context.Context, client *codersdk.Client, log slog.Logge
d.Config = dc
}

hr, err := client.GetDebugHealth(ctx)
hr, err := client.DebugHealth(ctx)
if err != nil {
log.Error(ctx, "fetch health report", slog.Error(err))
} else {
Expand All @@ -96,7 +97,6 @@ func DeploymentInfo(ctx context.Context, client *codersdk.Client, log slog.Logge
func NetworkInfo(ctx context.Context, client *codersdk.Client, log slog.Logger, agentID uuid.UUID) Network {
var n Network

// Get /api/v2/debug/coordinator
coordResp, err := client.Request(ctx, http.MethodGet, "/api/v2/debug/coordinator", nil)
if err != nil {
log.Error(ctx, "fetch coordinator debug page", slog.Error(err))
Expand All @@ -110,7 +110,6 @@ func NetworkInfo(ctx context.Context, client *codersdk.Client, log slog.Logger,
}
}

// Get /api/v2/debug/tailnet
tailResp, err := client.Request(ctx, http.MethodGet, "/api/v2/debug/tailnet", nil)
if err != nil {
log.Error(ctx, "fetch tailnet debug page", slog.Error(err))
Expand Down Expand Up @@ -202,6 +201,14 @@ func Run(ctx context.Context, d *Deps) (*Bundle, error) {
},
}

// Ensure we capture logs from the client.
var logw strings.Builder
d.Log.AppendSinks(sloghuman.Sink(&logw))
d.Client.SetLogger(d.Log)
defer func() {
b.Logs = strings.Split(logw.String(), "\n")
}()

authResp, err := d.Client.AuthCheck(ctx, codersdk.AuthorizationRequest{Checks: authChecks})
if err != nil {
return &b, xerrors.Errorf("check authorization: %w", err)
Expand All @@ -212,13 +219,6 @@ func Run(ctx context.Context, d *Deps) (*Bundle, error) {
}
}

// Ensure we capture logs from the client.
var logw strings.Builder
d.Log.AppendSinks(sloghuman.Sink(&logw))
defer func() {
b.Logs = strings.Split(logw.String(), "\n")
}()

b.Deployment = DeploymentInfo(ctx, d.Client, d.Log)
b.Workspace = WorkspaceInfo(ctx, d.Client, d.Log, d.WorkspaceID, d.AgentID)
b.Network = NetworkInfo(ctx, d.Client, d.Log, d.AgentID)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fetch concurrently?

Expand Down
27 changes: 20 additions & 7 deletions support/support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ package support_test

import (
"context"
"io"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbfake"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/util/ptr"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/support"
"github.com/coder/coder/v2/testutil"
Expand All @@ -29,13 +33,14 @@ func TestRun(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitShort)
client, db := coderdtest.NewWithDatabase(t, &coderdtest.Options{
DeploymentValues: cfg,
Logger: ptr.Ref(slog.Make(sloghuman.Sink(io.Discard))),
})
admin := coderdtest.CreateFirstUser(t, client)
ws, agt := setupWorkspaceAndAgent(ctx, t, client, db, admin)

bun, err := support.Run(ctx, &support.Deps{
Client: client,
Log: slogtest.Make(t, nil),
Log: slogtest.Make(t, nil).Named("bundle").Leveled(slog.LevelDebug),
WorkspaceID: ws.ID,
AgentID: agt.ID,
})
Expand Down Expand Up @@ -64,12 +69,12 @@ func TestRun(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitShort)
client := coderdtest.New(t, &coderdtest.Options{
DeploymentValues: cfg,
Logger: ptr.Ref(slog.Make(sloghuman.Sink(io.Discard))),
})
_ = coderdtest.CreateFirstUser(t, client)
log := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
bun, err := support.Run(ctx, &support.Deps{
Client: client,
Log: log, // error due to missing workspace/agent
Log: slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Named("bundle").Leveled(slog.LevelDebug),
})
require.NoError(t, err)
require.NotEmpty(t, bun)
Expand All @@ -88,28 +93,36 @@ func TestRun(t *testing.T) {
t.Run("NoAuth", func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
client := coderdtest.New(t, nil)
client := coderdtest.New(t, &coderdtest.Options{
Logger: ptr.Ref(slog.Make(sloghuman.Sink(io.Discard))),
})
bun, err := support.Run(ctx, &support.Deps{
Client: client,
Log: slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Named("bundle").Leveled(slog.LevelDebug),
})
var sdkErr *codersdk.Error
require.NotNil(t, bun)
require.ErrorAs(t, err, &sdkErr)
require.Equal(t, http.StatusUnauthorized, sdkErr.StatusCode())
require.Empty(t, bun)
require.NotEmpty(t, bun)
require.NotEmpty(t, bun.Logs)
})

t.Run("MissingPrivilege", func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
client := coderdtest.New(t, nil)
client := coderdtest.New(t, &coderdtest.Options{
Logger: ptr.Ref(slog.Make(sloghuman.Sink(io.Discard))),
})
admin := coderdtest.CreateFirstUser(t, client)
memberClient, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
bun, err := support.Run(ctx, &support.Deps{
Client: memberClient,
Log: slogtest.Make(t, nil).Named("bundle").Leveled(slog.LevelDebug),
})
require.ErrorContains(t, err, "failed authorization check")
require.Empty(t, bun)
require.NotEmpty(t, bun)
require.NotEmpty(t, bun.Logs)
})
}

Expand Down