Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix logging in tf-runner #645

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions runner/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,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)
Expand All @@ -191,14 +203,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
}
Expand Down
32 changes: 31 additions & 1 deletion runner/server_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions runner/server_save_tfplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime"
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")
Expand Down Expand Up @@ -47,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
Expand All @@ -63,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
Expand Down
4 changes: 2 additions & 2 deletions runner/server_show_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down