@@ -286,30 +286,97 @@ func (pn *pkgNode) runPipeline(ctx context.Context, hctx *hydrationContext, inpu
286
286
return input , nil
287
287
}
288
288
289
- fnChain , err := fnChain (ctx , pl , pn . pkg . UniquePath )
289
+ mutatedResources , err := pn . runMutators (ctx , hctx , input )
290
290
if err != nil {
291
291
return nil , errors .E (op , pn .pkg .UniquePath , err )
292
292
}
293
293
294
+ if err = pn .runValidators (ctx , hctx , mutatedResources ); err != nil {
295
+ return nil , errors .E (op , pn .pkg .UniquePath , err )
296
+ }
297
+ // print a new line after a pipeline running
298
+ pr .Printf ("\n " )
299
+ return mutatedResources , nil
300
+ }
301
+
302
+ // runMutators runs a set of mutators functions on given input resources.
303
+ func (pn * pkgNode ) runMutators (ctx context.Context , hctx * hydrationContext , input []* yaml.RNode ) ([]* yaml.RNode , error ) {
304
+ if len (input ) == 0 {
305
+ return input , nil
306
+ }
307
+
308
+ pl , err := pn .pkg .Pipeline ()
309
+ if err != nil {
310
+ return nil , err
311
+ }
312
+
313
+ if len (pl .Mutators ) == 0 {
314
+ return input , nil
315
+ }
316
+
317
+ mutators , err := fnChain (ctx , pn .pkg .UniquePath , pl .Mutators )
318
+ if err != nil {
319
+ return nil , err
320
+ }
321
+
294
322
output := & kio.PackageBuffer {}
295
323
// create a kio pipeline from kyaml library to execute the function chains
296
- kioPipeline := kio.Pipeline {
324
+ mutation := kio.Pipeline {
297
325
Inputs : []kio.Reader {
298
326
& kio.PackageBuffer {Nodes : input },
299
327
},
300
- Filters : fnChain ,
328
+ Filters : mutators ,
301
329
Outputs : []kio.Writer {output },
302
330
}
303
- err = kioPipeline .Execute ()
331
+ err = mutation .Execute ()
304
332
if err != nil {
305
- return nil , errors . E ( op , pn . pkg . UniquePath , err )
333
+ return nil , err
306
334
}
307
- hctx .executedFunctionCnt += len (fnChain )
308
- // print a new line after a pipeline running
309
- pr .Printf ("\n " )
335
+ hctx .executedFunctionCnt += len (mutators )
310
336
return output .Nodes , nil
311
337
}
312
338
339
+ // runValidators runs a set of validator functions on input resources.
340
+ // We bail out on first validation failure today, but the logic can be
341
+ // improved to report multiple failures. Reporting multiple failures
342
+ // will require changes to the way we print errors
343
+ func (pn * pkgNode ) runValidators (ctx context.Context , hctx * hydrationContext , input []* yaml.RNode ) error {
344
+ if len (input ) == 0 {
345
+ return nil
346
+ }
347
+
348
+ pl , err := pn .pkg .Pipeline ()
349
+ if err != nil {
350
+ return err
351
+ }
352
+
353
+ if len (pl .Validators ) == 0 {
354
+ return nil
355
+ }
356
+
357
+ for i := range pl .Validators {
358
+ fn := pl .Validators [i ]
359
+ validator , err := newFnRunner (ctx , & fn , pn .pkg .UniquePath )
360
+ if err != nil {
361
+ return err
362
+ }
363
+ // validators are run on a copy of mutated resources to ensure
364
+ // resources are not mutated.
365
+ if _ , err = validator .Filter (cloneResources (input )); err != nil {
366
+ return err
367
+ }
368
+ hctx .executedFunctionCnt ++
369
+ }
370
+ return nil
371
+ }
372
+
373
+ func cloneResources (input []* yaml.RNode ) (output []* yaml.RNode ) {
374
+ for _ , resource := range input {
375
+ output = append (output , resource .Copy ())
376
+ }
377
+ return
378
+ }
379
+
313
380
// path (location) of a KRM resources is tracked in a special key in
314
381
// metadata.annotation field. adjustRelPath updates that path annotation by prepending
315
382
// the given relPath to the current path annotation if it doesn't exist already.
@@ -338,13 +405,8 @@ func adjustRelPath(resources []*yaml.RNode, relPath string) ([]*yaml.RNode, erro
338
405
return resources , nil
339
406
}
340
407
341
- // fnChain returns a slice of function runners from the
342
- // functions and configs defined in pipeline.
343
- func fnChain (ctx context.Context , pl * kptfilev1alpha2.Pipeline , pkgPath types.UniquePath ) ([]kio.Filter , error ) {
344
- fns := []kptfilev1alpha2.Function {}
345
- fns = append (fns , pl .Mutators ... )
346
- // TODO: Validators cannot modify resources.
347
- fns = append (fns , pl .Validators ... )
408
+ // fnChain returns a slice of function runners given a list of functions defined in pipeline.
409
+ func fnChain (ctx context.Context , pkgPath types.UniquePath , fns []kptfilev1alpha2.Function ) ([]kio.Filter , error ) {
348
410
var runners []kio.Filter
349
411
for i := range fns {
350
412
fn := fns [i ]
0 commit comments