Skip to content

Commit 7932eac

Browse files
committed
fix logging in tf-runner
Signed-off-by: Chanwit Kaewkasi <[email protected]>
1 parent 55f3aee commit 7932eac

File tree

6 files changed

+58
-20
lines changed

6 files changed

+58
-20
lines changed

config/tilt/test/tf-dev-subject.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
apiVersion: source.toolkit.fluxcd.io/v1
2+
apiVersion: source.toolkit.fluxcd.io/v1beta2
33
kind: GitRepository
44
metadata:
55
name: helloworld
@@ -18,7 +18,7 @@ metadata:
1818
stringData:
1919
test-data: "test"
2020
---
21-
apiVersion: infra.contrib.fluxcd.io/v1alpha2
21+
apiVersion: infra.contrib.fluxcd.io/v1alpha1
2222
kind: Terraform
2323
metadata:
2424
name: helloworld-tf
@@ -47,3 +47,4 @@ spec:
4747
hostAliases:
4848
- hostnames:
4949
- host.docker.internal
50+
ip: 127.0.0.1

runner/server.go

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

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

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

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

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

tools/reboot.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,4 @@ if [ "${kind_network}" != "bridge" ]; then
9292
fi
9393
fi
9494

95-
flux install
95+
./tools/flux install

0 commit comments

Comments
 (0)