Remove Provider interface

This commit is contained in:
Mikhail Mazurskiy 2021-06-24 22:49:41 +10:00
parent 3a81895d9d
commit c1c7c9ef9c
No known key found for this signature in database
GPG Key ID: FA7917C48932DD55
15 changed files with 145 additions and 206 deletions

View File

@ -19,15 +19,16 @@ import (
"sigs.k8s.io/cli-utils/pkg/common" "sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/inventory" "sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/manifestreader" "sigs.k8s.io/cli-utils/pkg/manifestreader"
"sigs.k8s.io/cli-utils/pkg/provider"
"sigs.k8s.io/cli-utils/pkg/util/factory" "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{ r := &ApplyRunner{
ioStreams: ioStreams, ioStreams: ioStreams,
provider: provider, factory: factory,
loader: loader, invFactory: invFactory,
loader: loader,
} }
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "apply (DIRECTORY | STDIN)", Use: "apply (DIRECTORY | STDIN)",
@ -63,17 +64,17 @@ func GetApplyRunner(provider provider.Provider, loader manifestreader.ManifestLo
return r return r
} }
func ApplyCommand(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command { func ApplyCommand(f cmdutil.Factory, invFactory inventory.InventoryClientFactory, loader manifestreader.ManifestLoader,
provider := provider.NewProvider(f) ioStreams genericclioptions.IOStreams) *cobra.Command {
loader := manifestreader.NewManifestLoader(f) return GetApplyRunner(f, invFactory, loader, ioStreams).Command
return GetApplyRunner(provider, loader, ioStreams).Command
} }
type ApplyRunner struct { type ApplyRunner struct {
Command *cobra.Command Command *cobra.Command
PreProcess func(info inventory.InventoryInfo, strategy common.DryRunStrategy) (inventory.InventoryPolicy, error) PreProcess func(info inventory.InventoryInfo, strategy common.DryRunStrategy) (inventory.InventoryPolicy, error)
ioStreams genericclioptions.IOStreams ioStreams genericclioptions.IOStreams
provider provider.Provider factory cmdutil.Factory
invFactory inventory.InventoryClientFactory
loader manifestreader.ManifestLoader loader manifestreader.ManifestLoader
serverSideOptions common.ServerSideOptions serverSideOptions common.ServerSideOptions
@ -131,15 +132,18 @@ func (r *ApplyRunner) RunE(cmd *cobra.Command, args []string) error {
return err return err
} }
} }
f := r.provider.Factory() statusPoller, err := factory.NewStatusPoller(r.factory)
statusPoller, err := factory.NewStatusPoller(f) if err != nil {
return err
}
invClient, err := r.invFactory.NewInventoryClient(r.factory)
if err != nil { if err != nil {
return err return err
} }
// Run the applier. It will return a channel where we can receive updates // Run the applier. It will return a channel where we can receive updates
// to keep track of progress and any issues. // 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 { if err != nil {
return err return err
} }

View File

@ -18,16 +18,17 @@ import (
"sigs.k8s.io/cli-utils/pkg/common" "sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/inventory" "sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/manifestreader" "sigs.k8s.io/cli-utils/pkg/manifestreader"
"sigs.k8s.io/cli-utils/pkg/provider"
"sigs.k8s.io/cli-utils/pkg/util/factory" "sigs.k8s.io/cli-utils/pkg/util/factory"
) )
// GetDestroyRunner creates and returns the DestroyRunner which stores the cobra command. // 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{ r := &DestroyRunner{
ioStreams: ioStreams, ioStreams: ioStreams,
provider: provider, factory: factory,
loader: loader, invFactory: invFactory,
loader: loader,
} }
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "destroy (DIRECTORY | STDIN)", Use: "destroy (DIRECTORY | STDIN)",
@ -51,10 +52,9 @@ func GetDestroyRunner(provider provider.Provider, loader manifestreader.Manifest
} }
// DestroyCommand creates the DestroyRunner, returning the cobra command associated with it. // DestroyCommand creates the DestroyRunner, returning the cobra command associated with it.
func DestroyCommand(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command { func DestroyCommand(f cmdutil.Factory, invFactory inventory.InventoryClientFactory, loader manifestreader.ManifestLoader,
provider := provider.NewProvider(f) ioStreams genericclioptions.IOStreams) *cobra.Command {
loader := manifestreader.NewManifestLoader(f) return GetDestroyRunner(f, invFactory, loader, ioStreams).Command
return GetDestroyRunner(provider, loader, ioStreams).Command
} }
// DestroyRunner encapsulates data necessary to run the destroy command. // DestroyRunner encapsulates data necessary to run the destroy command.
@ -62,7 +62,8 @@ type DestroyRunner struct {
Command *cobra.Command Command *cobra.Command
PreProcess func(info inventory.InventoryInfo, strategy common.DryRunStrategy) (inventory.InventoryPolicy, error) PreProcess func(info inventory.InventoryInfo, strategy common.DryRunStrategy) (inventory.InventoryPolicy, error)
ioStreams genericclioptions.IOStreams ioStreams genericclioptions.IOStreams
provider provider.Provider factory cmdutil.Factory
invFactory inventory.InventoryClientFactory
loader manifestreader.ManifestLoader loader manifestreader.ManifestLoader
output string output string
@ -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 { if err != nil {
return err 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 { if err != nil {
return err return err
} }

View File

@ -19,6 +19,8 @@ import (
"sigs.k8s.io/cli-utils/cmd/preview" "sigs.k8s.io/cli-utils/cmd/preview"
"sigs.k8s.io/cli-utils/cmd/status" "sigs.k8s.io/cli-utils/cmd/status"
"sigs.k8s.io/cli-utils/pkg/errors" "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" "sigs.k8s.io/cli-utils/pkg/util/factory"
// This is here rather than in the libraries because of // This is here rather than in the libraries because of
@ -57,15 +59,17 @@ func main() {
names := []string{"init", "apply", "preview", "diff", "destroy", "status"} names := []string{"init", "apply", "preview", "diff", "destroy", "status"}
initCmd := initcmd.NewCmdInit(f, ioStreams) initCmd := initcmd.NewCmdInit(f, ioStreams)
updateHelp(names, initCmd) 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) updateHelp(names, applyCmd)
previewCmd := preview.PreviewCommand(f, ioStreams) previewCmd := preview.PreviewCommand(f, invFactory, loader, ioStreams)
updateHelp(names, previewCmd) updateHelp(names, previewCmd)
diffCmd := diff.NewCmdDiff(f, ioStreams) diffCmd := diff.NewCmdDiff(f, ioStreams)
updateHelp(names, diffCmd) updateHelp(names, diffCmd)
destroyCmd := destroy.DestroyCommand(f, ioStreams) destroyCmd := destroy.DestroyCommand(f, invFactory, loader, ioStreams)
updateHelp(names, destroyCmd) updateHelp(names, destroyCmd)
statusCmd := status.StatusCommand(f) statusCmd := status.StatusCommand(f, invFactory, loader)
updateHelp(names, statusCmd) updateHelp(names, statusCmd)
cmd.AddCommand(initCmd, applyCmd, diffCmd, destroyCmd, previewCmd, statusCmd) cmd.AddCommand(initCmd, applyCmd, diffCmd, destroyCmd, previewCmd, statusCmd)

View File

@ -19,7 +19,6 @@ import (
"sigs.k8s.io/cli-utils/pkg/common" "sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/inventory" "sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/manifestreader" "sigs.k8s.io/cli-utils/pkg/manifestreader"
"sigs.k8s.io/cli-utils/pkg/provider"
"sigs.k8s.io/cli-utils/pkg/util/factory" "sigs.k8s.io/cli-utils/pkg/util/factory"
) )
@ -29,11 +28,13 @@ var (
) )
// GetPreviewRunner creates and returns the PreviewRunner which stores the cobra command. // 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{ r := &PreviewRunner{
ioStreams: ioStreams, factory: factory,
provider: provider, invFactory: invFactory,
loader: loader, loader: loader,
ioStreams: ioStreams,
} }
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "preview (DIRECTORY | STDIN)", Use: "preview (DIRECTORY | STDIN)",
@ -62,19 +63,19 @@ func GetPreviewRunner(provider provider.Provider, loader manifestreader.Manifest
} }
// PreviewCommand creates the PreviewRunner, returning the cobra command associated with it. // PreviewCommand creates the PreviewRunner, returning the cobra command associated with it.
func PreviewCommand(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command { func PreviewCommand(f cmdutil.Factory, invFactory inventory.InventoryClientFactory, loader manifestreader.ManifestLoader,
provider := provider.NewProvider(f) ioStreams genericclioptions.IOStreams) *cobra.Command {
loader := manifestreader.NewManifestLoader(f) return GetPreviewRunner(f, invFactory, loader, ioStreams).Command
return GetPreviewRunner(provider, loader, ioStreams).Command
} }
// PreviewRunner encapsulates data necessary to run the preview command. // PreviewRunner encapsulates data necessary to run the preview command.
type PreviewRunner struct { type PreviewRunner struct {
Command *cobra.Command Command *cobra.Command
PreProcess func(info inventory.InventoryInfo, strategy common.DryRunStrategy) (inventory.InventoryPolicy, error) PreProcess func(info inventory.InventoryInfo, strategy common.DryRunStrategy) (inventory.InventoryPolicy, error)
ioStreams genericclioptions.IOStreams factory cmdutil.Factory
provider provider.Provider invFactory inventory.InventoryClientFactory
loader manifestreader.ManifestLoader loader manifestreader.ManifestLoader
ioStreams genericclioptions.IOStreams
serverSideOptions common.ServerSideOptions serverSideOptions common.ServerSideOptions
output string output string
@ -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 { if err != nil {
return err return err
} }
@ -128,7 +134,7 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
if err != nil { if err != nil {
return err return err
} }
a, err := apply.NewApplier(r.provider, statusPoller) a, err := apply.NewApplier(r.factory, invClient, statusPoller)
if err != nil { if err != nil {
return err return err
} }
@ -146,7 +152,7 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
InventoryPolicy: inventoryPolicy, InventoryPolicy: inventoryPolicy,
}) })
} else { } else {
d, err := apply.NewDestroyer(r.provider, statusPoller) d, err := apply.NewDestroyer(r.factory, invClient, statusPoller)
if err != nil { if err != nil {
return err return err
} }

View File

@ -16,19 +16,20 @@ import (
"sigs.k8s.io/cli-utils/cmd/status/printers" "sigs.k8s.io/cli-utils/cmd/status/printers"
"sigs.k8s.io/cli-utils/pkg/apply/poller" "sigs.k8s.io/cli-utils/pkg/apply/poller"
"sigs.k8s.io/cli-utils/pkg/common" "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"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/aggregator" "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/collector"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/event" "sigs.k8s.io/cli-utils/pkg/kstatus/polling/event"
"sigs.k8s.io/cli-utils/pkg/kstatus/status" "sigs.k8s.io/cli-utils/pkg/kstatus/status"
"sigs.k8s.io/cli-utils/pkg/manifestreader" "sigs.k8s.io/cli-utils/pkg/manifestreader"
"sigs.k8s.io/cli-utils/pkg/provider"
"sigs.k8s.io/cli-utils/pkg/util/factory" "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{ r := &StatusRunner{
provider: provider, factory: factory,
invFactory: invFactory,
loader: loader, loader: loader,
pollerFactoryFunc: pollerFactoryFunc, pollerFactoryFunc: pollerFactoryFunc,
} }
@ -48,18 +49,17 @@ func GetStatusRunner(provider provider.Provider, loader manifestreader.ManifestL
return r return r
} }
func StatusCommand(f cmdutil.Factory) *cobra.Command { func StatusCommand(f cmdutil.Factory, invFactory inventory.InventoryClientFactory, loader manifestreader.ManifestLoader) *cobra.Command {
provider := provider.NewProvider(f) return GetStatusRunner(f, invFactory, loader).Command
loader := manifestreader.NewManifestLoader(f)
return GetStatusRunner(provider, loader).Command
} }
// StatusRunner captures the parameters for the command and contains // StatusRunner captures the parameters for the command and contains
// the run function. // the run function.
type StatusRunner struct { type StatusRunner struct {
Command *cobra.Command Command *cobra.Command
provider provider.Provider factory cmdutil.Factory
loader manifestreader.ManifestLoader invFactory inventory.InventoryClientFactory
loader manifestreader.ManifestLoader
period time.Duration period time.Duration
pollUntil string pollUntil string
@ -92,7 +92,7 @@ func (r *StatusRunner) runE(cmd *cobra.Command, args []string) error {
return err return err
} }
invClient, err := r.provider.InventoryClient() invClient, err := r.invFactory.NewInventoryClient(r.factory)
if err != nil { if err != nil {
return err return err
} }
@ -110,7 +110,7 @@ func (r *StatusRunner) runE(cmd *cobra.Command, args []string) error {
return nil return nil
} }
statusPoller, err := r.pollerFactoryFunc(r.provider.Factory()) statusPoller, err := r.pollerFactoryFunc(r.factory)
if err != nil { if err != nil {
return err return err
} }

View File

@ -16,12 +16,12 @@ import (
cmdtesting "k8s.io/kubectl/pkg/cmd/testing" cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
cmdutil "k8s.io/kubectl/pkg/cmd/util" cmdutil "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/apply/poller" "sigs.k8s.io/cli-utils/pkg/apply/poller"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling" "sigs.k8s.io/cli-utils/pkg/kstatus/polling"
pollevent "sigs.k8s.io/cli-utils/pkg/kstatus/polling/event" pollevent "sigs.k8s.io/cli-utils/pkg/kstatus/polling/event"
"sigs.k8s.io/cli-utils/pkg/kstatus/status" "sigs.k8s.io/cli-utils/pkg/kstatus/status"
"sigs.k8s.io/cli-utils/pkg/manifestreader" "sigs.k8s.io/cli-utils/pkg/manifestreader"
"sigs.k8s.io/cli-utils/pkg/object" "sigs.k8s.io/cli-utils/pkg/object"
"sigs.k8s.io/cli-utils/pkg/provider"
) )
var ( var (
@ -222,11 +222,11 @@ deployment.apps/foo is InProgress: inProgress
tf := cmdtesting.NewTestFactory().WithNamespace("namespace") tf := cmdtesting.NewTestFactory().WithNamespace("namespace")
defer tf.Cleanup() defer tf.Cleanup()
provider := provider.NewFakeProvider(tf, tc.inventory)
loader := manifestreader.NewFakeLoader(tf, tc.inventory) loader := manifestreader.NewFakeLoader(tf, tc.inventory)
runner := &StatusRunner{ runner := &StatusRunner{
provider: provider, factory: tf,
loader: loader, invFactory: inventory.FakeInventoryClientFactory(tc.inventory),
loader: loader,
pollerFactoryFunc: func(c cmdutil.Factory) (poller.Poller, error) { pollerFactoryFunc: func(c cmdutil.Factory) (poller.Poller, error) {
return &fakePoller{tc.events}, nil return &fakePoller{tc.events}, nil
}, },

View File

@ -14,7 +14,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2" "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/event"
"sigs.k8s.io/cli-utils/pkg/apply/filter" "sigs.k8s.io/cli-utils/pkg/apply/filter"
"sigs.k8s.io/cli-utils/pkg/apply/info" "sigs.k8s.io/cli-utils/pkg/apply/info"
@ -26,21 +26,10 @@ import (
"sigs.k8s.io/cli-utils/pkg/inventory" "sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/object" "sigs.k8s.io/cli-utils/pkg/object"
"sigs.k8s.io/cli-utils/pkg/ordering" "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 // NewApplier returns a new Applier.
// StatusOptions which are responsible for capturing any command line flags. func NewApplier(factory cmdutil.Factory, invClient inventory.InventoryClient, statusPoller poller.Poller) (*Applier, error) {
// 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()
pruneOpts, err := prune.NewPruneOptions(factory, invClient) pruneOpts, err := prune.NewPruneOptions(factory, invClient)
if err != nil { if err != nil {
return nil, err return nil, err
@ -67,7 +56,7 @@ func NewApplier(provider provider.Provider, statusPoller poller.Poller) (*Applie
type Applier struct { type Applier struct {
pruneOptions *prune.PruneOptions pruneOptions *prune.PruneOptions
statusPoller poller.Poller statusPoller poller.Poller
factory util.Factory factory cmdutil.Factory
invClient inventory.InventoryClient invClient inventory.InventoryClient
infoHelper info.InfoHelper infoHelper info.InfoHelper
} }

View File

@ -36,7 +36,6 @@ import (
pollevent "sigs.k8s.io/cli-utils/pkg/kstatus/polling/event" pollevent "sigs.k8s.io/cli-utils/pkg/kstatus/polling/event"
"sigs.k8s.io/cli-utils/pkg/kstatus/status" "sigs.k8s.io/cli-utils/pkg/kstatus/status"
"sigs.k8s.io/cli-utils/pkg/object" "sigs.k8s.io/cli-utils/pkg/object"
"sigs.k8s.io/cli-utils/pkg/provider"
"sigs.k8s.io/cli-utils/pkg/testutil" "sigs.k8s.io/cli-utils/pkg/testutil"
) )
@ -552,20 +551,22 @@ func TestApplier(t *testing.T) {
tf.UnstructuredClient = newFakeRESTClient(t, handlers) tf.UnstructuredClient = newFakeRESTClient(t, handlers)
tf.FakeDynamicClient = fakeDynamicClient(t, mapper, objs...) tf.FakeDynamicClient = fakeDynamicClient(t, mapper, objs...)
cf := provider.NewProvider(tf)
poller := &fakePoller{ poller := &fakePoller{
events: tc.statusEvents, events: tc.statusEvents,
start: make(chan struct{}), start: make(chan struct{}),
} }
applier, err := NewApplier(cf, poller) invClient, err := inventory.ClusterInventoryClientFactory{}.NewInventoryClient(tf)
require.NoError(t, err) require.NoError(t, err)
applier.infoHelper = &fakeInfoHelper{ infoHelper := &fakeInfoHelper{
factory: tf, factory: tf,
} }
// TODO(mortent): This is not great, but at least this keeps the // TODO(mortent): This is not great, but at least this keeps the
// ugliness in the test code until we can find a way to wire it // ugliness in the test code until we can find a way to wire it
// up so to avoid it. // up so to avoid it.
applier.invClient.(*inventory.ClusterInventoryClient).InfoHelper = applier.infoHelper invClient.(*inventory.ClusterInventoryClient).InfoHelper = infoHelper
applier, err := NewApplier(tf, invClient, poller)
require.NoError(t, err)
applier.infoHelper = infoHelper
ctx := context.Background() ctx := context.Background()
eventChannel := applier.Run(ctx, tc.invInfo.toWrapped(), tc.resources, Options{ eventChannel := applier.Run(ctx, tc.invInfo.toWrapped(), tc.resources, Options{
ReconcileTimeout: tc.reconcileTimeout, ReconcileTimeout: tc.reconcileTimeout,

View File

@ -11,7 +11,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/klog/v2" "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/event"
"sigs.k8s.io/cli-utils/pkg/apply/filter" "sigs.k8s.io/cli-utils/pkg/apply/filter"
"sigs.k8s.io/cli-utils/pkg/apply/poller" "sigs.k8s.io/cli-utils/pkg/apply/poller"
@ -21,7 +21,6 @@ import (
"sigs.k8s.io/cli-utils/pkg/common" "sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/inventory" "sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/object" "sigs.k8s.io/cli-utils/pkg/object"
"sigs.k8s.io/cli-utils/pkg/provider"
) )
// NewDestroyer returns a new destroyer. It will set up the ApplyOptions and // NewDestroyer returns a new destroyer. It will set up the ApplyOptions and
@ -30,12 +29,7 @@ import (
// the ApplyOptions were responsible for printing progress. This is now // the ApplyOptions were responsible for printing progress. This is now
// handled by a separate printer with the KubectlPrinterAdapter bridging // handled by a separate printer with the KubectlPrinterAdapter bridging
// between the two. // between the two.
func NewDestroyer(provider provider.Provider, statusPoller poller.Poller) (*Destroyer, error) { func NewDestroyer(factory cmdutil.Factory, invClient inventory.InventoryClient, statusPoller poller.Poller) (*Destroyer, error) {
invClient, err := provider.InventoryClient()
if err != nil {
return nil, errors.WrapPrefix(err, "error creating inventory client", 1)
}
factory := provider.Factory()
pruneOpts, err := prune.NewPruneOptions(factory, invClient) pruneOpts, err := prune.NewPruneOptions(factory, invClient)
if err != nil { if err != nil {
return nil, errors.WrapPrefix(err, "error setting up PruneOptions", 1) return nil, errors.WrapPrefix(err, "error setting up PruneOptions", 1)
@ -53,7 +47,7 @@ func NewDestroyer(provider provider.Provider, statusPoller poller.Poller) (*Dest
type Destroyer struct { type Destroyer struct {
pruneOptions *prune.PruneOptions pruneOptions *prune.PruneOptions
statusPoller poller.Poller statusPoller poller.Poller
factory util.Factory factory cmdutil.Factory
invClient inventory.InventoryClient invClient inventory.InventoryClient
} }

View File

@ -5,6 +5,7 @@ package inventory
import ( import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/common" "sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/object" "sigs.k8s.io/cli-utils/pkg/object"
) )
@ -15,7 +16,16 @@ type FakeInventoryClient struct {
Err error Err error
} }
var _ InventoryClient = &FakeInventoryClient{} var (
_ InventoryClient = &FakeInventoryClient{}
_ InventoryClientFactory = FakeInventoryClientFactory{}
)
type FakeInventoryClientFactory []object.ObjMetadata
func (f FakeInventoryClientFactory) NewInventoryClient(factory cmdutil.Factory) (InventoryClient, error) {
return NewFakeInventoryClient(f), nil
}
// NewFakeInventoryClient returns a FakeInventoryClient. // NewFakeInventoryClient returns a FakeInventoryClient.
func NewFakeInventoryClient(initObjs []object.ObjMetadata) *FakeInventoryClient { func NewFakeInventoryClient(initObjs []object.ObjMetadata) *FakeInventoryClient {

View File

@ -0,0 +1,23 @@
// Copyright 2020 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package inventory
import cmdutil "k8s.io/kubectl/pkg/cmd/util"
var (
_ InventoryClientFactory = ClusterInventoryClientFactory{}
)
// InventoryClientFactory is a factory that constructs new InventoryClient instances.
type InventoryClientFactory interface {
NewInventoryClient(factory cmdutil.Factory) (InventoryClient, error)
}
// ClusterInventoryClientFactory is a factory that creates instances of ClusterInventoryClient inventory client.
type ClusterInventoryClientFactory struct {
}
func (ClusterInventoryClientFactory) NewInventoryClient(factory cmdutil.Factory) (InventoryClient, error) {
return NewInventoryClient(factory, WrapInventoryObj, InvInfoToConfigMap)
}

View File

@ -1,37 +0,0 @@
// Copyright 2020 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package provider
import (
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/object"
)
type FakeProvider struct {
factory util.Factory
InvClient *inventory.FakeInventoryClient
}
var _ Provider = &FakeProvider{}
func NewFakeProvider(f util.Factory, objs []object.ObjMetadata) *FakeProvider {
return &FakeProvider{
factory: f,
InvClient: inventory.NewFakeInventoryClient(objs),
}
}
func (f *FakeProvider) Factory() util.Factory {
return f.factory
}
func (f *FakeProvider) InventoryClient() (inventory.InventoryClient, error) {
return f.InvClient, nil
}
func (f *FakeProvider) ToRESTMapper() (meta.RESTMapper, error) {
return f.factory.ToRESTMapper()
}

View File

@ -1,45 +0,0 @@
// Copyright 2020 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package provider
import (
"k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/inventory"
)
// InventoryProvider implements the Provider interface.
var _ Provider = &InventoryProvider{}
// Provider is an interface which wraps the kubectl factory and
// the inventory client.
type Provider interface {
Factory() util.Factory
InventoryClient() (inventory.InventoryClient, error)
}
// InventoryProvider implements the Provider interface.
type InventoryProvider struct {
factory util.Factory
}
// NewProvider returns a Provider that implements a ConfigMap inventory object.
func NewProvider(f util.Factory) *InventoryProvider {
return &InventoryProvider{
factory: f,
}
}
// Factory returns the kubectl factory.
func (f *InventoryProvider) Factory() util.Factory {
return f.factory
}
// InventoryClient returns an InventoryClient created with the stored
// factory and InventoryFactoryFunc values, or an error if one occurred.
func (f *InventoryProvider) InventoryClient() (inventory.InventoryClient, error) {
return inventory.NewInventoryClient(f.factory,
inventory.WrapInventoryObj,
inventory.InvInfoToConfigMap,
)
}

View File

@ -12,7 +12,6 @@ import (
"sigs.k8s.io/cli-utils/pkg/common" "sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/inventory" "sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/object" "sigs.k8s.io/cli-utils/pkg/object"
"sigs.k8s.io/cli-utils/pkg/provider"
) )
var InventoryCRD = []byte(strings.TrimSpace(` var InventoryCRD = []byte(strings.TrimSpace(`
@ -75,24 +74,13 @@ var InventoryGVK = schema.GroupVersionKind{
Kind: "Inventory", Kind: "Inventory",
} }
var _ provider.Provider = &CustomProvider{} var _ inventory.InventoryClientFactory = CustomInventoryClientFactory{}
func NewCustomProvider(f util.Factory) provider.Provider { type CustomInventoryClientFactory struct {
return &CustomProvider{
factory: f,
}
} }
type CustomProvider struct { func (CustomInventoryClientFactory) NewInventoryClient(factory util.Factory) (inventory.InventoryClient, error) {
factory util.Factory return inventory.NewInventoryClient(factory, WrapInventoryObj, invToUnstructuredFunc)
}
func (c *CustomProvider) Factory() util.Factory {
return c.factory
}
func (c *CustomProvider) InventoryClient() (inventory.InventoryClient, error) {
return inventory.NewInventoryClient(c.factory, WrapInventoryObj, invToUnstructuredFunc)
} }
func invToUnstructuredFunc(inv inventory.InventoryInfo) *unstructured.Unstructured { func invToUnstructuredFunc(inv inventory.InventoryInfo) *unstructured.Unstructured {

View File

@ -20,7 +20,6 @@ import (
"sigs.k8s.io/cli-utils/pkg/apply" "sigs.k8s.io/cli-utils/pkg/apply"
"sigs.k8s.io/cli-utils/pkg/common" "sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/inventory" "sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/provider"
"sigs.k8s.io/cli-utils/pkg/util/factory" "sigs.k8s.io/cli-utils/pkg/util/factory"
"sigs.k8s.io/cli-utils/test/e2e/customprovider" "sigs.k8s.io/cli-utils/test/e2e/customprovider"
ctrl "sigs.k8s.io/controller-runtime" ctrl "sigs.k8s.io/controller-runtime"
@ -205,15 +204,11 @@ func deleteNamespace(c client.Client, namespace *v1.Namespace) {
} }
func newDefaultInvApplier() *apply.Applier { func newDefaultInvApplier() *apply.Applier {
return newApplierFromProvider(newDefaultInvProvider()) return newApplierFromInvFactory(inventory.ClusterInventoryClientFactory{})
} }
func newDefaultInvDestroyer() *apply.Destroyer { func newDefaultInvDestroyer() *apply.Destroyer {
return newDestroyerFromProvider(newDefaultInvProvider()) return newDestroyerFromInvFactory(inventory.ClusterInventoryClientFactory{})
}
func newDefaultInvProvider() provider.Provider {
return provider.NewProvider(newFactory())
} }
func defaultInvSizeVerifyFunc(c client.Client, name, namespace, id string, count int) { func defaultInvSizeVerifyFunc(c client.Client, name, namespace, id string, count int) {
@ -239,15 +234,11 @@ func defaultInvCountVerifyFunc(c client.Client, namespace string, count int) {
} }
func newCustomInvApplier() *apply.Applier { func newCustomInvApplier() *apply.Applier {
return newApplierFromProvider(newCustomInvProvider()) return newApplierFromInvFactory(customprovider.CustomInventoryClientFactory{})
} }
func newCustomInvDestroyer() *apply.Destroyer { func newCustomInvDestroyer() *apply.Destroyer {
return newDestroyerFromProvider(newCustomInvProvider()) return newDestroyerFromInvFactory(customprovider.CustomInventoryClientFactory{})
}
func newCustomInvProvider() provider.Provider {
return customprovider.NewCustomProvider(newFactory())
} }
func newFactory() util.Factory { func newFactory() util.Factory {
@ -286,20 +277,26 @@ func customInvCountVerifyFunc(c client.Client, namespace string, count int) {
Expect(len(u.Items)).To(Equal(count)) Expect(len(u.Items)).To(Equal(count))
} }
func newApplierFromProvider(prov provider.Provider) *apply.Applier { func newApplierFromInvFactory(invFactory inventory.InventoryClientFactory) *apply.Applier {
statusPoller, err := factory.NewStatusPoller(prov.Factory()) f := newFactory()
invClient, err := invFactory.NewInventoryClient(f)
Expect(err).NotTo(HaveOccurred())
statusPoller, err := factory.NewStatusPoller(f)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
a, err := apply.NewApplier(prov, statusPoller) a, err := apply.NewApplier(f, invClient, statusPoller)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
return a return a
} }
func newDestroyerFromProvider(prov provider.Provider) *apply.Destroyer { func newDestroyerFromInvFactory(invFactory inventory.InventoryClientFactory) *apply.Destroyer {
statusPoller, err := factory.NewStatusPoller(prov.Factory()) f := newFactory()
invClient, err := invFactory.NewInventoryClient(f)
Expect(err).NotTo(HaveOccurred())
statusPoller, err := factory.NewStatusPoller(f)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
d, err := apply.NewDestroyer(prov, statusPoller) d, err := apply.NewDestroyer(f, invClient, statusPoller)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
return d return d
} }