Skip to content

Commit

Permalink
Merge pull request #1304 from flux-iac/print-plan
Browse files Browse the repository at this point in the history
Print plan before apply
  • Loading branch information
akselleirv committed Jun 18, 2024
2 parents d0a4fbb + bde16cc commit bcf75fa
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/use-tf-controller/with-tf-runner-logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ the `DISABLE_TF_LOGS` variable must also be set to "1".

For more information on configuring the Terraform Runner and its environment variables,
please consult the documentation on [customizing runners](provision-resources-with-customized-runner-pods.md) within the Weave TF-controller.

## Logging human-readable plan

The plan can be logged in a human-readable format just before the applying it in the `tf-runner`.
To enable this, set the environment variable `LOG_HUMAN_READABLE_PLAN` to "1" on the runner.
22 changes: 22 additions & 0 deletions runner/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ func (r *TerraformRunnerServer) Apply(ctx context.Context, req *ApplyRequest) (*
applyOpt = append(applyOpt, tfexec.Parallelism(int(req.Parallelism)))
}

if err := printHumanReadablePlanIfEnabled(ctx, req.DirOrPlan, r.tfShowPlanFileRaw); err != nil {
log.Error(err, "unable to print plan")
return nil, err
}

if err := r.tf.Apply(ctx, applyOpt...); err != nil {
st := status.New(codes.Internal, err.Error())
var stateErr *tfexec.ErrStateLocked
Expand All @@ -401,6 +406,23 @@ func (r *TerraformRunnerServer) Apply(ctx context.Context, req *ApplyRequest) (*
return &ApplyReply{Message: "ok"}, nil
}

func printHumanReadablePlanIfEnabled(ctx context.Context, planName string, tfShowPlanFileRaw func(ctx context.Context, planPath string, opts ...tfexec.ShowOption) (string, error)) error {
if os.Getenv("LOG_HUMAN_READABLE_PLAN") == "1" {
if planName == "" {
planName = TFPlanName
}

rawOutput, err := tfShowPlanFileRaw(ctx, planName)
if err != nil {
return err
}

fmt.Println(rawOutput)
}

return nil
}

func getInventoryFromTerraformModule(m *tfjson.StateModule) []*Inventory {
var result []*Inventory
for _, r := range m.Resources {
Expand Down
52 changes: 52 additions & 0 deletions runner/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package runner

import (
"context"
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-exec/tfexec"
. "github.com/onsi/gomega"
)

func Test_printHumanReadablePlanIfEnabled(t *testing.T) {
g := NewGomegaWithT(t)
ctx := context.Background()

defer func() {
g.Expect(os.Unsetenv("LOG_HUMAN_READABLE_PLAN")).Should(Succeed())
}()

var tfShowPlanFileRawCalled int
expectedPlan := TFPlanName
tfShowPlanFileRaw := func(ctx context.Context, planPath string, opts ...tfexec.ShowOption) (string, error) {
g.Expect(planPath).To(Equal(expectedPlan))
tfShowPlanFileRawCalled++
return "", nil
}

// When plan is enabled, then it should be called once
g.Expect(os.Setenv("LOG_HUMAN_READABLE_PLAN", "1")).Should(Succeed())
g.Expect(printHumanReadablePlanIfEnabled(ctx, "", tfShowPlanFileRaw)).Should(Succeed())
g.Expect(tfShowPlanFileRawCalled).To(Equal(1))

// When the planName is non-empty, then it should use the planName
expectedPlan = "foo"
g.Expect(printHumanReadablePlanIfEnabled(ctx, expectedPlan, tfShowPlanFileRaw)).Should(Succeed())
g.Expect(tfShowPlanFileRawCalled).To(Equal(2))

// When it is disabled, then it should not be called
expectedPlan = TFPlanName
g.Expect(os.Setenv("LOG_HUMAN_READABLE_PLAN", "0")).Should(Succeed())
g.Expect(printHumanReadablePlanIfEnabled(ctx, "", tfShowPlanFileRaw)).Should(Succeed())
g.Expect(tfShowPlanFileRawCalled).To(Equal(2))

// When tfShowPlanFileRaw fails, then it should return an error
g.Expect(os.Setenv("LOG_HUMAN_READABLE_PLAN", "1")).Should(Succeed())
tfShowPlanFileRaw = func(ctx context.Context, planPath string, opts ...tfexec.ShowOption) (string, error) {
return "", fmt.Errorf("error")
}

g.Expect(printHumanReadablePlanIfEnabled(ctx, "", tfShowPlanFileRaw)).ShouldNot(Succeed())
}

0 comments on commit bcf75fa

Please sign in to comment.