@@ -394,6 +394,8 @@ func (r *userCleanupRunner) Run(ctx context.Context, _ string, _ io.Writer) erro
394
394
}
395
395
396
396
func (r * RootCmd ) scaletestCleanup () * clibase.Cmd {
397
+ var template string
398
+
397
399
cleanupStrategy := & scaletestStrategyFlags {cleanup : true }
398
400
client := new (codersdk.Client )
399
401
@@ -407,7 +409,7 @@ func (r *RootCmd) scaletestCleanup() *clibase.Cmd {
407
409
Handler : func (inv * clibase.Invocation ) error {
408
410
ctx := inv .Context ()
409
411
410
- _ , err := requireAdmin (ctx , client )
412
+ me , err := requireAdmin (ctx , client )
411
413
if err != nil {
412
414
return err
413
415
}
@@ -421,8 +423,15 @@ func (r *RootCmd) scaletestCleanup() *clibase.Cmd {
421
423
},
422
424
}
423
425
426
+ if template != "" {
427
+ _ , err := parseTemplate (ctx , client , me .OrganizationIDs , template )
428
+ if err != nil {
429
+ return xerrors .Errorf ("parse template: %w" , err )
430
+ }
431
+ }
432
+
424
433
cliui .Infof (inv .Stdout , "Fetching scaletest workspaces..." )
425
- workspaces , err := getScaletestWorkspaces (ctx , client )
434
+ workspaces , err := getScaletestWorkspaces (ctx , client , template )
426
435
if err != nil {
427
436
return err
428
437
}
@@ -494,6 +503,15 @@ func (r *RootCmd) scaletestCleanup() *clibase.Cmd {
494
503
},
495
504
}
496
505
506
+ cmd .Options = clibase.OptionSet {
507
+ {
508
+ Flag : "template" ,
509
+ Env : "CODER_SCALETEST_CLEANUP_TEMPLATE" ,
510
+ Description : "Name or ID of the template. Only delete workspaces created from the given template." ,
511
+ Value : clibase .StringOf (& template ),
512
+ },
513
+ }
514
+
497
515
cleanupStrategy .attach (& cmd .Options )
498
516
return cmd
499
517
}
@@ -564,34 +582,12 @@ func (r *RootCmd) scaletestCreateWorkspaces() *clibase.Cmd {
564
582
return xerrors .Errorf ("could not parse --output flags" )
565
583
}
566
584
567
- var tpl codersdk.Template
568
585
if template == "" {
569
586
return xerrors .Errorf ("--template is required" )
570
587
}
571
- if id , err := uuid .Parse (template ); err == nil && id != uuid .Nil {
572
- tpl , err = client .Template (ctx , id )
573
- if err != nil {
574
- return xerrors .Errorf ("get template by ID %q: %w" , template , err )
575
- }
576
- } else {
577
- // List templates in all orgs until we find a match.
578
- orgLoop:
579
- for _ , orgID := range me .OrganizationIDs {
580
- tpls , err := client .TemplatesByOrganization (ctx , orgID )
581
- if err != nil {
582
- return xerrors .Errorf ("list templates in org %q: %w" , orgID , err )
583
- }
584
-
585
- for _ , t := range tpls {
586
- if t .Name == template {
587
- tpl = t
588
- break orgLoop
589
- }
590
- }
591
- }
592
- }
593
- if tpl .ID == uuid .Nil {
594
- return xerrors .Errorf ("could not find template %q in any organization" , template )
588
+ tpl , err := parseTemplate (ctx , client , me .OrganizationIDs , template )
589
+ if err != nil {
590
+ return xerrors .Errorf ("parse template: %w" , err )
595
591
}
596
592
597
593
cliRichParameters , err := asWorkspaceBuildParameters (parameterFlags .richParameters )
@@ -859,6 +855,7 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *clibase.Cmd {
859
855
tickInterval time.Duration
860
856
bytesPerTick int64
861
857
ssh bool
858
+ template string
862
859
863
860
client = & codersdk.Client {}
864
861
tracingFlags = & scaletestTracingFlags {}
@@ -876,6 +873,12 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *clibase.Cmd {
876
873
),
877
874
Handler : func (inv * clibase.Invocation ) error {
878
875
ctx := inv .Context ()
876
+
877
+ me , err := requireAdmin (ctx , client )
878
+ if err != nil {
879
+ return err
880
+ }
881
+
879
882
reg := prometheus .NewRegistry ()
880
883
metrics := workspacetraffic .NewMetrics (reg , "username" , "workspace_name" , "agent_name" )
881
884
@@ -893,7 +896,14 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *clibase.Cmd {
893
896
},
894
897
}
895
898
896
- workspaces , err := getScaletestWorkspaces (inv .Context (), client )
899
+ if template != "" {
900
+ _ , err := parseTemplate (ctx , client , me .OrganizationIDs , template )
901
+ if err != nil {
902
+ return xerrors .Errorf ("parse template: %w" , err )
903
+ }
904
+ }
905
+
906
+ workspaces , err := getScaletestWorkspaces (inv .Context (), client , template )
897
907
if err != nil {
898
908
return err
899
909
}
@@ -997,6 +1007,13 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *clibase.Cmd {
997
1007
}
998
1008
999
1009
cmd .Options = []clibase.Option {
1010
+ {
1011
+ Flag : "template" ,
1012
+ FlagShorthand : "t" ,
1013
+ Env : "CODER_SCALETEST_TEMPLATE" ,
1014
+ Description : "Name or ID of the template. Traffic generation will be limited to workspaces created from this template." ,
1015
+ Value : clibase .StringOf (& template ),
1016
+ },
1000
1017
{
1001
1018
Flag : "bytes-per-tick" ,
1002
1019
Env : "CODER_SCALETEST_WORKSPACE_TRAFFIC_BYTES_PER_TICK" ,
@@ -1281,7 +1298,7 @@ func isScaleTestWorkspace(workspace codersdk.Workspace) bool {
1281
1298
strings .HasPrefix (workspace .Name , "scaletest-" )
1282
1299
}
1283
1300
1284
- func getScaletestWorkspaces (ctx context.Context , client * codersdk.Client ) ([]codersdk.Workspace , error ) {
1301
+ func getScaletestWorkspaces (ctx context.Context , client * codersdk.Client , template string ) ([]codersdk.Workspace , error ) {
1285
1302
var (
1286
1303
pageNumber = 0
1287
1304
limit = 100
@@ -1290,9 +1307,10 @@ func getScaletestWorkspaces(ctx context.Context, client *codersdk.Client) ([]cod
1290
1307
1291
1308
for {
1292
1309
page , err := client .Workspaces (ctx , codersdk.WorkspaceFilter {
1293
- Name : "scaletest-" ,
1294
- Offset : pageNumber * limit ,
1295
- Limit : limit ,
1310
+ Name : "scaletest-" ,
1311
+ Template : template ,
1312
+ Offset : pageNumber * limit ,
1313
+ Limit : limit ,
1296
1314
})
1297
1315
if err != nil {
1298
1316
return nil , xerrors .Errorf ("fetch scaletest workspaces page %d: %w" , pageNumber , err )
@@ -1349,3 +1367,33 @@ func getScaletestUsers(ctx context.Context, client *codersdk.Client) ([]codersdk
1349
1367
1350
1368
return users , nil
1351
1369
}
1370
+
1371
+ func parseTemplate (ctx context.Context , client * codersdk.Client , organizationIDs []uuid.UUID , template string ) (tpl codersdk.Template , err error ) {
1372
+ if id , err := uuid .Parse (template ); err == nil && id != uuid .Nil {
1373
+ tpl , err = client .Template (ctx , id )
1374
+ if err != nil {
1375
+ return tpl , xerrors .Errorf ("get template by ID %q: %w" , template , err )
1376
+ }
1377
+ } else {
1378
+ // List templates in all orgs until we find a match.
1379
+ orgLoop:
1380
+ for _ , orgID := range organizationIDs {
1381
+ tpls , err := client .TemplatesByOrganization (ctx , orgID )
1382
+ if err != nil {
1383
+ return tpl , xerrors .Errorf ("list templates in org %q: %w" , orgID , err )
1384
+ }
1385
+
1386
+ for _ , t := range tpls {
1387
+ if t .Name == template {
1388
+ tpl = t
1389
+ break orgLoop
1390
+ }
1391
+ }
1392
+ }
1393
+ }
1394
+ if tpl .ID == uuid .Nil {
1395
+ return tpl , xerrors .Errorf ("could not find template %q in any organization" , template )
1396
+ }
1397
+
1398
+ return tpl , nil
1399
+ }
0 commit comments