Skip to content

Commit f60dcc6

Browse files
committed
separate workspace and agent info
1 parent 851112f commit f60dcc6

File tree

2 files changed

+55
-51
lines changed

2 files changed

+55
-51
lines changed

support/support.go

+33-27
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,24 @@ import (
2020
// Even though we do attempt to sanitize data, it may still contain
2121
// sensitive information and should thus be treated as secret.
2222
type Bundle struct {
23-
BuildInfo *codersdk.BuildInfoResponse `json:"build_info"`
24-
DeploymentConfig *codersdk.DeploymentConfig `json:"deployment_config"`
25-
Experiments codersdk.Experiments `json:"experiments"`
26-
HealthReport *codersdk.HealthcheckReport `json:"health_report"`
27-
Workspace *codersdk.Workspace `json:"workspace"`
28-
WorkspaceBuildLogs []codersdk.ProvisionerJobLog `json:"workspace_build_logs"`
29-
Agent *Agent `json:"agent"`
30-
AgentLogs []string `json:"agent_logs"`
23+
DeploymentInfo DeploymentInfo `json:"deployment_info"`
24+
WorkspaceInfo *WorkspaceInfo `json:"workspace_info"`
25+
Logs []string `json:"logs"`
3126
}
3227

33-
// AgentInfo is a set of information derived from a given workspace agent.
34-
type Agent struct {
35-
ConnectionInfo codersdk.WorkspaceAgentConnectionInfo
36-
Logs []codersdk.WorkspaceAgentLog
28+
type DeploymentInfo struct {
29+
BuildInfo *codersdk.BuildInfoResponse `json:"build_info"`
30+
Config *codersdk.DeploymentConfig `json:"deployment_config"`
31+
Experiments codersdk.Experiments `json:"experiments"`
32+
HealthReport *codersdk.HealthcheckReport `json:"health_report"`
33+
}
34+
35+
type WorkspaceInfo struct {
36+
Workspace codersdk.Workspace `json:"workspace"`
37+
BuildLogs []codersdk.ProvisionerJobLog `json:"build_logs"`
38+
Agent codersdk.WorkspaceAgent `json:"agent"`
39+
AgentConnectionInfo codersdk.WorkspaceAgentConnectionInfo
40+
AgentStartupLogs []codersdk.WorkspaceAgentLog `json:"startup_logs"`
3741
}
3842

3943
// String implements fmt.Stringer for Bundle.
@@ -77,12 +81,6 @@ func Run(ctx context.Context, d *Deps) (*Bundle, error) {
7781
return nil, xerrors.Errorf("developer error: no information source provided!")
7882
}
7983

80-
var logw strings.Builder
81-
d.Log.AppendSinks(sloghuman.Sink(&logw))
82-
defer func() {
83-
b.AgentLogs = strings.Split(logw.String(), "\n")
84-
}()
85-
8684
authChecks := map[string]codersdk.AuthorizationCheck{
8785
"Read DeploymentValues": {
8886
Object: codersdk.AuthorizationObject{
@@ -102,32 +100,39 @@ func Run(ctx context.Context, d *Deps) (*Bundle, error) {
102100
}
103101
}
104102

103+
// Ensure we capture logs from the client.
104+
var logw strings.Builder
105+
d.Log.AppendSinks(sloghuman.Sink(&logw))
106+
defer func() {
107+
b.Logs = strings.Split(logw.String(), "\n")
108+
}()
109+
105110
bi, err := d.Source.BuildInfo(ctx)
106111
if err != nil {
107112
d.Log.Error(ctx, "fetch build info", slog.Error(err))
108113
} else {
109-
b.BuildInfo = &bi
114+
b.DeploymentInfo.BuildInfo = &bi
110115
}
111116

112117
dc, err := d.Source.DeploymentConfig(ctx)
113118
if err != nil {
114119
d.Log.Error(ctx, "fetch deployment config", slog.Error(err))
115120
} else {
116-
b.DeploymentConfig = dc
121+
b.DeploymentInfo.Config = dc
117122
}
118123

119124
hr, err := d.Source.GetDebugHealth(ctx)
120125
if err != nil {
121126
d.Log.Error(ctx, "fetch health report", slog.Error(err))
122127
} else {
123-
b.HealthReport = &hr
128+
b.DeploymentInfo.HealthReport = &hr
124129
}
125130

126131
exp, err := d.Source.Experiments(ctx)
127132
if err != nil {
128133
d.Log.Error(ctx, "fetch experiments", slog.Error(err))
129134
} else {
130-
b.Experiments = exp
135+
b.DeploymentInfo.Experiments = exp
131136
}
132137

133138
// Below checks require a workspace and agent
@@ -136,11 +141,13 @@ func Run(ctx context.Context, d *Deps) (*Bundle, error) {
136141
return &b, nil
137142
}
138143

144+
b.WorkspaceInfo = &WorkspaceInfo{}
145+
139146
ws, err := d.Source.Workspace(ctx, d.WorkspaceID)
140147
if err != nil {
141148
d.Log.Error(ctx, "fetch workspace", slog.Error(err), slog.F("workspace_id", d.WorkspaceID.String()))
142149
} else {
143-
b.Workspace = &ws
150+
b.WorkspaceInfo.Workspace = ws
144151
}
145152

146153
buildLogCh, closer, err := d.Source.WorkspaceBuildLogsAfter(ctx, ws.LatestBuild.ID, 0)
@@ -149,7 +156,7 @@ func Run(ctx context.Context, d *Deps) (*Bundle, error) {
149156
} else {
150157
defer closer.Close()
151158
for log := range buildLogCh {
152-
b.WorkspaceBuildLogs = append(b.WorkspaceBuildLogs, log)
159+
b.WorkspaceInfo.BuildLogs = append(b.WorkspaceInfo.BuildLogs, log)
153160
}
154161
}
155162

@@ -169,12 +176,11 @@ func Run(ctx context.Context, d *Deps) (*Bundle, error) {
169176
}
170177
}
171178

172-
b.Agent = &Agent{}
173179
connInfo, err := d.Source.WorkspaceAgentConnectionInfo(ctx, agentID)
174180
if err != nil {
175181
d.Log.Error(ctx, "fetch agent conn info", slog.Error(err), slog.F("agent_id", agentID.String()))
176182
} else {
177-
b.Agent.ConnectionInfo = connInfo
183+
b.WorkspaceInfo.AgentConnectionInfo = connInfo
178184
}
179185

180186
agentLogCh, closer, err := d.Source.WorkspaceAgentLogsAfter(ctx, agentID, 0, false)
@@ -183,7 +189,7 @@ func Run(ctx context.Context, d *Deps) (*Bundle, error) {
183189
} else {
184190
defer closer.Close()
185191
for logChunk := range agentLogCh {
186-
b.Agent.Logs = append(b.Agent.Logs, logChunk...)
192+
b.WorkspaceInfo.AgentStartupLogs = append(b.WorkspaceInfo.AgentStartupLogs, logChunk...)
187193
}
188194
}
189195

support/support_test.go

+22-24
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ func TestRun(t *testing.T) {
4141
})
4242
require.NoError(t, err)
4343
require.NotEmpty(t, bun)
44-
require.NotEmpty(t, bun.AgentLogs)
45-
require.NotEmpty(t, bun.BuildInfo)
46-
require.NotEmpty(t, bun.DeploymentConfig)
47-
require.NotEmpty(t, bun.DeploymentConfig.Options)
48-
require.NotEmpty(t, bun.HealthReport)
49-
require.NotEmpty(t, bun.Experiments)
50-
require.NotNil(t, bun.Workspace)
51-
require.NotEmpty(t, bun.WorkspaceBuildLogs)
52-
require.NotNil(t, bun.Agent)
53-
require.NotNil(t, bun.Agent.ConnectionInfo)
54-
require.NotEmpty(t, bun.Agent.Logs) // no logs right now
55-
assertSanitizedDeploymentConfig(t, bun.DeploymentConfig)
44+
require.NotEmpty(t, bun.DeploymentInfo.BuildInfo)
45+
require.NotEmpty(t, bun.DeploymentInfo.Config)
46+
require.NotEmpty(t, bun.DeploymentInfo.Config.Options)
47+
assertSanitizedDeploymentConfig(t, bun.DeploymentInfo.Config)
48+
require.NotEmpty(t, bun.DeploymentInfo.HealthReport)
49+
require.NotEmpty(t, bun.DeploymentInfo.Experiments)
50+
require.NotNil(t, bun.WorkspaceInfo.Workspace)
51+
require.NotEmpty(t, bun.WorkspaceInfo.BuildLogs)
52+
require.NotNil(t, bun.WorkspaceInfo.Agent)
53+
require.NotNil(t, bun.WorkspaceInfo.AgentConnectionInfo)
54+
require.NotEmpty(t, bun.WorkspaceInfo.AgentStartupLogs) // no logs right now
55+
require.NotEmpty(t, bun.Logs)
5656
})
5757

5858
t.Run("OK_NoAgent", func(t *testing.T) {
@@ -70,16 +70,14 @@ func TestRun(t *testing.T) {
7070
})
7171
require.NoError(t, err)
7272
require.NotEmpty(t, bun)
73-
require.NotEmpty(t, bun.AgentLogs)
74-
require.NotEmpty(t, bun.BuildInfo)
75-
require.NotEmpty(t, bun.DeploymentConfig)
76-
require.NotEmpty(t, bun.DeploymentConfig.Options)
77-
require.NotEmpty(t, bun.HealthReport)
78-
require.NotEmpty(t, bun.Experiments)
79-
require.Nil(t, bun.Workspace)
80-
require.Nil(t, bun.Agent)
81-
assertSanitizedDeploymentConfig(t, bun.DeploymentConfig)
82-
t.Logf(bun.String())
73+
require.NotEmpty(t, bun.DeploymentInfo.BuildInfo)
74+
require.NotEmpty(t, bun.DeploymentInfo.Config)
75+
require.NotEmpty(t, bun.DeploymentInfo.Config.Options)
76+
assertSanitizedDeploymentConfig(t, bun.DeploymentInfo.Config)
77+
require.NotEmpty(t, bun.DeploymentInfo.HealthReport)
78+
require.NotEmpty(t, bun.DeploymentInfo.Experiments)
79+
require.Nil(t, bun.WorkspaceInfo)
80+
require.NotEmpty(t, bun.Logs)
8381
})
8482

8583
t.Run("NoAuth", func(t *testing.T) {
@@ -93,7 +91,7 @@ func TestRun(t *testing.T) {
9391
require.NotNil(t, bun)
9492
require.ErrorAs(t, err, &sdkErr)
9593
require.Equal(t, http.StatusUnauthorized, sdkErr.StatusCode())
96-
require.NotEmpty(t, bun.AgentLogs)
94+
require.Empty(t, bun)
9795
})
9896

9997
t.Run("MissingPrivilege", func(t *testing.T) {
@@ -106,7 +104,7 @@ func TestRun(t *testing.T) {
106104
Source: memberClient,
107105
})
108106
require.ErrorContains(t, err, "failed authorization check")
109-
require.NotEmpty(t, bun.AgentLogs)
107+
require.Empty(t, bun)
110108
})
111109
}
112110

0 commit comments

Comments
 (0)