Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 3623ecb

Browse files
authored
sg/msp: filter generated environments by category (#62131)
Part of https://github.com/sourcegraph/managed-services/issues/599 See sourcegraph/managed-services#1288 for how this mechanism will be used. ## Test plan ```sh sg msp generate -all -category=test ``` ![image](https://github.com/sourcegraph/sourcegraph/assets/23356519/d27c5df5-6d13-43fa-96c2-5523da24693a)
1 parent 2ecd2d1 commit 3623ecb

File tree

5 files changed

+49
-9
lines changed

5 files changed

+49
-9
lines changed

dev/managedservicesplatform/cdktf.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ func (c CDKTF) OutputDir() string {
3232
// configured.
3333
func (c CDKTF) Synthesize() error {
3434
// Forcibly shut down the JSII runtime post-Synth to make sure that we don't
35-
// get bizarre side-effects from multiple apps being rendered.
36-
defer jsiiruntime.Close()
35+
// get bizarre side-effects from multiple apps being rendered. We use a panic
36+
// catcher to discard useless panics from the library.
37+
defer (&panics.Catcher{}).Try(jsiiruntime.Close)
3738

3839
// CDKTF is prone to panics for no good reason, so make a best-effort
3940
// attempt to capture them.

dev/sg/msp/helpers.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ func syncEnvironmentWorkspaces(c *cli.Context, tfc *terraformcloud.Client, servi
155155
type generateTerraformOptions struct {
156156
// targetEnv generates the specified env only, otherwise generates all
157157
targetEnv string
158+
// targetCategory generates the specified category only
159+
targetCategory spec.EnvironmentCategory
158160
// stableGenerate disables updating of any values that are evaluated at
159161
// generation time
160162
stableGenerate bool
@@ -178,8 +180,16 @@ func generateTerraform(service *spec.Spec, opts generateTerraformOptions) error
178180
for _, env := range envs {
179181
env := env
180182

181-
pending := std.Out.Pending(output.Styledf(output.StylePending,
182-
"[%s] Preparing Terraform for environment %q", serviceID, env.ID))
183+
if opts.targetCategory != "" && env.Category != opts.targetCategory {
184+
// Quietly skip environments that don't match specified category
185+
std.Out.WriteLine(output.StyleSuggestion.Linef(
186+
"[%s] Skipping non-%q environment %q (category %q)",
187+
serviceID, opts.targetCategory, env.ID, env.Category))
188+
continue
189+
}
190+
191+
pending := std.Out.Pending(output.StylePending.Linef(
192+
"[%s] Preparing Terraform for %q environment %q", serviceID, env.Category, env.ID))
183193
renderer := managedservicesplatform.Renderer{
184194
OutputDir: filepath.Join(filepath.Dir(serviceSpecPath), "terraform", env.ID),
185195
StableGenerate: opts.stableGenerate,
@@ -204,8 +214,8 @@ func generateTerraform(service *spec.Spec, opts generateTerraformOptions) error
204214
return err
205215
}
206216

207-
pending.Updatef("[%s] Generating Terraform assets in %q for environment %q...",
208-
serviceID, renderer.OutputDir, env.ID)
217+
pending.Updatef("[%s] Generating Terraform assets in %q for %q environment %q...",
218+
serviceID, renderer.OutputDir, env.Category, env.ID)
209219
if err := cdktf.Synthesize(); err != nil {
210220
return err
211221
}

dev/sg/msp/sg_msp.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,15 @@ Supports completions on services and environments.`,
272272
UsageText: `
273273
# generate single env for a single service
274274
sg msp generate <service> <env>
275+
275276
# generate all envs for a single service
276277
sg msp generate -all <service>
278+
277279
# generate all envs across all services
278280
sg msp generate -all
281+
282+
# generate all test envs across all services
283+
sg msp generate -all -category=test
279284
`,
280285
Before: msprepo.UseManagedServicesRepo,
281286
Flags: []cli.Flag{
@@ -284,6 +289,10 @@ sg msp generate -all
284289
Usage: "Generate infrastructure stacks for all services, or all envs for a service if service ID is provided",
285290
Value: false,
286291
},
292+
&cli.StringFlag{
293+
Name: "category",
294+
Usage: "Filter generated environments by category (one of 'test', 'internal', 'external') - must be used with '-all'",
295+
},
287296
&cli.BoolFlag{
288297
Name: "stable",
289298
Usage: "Configure updating of any values that are evaluated at generation time",
@@ -293,14 +302,24 @@ sg msp generate -all
293302
BashComplete: msprepo.ServicesAndEnvironmentsCompletion(),
294303
Action: func(c *cli.Context) error {
295304
var (
296-
generateAll = c.Bool("all")
297-
stableGenerate = c.Bool("stable")
305+
generateAll = c.Bool("all")
306+
generateCategory = spec.EnvironmentCategory(c.String("category"))
307+
stableGenerate = c.Bool("stable")
298308
)
299309

300310
if stableGenerate {
301311
std.Out.WriteSuggestionf("Using stable generate - tfvars will not be updated.")
302312
}
303313

314+
if generateCategory != "" {
315+
if !generateAll {
316+
return errors.New("'-category' must be used with '-all'")
317+
}
318+
if err := generateCategory.Validate(); err != nil {
319+
return errors.Wrap(err, "invalid value for '-category'")
320+
}
321+
}
322+
304323
// Generate a specific service environment if '-all' is not provided
305324
if !generateAll {
306325
std.Out.WriteNoticef("Generating a specific service environment...")
@@ -323,6 +342,7 @@ sg msp generate -all
323342
}
324343
return generateTerraform(svc, generateTerraformOptions{
325344
stableGenerate: stableGenerate,
345+
targetCategory: generateCategory,
326346
})
327347
}
328348

@@ -341,6 +361,7 @@ sg msp generate -all
341361
}
342362
if err := generateTerraform(s, generateTerraformOptions{
343363
stableGenerate: stableGenerate,
364+
targetCategory: generateCategory,
344365
}); err != nil {
345366
return errors.Wrap(err, serviceID)
346367
}

lib/output/line.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func Emoji(emoji string, s string) FancyLine {
4545
return Line(emoji, StyleReset, s)
4646
}
4747

48-
// Emoji creates a new FancyLine with an emoji prefix and style.
48+
// Emoji creates a new FancyLine with an emoji prefix and format string.
4949
func Emojif(emoji string, s string, a ...any) FancyLine {
5050
return Linef(emoji, StyleReset, s, a...)
5151
}

lib/output/style.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ type Style struct{ code string }
99

1010
func (s Style) String() string { return s.code }
1111

12+
// Line returns a FancyLine using this style as an alias for using output.Styledf(...)
13+
func (s Style) Line(format string) FancyLine { return Styled(s, format) }
14+
15+
// Linef returns a FancyLine using this style as an alias for using output.Styledf(...)
16+
func (s Style) Linef(format string, args ...interface{}) FancyLine {
17+
return Styledf(s, format, args...)
18+
}
19+
1220
func CombineStyles(styles ...Style) Style {
1321
sb := strings.Builder{}
1422
for _, s := range styles {

0 commit comments

Comments
 (0)