Merge pull request #604 from chunglu-chou/loader

add Loader interface for easier customized implementation
This commit is contained in:
Kubernetes Prow Robot 2022-08-18 14:12:09 -07:00 committed by GitHub
commit eb8ebd7363
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 20 deletions

View File

@ -30,7 +30,7 @@ func GetRunner(factory cmdutil.Factory, invFactory inventory.ClientFactory, load
r := &Runner{
factory: factory,
invFactory: invFactory,
loader: loader,
loader: NewInventoryLoader(loader),
pollerFactoryFunc: pollerFactoryFunc,
}
c := &cobra.Command{
@ -59,7 +59,7 @@ type Runner struct {
Command *cobra.Command
factory cmdutil.Factory
invFactory inventory.ClientFactory
loader manifestreader.ManifestLoader
loader Loader
period time.Duration
pollUntil string
@ -73,26 +73,11 @@ type Runner struct {
// poller to compute status for each of the resources. One of the printer
// implementations takes care of printing the output.
func (r *Runner) runE(cmd *cobra.Command, args []string) error {
_, err := common.DemandOneDirectory(args)
inv, err := r.loader.GetInvInfo(cmd, args)
if err != nil {
return err
}
reader, err := r.loader.ManifestReader(cmd.InOrStdin(), flagutils.PathFromArgs(args))
if err != nil {
return err
}
objs, err := reader.Read()
if err != nil {
return err
}
invObj, _, err := inventory.SplitUnstructureds(objs)
if err != nil {
return err
}
inv := inventory.WrapInventoryInfoObj(invObj)
invClient, err := r.invFactory.NewClient(r.factory)
if err != nil {
return err
@ -207,3 +192,40 @@ func allKnownNotifierFunc(cancelFunc context.CancelFunc) collector.ObserverFunc
func pollerFactoryFunc(f cmdutil.Factory) (poller.Poller, error) {
return polling.NewStatusPollerFromFactory(f, polling.Options{})
}
type Loader interface {
GetInvInfo(cmd *cobra.Command, args []string) (inventory.Info, error)
}
type InventoryLoader struct {
Loader manifestreader.ManifestLoader
}
func NewInventoryLoader(loader manifestreader.ManifestLoader) *InventoryLoader {
return &InventoryLoader{
Loader: loader,
}
}
func (ir *InventoryLoader) GetInvInfo(cmd *cobra.Command, args []string) (inventory.Info, error) {
_, err := common.DemandOneDirectory(args)
if err != nil {
return nil, err
}
reader, err := ir.Loader.ManifestReader(cmd.InOrStdin(), flagutils.PathFromArgs(args))
if err != nil {
return nil, err
}
objs, err := reader.Read()
if err != nil {
return nil, err
}
invObj, _, err := inventory.SplitUnstructureds(objs)
if err != nil {
return nil, err
}
inv := inventory.WrapInventoryInfoObj(invObj)
return inv, nil
}

View File

@ -499,7 +499,7 @@ foo/deployment.apps/default/foo is InProgress: inProgress
runner := &Runner{
factory: tf,
invFactory: inventory.FakeClientFactory(tc.inventory),
loader: loader,
loader: NewInventoryLoader(loader),
pollerFactoryFunc: func(c cmdutil.Factory) (poller.Poller, error) {
return &fakePoller{tc.events}, nil
},
@ -541,7 +541,7 @@ foo/deployment.apps/default/foo is InProgress: inProgress
runner := &Runner{
factory: tf,
invFactory: inventory.FakeClientFactory(tc.inventory),
loader: loader,
loader: NewInventoryLoader(loader),
pollerFactoryFunc: func(c cmdutil.Factory) (poller.Poller, error) {
return &fakePoller{tc.events}, nil
},