Skip to content

Commit

Permalink
Merge pull request #604 from chunglu-chou/loader
Browse files Browse the repository at this point in the history
add Loader interface for easier customized implementation
  • Loading branch information
k8s-ci-robot authored Aug 18, 2022
2 parents 3fe57dd + 729997b commit eb8ebd7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
58 changes: 40 additions & 18 deletions cmd/status/cmdstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand All @@ -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)
if err != nil {
return err
}

reader, err := r.loader.ManifestReader(cmd.InOrStdin(), flagutils.PathFromArgs(args))
if err != nil {
return err
}
objs, err := reader.Read()
inv, err := r.loader.GetInvInfo(cmd, args)
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
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions cmd/status/cmdstatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down Expand Up @@ -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
},
Expand Down

0 comments on commit eb8ebd7

Please sign in to comment.