Skip to content

Commit 556385b

Browse files
authored
feat: support templating in files (#1826)
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
1 parent e7d0047 commit 556385b

File tree

2 files changed

+50
-38
lines changed

2 files changed

+50
-38
lines changed

.release-notes/main.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ Release notes for `TODO`.
55
<!--
66
## ‼️ Breaking changes ‼️
77
8-
## 💫 New features 💫
9-
108
## ✨ UI changes ✨
119
1210
## ⭐ Examples ⭐
@@ -19,3 +17,7 @@ Release notes for `TODO`.
1917
2018
## 🎸 Misc 🎸
2119
-->
20+
21+
## 💫 New features 💫
22+
23+
- Added support for templating filenames used in operations

pkg/runner/processors/step.go

+46-36
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"time"
99

10+
"github.com/jmespath-community/go-jmespath/pkg/binding"
1011
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
1112
"github.com/kyverno/chainsaw/pkg/cleanup/cleaner"
1213
"github.com/kyverno/chainsaw/pkg/engine"
@@ -24,6 +25,7 @@ import (
2425
opscript "github.com/kyverno/chainsaw/pkg/engine/operations/script"
2526
opsleep "github.com/kyverno/chainsaw/pkg/engine/operations/sleep"
2627
opupdate "github.com/kyverno/chainsaw/pkg/engine/operations/update"
28+
"github.com/kyverno/chainsaw/pkg/engine/templating"
2729
"github.com/kyverno/chainsaw/pkg/loaders/resource"
2830
"github.com/kyverno/chainsaw/pkg/report"
2931
"github.com/kyverno/chainsaw/pkg/runner/failer"
@@ -121,7 +123,7 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
121123
failer.Fail(ctx)
122124
}
123125
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)
125127
if err != nil {
126128
logger.Log(logging.Cleanup, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
127129
failer.Fail(ctx)
@@ -139,7 +141,7 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
139141
logger.Log(logging.Finally, logging.DoneStatus, color.BoldFgCyan)
140142
}()
141143
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)
143145
if err != nil {
144146
logger.Log(logging.Finally, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
145147
failer.Fail(ctx)
@@ -158,7 +160,7 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
158160
logger.Log(logging.Catch, logging.DoneStatus, color.BoldFgCyan)
159161
}()
160162
for i, operation := range p.catch {
161-
operations, err := p.catchOperation(i, namespacer, operation)
163+
operations, err := p.catchOperation(i, namespacer, tc.Bindings(), operation)
162164
if err != nil {
163165
logger.Log(logging.Catch, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
164166
failer.Fail(ctx)
@@ -175,7 +177,7 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
175177
logger.Log(logging.Try, logging.DoneStatus, color.BoldFgCyan)
176178
}()
177179
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)
179181
if err != nil {
180182
logger.Log(logging.Try, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err))
181183
failer.FailNow(ctx)
@@ -188,7 +190,7 @@ func (p *stepProcessor) Run(ctx context.Context, namespacer namespacer.Namespace
188190
}
189191
}
190192

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) {
192194
var ops []operation
193195
register := func(o ...operation) {
194196
continueOnError := handler.ContinueOnError != nil && *handler.ContinueOnError
@@ -198,35 +200,35 @@ func (p *stepProcessor) tryOperation(id int, namespacer namespacer.Namespacer, h
198200
}
199201
}
200202
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)
202204
if err != nil {
203205
return nil, err
204206
}
205207
register(loaded...)
206208
} 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)
208210
if err != nil {
209211
return nil, err
210212
}
211213
register(loaded...)
212214
} else if handler.Command != nil {
213215
register(p.commandOperation(id+1, namespacer, *handler.Command))
214216
} 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)
216218
if err != nil {
217219
return nil, err
218220
}
219221
register(loaded...)
220222
} 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)
222224
if err != nil {
223225
return nil, err
224226
}
225227
register(loaded...)
226228
} else if handler.Describe != nil {
227229
register(p.describeOperation(id+1, namespacer, *handler.Describe))
228230
} 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)
230232
if err != nil {
231233
return nil, err
232234
}
@@ -248,7 +250,7 @@ func (p *stepProcessor) tryOperation(id int, namespacer namespacer.Namespacer, h
248250
} else if handler.Get != nil {
249251
register(p.getOperation(id+1, namespacer, *handler.Get))
250252
} 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)
252254
if err != nil {
253255
return nil, err
254256
}
@@ -262,7 +264,7 @@ func (p *stepProcessor) tryOperation(id int, namespacer namespacer.Namespacer, h
262264
} else if handler.Sleep != nil {
263265
register(p.sleepOperation(id+1, *handler.Sleep))
264266
} 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)
266268
if err != nil {
267269
return nil, err
268270
}
@@ -275,7 +277,7 @@ func (p *stepProcessor) tryOperation(id int, namespacer namespacer.Namespacer, h
275277
return ops, nil
276278
}
277279

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) {
279281
var ops []operation
280282
register := func(o ...operation) {
281283
for _, o := range o {
@@ -304,7 +306,7 @@ func (p *stepProcessor) catchOperation(id int, namespacer namespacer.Namespacer,
304306
} else if handler.Get != nil {
305307
register(p.getOperation(id+1, namespacer, *handler.Get))
306308
} 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)
308310
if err != nil {
309311
return nil, err
310312
}
@@ -323,7 +325,7 @@ func (p *stepProcessor) catchOperation(id int, namespacer namespacer.Namespacer,
323325
return ops, nil
324326
}
325327

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) {
327329
var ops []operation
328330
register := func(o ...operation) {
329331
for _, o := range o {
@@ -352,7 +354,7 @@ func (p *stepProcessor) finallyOperation(id int, namespacer namespacer.Namespace
352354
} else if handler.Get != nil {
353355
register(p.getOperation(id+1, namespacer, *handler.Get))
354356
} 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)
356358
if err != nil {
357359
return nil, err
358360
}
@@ -371,12 +373,12 @@ func (p *stepProcessor) finallyOperation(id int, namespacer namespacer.Namespace
371373
return ops, nil
372374
}
373375

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) {
375377
var operationReport *report.OperationReport
376378
if p.report != nil {
377379
operationReport = p.report.ForOperation("Apply "+op.File, report.OperationTypeApply)
378380
}
379-
resources, err := p.fileRefOrResource(op.ActionResourceRef)
381+
resources, err := p.fileRefOrResource(context.TODO(), op.ActionResourceRef, bindings)
380382
if err != nil {
381383
return nil, err
382384
}
@@ -426,8 +428,8 @@ func (p *stepProcessor) applyOperation(id int, namespacer namespacer.Namespacer,
426428
return ops, nil
427429
}
428430

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)
431433
if err != nil {
432434
return nil, err
433435
}
@@ -511,8 +513,8 @@ func (p *stepProcessor) commandOperation(id int, namespacer namespacer.Namespace
511513
)
512514
}
513515

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)
516518
if err != nil {
517519
return nil, err
518520
}
@@ -564,7 +566,7 @@ func (p *stepProcessor) createOperation(id int, namespacer namespacer.Namespacer
564566
return ops, nil
565567
}
566568

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) {
568570
ref := v1alpha1.ActionResourceRef{
569571
FileRef: v1alpha1.FileRef{
570572
File: op.File,
@@ -579,7 +581,7 @@ func (p *stepProcessor) deleteOperation(id int, namespacer namespacer.Namespacer
579581
resource.SetLabels(op.Ref.Labels)
580582
ref.Resource = &resource
581583
}
582-
resources, err := p.fileRefOrResource(ref)
584+
resources, err := p.fileRefOrResource(context.TODO(), ref, bindings)
583585
if err != nil {
584586
return nil, err
585587
}
@@ -675,8 +677,8 @@ func (p *stepProcessor) describeOperation(id int, namespacer namespacer.Namespac
675677
)
676678
}
677679

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)
680682
if err != nil {
681683
return nil, err
682684
}
@@ -817,8 +819,8 @@ func (p *stepProcessor) logsOperation(id int, namespacer namespacer.Namespacer,
817819
)
818820
}
819821

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)
822824
if err != nil {
823825
return nil, err
824826
}
@@ -973,8 +975,8 @@ func (p *stepProcessor) sleepOperation(id int, op v1alpha1.Sleep) operation {
973975
)
974976
}
975977

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)
978980
if err != nil {
979981
return nil, err
980982
}
@@ -1076,7 +1078,7 @@ func (p *stepProcessor) waitOperation(id int, namespacer namespacer.Namespacer,
10761078
)
10771079
}
10781080

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) {
10801082
if ref.Check != nil && ref.Check.Value != nil {
10811083
if object, ok := ref.Check.Value.(map[string]any); !ok {
10821084
return nil, errors.New("resource must be an object")
@@ -1085,24 +1087,32 @@ func (p *stepProcessor) fileRefOrCheck(ref v1alpha1.ActionCheckRef) ([]unstructu
10851087
}
10861088
}
10871089
if ref.File != "" {
1088-
url, err := url.ParseRequestURI(ref.File)
1090+
ref, err := templating.String(ctx, ref.File, bindings)
10891091
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)
10911097
} else {
10921098
return resource.LoadFromURI(url, false)
10931099
}
10941100
}
10951101
return nil, errors.New("file or resource must be set")
10961102
}
10971103

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) {
10991105
if ref.Resource != nil {
11001106
return []unstructured.Unstructured{*ref.Resource}, nil
11011107
}
11021108
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)
11041114
if err != nil {
1105-
return resource.Load(filepath.Join(p.basePath, ref.File), true)
1115+
return resource.Load(filepath.Join(p.basePath, ref), true)
11061116
} else {
11071117
return resource.LoadFromURI(url, true)
11081118
}

0 commit comments

Comments
 (0)