From 6faee200acf72f5f452511eca7844f23f609acee Mon Sep 17 00:00:00 2001 From: Ashok Siyani Date: Tue, 8 Oct 2024 11:58:14 +0100 Subject: [PATCH 1/2] on apply runs run apply command even if no diff detected --- api/v1beta1/run.go | 1 - prplanner/msg_test.go | 2 +- runner/runner.go | 12 +----------- webserver/template_test.go | 16 ++++++++-------- webserver/templates/status.html | 2 +- 5 files changed, 11 insertions(+), 22 deletions(-) diff --git a/api/v1beta1/run.go b/api/v1beta1/run.go index 3525cc0b..b0db158f 100644 --- a/api/v1beta1/run.go +++ b/api/v1beta1/run.go @@ -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"` diff --git a/prplanner/msg_test.go b/prplanner/msg_test.go index 8d687223..77b09304 100644 --- a/prplanner/msg_test.go +++ b/prplanner/msg_test.go @@ -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" + diff --git a/runner/runner.go b/runner/runner.go index 1cc6ad56..9cd71a76 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -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 { @@ -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 @@ -478,7 +468,7 @@ func (r *Runner) updateRedis(ctx context.Context, run *tfaplv1beta1.Run) error { return err } - if run.Applied { + if run.DiffDetected { // set default last applied run if err := r.Redis.SetDefaultApply(ctx, run); err != nil { return err diff --git a/webserver/template_test.go b/webserver/template_test.go index 845233cd..4cc5c3da 100644 --- a/webserver/template_test.go +++ b/webserver/template_test.go @@ -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", diff --git a/webserver/templates/status.html b/webserver/templates/status.html index b4ec89b6..4ef16096 100644 --- a/webserver/templates/status.html +++ b/webserver/templates/status.html @@ -222,7 +222,7 @@

{{.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 From c4c998669b16751e7d45427e97761ab743e2b142 Mon Sep 17 00:00:00 2001 From: Ashok Siyani Date: Tue, 8 Oct 2024 12:23:52 +0100 Subject: [PATCH 2/2] fixed logic of storing applied diff output --- runner/runner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runner/runner.go b/runner/runner.go index 9cd71a76..359a5706 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -468,7 +468,7 @@ func (r *Runner) updateRedis(ctx context.Context, run *tfaplv1beta1.Run) error { return err } - if run.DiffDetected { + if run.DiffDetected && !run.PlanOnly { // set default last applied run if err := r.Redis.SetDefaultApply(ctx, run); err != nil { return err