@@ -66,18 +66,24 @@ func (r *Runner) unlock() {
66
66
if err != nil {
67
67
log .Fatalf ("could not remove lease lock for terraform layer %s: %s" , r .layer .Name , err )
68
68
}
69
+ log .Infof ("successfully removed lease lock for terraform layer %s" , r .layer .Name )
69
70
}
70
71
71
- func (r * Runner ) Exec () {
72
+ func (r * Runner ) Exec () error {
72
73
defer r .unlock ()
74
+
73
75
var sum string
74
- err := r . init ()
76
+ var commit string
75
77
ann := map [string ]string {}
78
+
79
+ err := r .init ()
76
80
if err != nil {
77
81
log .Errorf ("error initializing runner: %s" , err )
78
82
}
79
- ref , _ := r .gitRepository .Head ()
80
- commit := ref .Hash ().String ()
83
+ if r .gitRepository != nil {
84
+ ref , _ := r .gitRepository .Head ()
85
+ commit = ref .Hash ().String ()
86
+ }
81
87
82
88
switch r .config .Runner .Action {
83
89
case "plan" :
@@ -95,8 +101,9 @@ func (r *Runner) Exec() {
95
101
ann [annotations .LastApplyCommit ] = commit
96
102
}
97
103
default :
98
- err = errors .New ("unrecognized runner action, If this is happening there might be a version mismatch between the controller and runner" )
104
+ err = errors .New ("unrecognized runner action, if this is happening there might be a version mismatch between the controller and runner" )
99
105
}
106
+
100
107
if err != nil {
101
108
log .Errorf ("error during runner execution: %s" , err )
102
109
n , ok := r .layer .Annotations [annotations .Failure ]
@@ -109,10 +116,14 @@ func (r *Runner) Exec() {
109
116
} else {
110
117
ann [annotations .Failure ] = "0"
111
118
}
112
- err = annotations .Add (context .TODO (), r .client , r .layer , ann )
113
- if err != nil {
119
+
120
+ annotErr := annotations .Add (context .TODO (), r .client , r .layer , ann )
121
+ if annotErr != nil {
114
122
log .Errorf ("could not update terraform layer annotations: %s" , err )
115
123
}
124
+ log .Infof ("successfully updated terraform layer annotations" )
125
+
126
+ return err
116
127
}
117
128
118
129
func (r * Runner ) getLayerAndRepository () error {
@@ -195,6 +206,8 @@ func (r *Runner) init() error {
195
206
log .Infof ("cloning repository %s %s branch" , r .repository .Spec .Repository .Url , r .layer .Spec .Branch )
196
207
r .gitRepository , err = clone (r .config .Runner .Repository , r .repository .Spec .Repository .Url , r .layer .Spec .Branch , r .layer .Spec .Path )
197
208
if err != nil {
209
+ r .gitRepository = nil // reset git repository for the caller
210
+ log .Errorf ("error cloning repository: %s" , err )
198
211
return err
199
212
}
200
213
log .Infof ("repository cloned successfully" )
@@ -211,14 +224,18 @@ func (r *Runner) init() error {
211
224
log .Infof ("Launching terraform init in %s" , workingDir )
212
225
err = r .exec .Init (workingDir )
213
226
if err != nil {
214
- log .Errorf ("" )
227
+ log .Errorf ("error executing terraform init: %s" , err )
215
228
return err
216
229
}
217
230
return nil
218
231
}
219
232
220
233
func (r * Runner ) plan () (string , error ) {
221
234
log .Infof ("starting terraform plan" )
235
+ if r .exec == nil {
236
+ err := errors .New ("terraform or terragrunt binary not installed" )
237
+ return "" , err
238
+ }
222
239
err := r .exec .Plan ()
223
240
if err != nil {
224
241
log .Errorf ("error executing terraform plan: %s" , err )
@@ -246,7 +263,7 @@ func (r *Runner) plan() (string, error) {
246
263
log .Errorf ("error parsing terraform json plan: %s" , err )
247
264
return "" , err
248
265
}
249
- diff , shortDiff := getDiff (plan )
266
+ _ , shortDiff := getDiff (plan )
250
267
planJsonKey := storage .GenerateKey (storage .LastPlannedArtifactJson , r .layer )
251
268
log .Infof ("setting plan json into storage at key %s" , planJsonKey )
252
269
err = r .storage .Set (planJsonKey , planJsonBytes , 3600 )
@@ -257,10 +274,6 @@ func (r *Runner) plan() (string, error) {
257
274
if err != nil {
258
275
log .Errorf ("could not put short plan in cache: %s" , err )
259
276
}
260
- if ! diff {
261
- log .Infof ("terraform plan diff empty, no subsequent apply should be launched" )
262
- return "" , nil
263
- }
264
277
planBin , err := os .ReadFile (PlanArtifact )
265
278
if err != nil {
266
279
log .Errorf ("could not read plan output: %s" , err )
@@ -280,6 +293,10 @@ func (r *Runner) plan() (string, error) {
280
293
281
294
func (r * Runner ) apply () (string , error ) {
282
295
log .Infof ("starting terraform apply" )
296
+ if r .exec == nil {
297
+ err := errors .New ("terraform or terragrunt binary not installed" )
298
+ return "" , err
299
+ }
283
300
planBinKey := storage .GenerateKey (storage .LastPlannedArtifactBin , r .layer )
284
301
log .Infof ("getting plan binary in cache at key %s" , planBinKey )
285
302
plan , err := r .storage .Get (planBinKey )
0 commit comments