Skip to content

🐛clusterctl: fix public library inconsistencies #2220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/clusterctl/pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
11 changes: 2 additions & 9 deletions cmd/clusterctl/pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
35 changes: 25 additions & 10 deletions cmd/clusterctl/pkg/client/cluster/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cmd/clusterctl/pkg/client/config/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
19 changes: 10 additions & 9 deletions cmd/clusterctl/pkg/client/repository/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down