mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 04:33:26 +00:00
Adds PreProcessor and PostProcessor functions for modifying apply behavior
This commit is contained in:
parent
4e45421f61
commit
79bc63fb4b
@ -101,10 +101,10 @@ type ApplyOptions struct {
|
|||||||
// Function run after the objects are generated and
|
// Function run after the objects are generated and
|
||||||
// stored in the "objects" field, but before the
|
// stored in the "objects" field, but before the
|
||||||
// apply is run on these objects.
|
// apply is run on these objects.
|
||||||
preProcessorFn func(*ApplyOptions) error
|
PreProcessorFn func() error
|
||||||
// Function run after all objects have been applied.
|
// Function run after all objects have been applied.
|
||||||
// The standard postprocessorFn is "PrintAndPrune()".
|
// The standard PostProcessorFn is "PrintAndPrunePostProcessor()".
|
||||||
postProcessorFn func(*ApplyOptions) error
|
PostProcessorFn func() error
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -156,8 +156,6 @@ func NewApplyOptions(ioStreams genericclioptions.IOStreams) *ApplyOptions {
|
|||||||
|
|
||||||
VisitedUids: sets.NewString(),
|
VisitedUids: sets.NewString(),
|
||||||
VisitedNamespaces: 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -350,6 +350,13 @@ func (o *ApplyOptions) GetObjects() ([]*resource.Info, error) {
|
|||||||
return o.objects, nil
|
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.
|
// Run executes the `apply` command.
|
||||||
func (o *ApplyOptions) Run() error {
|
func (o *ApplyOptions) Run() error {
|
||||||
|
|
||||||
@ -358,6 +365,15 @@ func (o *ApplyOptions) Run() error {
|
|||||||
OpenAPIGetter: o.DiscoveryClient,
|
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()
|
infos, err := o.GetObjects()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -365,7 +381,6 @@ func (o *ApplyOptions) Run() error {
|
|||||||
if len(infos) == 0 {
|
if len(infos) == 0 {
|
||||||
return fmt.Errorf("no objects passed to apply")
|
return fmt.Errorf("no objects passed to apply")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, info := range infos {
|
for _, info := range infos {
|
||||||
|
|
||||||
// If server-dry-run is requested but the type doesn't support it, fail right away.
|
// 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 {
|
if o.PostProcessorFn != nil {
|
||||||
return err
|
klog.V(4).Infof("Running apply post-processor function")
|
||||||
}
|
if err := o.PostProcessorFn(); err != nil {
|
||||||
|
return err
|
||||||
if o.Prune {
|
}
|
||||||
p := newPruner(o)
|
|
||||||
return p.pruneAll(o)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -627,6 +640,27 @@ func (o *ApplyOptions) MarkObjectVisited(info *resource.Info) error {
|
|||||||
return nil
|
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
|
// DryRunVerifier verifies if a given group-version-kind supports DryRun
|
||||||
// against the current server. Sending dryRun requests to apiserver that
|
// against the current server. Sending dryRun requests to apiserver that
|
||||||
// don't support it will result in objects being unwillingly persisted.
|
// don't support it will result in objects being unwillingly persisted.
|
||||||
|
Loading…
Reference in New Issue
Block a user