Skip to content

Commit 28b37e4

Browse files
Fix terraform parallelism implementation (#455)
Use plan and apply args modifiers directly instead of env vars that break init
1 parent d01afab commit 28b37e4

File tree

7 files changed

+46
-16
lines changed

7 files changed

+46
-16
lines changed

pkg/harness/controller/controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ func (in *stackRunController) prepare() error {
216216
ExecDir: in.execWorkDir(),
217217
Variables: variables,
218218
Scanner: security.NewScanner(in.stackRun.PolicyEngine),
219+
Run: in.stackRun,
219220
})
220221

221222
return nil

pkg/harness/stackrun/v1/types.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,7 @@ func (in *StackRun) Env() []string {
8787
env = append(env, fmt.Sprintf("PLURAL_CONSOLE_URL=%s", lo.FromPtr(in.Creds.URL)))
8888
}
8989

90-
tfArgs := "TF_CLI_ARGS="
91-
if in.Parallelism != nil {
92-
tfArgs += fmt.Sprintf("-parallelism=%d ", *in.Parallelism)
93-
}
94-
95-
if in.Refresh != nil {
96-
tfArgs += fmt.Sprintf("-refresh=%t", *in.Refresh)
97-
}
98-
99-
return append(env, tfArgs)
90+
return env
10091
}
10192

10293
// Vars parses the StackRun.Variables map as a valid JSON.

pkg/harness/tool/terraform/modifier.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@ func (in *PlanArgsModifier) Args(args []string) []string {
1616
return args
1717
}
1818

19+
if in.parallelism != nil {
20+
args = append(args, fmt.Sprintf("-parallelism=%d", *in.parallelism))
21+
}
22+
23+
if in.refresh != nil {
24+
args = append(args, fmt.Sprintf("-refresh=%t", *in.refresh))
25+
}
26+
1927
return append(args, fmt.Sprintf("-out=%s", in.planFileName))
2028
}
2129

22-
func NewPlanArgsModifier(planFileName string) v1.Modifier {
23-
return &PlanArgsModifier{planFileName: planFileName}
30+
func (tf *Terraform) NewPlanArgsModifier(planFileName string) v1.Modifier {
31+
return &PlanArgsModifier{planFileName: planFileName, parallelism: tf.parallelism, refresh: tf.refresh}
2432
}
2533

2634
// Args implements [v1.ArgsModifier] type.
@@ -33,9 +41,13 @@ func (in *ApplyArgsModifier) Args(args []string) []string {
3341
return args
3442
}
3543

44+
if in.parallelism != nil {
45+
args = append(args, fmt.Sprintf("-parallelism=%d", *in.parallelism))
46+
}
47+
3648
return append(args, in.planFileName)
3749
}
3850

39-
func NewApplyArgsModifier(dir, planFileName string) v1.Modifier {
40-
return &ApplyArgsModifier{planFileName: planFileName, dir: dir}
51+
func (tf *Terraform) NewApplyArgsModifier(dir, planFileName string) v1.Modifier {
52+
return &ApplyArgsModifier{planFileName: planFileName, dir: dir, parallelism: tf.parallelism, refresh: tf.refresh}
4153
}

pkg/harness/tool/terraform/modifier_types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ type PlanArgsModifier struct {
1010

1111
// planFileName
1212
planFileName string
13+
14+
// parallelism
15+
parallelism *int64
16+
17+
// refresh
18+
refresh *bool
1319
}
1420

1521
// ApplyArgsModifier implements [v1.ArgsModifier] interface.
@@ -21,4 +27,10 @@ type ApplyArgsModifier struct {
2127

2228
// dir
2329
dir string
30+
31+
// parallelism
32+
parallelism *int64
33+
34+
// refresh
35+
refresh *bool
2436
}

pkg/harness/tool/terraform/terraform.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ func (in *Terraform) Output() ([]*console.StackOutputAttributes, error) {
8080
func (in *Terraform) Modifier(stage console.StepStage) v1.Modifier {
8181
switch stage {
8282
case console.StepStagePlan:
83-
return NewPlanArgsModifier(in.planFileName)
83+
return in.NewPlanArgsModifier(in.planFileName)
8484
case console.StepStageApply:
85-
return NewApplyArgsModifier(in.dir, in.planFileName)
85+
return in.NewApplyArgsModifier(in.dir, in.planFileName)
8686
}
8787

8888
return v1.NewDefaultModifier()
@@ -182,5 +182,7 @@ func New(config v1.Config) v1.Tool {
182182
DefaultTool: v1.DefaultTool{Scanner: config.Scanner},
183183
dir: config.ExecDir,
184184
variables: config.Variables,
185+
parallelism: config.Run.Parallelism,
186+
refresh: config.Run.Refresh,
185187
}).init()
186188
}

pkg/harness/tool/terraform/terraform_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,12 @@ type Terraform struct {
2222
// variables is a JSON encoded string representing
2323
// terraform variable file.
2424
variables *string
25+
26+
// parallelism is the number of parallel terraform operations.
27+
// Default: 10
28+
parallelism *int64
29+
30+
// refresh is a flag to refresh the state.
31+
// Default: true
32+
refresh *bool
2533
}

pkg/harness/tool/v1/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
console "github.com/pluralsh/console/go/client"
77

88
securityv1 "github.com/pluralsh/deployment-operator/pkg/harness/security/v1"
9+
10+
stackrunv1 "github.com/pluralsh/deployment-operator/pkg/harness/stackrun/v1"
911
)
1012

1113
// Tool handles one of the supported infrastructure management tools.
@@ -93,4 +95,6 @@ type Config struct {
9395

9496
// Scanner TODO
9597
Scanner securityv1.Scanner
98+
99+
Run *stackrunv1.StackRun
96100
}

0 commit comments

Comments
 (0)