Skip to content

Commit 79bc63f

Browse files
committed
Adds PreProcessor and PostProcessor functions for modifying apply behavior
1 parent 4e45421 commit 79bc63f

File tree

1 file changed

+47
-13
lines changed
  • staging/src/k8s.io/kubectl/pkg/cmd/apply

1 file changed

+47
-13
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ type ApplyOptions struct {
101101
// Function run after the objects are generated and
102102
// stored in the "objects" field, but before the
103103
// apply is run on these objects.
104-
preProcessorFn func(*ApplyOptions) error
104+
PreProcessorFn func() error
105105
// 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
108108
}
109109

110110
var (
@@ -156,8 +156,6 @@ func NewApplyOptions(ioStreams genericclioptions.IOStreams) *ApplyOptions {
156156

157157
VisitedUids: sets.NewString(),
158158
VisitedNamespaces: sets.NewString(),
159-
160-
postProcessorFn: PrintAndPrune,
161159
}
162160
}
163161

@@ -288,6 +286,8 @@ func (o *ApplyOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
288286
}
289287
}
290288

289+
o.PostProcessorFn = o.PrintAndPrunePostProcessor()
290+
291291
return nil
292292
}
293293

@@ -350,6 +350,13 @@ func (o *ApplyOptions) GetObjects() ([]*resource.Info, error) {
350350
return o.objects, nil
351351
}
352352

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+
353360
// Run executes the `apply` command.
354361
func (o *ApplyOptions) Run() error {
355362

@@ -358,14 +365,22 @@ func (o *ApplyOptions) Run() error {
358365
OpenAPIGetter: o.DiscoveryClient,
359366
}
360367

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.
361377
infos, err := o.GetObjects()
362378
if err != nil {
363379
return err
364380
}
365381
if len(infos) == 0 {
366382
return fmt.Errorf("no objects passed to apply")
367383
}
368-
369384
for _, info := range infos {
370385

371386
// 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)
541556
}
542557
}
543558

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+
}
551564
}
552565

553566
return nil
@@ -627,6 +640,27 @@ func (o *ApplyOptions) MarkObjectVisited(info *resource.Info) error {
627640
return nil
628641
}
629642

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+
630664
// DryRunVerifier verifies if a given group-version-kind supports DryRun
631665
// against the current server. Sending dryRun requests to apiserver that
632666
// don't support it will result in objects being unwillingly persisted.

0 commit comments

Comments
 (0)