Skip to content

Commit dc27761

Browse files
authored
chore: refactor flags that target workspaces in scaletest (coder#20537)
For the coder/internal#913 we are going to be targeting running workspaces. So this PR modularizes the CLI flags and logic that select those targets so we can reuse it.
1 parent b90c74a commit dc27761

File tree

1 file changed

+86
-59
lines changed

1 file changed

+86
-59
lines changed

cli/exp_scaletest.go

Lines changed: 86 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,88 @@ func (s *scaletestPrometheusFlags) attach(opts *serpent.OptionSet) {
384384
)
385385
}
386386

387+
// workspaceTargetFlags holds common flags for targeting specific workspaces in scale tests.
388+
type workspaceTargetFlags struct {
389+
template string
390+
targetWorkspaces string
391+
useHostLogin bool
392+
}
393+
394+
// attach adds the workspace target flags to the given options set.
395+
func (f *workspaceTargetFlags) attach(opts *serpent.OptionSet) {
396+
*opts = append(*opts,
397+
serpent.Option{
398+
Flag: "template",
399+
FlagShorthand: "t",
400+
Env: "CODER_SCALETEST_TEMPLATE",
401+
Description: "Name or ID of the template. Traffic generation will be limited to workspaces created from this template.",
402+
Value: serpent.StringOf(&f.template),
403+
},
404+
serpent.Option{
405+
Flag: "target-workspaces",
406+
Env: "CODER_SCALETEST_TARGET_WORKSPACES",
407+
Description: "Target a specific range of workspaces in the format [START]:[END] (exclusive). Example: 0:10 will target the 10 first alphabetically sorted workspaces (0-9).",
408+
Value: serpent.StringOf(&f.targetWorkspaces),
409+
},
410+
serpent.Option{
411+
Flag: "use-host-login",
412+
Env: "CODER_SCALETEST_USE_HOST_LOGIN",
413+
Default: "false",
414+
Description: "Connect as the currently logged in user.",
415+
Value: serpent.BoolOf(&f.useHostLogin),
416+
},
417+
)
418+
}
419+
420+
// getTargetedWorkspaces retrieves the workspaces based on the template filter and target range. warnWriter is where to
421+
// write a warning message if any workspaces were skipped due to ownership mismatch.
422+
func (f *workspaceTargetFlags) getTargetedWorkspaces(ctx context.Context, client *codersdk.Client, organizationIDs []uuid.UUID, warnWriter io.Writer) ([]codersdk.Workspace, error) {
423+
// Validate template if provided
424+
if f.template != "" {
425+
_, err := parseTemplate(ctx, client, organizationIDs, f.template)
426+
if err != nil {
427+
return nil, xerrors.Errorf("parse template: %w", err)
428+
}
429+
}
430+
431+
// Parse target range
432+
targetStart, targetEnd, err := parseTargetRange("workspaces", f.targetWorkspaces)
433+
if err != nil {
434+
return nil, xerrors.Errorf("parse target workspaces: %w", err)
435+
}
436+
437+
// Determine owner based on useHostLogin
438+
var owner string
439+
if f.useHostLogin {
440+
owner = codersdk.Me
441+
}
442+
443+
// Get workspaces
444+
workspaces, numSkipped, err := getScaletestWorkspaces(ctx, client, owner, f.template)
445+
if err != nil {
446+
return nil, err
447+
}
448+
if numSkipped > 0 {
449+
cliui.Warnf(warnWriter, "CODER_DISABLE_OWNER_WORKSPACE_ACCESS is set on the deployment.\n\t%d workspace(s) were skipped due to ownership mismatch.\n\tSet --use-host-login to only target workspaces you own.", numSkipped)
450+
}
451+
452+
// Adjust targetEnd if not specified
453+
if targetEnd == 0 {
454+
targetEnd = len(workspaces)
455+
}
456+
457+
// Validate range
458+
if len(workspaces) == 0 {
459+
return nil, xerrors.Errorf("no scaletest workspaces exist")
460+
}
461+
if targetEnd > len(workspaces) {
462+
return nil, xerrors.Errorf("target workspace end %d is greater than the number of workspaces %d", targetEnd, len(workspaces))
463+
}
464+
465+
// Return the sliced workspaces
466+
return workspaces[targetStart:targetEnd], nil
467+
}
468+
387469
func requireAdmin(ctx context.Context, client *codersdk.Client) (codersdk.User, error) {
388470
me, err := client.User(ctx, codersdk.Me)
389471
if err != nil {
@@ -1193,12 +1275,10 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *serpent.Command {
11931275
bytesPerTick int64
11941276
ssh bool
11951277
disableDirect bool
1196-
useHostLogin bool
11971278
app string
1198-
template string
1199-
targetWorkspaces string
12001279
workspaceProxyURL string
12011280

1281+
targetFlags = &workspaceTargetFlags{}
12021282
tracingFlags = &scaletestTracingFlags{}
12031283
strategy = &scaletestStrategyFlags{}
12041284
cleanupStrategy = newScaletestCleanupStrategy()
@@ -1243,46 +1323,16 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *serpent.Command {
12431323
},
12441324
}
12451325

1246-
if template != "" {
1247-
_, err := parseTemplate(ctx, client, me.OrganizationIDs, template)
1248-
if err != nil {
1249-
return xerrors.Errorf("parse template: %w", err)
1250-
}
1251-
}
1252-
targetWorkspaceStart, targetWorkspaceEnd, err := parseTargetRange("workspaces", targetWorkspaces)
1326+
workspaces, err := targetFlags.getTargetedWorkspaces(ctx, client, me.OrganizationIDs, inv.Stdout)
12531327
if err != nil {
1254-
return xerrors.Errorf("parse target workspaces: %w", err)
1328+
return err
12551329
}
12561330

12571331
appHost, err := client.AppHost(ctx)
12581332
if err != nil {
12591333
return xerrors.Errorf("get app host: %w", err)
12601334
}
12611335

1262-
var owner string
1263-
if useHostLogin {
1264-
owner = codersdk.Me
1265-
}
1266-
1267-
workspaces, numSkipped, err := getScaletestWorkspaces(inv.Context(), client, owner, template)
1268-
if err != nil {
1269-
return err
1270-
}
1271-
if numSkipped > 0 {
1272-
cliui.Warnf(inv.Stdout, "CODER_DISABLE_OWNER_WORKSPACE_ACCESS is set on the deployment.\n\t%d workspace(s) were skipped due to ownership mismatch.\n\tSet --use-host-login to only target workspaces you own.", numSkipped)
1273-
}
1274-
1275-
if targetWorkspaceEnd == 0 {
1276-
targetWorkspaceEnd = len(workspaces)
1277-
}
1278-
1279-
if len(workspaces) == 0 {
1280-
return xerrors.Errorf("no scaletest workspaces exist")
1281-
}
1282-
if targetWorkspaceEnd > len(workspaces) {
1283-
return xerrors.Errorf("target workspace end %d is greater than the number of workspaces %d", targetWorkspaceEnd, len(workspaces))
1284-
}
1285-
12861336
tracerProvider, closeTracing, tracingEnabled, err := tracingFlags.provider(ctx)
12871337
if err != nil {
12881338
return xerrors.Errorf("create tracer provider: %w", err)
@@ -1307,10 +1357,6 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *serpent.Command {
13071357

13081358
th := harness.NewTestHarness(strategy.toStrategy(), cleanupStrategy.toStrategy())
13091359
for idx, ws := range workspaces {
1310-
if idx < targetWorkspaceStart || idx >= targetWorkspaceEnd {
1311-
continue
1312-
}
1313-
13141360
var (
13151361
agent codersdk.WorkspaceAgent
13161362
name = "workspace-traffic"
@@ -1415,19 +1461,6 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *serpent.Command {
14151461
}
14161462

14171463
cmd.Options = []serpent.Option{
1418-
{
1419-
Flag: "template",
1420-
FlagShorthand: "t",
1421-
Env: "CODER_SCALETEST_TEMPLATE",
1422-
Description: "Name or ID of the template. Traffic generation will be limited to workspaces created from this template.",
1423-
Value: serpent.StringOf(&template),
1424-
},
1425-
{
1426-
Flag: "target-workspaces",
1427-
Env: "CODER_SCALETEST_TARGET_WORKSPACES",
1428-
Description: "Target a specific range of workspaces in the format [START]:[END] (exclusive). Example: 0:10 will target the 10 first alphabetically sorted workspaces (0-9).",
1429-
Value: serpent.StringOf(&targetWorkspaces),
1430-
},
14311464
{
14321465
Flag: "bytes-per-tick",
14331466
Env: "CODER_SCALETEST_WORKSPACE_TRAFFIC_BYTES_PER_TICK",
@@ -1463,13 +1496,6 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *serpent.Command {
14631496
Description: "Send WebSocket traffic to a workspace app (proxied via coderd), cannot be used with --ssh.",
14641497
Value: serpent.StringOf(&app),
14651498
},
1466-
{
1467-
Flag: "use-host-login",
1468-
Env: "CODER_SCALETEST_USE_HOST_LOGIN",
1469-
Default: "false",
1470-
Description: "Connect as the currently logged in user.",
1471-
Value: serpent.BoolOf(&useHostLogin),
1472-
},
14731499
{
14741500
Flag: "workspace-proxy-url",
14751501
Env: "CODER_SCALETEST_WORKSPACE_PROXY_URL",
@@ -1479,6 +1505,7 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *serpent.Command {
14791505
},
14801506
}
14811507

1508+
targetFlags.attach(&cmd.Options)
14821509
tracingFlags.attach(&cmd.Options)
14831510
strategy.attach(&cmd.Options)
14841511
cleanupStrategy.attach(&cmd.Options)

0 commit comments

Comments
 (0)