@@ -4,12 +4,40 @@ import (
4
4
"context"
5
5
"errors"
6
6
"fmt"
7
+ "io"
8
+
7
9
"github.com/hashicorp/terraform-exec/tfexec"
10
+ tfjson "github.com/hashicorp/terraform-json"
8
11
"google.golang.org/grpc/codes"
9
12
"google.golang.org/grpc/status"
10
13
"sigs.k8s.io/controller-runtime"
14
+ ctrl "sigs.k8s.io/controller-runtime"
11
15
)
12
16
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
+
13
41
func (r * TerraformRunnerServer ) Plan (ctx context.Context , req * PlanRequest ) (* PlanReply , error ) {
14
42
log := controllerruntime .LoggerFrom (ctx , "instance-id" , r .InstanceID ).WithName (loggerName )
15
43
log .Info ("creating a plan" )
@@ -68,7 +96,8 @@ func (r *TerraformRunnerServer) Plan(ctx context.Context, req *PlanRequest) (*Pl
68
96
planCreated := false
69
97
if req .Out != "" {
70
98
planCreated = true
71
- plan , err := r .tf .ShowPlanFile (ctx , req .Out )
99
+
100
+ plan , err := r .tfShowPlanFile (ctx , req .Out )
72
101
if err != nil {
73
102
return nil , err
74
103
}
@@ -81,6 +110,7 @@ func (r *TerraformRunnerServer) Plan(ctx context.Context, req *PlanRequest) (*Pl
81
110
plan .OutputChanges == nil {
82
111
planCreated = false
83
112
}
113
+
84
114
}
85
115
86
116
return & PlanReply {Message : "ok" , Drifted : drifted , PlanCreated : planCreated }, nil
0 commit comments