Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runner): implement apply without plan artifact option #265

Merged
merged 2 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions api/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,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 @@ -106,6 +107,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 @@ -49,7 +49,7 @@ type TerraformExec interface {
Install() error
Init(string) error
Plan() error
Apply() error
Apply(bool) error
Show(string) ([]byte, error)
}

Expand Down Expand Up @@ -299,7 +299,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 @@ -1992,6 +1992,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 @@ -1978,6 +1978,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 @@ -1239,6 +1239,8 @@ spec:
type: string
remediationStrategy:
properties:
applyWithoutPlanArtifact:
type: boolean
autoApply:
type: boolean
onError:
Expand Down Expand Up @@ -2685,6 +2687,8 @@ spec:
type: object
remediationStrategy:
properties:
applyWithoutPlanArtifact:
type: boolean
autoApply:
type: boolean
onError:
Expand Down
Loading