From f33a0cdb322d3c780aad6406fd30f6e0fc23df99 Mon Sep 17 00:00:00 2001 From: Morten Torkildsen Date: Thu, 16 Dec 2021 19:22:59 +0100 Subject: [PATCH] Remove StatusPoller factory and support custom status readers --- cmd/status/cmdstatus.go | 4 ++-- pkg/apply/applier.go | 5 +++-- pkg/apply/destroyer.go | 5 +++-- pkg/kstatus/polling/polling.go | 24 ++++++++++++++++++++++ pkg/util/factory/statuspoller.go | 35 -------------------------------- 5 files changed, 32 insertions(+), 41 deletions(-) delete mode 100644 pkg/util/factory/statuspoller.go diff --git a/cmd/status/cmdstatus.go b/cmd/status/cmdstatus.go index c373e38e..b37ea877 100644 --- a/cmd/status/cmdstatus.go +++ b/cmd/status/cmdstatus.go @@ -19,10 +19,10 @@ import ( "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/engine" "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/util/factory" ) func GetStatusRunner(factory cmdutil.Factory, invFactory inventory.InventoryClientFactory, loader manifestreader.ManifestLoader) *StatusRunner { @@ -193,5 +193,5 @@ func allKnownNotifierFunc(cancelFunc context.CancelFunc) collector.ObserverFunc } func pollerFactoryFunc(f cmdutil.Factory) (poller.Poller, error) { - return factory.NewStatusPoller(f) + return polling.NewStatusPollerFromFactory(f, []engine.StatusReader{}) } diff --git a/pkg/apply/applier.go b/pkg/apply/applier.go index 0708c8d3..b4870e8d 100644 --- a/pkg/apply/applier.go +++ b/pkg/apply/applier.go @@ -24,9 +24,10 @@ import ( "sigs.k8s.io/cli-utils/pkg/apply/taskrunner" "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/engine" "sigs.k8s.io/cli-utils/pkg/object" "sigs.k8s.io/cli-utils/pkg/ordering" - statusfactory "sigs.k8s.io/cli-utils/pkg/util/factory" ) // NewApplier returns a new Applier. @@ -35,7 +36,7 @@ func NewApplier(factory cmdutil.Factory, invClient inventory.InventoryClient) (* if err != nil { return nil, err } - statusPoller, err := statusfactory.NewStatusPoller(factory) + statusPoller, err := polling.NewStatusPollerFromFactory(factory, []engine.StatusReader{}) if err != nil { return nil, err } diff --git a/pkg/apply/destroyer.go b/pkg/apply/destroyer.go index 93b63610..5d73a66b 100644 --- a/pkg/apply/destroyer.go +++ b/pkg/apply/destroyer.go @@ -20,8 +20,9 @@ import ( "sigs.k8s.io/cli-utils/pkg/apply/taskrunner" "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/engine" "sigs.k8s.io/cli-utils/pkg/object" - statusfactory "sigs.k8s.io/cli-utils/pkg/util/factory" ) // NewDestroyer returns a new destroyer. It will set up the ApplyOptions and @@ -35,7 +36,7 @@ func NewDestroyer(factory cmdutil.Factory, invClient inventory.InventoryClient) if err != nil { return nil, fmt.Errorf("error setting up PruneOptions: %w", err) } - statusPoller, err := statusfactory.NewStatusPoller(factory) + statusPoller, err := polling.NewStatusPollerFromFactory(factory, []engine.StatusReader{}) if err != nil { return nil, err } diff --git a/pkg/kstatus/polling/polling.go b/pkg/kstatus/polling/polling.go index f9754b28..bf2efb1d 100644 --- a/pkg/kstatus/polling/polling.go +++ b/pkg/kstatus/polling/polling.go @@ -5,9 +5,12 @@ package polling import ( "context" + "fmt" "time" "k8s.io/apimachinery/pkg/api/meta" + cmdutil "k8s.io/kubectl/pkg/cmd/util" + "k8s.io/kubectl/pkg/scheme" "sigs.k8s.io/cli-utils/pkg/kstatus/polling/clusterreader" "sigs.k8s.io/cli-utils/pkg/kstatus/polling/engine" "sigs.k8s.io/cli-utils/pkg/kstatus/polling/event" @@ -37,6 +40,27 @@ func NewStatusPoller(reader client.Reader, mapper meta.RESTMapper, customStatusR } } +// NewStatusPollerFromFactory creates a new StatusPoller instance from the +// passed in factory. +func NewStatusPollerFromFactory(f cmdutil.Factory, statusReaders []engine.StatusReader) (*StatusPoller, error) { + config, err := f.ToRESTConfig() + if err != nil { + return nil, fmt.Errorf("error getting RESTConfig: %w", err) + } + + mapper, err := f.ToRESTMapper() + if err != nil { + return nil, fmt.Errorf("error getting RESTMapper: %w", err) + } + + c, err := client.New(config, client.Options{Scheme: scheme.Scheme, Mapper: mapper}) + if err != nil { + return nil, fmt.Errorf("error creating client: %w", err) + } + + return NewStatusPoller(c, mapper, statusReaders), nil +} + // StatusPoller provides functionality for polling a cluster for status for a set of resources. type StatusPoller struct { engine *engine.PollerEngine diff --git a/pkg/util/factory/statuspoller.go b/pkg/util/factory/statuspoller.go deleted file mode 100644 index ac0763ac..00000000 --- a/pkg/util/factory/statuspoller.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2020 The Kubernetes Authors. -// SPDX-License-Identifier: Apache-2.0 - -package factory - -import ( - "fmt" - - cmdutil "k8s.io/kubectl/pkg/cmd/util" - "k8s.io/kubectl/pkg/scheme" - "sigs.k8s.io/cli-utils/pkg/kstatus/polling" - "sigs.k8s.io/cli-utils/pkg/kstatus/polling/engine" - "sigs.k8s.io/controller-runtime/pkg/client" -) - -// NewStatusPoller creates a new StatusPoller instance from the -// passed in factory. -func NewStatusPoller(f cmdutil.Factory) (*polling.StatusPoller, error) { - config, err := f.ToRESTConfig() - if err != nil { - return nil, fmt.Errorf("error getting RESTConfig: %w", err) - } - - mapper, err := f.ToRESTMapper() - if err != nil { - return nil, fmt.Errorf("error getting RESTMapper: %w", err) - } - - c, err := client.New(config, client.Options{Scheme: scheme.Scheme, Mapper: mapper}) - if err != nil { - return nil, fmt.Errorf("error creating client: %w", err) - } - - return polling.NewStatusPoller(c, mapper, []engine.StatusReader{}), nil -}