Skip to content

Commit e84410f

Browse files
authored
enos generate: fix slow decoding (#105)
The module generator operation was still using the old method of decoding whereby we'd decode and validate the entire flightplan before selecting matching scenarios. This updates it to the new lazy decoder that only decodes matching scenarios. Signed-off-by: Ryan Cragun <[email protected]>
1 parent ff334f7 commit e84410f

File tree

6 files changed

+25
-57
lines changed

6 files changed

+25
-57
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ linters:
8686
- goconst
8787
# disabled for now
8888
- gocritic
89+
- gocyclo
8990
# disabled for now
9091
- godox
9192
- goerr113

internal/flightplan/scenario_decoder.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ func (d *ScenarioDecoder) DecodeScenarioBlocks(ctx context.Context, blocks []*hc
149149

150150
scenarioBlocks := d.filterScenarioBlocks(blocks)
151151
for i := range scenarioBlocks {
152+
// Don't worry about decoding scenario blocks that don't match our name if we've been
153+
// given a name.
154+
if d.ScenarioFilter != nil && d.ScenarioFilter.Name != "" {
155+
if d.ScenarioFilter.Name != scenarioBlocks[i].Name {
156+
continue
157+
}
158+
}
159+
152160
if d.DecodeTarget >= DecodeTargetScenariosMatrixOnly {
153161
var diags hcl.Diagnostics
154162
scenarioBlocks[i].Matrix, diags = decodeMatrix(d.EvalContext, scenarioBlocks[i].Block)

internal/generate/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func (g *Generator) ensureOutDir() error {
300300

301301
// maybeWriteTerraformSettings writes any configured "terraform" settings
302302
//
303-
//nolint:cyclop,gocyclo // writing out our terraform settings is complicated.
303+
//nolint:cyclop // writing out our terraform settings is complicated.
304304
func (g *Generator) maybeWriteTerraformSettings(rootBody *hclwrite.Body) {
305305
s := g.Scenario.TerraformSetting
306306
if s == nil {

internal/operation/flightplan.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package operation
22

33
import (
4-
"context"
54
"path/filepath"
65

7-
"github.com/hashicorp/enos/internal/diagnostics"
8-
"github.com/hashicorp/enos/internal/flightplan"
96
"github.com/hashicorp/enos/proto/hashicorp/enos/v1/pb"
107
)
118

@@ -20,37 +17,3 @@ func isAbs(path string) (string, error) {
2017
func outDirForWorkspace(w *pb.Workspace) string {
2118
return filepath.Join(w.GetFlightplan().GetBaseDir(), ".enos")
2219
}
23-
24-
func decodeFlightPlan(ctx context.Context, pfp *pb.FlightPlan) (*flightplan.FlightPlan, *pb.DecodeResponse) {
25-
res := &pb.DecodeResponse{
26-
Diagnostics: []*pb.Diagnostic{},
27-
}
28-
29-
dec, err := flightplan.NewDecoder(
30-
flightplan.WithDecoderBaseDir(pfp.GetBaseDir()),
31-
flightplan.WithDecoderFPFiles(pfp.GetEnosHcl()),
32-
flightplan.WithDecoderVarFiles(pfp.GetEnosVarsHcl()),
33-
flightplan.WithDecoderEnv(pfp.GetEnosVarsEnv()),
34-
)
35-
if err != nil {
36-
res.Diagnostics = diagnostics.FromErr(err)
37-
38-
return nil, res
39-
}
40-
41-
hclDiags := dec.Parse()
42-
if len(hclDiags) > 0 {
43-
res.Diagnostics = append(res.Diagnostics, diagnostics.FromHCL(dec.ParserFiles(), hclDiags)...)
44-
}
45-
46-
if diagnostics.HasErrors(res.GetDiagnostics()) {
47-
return nil, res
48-
}
49-
50-
fp, hclDiags := dec.Decode(ctx)
51-
if len(hclDiags) > 0 {
52-
res.Diagnostics = append(res.Diagnostics, diagnostics.FromHCL(dec.ParserFiles(), hclDiags)...)
53-
}
54-
55-
return fp, res
56-
}

internal/operation/runner_module_generate.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,33 +117,29 @@ func scenarioAndModuleGeneratorForReq(ctx context.Context, req *pb.Operation_Req
117117
*flightplan.Scenario,
118118
[]*pb.Diagnostic,
119119
) {
120-
ws := req.GetWorkspace()
121-
if ws == nil {
122-
return nil, nil, diagnostics.FromErr(fmt.Errorf("unable to create generator for nil workspace"))
120+
filter, err := flightplan.NewScenarioFilter(
121+
flightplan.WithScenarioFilterFromScenarioRef(req.GetScenario()),
122+
)
123+
if err != nil {
124+
return nil, nil, diagnostics.FromErr(err)
123125
}
124126

125-
s := req.GetScenario()
126-
if s == nil {
127-
return nil, nil, diagnostics.FromErr(fmt.Errorf("unable to create generator for nil scenario reference"))
128-
}
127+
fp, decRes := flightplan.DecodeProto(
128+
ctx,
129+
req.GetWorkspace().GetFlightplan(),
130+
flightplan.DecodeTargetAll,
131+
filter.Proto(),
132+
)
129133

130-
// Decode our flight plan, find our scenario
131-
fp, decRes := decodeFlightPlan(ctx, ws.GetFlightplan())
132134
if diagnostics.HasFailed(
133-
ws.GetTfExecCfg().GetFailOnWarnings(),
135+
req.GetWorkspace().GetTfExecCfg().GetFailOnWarnings(),
134136
decRes.GetDiagnostics(),
135137
) {
136138
return nil, nil, decRes.GetDiagnostics()
137139
}
138140

139-
filter, err := flightplan.NewScenarioFilter(
140-
flightplan.WithScenarioFilterFromScenarioRef(s),
141-
)
142-
if err != nil {
143-
return nil, nil, diagnostics.FromErr(err)
144-
}
145-
146-
scenarios := fp.ScenariosSelect(filter)
141+
ws := req.GetWorkspace()
142+
scenarios := fp.Scenarios()
147143
switch len(scenarios) {
148144
case 0:
149145
return nil, nil, diagnostics.FromErr(fmt.Errorf("no matching scenarios found"))

version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var (
1414
//
1515
// Version must conform to the format expected by github.com/hashicorp/go-version
1616
// for tests to work.
17-
Version = "0.0.20"
17+
Version = "0.0.21"
1818

1919
// VersionPrerelease is a pre-release marker for the version. If this is ""
2020
// (empty string) then it means that it is a final release. Otherwise, this

0 commit comments

Comments
 (0)