Skip to content

Commit

Permalink
Merge pull request #278 from utilitywarehouse/as-apply-run
Browse files Browse the repository at this point in the history
on apply runs run apply command even if no diff detected
  • Loading branch information
asiyani authored Oct 8, 2024
2 parents 7dce762 + c4c9986 commit b4f7999
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 22 deletions.
1 change: 0 additions & 1 deletion api/v1beta1/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type Run struct {
CommitHash string `json:"commitHash,omitempty"`
CommitMsg string `json:"commitMsg,omitempty"`
DiffDetected bool `json:"diffDetected,omitempty"`
Applied bool `json:"applied,omitempty"`
InitOutput string `json:"initOutput,omitempty"`
Output string `json:"output,omitempty"`
Summary string `json:"summary,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion prplanner/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func Test_runOutputMsg(t *testing.T) {
"\n> To manually trigger plan again please post `@terraform-applier plan path/baz/one` as comment.",
}, {
"3",
args{cluster: "default", module: "baz/one", path: "path/baz/one", run: &v1beta1.Run{Status: v1beta1.StatusOk, Applied: true, CommitHash: "hash2", Summary: "Applied: x to add, x to change, x to destroy.", Output: "Terraform apply output...."}},
args{cluster: "default", module: "baz/one", path: "path/baz/one", run: &v1beta1.Run{Status: v1beta1.StatusOk, DiffDetected: true, CommitHash: "hash2", Summary: "Applied: x to add, x to change, x to destroy.", Output: "Terraform apply output...."}},
"Terraform run output for\n" +
"```\n" +
"Cluster: default\n" +
Expand Down
12 changes: 1 addition & 11 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,6 @@ func (r *Runner) runTF(
return false
}

// return if no drift detected
if !diffDetected {
if err = r.SetRunFinishedStatus(run, module, tfaplv1beta1.ReasonNoDriftDetected, planStatus, r.Clock.Now()); err != nil {
log.Error("unable to set no drift status", "err", err)
return false
}
return true
}

// return if plan only mode
if run.PlanOnly {
if err = r.SetRunFinishedStatus(run, module, tfaplv1beta1.ReasonDriftDetected, "PlanOnly/"+planStatus, r.Clock.Now()); err != nil {
Expand All @@ -374,7 +365,6 @@ func (r *Runner) runTF(
r.setFailedStatus(run, module, tfaplv1beta1.ReasonApplyFailed, "unable to apply module")
return false
}
run.Applied = true
module.Status.LastAppliedAt = &metav1.Time{Time: r.Clock.Now()}
module.Status.LastAppliedCommitHash = commitHash

Expand Down Expand Up @@ -478,7 +468,7 @@ func (r *Runner) updateRedis(ctx context.Context, run *tfaplv1beta1.Run) error {
return err
}

if run.Applied {
if run.DiffDetected && !run.PlanOnly {
// set default last applied run
if err := r.Redis.SetDefaultApply(ctx, run); err != nil {
return err
Expand Down
16 changes: 8 additions & 8 deletions webserver/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,14 @@ Terraform will perform the following actions:
Plan: 7 to add, 0 to change, 0 to destroy.`,
},
{
Module: types.NamespacedName{Name: "groups", Namespace: "bar"},
Request: &tfaplv1beta1.Request{Type: tfaplv1beta1.PollingRun, RequestedAt: getMetaTime(3, 1, 2)},
StartedAt: getMetaTime(10, 30, 1),
Status: tfaplv1beta1.StatusOk,
Duration: 60 * time.Second,
CommitHash: "abcccf2a0f758ba0d8e88a834a2acdba5885577c",
CommitMsg: `initial commit (john)`,
Applied: true,
Module: types.NamespacedName{Name: "groups", Namespace: "bar"},
Request: &tfaplv1beta1.Request{Type: tfaplv1beta1.PollingRun, RequestedAt: getMetaTime(3, 1, 2)},
StartedAt: getMetaTime(10, 30, 1),
Status: tfaplv1beta1.StatusOk,
Duration: 60 * time.Second,
CommitHash: "abcccf2a0f758ba0d8e88a834a2acdba5885577c",
CommitMsg: `initial commit (john)`,
DiffDetected: true,
InitOutput: `
{
"terraform_version": "1.8.2",
Expand Down
2 changes: 1 addition & 1 deletion webserver/templates/status.html
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ <h3 class="fw-bold" style="color: #550091;">{{.Module.Name}}
href="#{{sanitizedUniqueName .Module}}-{{$i}}" data-bs-toggle="tab" role="tab">
{{if $run.Request.PR}}
PR #{{$run.Request.PR.Number}} Run
{{else if $run.Applied}}
{{else if and $run.DiffDetected (not $run.PlanOnly)}}
Last Applied Run
{{else}}
Last Planned Run
Expand Down

0 comments on commit b4f7999

Please sign in to comment.