Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8c9b24f

Browse files
committed
chore: refactor flags that target workspaces in scaletest
1 parentd18441d commit8c9b24f

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+
typeworkspaceTargetFlagsstruct {
389+
templatestring
390+
targetWorkspacesstring
391+
useHostLoginbool
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+
iff.template!="" {
425+
_,err:=parseTemplate(ctx,client,organizationIDs,f.template)
426+
iferr!=nil {
427+
returnnil,xerrors.Errorf("parse template: %w",err)
428+
}
429+
}
430+
431+
// Parse target range
432+
targetStart,targetEnd,err:=parseTargetRange("workspaces",f.targetWorkspaces)
433+
iferr!=nil {
434+
returnnil,xerrors.Errorf("parse target workspaces: %w",err)
435+
}
436+
437+
// Determine owner based on useHostLogin
438+
varownerstring
439+
iff.useHostLogin {
440+
owner=codersdk.Me
441+
}
442+
443+
// Get workspaces
444+
workspaces,numSkipped,err:=getScaletestWorkspaces(ctx,client,owner,f.template)
445+
iferr!=nil {
446+
returnnil,err
447+
}
448+
ifnumSkipped>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+
iftargetEnd==0 {
454+
targetEnd=len(workspaces)
455+
}
456+
457+
// Validate range
458+
iflen(workspaces)==0 {
459+
returnnil,xerrors.Errorf("no scaletest workspaces exist")
460+
}
461+
iftargetEnd>len(workspaces) {
462+
returnnil,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+
returnworkspaces[targetStart:targetEnd],nil
467+
}
468+
387469
funcrequireAdmin(ctx context.Context,client*codersdk.Client) (codersdk.User,error) {
388470
me,err:=client.User(ctx,codersdk.Me)
389471
iferr!=nil {
@@ -1193,12 +1275,10 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *serpent.Command {
11931275
bytesPerTickint64
11941276
sshbool
11951277
disableDirectbool
1196-
useHostLoginbool
11971278
appstring
1198-
templatestring
1199-
targetWorkspacesstring
12001279
workspaceProxyURLstring
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-
iftemplate!="" {
1247-
_,err:=parseTemplate(ctx,client,me.OrganizationIDs,template)
1248-
iferr!=nil {
1249-
returnxerrors.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
iferr!=nil {
1254-
returnxerrors.Errorf("parse target workspaces: %w",err)
1328+
returnerr
12551329
}
12561330

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

1262-
varownerstring
1263-
ifuseHostLogin {
1264-
owner=codersdk.Me
1265-
}
1266-
1267-
workspaces,numSkipped,err:=getScaletestWorkspaces(inv.Context(),client,owner,template)
1268-
iferr!=nil {
1269-
returnerr
1270-
}
1271-
ifnumSkipped>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-
iftargetWorkspaceEnd==0 {
1276-
targetWorkspaceEnd=len(workspaces)
1277-
}
1278-
1279-
iflen(workspaces)==0 {
1280-
returnxerrors.Errorf("no scaletest workspaces exist")
1281-
}
1282-
iftargetWorkspaceEnd>len(workspaces) {
1283-
returnxerrors.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
iferr!=nil {
12881338
returnxerrors.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
foridx,ws:=rangeworkspaces {
1310-
ifidx<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)

[8]ページ先頭

©2009-2025 Movatter.jp