Skip to content

Commit

Permalink
Fix Terraform drift render logic that causes runtime error (#4444) (#…
Browse files Browse the repository at this point in the history
…4446)

Signed-off-by: khanhtc1202 <khanhtc1202@gmail.com>
  • Loading branch information
khanhtc1202 committed Jun 26, 2023
1 parent 9e1970f commit 0c2bb0d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
23 changes: 15 additions & 8 deletions pkg/app/piped/driftdetector/terraform/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,19 +272,23 @@ func (d *detector) checkApplication(ctx context.Context, app *model.Application,
return err
}

state := makeSyncState(result, headCommit.Hash)
state, err := makeSyncState(result, headCommit.Hash)
if err != nil {
fmt.Fprintf(buf, "failed while calculate terraform sync state (%v)\n", err)
return err
}

return d.reporter.ReportApplicationSyncState(ctx, app.Id, state)
return d.reporter.ReportApplicationSyncState(ctx, app.Id, *state)
}

func makeSyncState(r provider.PlanResult, commit string) model.ApplicationSyncState {
func makeSyncState(r provider.PlanResult, commit string) (*model.ApplicationSyncState, error) {
if r.NoChanges() {
return model.ApplicationSyncState{
return &model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_SYNCED,
ShortReason: "",
Reason: "",
Timestamp: time.Now().Unix(),
}
}, nil
}

total := r.Imports + r.Adds + r.Destroys + r.Changes
Expand All @@ -297,15 +301,18 @@ func makeSyncState(r provider.PlanResult, commit string) model.ApplicationSyncSt
b.WriteString(fmt.Sprintf("Diff between the defined state in Git at commit %s and actual live state:\n\n", commit))
b.WriteString("--- Actual (LiveState)\n+++ Expected (Git)\n\n")

details := r.Render()
details, err := r.Render()
if err != nil {
return nil, err
}
b.WriteString(details)

return model.ApplicationSyncState{
return &model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_OUT_OF_SYNC,
ShortReason: shortReason,
Reason: b.String(),
Timestamp: time.Now().Unix(),
}
}, nil
}

func (d *detector) cloneGitRepository(ctx context.Context, repoID string) (git.Repo, error) {
Expand Down
17 changes: 13 additions & 4 deletions pkg/app/piped/platformprovider/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,21 @@ func (r PlanResult) NoChanges() bool {
return r.Adds == 0 && r.Changes == 0 && r.Destroys == 0 && r.Imports == 0
}

func (r PlanResult) Render() string {
func (r PlanResult) Render() (string, error) {
terraformDiffStart := "Terraform will perform the following actions:"
terraformDiffEnd := fmt.Sprintf("Plan: %d to import, %d to add, %d to change, %d to destroy.", r.Imports, r.Adds, r.Changes, r.Destroys)

startIndex := strings.Index(r.PlanOutput, terraformDiffStart) + len(terraformDiffStart)

terraformDiffEnd := fmt.Sprintf("Plan: %d to import, %d to add, %d to change, %d to destroy.", r.Imports, r.Adds, r.Changes, r.Destroys)
endIndex := strings.Index(r.PlanOutput, terraformDiffEnd) + len(terraformDiffEnd)
if endIndex < startIndex {
terraformDiffEnd = fmt.Sprintf("Plan: %d to add, %d to change, %d to destroy.", r.Adds, r.Changes, r.Destroys)
endIndex = strings.Index(r.PlanOutput, terraformDiffEnd) + len(terraformDiffEnd)
}

if endIndex < startIndex {
return "", fmt.Errorf("unable to parse Terraform plan result")
}

out := r.PlanOutput[startIndex:endIndex]

rendered := ""
Expand Down Expand Up @@ -228,7 +237,7 @@ func (r PlanResult) Render() string {
rendered += "\n"
}

return rendered
return rendered, nil
}

// Return rune at the top of the stack, or r in case of error.
Expand Down

0 comments on commit 0c2bb0d

Please sign in to comment.