@@ -101,10 +101,10 @@ type ApplyOptions struct {
101
101
// Function run after the objects are generated and
102
102
// stored in the "objects" field, but before the
103
103
// apply is run on these objects.
104
- preProcessorFn func (* ApplyOptions ) error
104
+ PreProcessorFn func () error
105
105
// Function run after all objects have been applied.
106
- // The standard postprocessorFn is "PrintAndPrune ()".
107
- postProcessorFn func (* ApplyOptions ) error
106
+ // The standard PostProcessorFn is "PrintAndPrunePostProcessor ()".
107
+ PostProcessorFn func () error
108
108
}
109
109
110
110
var (
@@ -156,8 +156,6 @@ func NewApplyOptions(ioStreams genericclioptions.IOStreams) *ApplyOptions {
156
156
157
157
VisitedUids : sets .NewString (),
158
158
VisitedNamespaces : sets .NewString (),
159
-
160
- postProcessorFn : PrintAndPrune ,
161
159
}
162
160
}
163
161
@@ -288,6 +286,8 @@ func (o *ApplyOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
288
286
}
289
287
}
290
288
289
+ o .PostProcessorFn = o .PrintAndPrunePostProcessor ()
290
+
291
291
return nil
292
292
}
293
293
@@ -350,6 +350,13 @@ func (o *ApplyOptions) GetObjects() ([]*resource.Info, error) {
350
350
return o .objects , nil
351
351
}
352
352
353
+ // SetObjects stores the set of objects (as resource.Info) to be
354
+ // subsequently applied.
355
+ func (o * ApplyOptions ) SetObjects (infos []* resource.Info ) {
356
+ o .objects = infos
357
+ o .objectsCached = true
358
+ }
359
+
353
360
// Run executes the `apply` command.
354
361
func (o * ApplyOptions ) Run () error {
355
362
@@ -358,14 +365,22 @@ func (o *ApplyOptions) Run() error {
358
365
OpenAPIGetter : o .DiscoveryClient ,
359
366
}
360
367
368
+ if o .PreProcessorFn != nil {
369
+ klog .V (4 ).Infof ("Running apply pre-processor function" )
370
+ if err := o .PreProcessorFn (); err != nil {
371
+ return err
372
+ }
373
+ }
374
+
375
+ // Generates the objects using the resource builder if they have not
376
+ // already been stored by calling "SetObjects()" in the pre-processor.
361
377
infos , err := o .GetObjects ()
362
378
if err != nil {
363
379
return err
364
380
}
365
381
if len (infos ) == 0 {
366
382
return fmt .Errorf ("no objects passed to apply" )
367
383
}
368
-
369
384
for _ , info := range infos {
370
385
371
386
// If server-dry-run is requested but the type doesn't support it, fail right away.
@@ -541,13 +556,11 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
541
556
}
542
557
}
543
558
544
- if err := o .printObjects (); err != nil {
545
- return err
546
- }
547
-
548
- if o .Prune {
549
- p := newPruner (o )
550
- return p .pruneAll (o )
559
+ if o .PostProcessorFn != nil {
560
+ klog .V (4 ).Infof ("Running apply post-processor function" )
561
+ if err := o .PostProcessorFn (); err != nil {
562
+ return err
563
+ }
551
564
}
552
565
553
566
return nil
@@ -627,6 +640,27 @@ func (o *ApplyOptions) MarkObjectVisited(info *resource.Info) error {
627
640
return nil
628
641
}
629
642
643
+ // PrintAndPrune returns a function which meets the PostProcessorFn
644
+ // function signature. This returned function prints all the
645
+ // objects as a list (if configured for that), and prunes the
646
+ // objects not applied. The returned function is the standard
647
+ // apply post processor.
648
+ func (o * ApplyOptions ) PrintAndPrunePostProcessor () func () error {
649
+
650
+ return func () error {
651
+ if err := o .printObjects (); err != nil {
652
+ return err
653
+ }
654
+
655
+ if o .Prune {
656
+ p := newPruner (o )
657
+ return p .pruneAll (o )
658
+ }
659
+
660
+ return nil
661
+ }
662
+ }
663
+
630
664
// DryRunVerifier verifies if a given group-version-kind supports DryRun
631
665
// against the current server. Sending dryRun requests to apiserver that
632
666
// don't support it will result in objects being unwillingly persisted.
0 commit comments