Skip to content

Commit 9708fda

Browse files
committed
fix logging in tf-runner
Signed-off-by: Chanwit Kaewkasi <[email protected]>
1 parent d2701c2 commit 9708fda

File tree

4 files changed

+54
-17
lines changed

4 files changed

+54
-17
lines changed

runner/server.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,18 @@ func (r *TerraformRunnerServer) ProcessCliConfig(ctx context.Context, req *Proce
171171
return &ProcessCliConfigReply{FilePath: tfrcFilepath}, nil
172172
}
173173

174+
// initLogger sets up the logger for the terraform runner
175+
func (r *TerraformRunnerServer) initLogger(log logr.Logger) {
176+
disableTestLogging := os.Getenv("DISABLE_TF_LOGS") == "1"
177+
if !disableTestLogging {
178+
r.tf.SetStdout(os.Stdout)
179+
r.tf.SetStderr(os.Stderr)
180+
if os.Getenv("ENABLE_SENSITIVE_TF_LOGS") == "1" {
181+
r.tf.SetLogger(&LocalPrintfer{logger: log})
182+
}
183+
}
184+
}
185+
174186
func (r *TerraformRunnerServer) NewTerraform(ctx context.Context, req *NewTerraformRequest) (*NewTerraformReply, error) {
175187
r.InstanceID = req.GetInstanceID()
176188
log := ctrl.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName)
@@ -192,14 +204,8 @@ func (r *TerraformRunnerServer) NewTerraform(ctx context.Context, req *NewTerraf
192204
// cache the Terraform resource when initializing
193205
r.terraform = &terraform
194206

195-
disableTestLogging := os.Getenv("DISABLE_TF_LOGS") == "1"
196-
if !disableTestLogging {
197-
r.tf.SetStdout(os.Stdout)
198-
r.tf.SetStderr(os.Stderr)
199-
if os.Getenv("ENABLE_SENSITIVE_TF_LOGS") == "1" {
200-
r.tf.SetLogger(&LocalPrintfer{logger: log})
201-
}
202-
}
207+
// init default logger
208+
r.initLogger(log)
203209

204210
return &NewTerraformReply{Id: r.InstanceID}, nil
205211
}

runner/server_plan.go

+31-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,40 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"io"
8+
79
"github.com/hashicorp/terraform-exec/tfexec"
10+
tfjson "github.com/hashicorp/terraform-json"
811
"google.golang.org/grpc/codes"
912
"google.golang.org/grpc/status"
1013
"sigs.k8s.io/controller-runtime"
14+
ctrl "sigs.k8s.io/controller-runtime"
1115
)
1216

17+
func (r *TerraformRunnerServer) tfShowPlanFile(ctx context.Context, planPath string, opts ...tfexec.ShowOption) (*tfjson.Plan, error) {
18+
log := ctrl.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName)
19+
20+
// This is the only place where we disable the logger
21+
r.tf.SetStdout(io.Discard)
22+
r.tf.SetStderr(io.Discard)
23+
24+
defer r.initLogger(log)
25+
26+
return r.tf.ShowPlanFile(ctx, planPath, opts...)
27+
}
28+
29+
func (r *TerraformRunnerServer) tfShowPlanFileRaw(ctx context.Context, planPath string, opts ...tfexec.ShowOption) (string, error) {
30+
log := ctrl.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName)
31+
32+
// This is the only place where we disable the logger
33+
r.tf.SetStdout(io.Discard)
34+
r.tf.SetStderr(io.Discard)
35+
36+
defer r.initLogger(log)
37+
38+
return r.tf.ShowPlanFileRaw(ctx, planPath, opts...)
39+
}
40+
1341
func (r *TerraformRunnerServer) Plan(ctx context.Context, req *PlanRequest) (*PlanReply, error) {
1442
log := controllerruntime.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName)
1543
log.Info("creating a plan")
@@ -68,7 +96,8 @@ func (r *TerraformRunnerServer) Plan(ctx context.Context, req *PlanRequest) (*Pl
6896
planCreated := false
6997
if req.Out != "" {
7098
planCreated = true
71-
plan, err := r.tf.ShowPlanFile(ctx, req.Out)
99+
100+
plan, err := r.tfShowPlanFile(ctx, req.Out)
72101
if err != nil {
73102
return nil, err
74103
}
@@ -81,6 +110,7 @@ func (r *TerraformRunnerServer) Plan(ctx context.Context, req *PlanRequest) (*Pl
81110
plan.OutputChanges == nil {
82111
planCreated = false
83112
}
113+
84114
}
85115

86116
return &PlanReply{Message: "ok", Drifted: drifted, PlanCreated: planCreated}, nil

runner/server_save_tfplan.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"path/filepath"
8+
"strings"
9+
710
"github.com/go-logr/logr"
811
"github.com/weaveworks/tf-controller/api/v1alpha1"
912
"github.com/weaveworks/tf-controller/utils"
@@ -12,13 +15,11 @@ import (
1215
"k8s.io/apimachinery/pkg/api/errors"
1316
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
1417
"k8s.io/apimachinery/pkg/types"
15-
"path/filepath"
16-
"sigs.k8s.io/controller-runtime"
17-
"strings"
18+
ctrl "sigs.k8s.io/controller-runtime"
1819
)
1920

2021
func (r *TerraformRunnerServer) SaveTFPlan(ctx context.Context, req *SaveTFPlanRequest) (*SaveTFPlanReply, error) {
21-
log := controllerruntime.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName)
22+
log := ctrl.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName)
2223
log.Info("save the plan")
2324
if req.TfInstance != r.InstanceID {
2425
err := fmt.Errorf("no TF instance found")
@@ -46,7 +47,7 @@ func (r *TerraformRunnerServer) SaveTFPlan(ctx context.Context, req *SaveTFPlanR
4647
}
4748

4849
if r.terraform.Spec.StoreReadablePlan == "json" {
49-
planObj, err := r.tf.ShowPlanFile(ctx, TFPlanName)
50+
planObj, err := r.tfShowPlanFile(ctx, TFPlanName)
5051
if err != nil {
5152
log.Error(err, "unable to get the plan output for json")
5253
return nil, err
@@ -62,7 +63,7 @@ func (r *TerraformRunnerServer) SaveTFPlan(ctx context.Context, req *SaveTFPlanR
6263
}
6364

6465
} else if r.terraform.Spec.StoreReadablePlan == "human" {
65-
rawOutput, err := r.tf.ShowPlanFileRaw(ctx, TFPlanName)
66+
rawOutput, err := r.tfShowPlanFileRaw(ctx, TFPlanName)
6667
if err != nil {
6768
log.Error(err, "unable to get the plan output for human")
6869
return nil, err

runner/server_show_plan.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func (r *TerraformRunnerServer) ShowPlanFileRaw(ctx context.Context, req *ShowPl
1616
return nil, err
1717
}
1818

19-
rawOutput, err := r.tf.ShowPlanFileRaw(ctx, req.Filename)
19+
rawOutput, err := r.tfShowPlanFileRaw(ctx, req.Filename)
2020
if err != nil {
2121
log.Error(err, "unable to get the raw plan output")
2222
return nil, err
@@ -34,7 +34,7 @@ func (r *TerraformRunnerServer) ShowPlanFile(ctx context.Context, req *ShowPlanF
3434
return nil, err
3535
}
3636

37-
plan, err := r.tf.ShowPlanFile(ctx, req.Filename)
37+
plan, err := r.tfShowPlanFile(ctx, req.Filename)
3838
if err != nil {
3939
log.Error(err, "unable to get the json plan output")
4040
return nil, err

0 commit comments

Comments
 (0)