Skip to content

Commit

Permalink
feat(runner): implement apply without plan artifact option (#265)
Browse files Browse the repository at this point in the history
* feat(runner): implement apply without plan artifact option

* feat(api): add tests for GetApplyWithoutPlanArtifactEnabled
  • Loading branch information
LucasMrqes authored Apr 27, 2024
1 parent eb152d2 commit ae880ba
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 8 deletions.
16 changes: 14 additions & 2 deletions api/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ type RunHistoryPolicy struct {
}

type RemediationStrategy struct {
AutoApply bool `json:"autoApply,omitempty"`
OnError OnErrorRemediationStrategy `json:"onError,omitempty"`
AutoApply bool `json:"autoApply,omitempty"`
ApplyWithoutPlanArtifact *bool `json:"applyWithoutPlanArtifact,omitempty"`
OnError OnErrorRemediationStrategy `json:"onError,omitempty"`
}

type OnErrorRemediationStrategy struct {
Expand Down Expand Up @@ -108,6 +109,17 @@ func GetRunHistoryPolicy(repository *TerraformRepository, layer *TerraformLayer)
}
}

func GetApplyWithoutPlanArtifactEnabled(repository *TerraformRepository, layer *TerraformLayer) bool {
enabled := false
if repository.Spec.RemediationStrategy.ApplyWithoutPlanArtifact != nil {
enabled = *repository.Spec.RemediationStrategy.ApplyWithoutPlanArtifact
}
if layer.Spec.RemediationStrategy.ApplyWithoutPlanArtifact != nil {
enabled = *layer.Spec.RemediationStrategy.ApplyWithoutPlanArtifact
}
return enabled
}

func GetRemediationStrategy(repo *TerraformRepository, layer *TerraformLayer) RemediationStrategy {
result := RemediationStrategy{
AutoApply: false,
Expand Down
78 changes: 78 additions & 0 deletions api/v1alpha1/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,84 @@ func TestGetTerragruntEnabled(t *testing.T) {
}
}

func TestGetApplyWithoutPlanArtifactEnabled(t *testing.T) {
tt := []struct {
name string
repository *configv1alpha1.TerraformRepository
layer *configv1alpha1.TerraformLayer
expected bool
}{
{
"OnlyRepositoryEnabling",
&configv1alpha1.TerraformRepository{
Spec: configv1alpha1.TerraformRepositorySpec{
RemediationStrategy: configv1alpha1.RemediationStrategy{
ApplyWithoutPlanArtifact: &[]bool{true}[0],
},
},
},
&configv1alpha1.TerraformLayer{},
true,
},
{
"OnlyLayerEnabling",
&configv1alpha1.TerraformRepository{},
&configv1alpha1.TerraformLayer{
Spec: configv1alpha1.TerraformLayerSpec{
RemediationStrategy: configv1alpha1.RemediationStrategy{
ApplyWithoutPlanArtifact: &[]bool{true}[0],
},
},
},
true,
},
{
"DisabledInRepositoryEnabledInLayer",
&configv1alpha1.TerraformRepository{
Spec: configv1alpha1.TerraformRepositorySpec{
RemediationStrategy: configv1alpha1.RemediationStrategy{
ApplyWithoutPlanArtifact: &[]bool{false}[0],
},
},
},
&configv1alpha1.TerraformLayer{
Spec: configv1alpha1.TerraformLayerSpec{
RemediationStrategy: configv1alpha1.RemediationStrategy{
ApplyWithoutPlanArtifact: &[]bool{true}[0],
},
},
},
true,
},
{
"EnabledInRepositoryDisabledInLayer",
&configv1alpha1.TerraformRepository{
Spec: configv1alpha1.TerraformRepositorySpec{
RemediationStrategy: configv1alpha1.RemediationStrategy{
ApplyWithoutPlanArtifact: &[]bool{true}[0],
},
},
},
&configv1alpha1.TerraformLayer{
Spec: configv1alpha1.TerraformLayerSpec{
RemediationStrategy: configv1alpha1.RemediationStrategy{
ApplyWithoutPlanArtifact: &[]bool{false}[0],
},
},
},
false,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
result := configv1alpha1.GetApplyWithoutPlanArtifactEnabled(tc.repository, tc.layer)
if tc.expected != result {
t.Errorf("different enabled status computed: expected %t go %t", tc.expected, result)
}
})
}
}

func TestOverrideRunnerSpec(t *testing.T) {
tt := []struct {
name string
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type TerraformExec interface {
Install() error
Init(string) error
Plan() error
Apply() error
Apply(bool) error
Show(string) ([]byte, error)
}

Expand Down Expand Up @@ -304,7 +304,12 @@ func (r *Runner) apply() (string, error) {
return "", err
}
log.Print("launching terraform apply")
err = r.exec.Apply()
if configv1alpha1.GetApplyWithoutPlanArtifactEnabled(r.repository, r.layer) {
log.Infof("applying without reusing plan artifact from previous plan run")
err = r.exec.Apply(false)
} else {
err = r.exec.Apply(true)
}
if err != nil {
log.Errorf("error executing terraform apply: %s", err)
return "", err
Expand Down
9 changes: 7 additions & 2 deletions internal/runner/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,14 @@ func (t *Terraform) Plan() error {
return nil
}

func (t *Terraform) Apply() error {
func (t *Terraform) Apply(usePlanArtifact bool) error {
t.verbose()
err := t.exec.Apply(context.Background(), tfexec.DirOrPlan(t.planArtifactPath))
applyOpts := []tfexec.ApplyOption{}
if usePlanArtifact {
applyOpts = append(applyOpts, tfexec.DirOrPlan(t.planArtifactPath))
}

err := t.exec.Apply(context.Background(), applyOpts...)
if err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions internal/runner/terragrunt/terragrunt.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ func (t *Terragrunt) Plan() error {
return nil
}

func (t *Terragrunt) Apply() error {
options := append(t.getDefaultOptions("apply"), t.planArtifactPath)
func (t *Terragrunt) Apply(usePlanArtifact bool) error {
options := append(t.getDefaultOptions("apply"), "-auto-approve")
if usePlanArtifact {
options = append(options, t.planArtifactPath)
}

cmd := exec.Command(t.execPath, options...)
verbose(cmd)
cmd.Dir = t.workingDir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,8 @@ spec:
type: string
remediationStrategy:
properties:
applyWithoutPlanArtifact:
type: boolean
autoApply:
type: boolean
onError:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1990,6 +1990,8 @@ spec:
type: object
remediationStrategy:
properties:
applyWithoutPlanArtifact:
type: boolean
autoApply:
type: boolean
onError:
Expand Down
4 changes: 4 additions & 0 deletions manifests/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,8 @@ spec:
type: string
remediationStrategy:
properties:
applyWithoutPlanArtifact:
type: boolean
autoApply:
type: boolean
onError:
Expand Down Expand Up @@ -4048,6 +4050,8 @@ spec:
type: object
remediationStrategy:
properties:
applyWithoutPlanArtifact:
type: boolean
autoApply:
type: boolean
onError:
Expand Down

0 comments on commit ae880ba

Please sign in to comment.