Skip to content

Commit

Permalink
Move ManifestReader creation into Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
seans3 committed Sep 22, 2020
1 parent 5198c54 commit 62bb797
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 119 deletions.
37 changes: 4 additions & 33 deletions cmd/apply/cmdapply.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ import (
"sigs.k8s.io/cli-utils/pkg/apply"
"sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/manifestreader"
"sigs.k8s.io/cli-utils/pkg/provider"
"sigs.k8s.io/kustomize/kyaml/setters2"
)

func GetApplyRunner(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *ApplyRunner {
provider := provider.NewProvider(f, inventory.WrapInventoryObj)
func GetApplyRunner(provider provider.Provider, ioStreams genericclioptions.IOStreams) *ApplyRunner {
r := &ApplyRunner{
Applier: apply.NewApplier(provider, ioStreams),
ioStreams: ioStreams,
Expand Down Expand Up @@ -71,7 +69,8 @@ func GetApplyRunner(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *A
}

func ApplyCommand(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
return GetApplyRunner(f, ioStreams).Command
provider := provider.NewProvider(f, inventory.WrapInventoryObj)
return GetApplyRunner(provider, ioStreams).Command
}

type ApplyRunner struct {
Expand Down Expand Up @@ -116,35 +115,7 @@ func (r *ApplyRunner) RunE(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

// Fetch the namespace from the configloader. The source of this
// either the namespace flag or the context. If the namespace is provided
// with the flag, enforceNamespace will be true. In this case, it is
// an error if any of the resources in the package has a different
// namespace set.
namespace, enforceNamespace, err := r.provider.Factory().ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}

var reader manifestreader.ManifestReader
readerOptions := manifestreader.ReaderOptions{
Factory: r.provider.Factory(),
Namespace: namespace,
EnforceNamespace: enforceNamespace,
}
if len(args) == 0 {
reader = &manifestreader.StreamManifestReader{
ReaderName: "stdin",
Reader: cmd.InOrStdin(),
ReaderOptions: readerOptions,
}
} else {
reader = &manifestreader.PathManifestReader{
Path: args[0],
ReaderOptions: readerOptions,
}
}
reader := r.provider.ManifestReader(cmd.InOrStdin(), args)
infos, err := reader.Read()
if err != nil {
return err
Expand Down
21 changes: 17 additions & 4 deletions cmd/destroy/cmddestroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
)

// GetDestroyRunner creates and returns the DestroyRunner which stores the cobra command.
func GetDestroyRunner(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *DestroyRunner {
provider := provider.NewProvider(f, inventory.WrapInventoryObj)
func GetDestroyRunner(provider provider.Provider, ioStreams genericclioptions.IOStreams) *DestroyRunner {
r := &DestroyRunner{
Destroyer: apply.NewDestroyer(provider, ioStreams),
ioStreams: ioStreams,
provider: provider,
}
cmd := &cobra.Command{
Use: "destroy (DIRECTORY | STDIN)",
Expand Down Expand Up @@ -48,14 +48,16 @@ func GetDestroyRunner(f cmdutil.Factory, ioStreams genericclioptions.IOStreams)

// DestroyCommand creates the DestroyRunner, returning the cobra command associated with it.
func DestroyCommand(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
return GetDestroyRunner(f, ioStreams).Command
provider := provider.NewProvider(f, inventory.WrapInventoryObj)
return GetDestroyRunner(provider, ioStreams).Command
}

// DestroyRunner encapsulates data necessary to run the destroy command.
type DestroyRunner struct {
Command *cobra.Command
ioStreams genericclioptions.IOStreams
Destroyer *apply.Destroyer
provider provider.Provider
}

func (r *DestroyRunner) RunE(cmd *cobra.Command, args []string) error {
Expand All @@ -64,9 +66,20 @@ func (r *DestroyRunner) RunE(cmd *cobra.Command, args []string) error {
return err
}

// Retrieve the inventory object.
reader := r.provider.ManifestReader(cmd.InOrStdin(), args)
infos, err := reader.Read()
if err != nil {
return err
}
inv, _, err := inventory.SplitInfos(infos)
if err != nil {
return err
}

// Run the destroyer. It will return a channel where we can receive updates
// to keep track of progress and any issues.
ch := r.Destroyer.Run()
ch := r.Destroyer.Run(inv)

// The printer will print updates from the channel. It will block
// until the channel is closed.
Expand Down
52 changes: 14 additions & 38 deletions cmd/preview/cmdpreview.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"sigs.k8s.io/cli-utils/pkg/apply/event"
"sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/manifestreader"
"sigs.k8s.io/cli-utils/pkg/provider"
"sigs.k8s.io/kustomize/kyaml/setters2"
)
Expand All @@ -27,8 +26,7 @@ var (
)

// GetPreviewRunner creates and returns the PreviewRunner which stores the cobra command.
func GetPreviewRunner(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *PreviewRunner {
provider := provider.NewProvider(f, inventory.WrapInventoryObj)
func GetPreviewRunner(provider provider.Provider, ioStreams genericclioptions.IOStreams) *PreviewRunner {
r := &PreviewRunner{
Applier: apply.NewApplier(provider, ioStreams),
Destroyer: apply.NewDestroyer(provider, ioStreams),
Expand Down Expand Up @@ -68,7 +66,8 @@ func GetPreviewRunner(f cmdutil.Factory, ioStreams genericclioptions.IOStreams)

// PreviewCommand creates the PreviewRunner, returning the cobra command associated with it.
func PreviewCommand(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
return GetPreviewRunner(f, ioStreams).Command
provider := provider.NewProvider(f, inventory.WrapInventoryObj)
return GetPreviewRunner(provider, ioStreams).Command
}

// PreviewRunner encapsulates data necessary to run the preview command.
Expand Down Expand Up @@ -101,6 +100,12 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
r.Destroyer.DryRunStrategy = drs
}

reader := r.provider.ManifestReader(cmd.InOrStdin(), args)
infos, err := reader.Read()
if err != nil {
return err
}

// if destroy flag is set in preview, transmit it to destroyer DryRunStrategy flag
// and pivot execution to destroy with dry-run
if !r.Destroyer.DryRunStrategy.ClientOrServerDryRun() {
Expand All @@ -117,39 +122,6 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
return err
}

// Fetch the namespace from the configloader. The source of this
// either the namespace flag or the context. If the namespace is provided
// with the flag, enforceNamespace will be true. In this case, it is
// an error if any of the resources in the package has a different
// namespace set.
namespace, enforceNamespace, err := r.provider.Factory().ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}

var reader manifestreader.ManifestReader
readerOptions := manifestreader.ReaderOptions{
Factory: r.provider.Factory(),
Namespace: namespace,
EnforceNamespace: enforceNamespace,
}
if len(args) == 0 {
reader = &manifestreader.StreamManifestReader{
ReaderName: "stdin",
Reader: cmd.InOrStdin(),
ReaderOptions: readerOptions,
}
} else {
reader = &manifestreader.PathManifestReader{
Path: args[0],
ReaderOptions: readerOptions,
}
}
infos, err := reader.Read()
if err != nil {
return err
}

// Run the applier. It will return a channel where we can receive updates
// to keep track of progress and any issues.
ch = r.Applier.Run(ctx, infos, apply.Options{
Expand All @@ -158,7 +130,11 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
DryRunStrategy: drs,
})
} else {
ch = r.Destroyer.Run()
inv, _, err := inventory.SplitInfos(infos)
if err != nil {
return err
}
ch = r.Destroyer.Run(inv)
}

// The printer will print updates from the channel. It will block
Expand Down
20 changes: 1 addition & 19 deletions cmd/status/cmdstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/go-errors/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/cmd/status/printers"
Expand All @@ -22,7 +21,6 @@ import (
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/collector"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/event"
"sigs.k8s.io/cli-utils/pkg/kstatus/status"
"sigs.k8s.io/cli-utils/pkg/manifestreader"
"sigs.k8s.io/cli-utils/pkg/provider"
"sigs.k8s.io/cli-utils/pkg/util/factory"
)
Expand Down Expand Up @@ -76,23 +74,7 @@ func (r *StatusRunner) runE(cmd *cobra.Command, args []string) error {
return err
}

var reader manifestreader.ManifestReader
readerOptions := manifestreader.ReaderOptions{
Factory: r.provider.Factory(),
Namespace: metav1.NamespaceDefault,
}
if len(args) == 0 {
reader = &manifestreader.StreamManifestReader{
ReaderName: "stdin",
Reader: cmd.InOrStdin(),
ReaderOptions: readerOptions,
}
} else {
reader = &manifestreader.PathManifestReader{
Path: args[0],
ReaderOptions: readerOptions,
}
}
reader := r.provider.ManifestReader(cmd.InOrStdin(), args)
infos, err := reader.Read()
if err != nil {
return err
Expand Down
49 changes: 24 additions & 25 deletions pkg/apply/destroyer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,52 +78,51 @@ func (d *Destroyer) Initialize(cmd *cobra.Command, paths []string) error {
return nil
}

// Run performs the destroy step. This happens asynchronously
// on progress and any errors are reported back on the event channel.
func (d *Destroyer) Run() <-chan event.Event {
// Run performs the destroy step. Passes the inventory object. This
// happens asynchronously on progress and any errors are reported
// back on the event channel.
func (d *Destroyer) Run(inv *resource.Info) <-chan event.Event {
ch := make(chan event.Event)

go func() {
defer close(ch)
d.invClient.SetDryRunStrategy(d.DryRunStrategy)
infos, err := d.ApplyOptions.GetObjects()

// Force a pruning of all cluster resources by clearing out the
// local resources, and sending only the inventory object to the
// prune.
infos := []*resource.Info{inv}

// Start the event transformer goroutine so we can transform
// the Prune events emitted from the Prune function to Delete
// Events. That we use Prune to implement destroy is an
// implementation detail and the events should not be Prune events.
tempChannel, completedChannel := runPruneEventTransformer(ch)
err := d.PruneOptions.Prune(infos, tempChannel, prune.Options{
DryRunStrategy: d.DryRunStrategy,
PropagationPolicy: metav1.DeletePropagationBackground,
})
if err != nil {
ch <- event.Event{
Type: event.ErrorType,
ErrorEvent: event.ErrorEvent{
Err: errors.WrapPrefix(err, "error reading resource manifests", 1),
Err: errors.WrapPrefix(err, "error pruning resources in cluster", 1),
},
}
return
}
// Force a pruning of all cluster resources by clearing out the
// local resources, and sending only the inventory object to the
// prune.
invInfo, _, err := inventory.SplitInfos(infos)

// Now delete the inventory object as well.
err = d.invClient.DeleteInventoryObj(inv)
if err != nil {
ch <- event.Event{
Type: event.ErrorType,
ErrorEvent: event.ErrorEvent{
Err: errors.WrapPrefix(err, "error clearing inventory object", 1),
Err: errors.WrapPrefix(err, "error deleting inventory object", 1),
},
}
return
}
infos = []*resource.Info{invInfo}

// Start the event transformer goroutine so we can transform
// the Prune events emitted from the Prune function to Delete
// Events. That we use Prune to implement destroy is an
// implementation detail and the events should not be Prune events.
tempChannel, completedChannel := runPruneEventTransformer(ch)
err = d.PruneOptions.Prune(infos, tempChannel, prune.Options{
DryRunStrategy: d.DryRunStrategy,
PropagationPolicy: metav1.DeletePropagationBackground,
})
// Now delete the inventory object as well.
if invInfo != nil {
_ = d.invClient.DeleteInventoryObj(invInfo)
}

// Close the tempChannel to signal to the event transformer that
// it should terminate.
Expand Down
16 changes: 16 additions & 0 deletions pkg/provider/fake-provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
package provider

import (
"io"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/manifestreader"
"sigs.k8s.io/cli-utils/pkg/object"
)

Expand Down Expand Up @@ -35,3 +39,15 @@ func (f *FakeProvider) InventoryClient() (inventory.InventoryClient, error) {
func (f *FakeProvider) ToRESTMapper() (meta.RESTMapper, error) {
return f.factory.ToRESTMapper()
}

func (f *FakeProvider) ManifestReader(reader io.Reader, args []string) manifestreader.ManifestReader {
readerOptions := manifestreader.ReaderOptions{
Factory: f.factory,
Namespace: metav1.NamespaceDefault,
}
return &manifestreader.StreamManifestReader{
ReaderName: "stdin",
Reader: reader,
ReaderOptions: readerOptions,
}
}
Loading

0 comments on commit 62bb797

Please sign in to comment.