Skip to content

Commit 420dd38

Browse files
clusterctl fix public library
1 parent d63f047 commit 420dd38

File tree

5 files changed

+49
-32
lines changed

5 files changed

+49
-32
lines changed

cmd/clusterctl/pkg/client/client.go

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 9 deletions
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

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,48 @@ func (c *clusterClient) ObjectMover() ObjectMover {
110110
return newObjectMover(c.proxy, c.machineWaiter, log)
111111
}
112112

113+
// NewOptions carries the options supported by New
114+
type NewOptions struct {
115+
injectProxy Proxy
116+
injectMachineWaiter MachineWaiter
117+
}
118+
119+
// Option is a configuration option supplied to New
120+
type Option func(*NewOptions)
121+
122+
// InjectProxy implements a New Option that allows to override the default proxy used by clusterctl.
123+
func InjectProxy(proxy Proxy) Option {
124+
return func(c *NewOptions) {
125+
c.injectProxy = proxy
126+
}
127+
}
128+
129+
// InjectMachineWaiter implements a New Option that allows to override the default machine waiter used by clusterctl.
130+
func InjectMachineWaiter(machineWaiter MachineWaiter) Option {
131+
return func(c *NewOptions) {
132+
c.injectMachineWaiter = machineWaiter
133+
}
134+
}
135+
113136
// New returns a cluster.Client.
114-
func New(kubeconfig string, options Options) Client {
115-
return newClusterClient(kubeconfig, options)
137+
func New(kubeconfig string, options ...Option) Client {
138+
return newClusterClient(kubeconfig, options...)
116139
}
117140

118-
func newClusterClient(kubeconfig string, options Options) *clusterClient {
119-
// if there is an injected proxy, use it, otherwise use the default one
120-
proxy := options.InjectProxy
141+
func newClusterClient(kubeconfig string, options ...Option) *clusterClient {
142+
cfg := &NewOptions{}
143+
for _, o := range options {
144+
o(cfg)
145+
}
146+
147+
// if there is an injected proxy, use it, otherwise use a default one
148+
proxy := cfg.injectProxy
121149
if proxy == nil {
122150
proxy = newProxy(kubeconfig)
123151
}
124152

125153
// if there is an injected machineWaiter, use it, otherwise use the default one
126-
machineWaiter := options.InjectMachineWaiter
154+
machineWaiter := cfg.injectMachineWaiter
127155
if machineWaiter == nil {
128156
machineWaiter = waitForMachineReady
129157
}
@@ -135,12 +163,6 @@ func newClusterClient(kubeconfig string, options Options) *clusterClient {
135163
}
136164
}
137165

138-
// Options allow to set ConfigClient options
139-
type Options struct {
140-
InjectProxy Proxy
141-
InjectMachineWaiter MachineWaiter
142-
}
143-
144166
type Proxy interface {
145167
// CurrentNamespace returns the namespace from the current context in the kubeconfig file
146168
CurrentNamespace() (string, error)

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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 10 additions & 9 deletions
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)