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