From 79bc63fb4b79caea4b141d318b11a0892ca14c95 Mon Sep 17 00:00:00 2001 From: Sean Sullivan Date: Tue, 17 Dec 2019 14:58:54 -0800 Subject: [PATCH] Adds PreProcessor and PostProcessor functions for modifying apply behavior --- .../src/k8s.io/kubectl/pkg/cmd/apply/apply.go | 60 +++++++++++++++---- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go index f0e1693f338..1bc989f7f20 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go @@ -101,10 +101,10 @@ type ApplyOptions struct { // Function run after the objects are generated and // stored in the "objects" field, but before the // apply is run on these objects. - preProcessorFn func(*ApplyOptions) error + PreProcessorFn func() error // Function run after all objects have been applied. - // The standard postprocessorFn is "PrintAndPrune()". - postProcessorFn func(*ApplyOptions) error + // The standard PostProcessorFn is "PrintAndPrunePostProcessor()". + PostProcessorFn func() error } var ( @@ -156,8 +156,6 @@ func NewApplyOptions(ioStreams genericclioptions.IOStreams) *ApplyOptions { VisitedUids: sets.NewString(), VisitedNamespaces: sets.NewString(), - - postProcessorFn: PrintAndPrune, } } @@ -288,6 +286,8 @@ func (o *ApplyOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error { } } + o.PostProcessorFn = o.PrintAndPrunePostProcessor() + return nil } @@ -350,6 +350,13 @@ func (o *ApplyOptions) GetObjects() ([]*resource.Info, error) { return o.objects, nil } +// SetObjects stores the set of objects (as resource.Info) to be +// subsequently applied. +func (o *ApplyOptions) SetObjects(infos []*resource.Info) { + o.objects = infos + o.objectsCached = true +} + // Run executes the `apply` command. func (o *ApplyOptions) Run() error { @@ -358,6 +365,15 @@ func (o *ApplyOptions) Run() error { OpenAPIGetter: o.DiscoveryClient, } + if o.PreProcessorFn != nil { + klog.V(4).Infof("Running apply pre-processor function") + if err := o.PreProcessorFn(); err != nil { + return err + } + } + + // Generates the objects using the resource builder if they have not + // already been stored by calling "SetObjects()" in the pre-processor. infos, err := o.GetObjects() if err != nil { return err @@ -365,7 +381,6 @@ func (o *ApplyOptions) Run() error { if len(infos) == 0 { return fmt.Errorf("no objects passed to apply") } - for _, info := range infos { // 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) } } - if err := o.printObjects(); err != nil { - return err - } - - if o.Prune { - p := newPruner(o) - return p.pruneAll(o) + if o.PostProcessorFn != nil { + klog.V(4).Infof("Running apply post-processor function") + if err := o.PostProcessorFn(); err != nil { + return err + } } return nil @@ -627,6 +640,27 @@ func (o *ApplyOptions) MarkObjectVisited(info *resource.Info) error { return nil } +// PrintAndPrune returns a function which meets the PostProcessorFn +// function signature. This returned function prints all the +// objects as a list (if configured for that), and prunes the +// objects not applied. The returned function is the standard +// apply post processor. +func (o *ApplyOptions) PrintAndPrunePostProcessor() func() error { + + return func() error { + if err := o.printObjects(); err != nil { + return err + } + + if o.Prune { + p := newPruner(o) + return p.pruneAll(o) + } + + return nil + } +} + // DryRunVerifier verifies if a given group-version-kind supports DryRun // against the current server. Sending dryRun requests to apiserver that // don't support it will result in objects being unwillingly persisted.