Skip to content

Commit

Permalink
Remove Provider interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ash2k committed Jun 30, 2021
1 parent 3a81895 commit c1c7c9e
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 206 deletions.
30 changes: 17 additions & 13 deletions cmd/apply/cmdapply.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ import (
"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/cli-utils/pkg/util/factory"
)

func GetApplyRunner(provider provider.Provider, loader manifestreader.ManifestLoader, ioStreams genericclioptions.IOStreams) *ApplyRunner {
func GetApplyRunner(factory cmdutil.Factory, invFactory inventory.InventoryClientFactory,
loader manifestreader.ManifestLoader, ioStreams genericclioptions.IOStreams) *ApplyRunner {
r := &ApplyRunner{
ioStreams: ioStreams,
provider: provider,
loader: loader,
ioStreams: ioStreams,
factory: factory,
invFactory: invFactory,
loader: loader,
}
cmd := &cobra.Command{
Use: "apply (DIRECTORY | STDIN)",
Expand Down Expand Up @@ -63,17 +64,17 @@ func GetApplyRunner(provider provider.Provider, loader manifestreader.ManifestLo
return r
}

func ApplyCommand(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
provider := provider.NewProvider(f)
loader := manifestreader.NewManifestLoader(f)
return GetApplyRunner(provider, loader, ioStreams).Command
func ApplyCommand(f cmdutil.Factory, invFactory inventory.InventoryClientFactory, loader manifestreader.ManifestLoader,
ioStreams genericclioptions.IOStreams) *cobra.Command {
return GetApplyRunner(f, invFactory, loader, ioStreams).Command
}

type ApplyRunner struct {
Command *cobra.Command
PreProcess func(info inventory.InventoryInfo, strategy common.DryRunStrategy) (inventory.InventoryPolicy, error)
ioStreams genericclioptions.IOStreams
provider provider.Provider
factory cmdutil.Factory
invFactory inventory.InventoryClientFactory
loader manifestreader.ManifestLoader

serverSideOptions common.ServerSideOptions
Expand Down Expand Up @@ -131,15 +132,18 @@ func (r *ApplyRunner) RunE(cmd *cobra.Command, args []string) error {
return err
}
}
f := r.provider.Factory()
statusPoller, err := factory.NewStatusPoller(f)
statusPoller, err := factory.NewStatusPoller(r.factory)
if err != nil {
return err
}
invClient, err := r.invFactory.NewInventoryClient(r.factory)
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.
a, err := apply.NewApplier(r.provider, statusPoller)
a, err := apply.NewApplier(r.factory, invClient, statusPoller)
if err != nil {
return err
}
Expand Down
29 changes: 17 additions & 12 deletions cmd/destroy/cmddestroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ import (
"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/cli-utils/pkg/util/factory"
)

// GetDestroyRunner creates and returns the DestroyRunner which stores the cobra command.
func GetDestroyRunner(provider provider.Provider, loader manifestreader.ManifestLoader, ioStreams genericclioptions.IOStreams) *DestroyRunner {
func GetDestroyRunner(factory cmdutil.Factory, invFactory inventory.InventoryClientFactory,
loader manifestreader.ManifestLoader, ioStreams genericclioptions.IOStreams) *DestroyRunner {
r := &DestroyRunner{
ioStreams: ioStreams,
provider: provider,
loader: loader,
ioStreams: ioStreams,
factory: factory,
invFactory: invFactory,
loader: loader,
}
cmd := &cobra.Command{
Use: "destroy (DIRECTORY | STDIN)",
Expand All @@ -51,18 +52,18 @@ func GetDestroyRunner(provider provider.Provider, loader manifestreader.Manifest
}

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

// DestroyRunner encapsulates data necessary to run the destroy command.
type DestroyRunner struct {
Command *cobra.Command
PreProcess func(info inventory.InventoryInfo, strategy common.DryRunStrategy) (inventory.InventoryPolicy, error)
ioStreams genericclioptions.IOStreams
provider provider.Provider
factory cmdutil.Factory
invFactory inventory.InventoryClientFactory
loader manifestreader.ManifestLoader

output string
Expand Down Expand Up @@ -101,11 +102,15 @@ func (r *DestroyRunner) RunE(cmd *cobra.Command, args []string) error {
}
}

statusPoller, err := factory.NewStatusPoller(r.provider.Factory())
statusPoller, err := factory.NewStatusPoller(r.factory)
if err != nil {
return err
}
d, err := apply.NewDestroyer(r.provider, statusPoller)
invClient, err := r.invFactory.NewInventoryClient(r.factory)
if err != nil {
return err
}
d, err := apply.NewDestroyer(r.factory, invClient, statusPoller)
if err != nil {
return err
}
Expand Down
12 changes: 8 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"sigs.k8s.io/cli-utils/cmd/preview"
"sigs.k8s.io/cli-utils/cmd/status"
"sigs.k8s.io/cli-utils/pkg/errors"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/manifestreader"
"sigs.k8s.io/cli-utils/pkg/util/factory"

// This is here rather than in the libraries because of
Expand Down Expand Up @@ -57,15 +59,17 @@ func main() {
names := []string{"init", "apply", "preview", "diff", "destroy", "status"}
initCmd := initcmd.NewCmdInit(f, ioStreams)
updateHelp(names, initCmd)
applyCmd := apply.ApplyCommand(f, ioStreams)
loader := manifestreader.NewManifestLoader(f)
invFactory := inventory.ClusterInventoryClientFactory{}
applyCmd := apply.ApplyCommand(f, invFactory, loader, ioStreams)
updateHelp(names, applyCmd)
previewCmd := preview.PreviewCommand(f, ioStreams)
previewCmd := preview.PreviewCommand(f, invFactory, loader, ioStreams)
updateHelp(names, previewCmd)
diffCmd := diff.NewCmdDiff(f, ioStreams)
updateHelp(names, diffCmd)
destroyCmd := destroy.DestroyCommand(f, ioStreams)
destroyCmd := destroy.DestroyCommand(f, invFactory, loader, ioStreams)
updateHelp(names, destroyCmd)
statusCmd := status.StatusCommand(f)
statusCmd := status.StatusCommand(f, invFactory, loader)
updateHelp(names, statusCmd)

cmd.AddCommand(initCmd, applyCmd, diffCmd, destroyCmd, previewCmd, statusCmd)
Expand Down
34 changes: 20 additions & 14 deletions cmd/preview/cmdpreview.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"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/cli-utils/pkg/util/factory"
)

Expand All @@ -29,11 +28,13 @@ var (
)

// GetPreviewRunner creates and returns the PreviewRunner which stores the cobra command.
func GetPreviewRunner(provider provider.Provider, loader manifestreader.ManifestLoader, ioStreams genericclioptions.IOStreams) *PreviewRunner {
func GetPreviewRunner(factory cmdutil.Factory, invFactory inventory.InventoryClientFactory,
loader manifestreader.ManifestLoader, ioStreams genericclioptions.IOStreams) *PreviewRunner {
r := &PreviewRunner{
ioStreams: ioStreams,
provider: provider,
loader: loader,
factory: factory,
invFactory: invFactory,
loader: loader,
ioStreams: ioStreams,
}
cmd := &cobra.Command{
Use: "preview (DIRECTORY | STDIN)",
Expand Down Expand Up @@ -62,19 +63,19 @@ func GetPreviewRunner(provider provider.Provider, loader manifestreader.Manifest
}

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

// PreviewRunner encapsulates data necessary to run the preview command.
type PreviewRunner struct {
Command *cobra.Command
PreProcess func(info inventory.InventoryInfo, strategy common.DryRunStrategy) (inventory.InventoryPolicy, error)
ioStreams genericclioptions.IOStreams
provider provider.Provider
factory cmdutil.Factory
invFactory inventory.InventoryClientFactory
loader manifestreader.ManifestLoader
ioStreams genericclioptions.IOStreams

serverSideOptions common.ServerSideOptions
output string
Expand Down Expand Up @@ -116,7 +117,12 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
}
}

statusPoller, err := factory.NewStatusPoller(r.provider.Factory())
statusPoller, err := factory.NewStatusPoller(r.factory)
if err != nil {
return err
}

invClient, err := r.invFactory.NewInventoryClient(r.factory)
if err != nil {
return err
}
Expand All @@ -128,7 +134,7 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
a, err := apply.NewApplier(r.provider, statusPoller)
a, err := apply.NewApplier(r.factory, invClient, statusPoller)
if err != nil {
return err
}
Expand All @@ -146,7 +152,7 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
InventoryPolicy: inventoryPolicy,
})
} else {
d, err := apply.NewDestroyer(r.provider, statusPoller)
d, err := apply.NewDestroyer(r.factory, invClient, statusPoller)
if err != nil {
return err
}
Expand Down
24 changes: 12 additions & 12 deletions cmd/status/cmdstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@ import (
"sigs.k8s.io/cli-utils/cmd/status/printers"
"sigs.k8s.io/cli-utils/pkg/apply/poller"
"sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/aggregator"
"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"
)

func GetStatusRunner(provider provider.Provider, loader manifestreader.ManifestLoader) *StatusRunner {
func GetStatusRunner(factory cmdutil.Factory, invFactory inventory.InventoryClientFactory, loader manifestreader.ManifestLoader) *StatusRunner {
r := &StatusRunner{
provider: provider,
factory: factory,
invFactory: invFactory,
loader: loader,
pollerFactoryFunc: pollerFactoryFunc,
}
Expand All @@ -48,18 +49,17 @@ func GetStatusRunner(provider provider.Provider, loader manifestreader.ManifestL
return r
}

func StatusCommand(f cmdutil.Factory) *cobra.Command {
provider := provider.NewProvider(f)
loader := manifestreader.NewManifestLoader(f)
return GetStatusRunner(provider, loader).Command
func StatusCommand(f cmdutil.Factory, invFactory inventory.InventoryClientFactory, loader manifestreader.ManifestLoader) *cobra.Command {
return GetStatusRunner(f, invFactory, loader).Command
}

// StatusRunner captures the parameters for the command and contains
// the run function.
type StatusRunner struct {
Command *cobra.Command
provider provider.Provider
loader manifestreader.ManifestLoader
Command *cobra.Command
factory cmdutil.Factory
invFactory inventory.InventoryClientFactory
loader manifestreader.ManifestLoader

period time.Duration
pollUntil string
Expand Down Expand Up @@ -92,7 +92,7 @@ func (r *StatusRunner) runE(cmd *cobra.Command, args []string) error {
return err
}

invClient, err := r.provider.InventoryClient()
invClient, err := r.invFactory.NewInventoryClient(r.factory)
if err != nil {
return err
}
Expand All @@ -110,7 +110,7 @@ func (r *StatusRunner) runE(cmd *cobra.Command, args []string) error {
return nil
}

statusPoller, err := r.pollerFactoryFunc(r.provider.Factory())
statusPoller, err := r.pollerFactoryFunc(r.factory)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/status/cmdstatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/apply/poller"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling"
pollevent "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/object"
"sigs.k8s.io/cli-utils/pkg/provider"
)

var (
Expand Down Expand Up @@ -222,11 +222,11 @@ deployment.apps/foo is InProgress: inProgress
tf := cmdtesting.NewTestFactory().WithNamespace("namespace")
defer tf.Cleanup()

provider := provider.NewFakeProvider(tf, tc.inventory)
loader := manifestreader.NewFakeLoader(tf, tc.inventory)
runner := &StatusRunner{
provider: provider,
loader: loader,
factory: tf,
invFactory: inventory.FakeInventoryClientFactory(tc.inventory),
loader: loader,
pollerFactoryFunc: func(c cmdutil.Factory) (poller.Poller, error) {
return &fakePoller{tc.events}, nil
},
Expand Down
19 changes: 4 additions & 15 deletions pkg/apply/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
"k8s.io/kubectl/pkg/cmd/util"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/apply/event"
"sigs.k8s.io/cli-utils/pkg/apply/filter"
"sigs.k8s.io/cli-utils/pkg/apply/info"
Expand All @@ -26,21 +26,10 @@ import (
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/object"
"sigs.k8s.io/cli-utils/pkg/ordering"
"sigs.k8s.io/cli-utils/pkg/provider"
)

// NewApplier returns a new Applier. It will set up the ApplyOptions and
// StatusOptions which are responsible for capturing any command line flags.
// It currently requires IOStreams, but this is a legacy from when
// the ApplyOptions were responsible for printing progress. This is now
// handled by a separate printer with the KubectlPrinterAdapter bridging
// between the two.
func NewApplier(provider provider.Provider, statusPoller poller.Poller) (*Applier, error) {
invClient, err := provider.InventoryClient()
if err != nil {
return nil, err
}
factory := provider.Factory()
// NewApplier returns a new Applier.
func NewApplier(factory cmdutil.Factory, invClient inventory.InventoryClient, statusPoller poller.Poller) (*Applier, error) {
pruneOpts, err := prune.NewPruneOptions(factory, invClient)
if err != nil {
return nil, err
Expand All @@ -67,7 +56,7 @@ func NewApplier(provider provider.Provider, statusPoller poller.Poller) (*Applie
type Applier struct {
pruneOptions *prune.PruneOptions
statusPoller poller.Poller
factory util.Factory
factory cmdutil.Factory
invClient inventory.InventoryClient
infoHelper info.InfoHelper
}
Expand Down
Loading

0 comments on commit c1c7c9e

Please sign in to comment.