7
7
"path/filepath"
8
8
"time"
9
9
10
+ "github.com/jmespath-community/go-jmespath/pkg/binding"
10
11
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
11
12
"github.com/kyverno/chainsaw/pkg/cleanup/cleaner"
12
13
"github.com/kyverno/chainsaw/pkg/engine"
@@ -24,6 +25,7 @@ import (
24
25
opscript "github.com/kyverno/chainsaw/pkg/engine/operations/script"
25
26
opsleep "github.com/kyverno/chainsaw/pkg/engine/operations/sleep"
26
27
opupdate "github.com/kyverno/chainsaw/pkg/engine/operations/update"
28
+ "github.com/kyverno/chainsaw/pkg/engine/templating"
27
29
"github.com/kyverno/chainsaw/pkg/loaders/resource"
28
30
"github.com/kyverno/chainsaw/pkg/report"
29
31
"github.com/kyverno/chainsaw/pkg/runner/failer"
@@ -121,7 +123,7 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
121
123
failer .Fail (ctx )
122
124
}
123
125
for i , operation := range p .step .Cleanup {
124
- operations , err := p .finallyOperation (i , namespacer , operation )
126
+ operations , err := p .finallyOperation (i , namespacer , tc . Bindings (), operation )
125
127
if err != nil {
126
128
logger .Log (logging .Cleanup , logging .ErrorStatus , color .BoldRed , logging .ErrSection (err ))
127
129
failer .Fail (ctx )
@@ -139,7 +141,7 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
139
141
logger .Log (logging .Finally , logging .DoneStatus , color .BoldFgCyan )
140
142
}()
141
143
for i , operation := range p .step .Finally {
142
- operations , err := p .finallyOperation (i , namespacer , operation )
144
+ operations , err := p .finallyOperation (i , namespacer , tc . Bindings (), operation )
143
145
if err != nil {
144
146
logger .Log (logging .Finally , logging .ErrorStatus , color .BoldRed , logging .ErrSection (err ))
145
147
failer .Fail (ctx )
@@ -158,7 +160,7 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
158
160
logger .Log (logging .Catch , logging .DoneStatus , color .BoldFgCyan )
159
161
}()
160
162
for i , operation := range p .catch {
161
- operations , err := p .catchOperation (i , namespacer , operation )
163
+ operations , err := p .catchOperation (i , namespacer , tc . Bindings (), operation )
162
164
if err != nil {
163
165
logger .Log (logging .Catch , logging .ErrorStatus , color .BoldRed , logging .ErrSection (err ))
164
166
failer .Fail (ctx )
@@ -175,7 +177,7 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
175
177
logger .Log (logging .Try , logging .DoneStatus , color .BoldFgCyan )
176
178
}()
177
179
for i , operation := range p .step .Try {
178
- operations , err := p .tryOperation (i , namespacer , operation , cleaner )
180
+ operations , err := p .tryOperation (i , namespacer , tc . Bindings (), operation , cleaner )
179
181
if err != nil {
180
182
logger .Log (logging .Try , logging .ErrorStatus , color .BoldRed , logging .ErrSection (err ))
181
183
failer .FailNow (ctx )
@@ -188,7 +190,7 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
188
190
}
189
191
}
190
192
191
- func (p * stepProcessor ) tryOperation (id int , namespacer namespacer.Namespacer , handler v1alpha1.Operation , cleaner cleaner.CleanerCollector ) ([]operation , error ) {
193
+ func (p * stepProcessor ) tryOperation (id int , namespacer namespacer.Namespacer , bindings binding. Bindings , handler v1alpha1.Operation , cleaner cleaner.CleanerCollector ) ([]operation , error ) {
192
194
var ops []operation
193
195
register := func (o ... operation ) {
194
196
continueOnError := handler .ContinueOnError != nil && * handler .ContinueOnError
@@ -198,35 +200,35 @@ func (p *stepProcessor) tryOperation(id int, namespacer namespacer.Namespacer, h
198
200
}
199
201
}
200
202
if handler .Apply != nil {
201
- loaded , err := p .applyOperation (id + 1 , namespacer , cleaner , * handler .Apply )
203
+ loaded , err := p .applyOperation (id + 1 , namespacer , cleaner , bindings , * handler .Apply )
202
204
if err != nil {
203
205
return nil , err
204
206
}
205
207
register (loaded ... )
206
208
} else if handler .Assert != nil {
207
- loaded , err := p .assertOperation (id + 1 , namespacer , * handler .Assert )
209
+ loaded , err := p .assertOperation (id + 1 , namespacer , bindings , * handler .Assert )
208
210
if err != nil {
209
211
return nil , err
210
212
}
211
213
register (loaded ... )
212
214
} else if handler .Command != nil {
213
215
register (p .commandOperation (id + 1 , namespacer , * handler .Command ))
214
216
} else if handler .Create != nil {
215
- loaded , err := p .createOperation (id + 1 , namespacer , cleaner , * handler .Create )
217
+ loaded , err := p .createOperation (id + 1 , namespacer , cleaner , bindings , * handler .Create )
216
218
if err != nil {
217
219
return nil , err
218
220
}
219
221
register (loaded ... )
220
222
} else if handler .Delete != nil {
221
- loaded , err := p .deleteOperation (id + 1 , namespacer , * handler .Delete )
223
+ loaded , err := p .deleteOperation (id + 1 , namespacer , bindings , * handler .Delete )
222
224
if err != nil {
223
225
return nil , err
224
226
}
225
227
register (loaded ... )
226
228
} else if handler .Describe != nil {
227
229
register (p .describeOperation (id + 1 , namespacer , * handler .Describe ))
228
230
} else if handler .Error != nil {
229
- loaded , err := p .errorOperation (id + 1 , namespacer , * handler .Error )
231
+ loaded , err := p .errorOperation (id + 1 , namespacer , bindings , * handler .Error )
230
232
if err != nil {
231
233
return nil , err
232
234
}
@@ -248,7 +250,7 @@ func (p *stepProcessor) tryOperation(id int, namespacer namespacer.Namespacer, h
248
250
} else if handler .Get != nil {
249
251
register (p .getOperation (id + 1 , namespacer , * handler .Get ))
250
252
} else if handler .Patch != nil {
251
- loaded , err := p .patchOperation (id + 1 , namespacer , * handler .Patch )
253
+ loaded , err := p .patchOperation (id + 1 , namespacer , bindings , * handler .Patch )
252
254
if err != nil {
253
255
return nil , err
254
256
}
@@ -262,7 +264,7 @@ func (p *stepProcessor) tryOperation(id int, namespacer namespacer.Namespacer, h
262
264
} else if handler .Sleep != nil {
263
265
register (p .sleepOperation (id + 1 , * handler .Sleep ))
264
266
} else if handler .Update != nil {
265
- loaded , err := p .updateOperation (id + 1 , namespacer , * handler .Update )
267
+ loaded , err := p .updateOperation (id + 1 , namespacer , bindings , * handler .Update )
266
268
if err != nil {
267
269
return nil , err
268
270
}
@@ -275,7 +277,7 @@ func (p *stepProcessor) tryOperation(id int, namespacer namespacer.Namespacer, h
275
277
return ops , nil
276
278
}
277
279
278
- func (p * stepProcessor ) catchOperation (id int , namespacer namespacer.Namespacer , handler v1alpha1.CatchFinally ) ([]operation , error ) {
280
+ func (p * stepProcessor ) catchOperation (id int , namespacer namespacer.Namespacer , bindings binding. Bindings , handler v1alpha1.CatchFinally ) ([]operation , error ) {
279
281
var ops []operation
280
282
register := func (o ... operation ) {
281
283
for _ , o := range o {
@@ -304,7 +306,7 @@ func (p *stepProcessor) catchOperation(id int, namespacer namespacer.Namespacer,
304
306
} else if handler .Get != nil {
305
307
register (p .getOperation (id + 1 , namespacer , * handler .Get ))
306
308
} else if handler .Delete != nil {
307
- loaded , err := p .deleteOperation (id + 1 , namespacer , * handler .Delete )
309
+ loaded , err := p .deleteOperation (id + 1 , namespacer , bindings , * handler .Delete )
308
310
if err != nil {
309
311
return nil , err
310
312
}
@@ -323,7 +325,7 @@ func (p *stepProcessor) catchOperation(id int, namespacer namespacer.Namespacer,
323
325
return ops , nil
324
326
}
325
327
326
- func (p * stepProcessor ) finallyOperation (id int , namespacer namespacer.Namespacer , handler v1alpha1.CatchFinally ) ([]operation , error ) {
328
+ func (p * stepProcessor ) finallyOperation (id int , namespacer namespacer.Namespacer , bindings binding. Bindings , handler v1alpha1.CatchFinally ) ([]operation , error ) {
327
329
var ops []operation
328
330
register := func (o ... operation ) {
329
331
for _ , o := range o {
@@ -352,7 +354,7 @@ func (p *stepProcessor) finallyOperation(id int, namespacer namespacer.Namespace
352
354
} else if handler .Get != nil {
353
355
register (p .getOperation (id + 1 , namespacer , * handler .Get ))
354
356
} else if handler .Delete != nil {
355
- loaded , err := p .deleteOperation (id + 1 , namespacer , * handler .Delete )
357
+ loaded , err := p .deleteOperation (id + 1 , namespacer , bindings , * handler .Delete )
356
358
if err != nil {
357
359
return nil , err
358
360
}
@@ -371,12 +373,12 @@ func (p *stepProcessor) finallyOperation(id int, namespacer namespacer.Namespace
371
373
return ops , nil
372
374
}
373
375
374
- func (p * stepProcessor ) applyOperation (id int , namespacer namespacer.Namespacer , cleaner cleaner.CleanerCollector , op v1alpha1.Apply ) ([]operation , error ) {
376
+ func (p * stepProcessor ) applyOperation (id int , namespacer namespacer.Namespacer , cleaner cleaner.CleanerCollector , bindings binding. Bindings , op v1alpha1.Apply ) ([]operation , error ) {
375
377
var operationReport * report.OperationReport
376
378
if p .report != nil {
377
379
operationReport = p .report .ForOperation ("Apply " + op .File , report .OperationTypeApply )
378
380
}
379
- resources , err := p .fileRefOrResource (op .ActionResourceRef )
381
+ resources , err := p .fileRefOrResource (context . TODO (), op .ActionResourceRef , bindings )
380
382
if err != nil {
381
383
return nil , err
382
384
}
@@ -426,8 +428,8 @@ func (p *stepProcessor) applyOperation(id int, namespacer namespacer.Namespacer,
426
428
return ops , nil
427
429
}
428
430
429
- func (p * stepProcessor ) assertOperation (id int , namespacer namespacer.Namespacer , op v1alpha1.Assert ) ([]operation , error ) {
430
- resources , err := p .fileRefOrCheck (op .ActionCheckRef )
431
+ func (p * stepProcessor ) assertOperation (id int , namespacer namespacer.Namespacer , bindings binding. Bindings , op v1alpha1.Assert ) ([]operation , error ) {
432
+ resources , err := p .fileRefOrCheck (context . TODO (), op .ActionCheckRef , bindings )
431
433
if err != nil {
432
434
return nil , err
433
435
}
@@ -511,8 +513,8 @@ func (p *stepProcessor) commandOperation(id int, namespacer namespacer.Namespace
511
513
)
512
514
}
513
515
514
- func (p * stepProcessor ) createOperation (id int , namespacer namespacer.Namespacer , cleaner cleaner.CleanerCollector , op v1alpha1.Create ) ([]operation , error ) {
515
- resources , err := p .fileRefOrResource (op .ActionResourceRef )
516
+ func (p * stepProcessor ) createOperation (id int , namespacer namespacer.Namespacer , cleaner cleaner.CleanerCollector , bindings binding. Bindings , op v1alpha1.Create ) ([]operation , error ) {
517
+ resources , err := p .fileRefOrResource (context . TODO (), op .ActionResourceRef , bindings )
516
518
if err != nil {
517
519
return nil , err
518
520
}
@@ -564,7 +566,7 @@ func (p *stepProcessor) createOperation(id int, namespacer namespacer.Namespacer
564
566
return ops , nil
565
567
}
566
568
567
- func (p * stepProcessor ) deleteOperation (id int , namespacer namespacer.Namespacer , op v1alpha1.Delete ) ([]operation , error ) {
569
+ func (p * stepProcessor ) deleteOperation (id int , namespacer namespacer.Namespacer , bindings binding. Bindings , op v1alpha1.Delete ) ([]operation , error ) {
568
570
ref := v1alpha1.ActionResourceRef {
569
571
FileRef : v1alpha1.FileRef {
570
572
File : op .File ,
@@ -579,7 +581,7 @@ func (p *stepProcessor) deleteOperation(id int, namespacer namespacer.Namespacer
579
581
resource .SetLabels (op .Ref .Labels )
580
582
ref .Resource = & resource
581
583
}
582
- resources , err := p .fileRefOrResource (ref )
584
+ resources , err := p .fileRefOrResource (context . TODO (), ref , bindings )
583
585
if err != nil {
584
586
return nil , err
585
587
}
@@ -675,8 +677,8 @@ func (p *stepProcessor) describeOperation(id int, namespacer namespacer.Namespac
675
677
)
676
678
}
677
679
678
- func (p * stepProcessor ) errorOperation (id int , namespacer namespacer.Namespacer , op v1alpha1.Error ) ([]operation , error ) {
679
- resources , err := p .fileRefOrCheck (op .ActionCheckRef )
680
+ func (p * stepProcessor ) errorOperation (id int , namespacer namespacer.Namespacer , bindings binding. Bindings , op v1alpha1.Error ) ([]operation , error ) {
681
+ resources , err := p .fileRefOrCheck (context . TODO (), op .ActionCheckRef , bindings )
680
682
if err != nil {
681
683
return nil , err
682
684
}
@@ -817,8 +819,8 @@ func (p *stepProcessor) logsOperation(id int, namespacer namespacer.Namespacer,
817
819
)
818
820
}
819
821
820
- func (p * stepProcessor ) patchOperation (id int , namespacer namespacer.Namespacer , op v1alpha1.Patch ) ([]operation , error ) {
821
- resources , err := p .fileRefOrResource (op .ActionResourceRef )
822
+ func (p * stepProcessor ) patchOperation (id int , namespacer namespacer.Namespacer , bindings binding. Bindings , op v1alpha1.Patch ) ([]operation , error ) {
823
+ resources , err := p .fileRefOrResource (context . TODO (), op .ActionResourceRef , bindings )
822
824
if err != nil {
823
825
return nil , err
824
826
}
@@ -973,8 +975,8 @@ func (p *stepProcessor) sleepOperation(id int, op v1alpha1.Sleep) operation {
973
975
)
974
976
}
975
977
976
- func (p * stepProcessor ) updateOperation (id int , namespacer namespacer.Namespacer , op v1alpha1.Update ) ([]operation , error ) {
977
- resources , err := p .fileRefOrResource (op .ActionResourceRef )
978
+ func (p * stepProcessor ) updateOperation (id int , namespacer namespacer.Namespacer , bindings binding. Bindings , op v1alpha1.Update ) ([]operation , error ) {
979
+ resources , err := p .fileRefOrResource (context . TODO (), op .ActionResourceRef , bindings )
978
980
if err != nil {
979
981
return nil , err
980
982
}
@@ -1076,7 +1078,7 @@ func (p *stepProcessor) waitOperation(id int, namespacer namespacer.Namespacer,
1076
1078
)
1077
1079
}
1078
1080
1079
- func (p * stepProcessor ) fileRefOrCheck (ref v1alpha1.ActionCheckRef ) ([]unstructured.Unstructured , error ) {
1081
+ func (p * stepProcessor ) fileRefOrCheck (ctx context. Context , ref v1alpha1.ActionCheckRef , bindings binding. Bindings ) ([]unstructured.Unstructured , error ) {
1080
1082
if ref .Check != nil && ref .Check .Value != nil {
1081
1083
if object , ok := ref .Check .Value .(map [string ]any ); ! ok {
1082
1084
return nil , errors .New ("resource must be an object" )
@@ -1085,24 +1087,32 @@ func (p *stepProcessor) fileRefOrCheck(ref v1alpha1.ActionCheckRef) ([]unstructu
1085
1087
}
1086
1088
}
1087
1089
if ref .File != "" {
1088
- url , err := url . ParseRequestURI ( ref .File )
1090
+ ref , err := templating . String ( ctx , ref .File , bindings )
1089
1091
if err != nil {
1090
- return resource .Load (filepath .Join (p .basePath , ref .File ), false )
1092
+ return nil , err
1093
+ }
1094
+ url , err := url .ParseRequestURI (ref )
1095
+ if err != nil {
1096
+ return resource .Load (filepath .Join (p .basePath , ref ), false )
1091
1097
} else {
1092
1098
return resource .LoadFromURI (url , false )
1093
1099
}
1094
1100
}
1095
1101
return nil , errors .New ("file or resource must be set" )
1096
1102
}
1097
1103
1098
- func (p * stepProcessor ) fileRefOrResource (ref v1alpha1.ActionResourceRef ) ([]unstructured.Unstructured , error ) {
1104
+ func (p * stepProcessor ) fileRefOrResource (ctx context. Context , ref v1alpha1.ActionResourceRef , bindings binding. Bindings ) ([]unstructured.Unstructured , error ) {
1099
1105
if ref .Resource != nil {
1100
1106
return []unstructured.Unstructured {* ref .Resource }, nil
1101
1107
}
1102
1108
if ref .File != "" {
1103
- url , err := url .ParseRequestURI (ref .File )
1109
+ ref , err := templating .String (ctx , ref .File , bindings )
1110
+ if err != nil {
1111
+ return nil , err
1112
+ }
1113
+ url , err := url .ParseRequestURI (ref )
1104
1114
if err != nil {
1105
- return resource .Load (filepath .Join (p .basePath , ref . File ), true )
1115
+ return resource .Load (filepath .Join (p .basePath , ref ), true )
1106
1116
} else {
1107
1117
return resource .LoadFromURI (url , true )
1108
1118
}
0 commit comments