diff --git a/cmd/clusterctl/pkg/client/client.go b/cmd/clusterctl/pkg/client/client.go index a02d74442b7f..3b487154cea4 100644 --- a/cmd/clusterctl/pkg/client/client.go +++ b/cmd/clusterctl/pkg/client/client.go @@ -231,13 +231,13 @@ func newClusterctlClient(path string, options ...Option) (*clusterctlClient, err // defaultClusterFactory is a ClusterClientFactory func the uses the default client provided by the cluster low level library. func defaultClusterFactory() func(kubeconfig string) (cluster.Client, error) { return func(kubeconfig string) (cluster.Client, error) { - return cluster.New(kubeconfig, cluster.Options{}), nil + return cluster.New(kubeconfig), nil } } // defaultRepositoryFactory is a RepositoryClientFactory func the uses the default client provided by the repository low level library. func defaultRepositoryFactory(configClient config.Client) func(providerConfig config.Provider) (repository.Client, error) { return func(providerConfig config.Provider) (repository.Client, error) { - return repository.New(providerConfig, configClient.Variables(), repository.Options{}) + return repository.New(providerConfig, configClient.Variables()) } } diff --git a/cmd/clusterctl/pkg/client/client_test.go b/cmd/clusterctl/pkg/client/client_test.go index 03945b28367a..ff784e0a1188 100644 --- a/cmd/clusterctl/pkg/client/client_test.go +++ b/cmd/clusterctl/pkg/client/client_test.go @@ -140,11 +140,7 @@ func (f *fakeClient) WithRepository(repositoryClient repository.Client) *fakeCli func newFakeCluster(kubeconfig string) *fakeClusterClient { fakeProxy := test.NewFakeProxy() - options := cluster.Options{ - InjectProxy: fakeProxy, - } - - client := cluster.New("", options) + client := cluster.New("", cluster.InjectProxy(fakeProxy)) return &fakeClusterClient{ kubeconfig: kubeconfig, @@ -257,15 +253,12 @@ func (f *fakeConfigClient) WithProvider(provider config.Provider) *fakeConfigCli // the WithPaths or WithDefaultVersion methods to configure the repository and WithFile to set the map values. func newFakeRepository(provider config.Provider, configVariablesClient config.VariablesClient) *fakeRepositoryClient { fakeRepository := test.NewFakeRepository() - options := repository.Options{ - InjectRepository: fakeRepository, - } if configVariablesClient == nil { configVariablesClient = newFakeConfig().Variables() } - client, _ := repository.New(provider, configVariablesClient, options) + client, _ := repository.New(provider, configVariablesClient, repository.InjectRepository(fakeRepository)) return &fakeRepositoryClient{ Provider: provider, diff --git a/cmd/clusterctl/pkg/client/cluster/client.go b/cmd/clusterctl/pkg/client/cluster/client.go index 42a5ed4beb14..6e3f293c96ae 100644 --- a/cmd/clusterctl/pkg/client/cluster/client.go +++ b/cmd/clusterctl/pkg/client/cluster/client.go @@ -109,14 +109,34 @@ func (c *clusterClient) ObjectMover() ObjectMover { return newObjectMover(c.proxy, log) } +// NewOptions carries the options supported by New +type NewOptions struct { + injectProxy Proxy +} + +// Option is a configuration option supplied to New +type Option func(*NewOptions) + +// InjectProxy implements a New Option that allows to override the default proxy used by clusterctl. +func InjectProxy(proxy Proxy) Option { + return func(c *NewOptions) { + c.injectProxy = proxy + } +} + // New returns a cluster.Client. -func New(kubeconfig string, options Options) Client { - return newClusterClient(kubeconfig, options) +func New(kubeconfig string, options ...Option) Client { + return newClusterClient(kubeconfig, options...) } -func newClusterClient(kubeconfig string, options Options) *clusterClient { - // if there is an injected proxy, use it, otherwise use the default one - proxy := options.InjectProxy +func newClusterClient(kubeconfig string, options ...Option) *clusterClient { + cfg := &NewOptions{} + for _, o := range options { + o(cfg) + } + + // if there is an injected proxy, use it, otherwise use a default one + proxy := cfg.injectProxy if proxy == nil { proxy = newProxy(kubeconfig) } @@ -127,11 +147,6 @@ func newClusterClient(kubeconfig string, options Options) *clusterClient { } } -// Options allow to set ConfigClient options -type Options struct { - InjectProxy Proxy -} - type Proxy interface { // CurrentNamespace returns the namespace from the current context in the kubeconfig file CurrentNamespace() (string, error) diff --git a/cmd/clusterctl/pkg/client/config/client.go b/cmd/clusterctl/pkg/client/config/client.go index a0f764e5ef68..e81b44d5a56f 100644 --- a/cmd/clusterctl/pkg/client/config/client.go +++ b/cmd/clusterctl/pkg/client/config/client.go @@ -75,6 +75,7 @@ func newConfigClient(path string, options ...Option) (*configClient, error) { o(cfg) } + // if there is an injected reader, use it, otherwise use a default one reader := cfg.injectReader if reader == nil { reader = newViperReader() diff --git a/cmd/clusterctl/pkg/client/repository/client.go b/cmd/clusterctl/pkg/client/repository/client.go index 5d8d85ff24d5..41d64ef0a9e6 100644 --- a/cmd/clusterctl/pkg/client/repository/client.go +++ b/cmd/clusterctl/pkg/client/repository/client.go @@ -93,12 +93,18 @@ func InjectRepository(repository Repository) Option { } // New returns a Client. -func New(provider config.Provider, configVariablesClient config.VariablesClient, options Options) (Client, error) { - return newRepositoryClient(provider, configVariablesClient, options) +func New(provider config.Provider, configVariablesClient config.VariablesClient, options ...Option) (Client, error) { + return newRepositoryClient(provider, configVariablesClient, options...) } -func newRepositoryClient(provider config.Provider, configVariablesClient config.VariablesClient, options Options) (*repositoryClient, error) { - repository := options.InjectRepository +func newRepositoryClient(provider config.Provider, configVariablesClient config.VariablesClient, options ...Option) (*repositoryClient, error) { + cfg := &NewOptions{} + for _, o := range options { + o(cfg) + } + + // if there is an injected repository, use it, otherwise use a default one + repository := cfg.injectRepository if repository == nil { r, err := repositoryFactory(provider, configVariablesClient) if err != nil { @@ -114,11 +120,6 @@ func newRepositoryClient(provider config.Provider, configVariablesClient config. }, nil } -// Options allow to set Client options -type Options struct { - InjectRepository Repository -} - // Repository defines the behavior of a repository implementation. // clusterctl is designed to support different repository types; each repository implementation should be aware of // the provider version they are hosting, and possibly to host more than one version.