Skip to content

Commit

Permalink
store plan diff in status instead of plan-output to reduce size
Browse files Browse the repository at this point in the history
  • Loading branch information
asiyani committed Oct 20, 2023
1 parent fb8ab90 commit 3ac8840
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
27 changes: 20 additions & 7 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ func (r *Runner) Start(ctx context.Context, done chan bool) {

success := r.process(req, cancelChan)

r.Log.Info("run finished", "module", req.NamespacedName, "success", success)

if success {
r.Log.Info("run completed successfully", "module", req.NamespacedName)
} else {
r.Log.Error("run completed with error", "module", req.NamespacedName)
}
r.Metrics.UpdateModuleSuccess(req.Name, req.Namespace, success)
r.Metrics.UpdateModuleRunDuration(req.Name, req.Namespace, time.Since(start).Seconds(), success)
}(req)
Expand Down Expand Up @@ -254,15 +257,15 @@ func (r *Runner) runTF(
return false
}

initOut, err := te.init(ctx, backendConf)
_, err := te.init(ctx, backendConf)
if err != nil {
msg := fmt.Sprintf("unable to init module: err:%s", err)
log.Error(msg)
r.setFailedStatus(req, module, tfaplv1beta1.ReasonInitialiseFailed, msg, r.Clock.Now())
return false
}

log.Info("initialised", "output", initOut)
log.Info("Initialised successfully")
r.Recorder.Event(module, corev1.EventTypeNormal, tfaplv1beta1.ReasonInitialised, "Initialised successfully")

// Start Planing
Expand All @@ -280,8 +283,8 @@ func (r *Runner) runTF(
}

diffDetected, planOut, err := te.plan(ctx)
module.Status.RunOutput = planOut
if err != nil {
module.Status.RunOutput = planOut
msg := fmt.Sprintf("unable to plan module: err:%s", err)
log.Error(msg)
r.setFailedStatus(req, module, tfaplv1beta1.ReasonPlanFailed, msg, r.Clock.Now())
Expand All @@ -296,6 +299,16 @@ func (r *Runner) runTF(

log.Info("planed", "status", planStatus)

// get saved plan to update status
savedPlan, err := te.showPlanFileRaw(ctx)
if err != nil {
msg := fmt.Sprintf("unable to get saved plan: err:%s", err)
log.Error(msg)
r.setFailedStatus(req, module, tfaplv1beta1.ReasonPlanFailed, msg, r.Clock.Now())
return false
}
module.Status.RunOutput = savedPlan

// return if no drift detected
if !diffDetected {
if err = r.SetRunFinishedStatus(req.NamespacedName, module, tfaplv1beta1.ReasonPlanedNoDriftDetected, planStatus, r.Clock.Now()); err != nil {
Expand Down Expand Up @@ -329,15 +342,15 @@ func (r *Runner) runTF(
}

applyOut, err := te.apply(ctx)
module.Status.RunOutput = planOut + applyOut
if err != nil {
module.Status.RunOutput = savedPlan + applyOut
msg := fmt.Sprintf("unable to apply module: err:%s", err)
log.Error(msg)
r.setFailedStatus(req, module, tfaplv1beta1.ReasonApplyFailed, msg, r.Clock.Now())
return false
}

module.Status.LastApplyInfo = tfaplv1beta1.OutputStats{Timestamp: &metav1.Time{Time: r.Clock.Now()}, CommitHash: commitHash, Output: planOut + applyOut}
module.Status.LastApplyInfo = tfaplv1beta1.OutputStats{Timestamp: &metav1.Time{Time: r.Clock.Now()}, CommitHash: commitHash, Output: savedPlan + applyOut}

// extract last line of output
// Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Expand Down
8 changes: 8 additions & 0 deletions runner/tfexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const strongBoxEnv = "TF_APPLIER_STRONGBOX_KEYRING"
type TFExecuter interface {
init(ctx context.Context, backendConf map[string]string) (string, error)
plan(ctx context.Context) (bool, string, error)
showPlanFileRaw(ctx context.Context) (string, error)
apply(ctx context.Context) (string, error)
cleanUp()
}
Expand Down Expand Up @@ -176,6 +177,13 @@ func (te *tfRunner) plan(ctx context.Context) (bool, string, error) {
return changes, out.String(), nil
}

// showPlanFileRaw reads a given plan file and outputs the plan in a
// human-friendly, opaque format.
func (te *tfRunner) showPlanFileRaw(ctx context.Context) (string, error) {
planOut := filepath.Join(te.workingDir, te.planFileName)
return te.tf.ShowPlanFileRaw(ctx, planOut)
}

func (te *tfRunner) apply(ctx context.Context) (string, error) {
var out bytes.Buffer
te.tf.SetStdout(&out)
Expand Down
15 changes: 15 additions & 0 deletions runner/tfexec_mock.go

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

0 comments on commit 3ac8840

Please sign in to comment.