From 9708fda28ccd0466cb0a8fd409854ab4d92f7dca Mon Sep 17 00:00:00 2001 From: Chanwit Kaewkasi Date: Fri, 2 Jun 2023 19:51:36 +0700 Subject: [PATCH] fix logging in tf-runner Signed-off-by: Chanwit Kaewkasi --- runner/server.go | 22 ++++++++++++++-------- runner/server_plan.go | 32 +++++++++++++++++++++++++++++++- runner/server_save_tfplan.go | 13 +++++++------ runner/server_show_plan.go | 4 ++-- 4 files changed, 54 insertions(+), 17 deletions(-) diff --git a/runner/server.go b/runner/server.go index 5c1f7531..7e66e921 100644 --- a/runner/server.go +++ b/runner/server.go @@ -171,6 +171,18 @@ func (r *TerraformRunnerServer) ProcessCliConfig(ctx context.Context, req *Proce return &ProcessCliConfigReply{FilePath: tfrcFilepath}, nil } +// initLogger sets up the logger for the terraform runner +func (r *TerraformRunnerServer) initLogger(log logr.Logger) { + disableTestLogging := os.Getenv("DISABLE_TF_LOGS") == "1" + if !disableTestLogging { + r.tf.SetStdout(os.Stdout) + r.tf.SetStderr(os.Stderr) + if os.Getenv("ENABLE_SENSITIVE_TF_LOGS") == "1" { + r.tf.SetLogger(&LocalPrintfer{logger: log}) + } + } +} + func (r *TerraformRunnerServer) NewTerraform(ctx context.Context, req *NewTerraformRequest) (*NewTerraformReply, error) { r.InstanceID = req.GetInstanceID() log := ctrl.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName) @@ -192,14 +204,8 @@ func (r *TerraformRunnerServer) NewTerraform(ctx context.Context, req *NewTerraf // cache the Terraform resource when initializing r.terraform = &terraform - disableTestLogging := os.Getenv("DISABLE_TF_LOGS") == "1" - if !disableTestLogging { - r.tf.SetStdout(os.Stdout) - r.tf.SetStderr(os.Stderr) - if os.Getenv("ENABLE_SENSITIVE_TF_LOGS") == "1" { - r.tf.SetLogger(&LocalPrintfer{logger: log}) - } - } + // init default logger + r.initLogger(log) return &NewTerraformReply{Id: r.InstanceID}, nil } diff --git a/runner/server_plan.go b/runner/server_plan.go index 46e0e701..0012ecaa 100644 --- a/runner/server_plan.go +++ b/runner/server_plan.go @@ -4,12 +4,40 @@ import ( "context" "errors" "fmt" + "io" + "github.com/hashicorp/terraform-exec/tfexec" + tfjson "github.com/hashicorp/terraform-json" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "sigs.k8s.io/controller-runtime" + ctrl "sigs.k8s.io/controller-runtime" ) +func (r *TerraformRunnerServer) tfShowPlanFile(ctx context.Context, planPath string, opts ...tfexec.ShowOption) (*tfjson.Plan, error) { + log := ctrl.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName) + + // This is the only place where we disable the logger + r.tf.SetStdout(io.Discard) + r.tf.SetStderr(io.Discard) + + defer r.initLogger(log) + + return r.tf.ShowPlanFile(ctx, planPath, opts...) +} + +func (r *TerraformRunnerServer) tfShowPlanFileRaw(ctx context.Context, planPath string, opts ...tfexec.ShowOption) (string, error) { + log := ctrl.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName) + + // This is the only place where we disable the logger + r.tf.SetStdout(io.Discard) + r.tf.SetStderr(io.Discard) + + defer r.initLogger(log) + + return r.tf.ShowPlanFileRaw(ctx, planPath, opts...) +} + func (r *TerraformRunnerServer) Plan(ctx context.Context, req *PlanRequest) (*PlanReply, error) { log := controllerruntime.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName) log.Info("creating a plan") @@ -68,7 +96,8 @@ func (r *TerraformRunnerServer) Plan(ctx context.Context, req *PlanRequest) (*Pl planCreated := false if req.Out != "" { planCreated = true - plan, err := r.tf.ShowPlanFile(ctx, req.Out) + + plan, err := r.tfShowPlanFile(ctx, req.Out) if err != nil { return nil, err } @@ -81,6 +110,7 @@ func (r *TerraformRunnerServer) Plan(ctx context.Context, req *PlanRequest) (*Pl plan.OutputChanges == nil { planCreated = false } + } return &PlanReply{Message: "ok", Drifted: drifted, PlanCreated: planCreated}, nil diff --git a/runner/server_save_tfplan.go b/runner/server_save_tfplan.go index d73b285a..9d699e41 100644 --- a/runner/server_save_tfplan.go +++ b/runner/server_save_tfplan.go @@ -4,6 +4,9 @@ import ( "context" "encoding/json" "fmt" + "path/filepath" + "strings" + "github.com/go-logr/logr" "github.com/weaveworks/tf-controller/api/v1alpha1" "github.com/weaveworks/tf-controller/utils" @@ -12,13 +15,11 @@ import ( "k8s.io/apimachinery/pkg/api/errors" v12 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" - "path/filepath" - "sigs.k8s.io/controller-runtime" - "strings" + ctrl "sigs.k8s.io/controller-runtime" ) func (r *TerraformRunnerServer) SaveTFPlan(ctx context.Context, req *SaveTFPlanRequest) (*SaveTFPlanReply, error) { - log := controllerruntime.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName) + log := ctrl.LoggerFrom(ctx, "instance-id", r.InstanceID).WithName(loggerName) log.Info("save the plan") if req.TfInstance != r.InstanceID { err := fmt.Errorf("no TF instance found") @@ -46,7 +47,7 @@ func (r *TerraformRunnerServer) SaveTFPlan(ctx context.Context, req *SaveTFPlanR } if r.terraform.Spec.StoreReadablePlan == "json" { - planObj, err := r.tf.ShowPlanFile(ctx, TFPlanName) + planObj, err := r.tfShowPlanFile(ctx, TFPlanName) if err != nil { log.Error(err, "unable to get the plan output for json") return nil, err @@ -62,7 +63,7 @@ func (r *TerraformRunnerServer) SaveTFPlan(ctx context.Context, req *SaveTFPlanR } } else if r.terraform.Spec.StoreReadablePlan == "human" { - rawOutput, err := r.tf.ShowPlanFileRaw(ctx, TFPlanName) + rawOutput, err := r.tfShowPlanFileRaw(ctx, TFPlanName) if err != nil { log.Error(err, "unable to get the plan output for human") return nil, err diff --git a/runner/server_show_plan.go b/runner/server_show_plan.go index b7d64fc8..532e3d95 100644 --- a/runner/server_show_plan.go +++ b/runner/server_show_plan.go @@ -16,7 +16,7 @@ func (r *TerraformRunnerServer) ShowPlanFileRaw(ctx context.Context, req *ShowPl return nil, err } - rawOutput, err := r.tf.ShowPlanFileRaw(ctx, req.Filename) + rawOutput, err := r.tfShowPlanFileRaw(ctx, req.Filename) if err != nil { log.Error(err, "unable to get the raw plan output") return nil, err @@ -34,7 +34,7 @@ func (r *TerraformRunnerServer) ShowPlanFile(ctx context.Context, req *ShowPlanF return nil, err } - plan, err := r.tf.ShowPlanFile(ctx, req.Filename) + plan, err := r.tfShowPlanFile(ctx, req.Filename) if err != nil { log.Error(err, "unable to get the json plan output") return nil, err