Skip to content

Commit 57da6a8

Browse files
clusterctl fix public library
1 parent f068437 commit 57da6a8

File tree

5 files changed

+40
-30
lines changed

5 files changed

+40
-30
lines changed

cmd/clusterctl/pkg/client/client.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,13 @@ func newClusterctlClient(path string, options ...Option) (*clusterctlClient, err
231231
// defaultClusterFactory is a ClusterClientFactory func the uses the default client provided by the cluster low level library.
232232
func defaultClusterFactory() func(kubeconfig string) (cluster.Client, error) {
233233
return func(kubeconfig string) (cluster.Client, error) {
234-
return cluster.New(kubeconfig, cluster.Options{}), nil
234+
return cluster.New(kubeconfig), nil
235235
}
236236
}
237237

238238
// defaultRepositoryFactory is a RepositoryClientFactory func the uses the default client provided by the repository low level library.
239239
func defaultRepositoryFactory(configClient config.Client) func(providerConfig config.Provider) (repository.Client, error) {
240240
return func(providerConfig config.Provider) (repository.Client, error) {
241-
return repository.New(providerConfig, configClient.Variables(), repository.Options{})
241+
return repository.New(providerConfig, configClient.Variables())
242242
}
243243
}

cmd/clusterctl/pkg/client/client_test.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,7 @@ func (f *fakeClient) WithRepository(repositoryClient repository.Client) *fakeCli
140140
func newFakeCluster(kubeconfig string) *fakeClusterClient {
141141
fakeProxy := test.NewFakeProxy()
142142

143-
options := cluster.Options{
144-
InjectProxy: fakeProxy,
145-
}
146-
147-
client := cluster.New("", options)
143+
client := cluster.New("", cluster.InjectProxy(fakeProxy))
148144

149145
return &fakeClusterClient{
150146
kubeconfig: kubeconfig,
@@ -257,15 +253,12 @@ func (f *fakeConfigClient) WithProvider(provider config.Provider) *fakeConfigCli
257253
// the WithPaths or WithDefaultVersion methods to configure the repository and WithFile to set the map values.
258254
func newFakeRepository(provider config.Provider, configVariablesClient config.VariablesClient) *fakeRepositoryClient {
259255
fakeRepository := test.NewFakeRepository()
260-
options := repository.Options{
261-
InjectRepository: fakeRepository,
262-
}
263256

264257
if configVariablesClient == nil {
265258
configVariablesClient = newFakeConfig().Variables()
266259
}
267260

268-
client, _ := repository.New(provider, configVariablesClient, options)
261+
client, _ := repository.New(provider, configVariablesClient, repository.InjectRepository(fakeRepository))
269262

270263
return &fakeRepositoryClient{
271264
Provider: provider,

cmd/clusterctl/pkg/client/cluster/client.go

+25-10
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,34 @@ func (c *clusterClient) ObjectMover() ObjectMover {
109109
return newObjectMover(c.proxy, log)
110110
}
111111

112+
// NewOptions carries the options supported by New
113+
type NewOptions struct {
114+
injectProxy Proxy
115+
}
116+
117+
// Option is a configuration option supplied to New
118+
type Option func(*NewOptions)
119+
120+
// InjectProxy implements a New Option that allows to override the default proxy used by clusterctl.
121+
func InjectProxy(proxy Proxy) Option {
122+
return func(c *NewOptions) {
123+
c.injectProxy = proxy
124+
}
125+
}
126+
112127
// New returns a cluster.Client.
113-
func New(kubeconfig string, options Options) Client {
114-
return newClusterClient(kubeconfig, options)
128+
func New(kubeconfig string, options ...Option) Client {
129+
return newClusterClient(kubeconfig, options...)
115130
}
116131

117-
func newClusterClient(kubeconfig string, options Options) *clusterClient {
118-
// if there is an injected proxy, use it, otherwise use the default one
119-
proxy := options.InjectProxy
132+
func newClusterClient(kubeconfig string, options ...Option) *clusterClient {
133+
cfg := &NewOptions{}
134+
for _, o := range options {
135+
o(cfg)
136+
}
137+
138+
// if there is an injected proxy, use it, otherwise use a default one
139+
proxy := cfg.injectProxy
120140
if proxy == nil {
121141
proxy = newProxy(kubeconfig)
122142
}
@@ -127,11 +147,6 @@ func newClusterClient(kubeconfig string, options Options) *clusterClient {
127147
}
128148
}
129149

130-
// Options allow to set ConfigClient options
131-
type Options struct {
132-
InjectProxy Proxy
133-
}
134-
135150
type Proxy interface {
136151
// CurrentNamespace returns the namespace from the current context in the kubeconfig file
137152
CurrentNamespace() (string, error)

cmd/clusterctl/pkg/client/config/client.go

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func newConfigClient(path string, options ...Option) (*configClient, error) {
7575
o(cfg)
7676
}
7777

78+
// if there is an injected reader, use it, otherwise use a default one
7879
reader := cfg.injectReader
7980
if reader == nil {
8081
reader = newViperReader()

cmd/clusterctl/pkg/client/repository/client.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,18 @@ func InjectRepository(repository Repository) Option {
9393
}
9494

9595
// New returns a Client.
96-
func New(provider config.Provider, configVariablesClient config.VariablesClient, options Options) (Client, error) {
97-
return newRepositoryClient(provider, configVariablesClient, options)
96+
func New(provider config.Provider, configVariablesClient config.VariablesClient, options ...Option) (Client, error) {
97+
return newRepositoryClient(provider, configVariablesClient, options...)
9898
}
9999

100-
func newRepositoryClient(provider config.Provider, configVariablesClient config.VariablesClient, options Options) (*repositoryClient, error) {
101-
repository := options.InjectRepository
100+
func newRepositoryClient(provider config.Provider, configVariablesClient config.VariablesClient, options ...Option) (*repositoryClient, error) {
101+
cfg := &NewOptions{}
102+
for _, o := range options {
103+
o(cfg)
104+
}
105+
106+
// if there is an injected repository, use it, otherwise use a default one
107+
repository := cfg.injectRepository
102108
if repository == nil {
103109
r, err := repositoryFactory(provider, configVariablesClient)
104110
if err != nil {
@@ -114,11 +120,6 @@ func newRepositoryClient(provider config.Provider, configVariablesClient config.
114120
}, nil
115121
}
116122

117-
// Options allow to set Client options
118-
type Options struct {
119-
InjectRepository Repository
120-
}
121-
122123
// Repository defines the behavior of a repository implementation.
123124
// clusterctl is designed to support different repository types; each repository implementation should be aware of
124125
// the provider version they are hosting, and possibly to host more than one version.

0 commit comments

Comments
 (0)