Skip to content

Commit 098324d

Browse files
committed
add option to inject options for Cluster and Client
1 parent 740bd73 commit 098324d

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

pkg/clusters/cluster.go

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ type Cluster struct {
2323
client client.Client
2424
// cluster
2525
cluster cluster.Cluster
26+
27+
clientOpts *client.Options
28+
clusterOpts []cluster.Option
2629
}
2730

2831
// Initializes a new cluster.
@@ -56,6 +59,36 @@ func (c *Cluster) RegisterConfigPathFlag(flags *flag.FlagSet) {
5659
flags.StringVar(&c.cfgPath, fmt.Sprintf("%s-cluster", c.id), "", fmt.Sprintf("Path to the %s cluster kubeconfig file or directory containing either a kubeconfig or host, token, and ca file. Leave empty to use in-cluster config.", c.id))
5760
}
5861

62+
// WithClientOptions allows to overwrite the default client options.
63+
// It must be called before InitializeClient().
64+
// Note that using this method disables the the scheme injection during client initialization.
65+
// This means that the required scheme should already be set in the options that are passed into this method.
66+
// Returns the cluster for chaining.
67+
func (c *Cluster) WithClientOptions(opts client.Options) *Cluster {
68+
c.clientOpts = &opts
69+
return c
70+
}
71+
72+
// WithClusterOptions allows to overwrite the default cluster options.
73+
// It must be called before InitializeClient().
74+
// Note that using this method disables the the scheme injection during client initialization.
75+
// This means that the required scheme should be set by the cluster options that are passed into this method.
76+
// The DefaultClusterOptions function can be passed in as a cluster option to set the scheme.
77+
// Returns the cluster for chaining.
78+
func (c *Cluster) WithClusterOptions(opts ...cluster.Option) *Cluster {
79+
c.clusterOpts = opts
80+
return c
81+
}
82+
83+
// DefaultClusterOptions returns the default cluster options.
84+
// This is useful when one wants to add custom cluster options without overwriting the default ones via WithClusterOptions().
85+
func DefaultClusterOptions(scheme *runtime.Scheme) cluster.Option {
86+
return func(o *cluster.Options) {
87+
o.Scheme = scheme
88+
o.Cache.Scheme = scheme
89+
}
90+
}
91+
5992
///////////////////
6093
// STATUS CHECKS //
6194
///////////////////
@@ -92,15 +125,11 @@ func (c *Cluster) InitializeID(id string) {
92125
}
93126

94127
// InitializeRESTConfig loads the cluster's REST config.
95-
// If the config has already been loaded, this is a no-op.
96128
// Panics if the cluster's id is not set (InitializeID must be called first).
97129
func (c *Cluster) InitializeRESTConfig() error {
98130
if !c.HasID() {
99131
panic("cluster id must be set before loading the config")
100132
}
101-
if c.HasRESTConfig() {
102-
return nil
103-
}
104133
cfg, err := controller.LoadKubeconfig(c.cfgPath)
105134
if err != nil {
106135
return fmt.Errorf("failed to load '%s' cluster kubeconfig: %w", c.ID(), err)
@@ -111,20 +140,29 @@ func (c *Cluster) InitializeRESTConfig() error {
111140

112141
// InitializeClient creates a new client for the cluster.
113142
// This also initializes the cluster's controller-runtime 'Cluster' representation.
114-
// If the client has already been initialized, this is a no-op.
115143
// Panics if the cluster's REST config has not been loaded (InitializeRESTConfig must be called first).
116144
func (c *Cluster) InitializeClient(scheme *runtime.Scheme) error {
117145
if !c.HasRESTConfig() {
118146
panic("cluster REST config must be set before creating the client")
119147
}
120-
if c.HasClient() {
121-
return nil
148+
if c.clientOpts == nil {
149+
c.clientOpts = &client.Options{
150+
Scheme: scheme,
151+
}
122152
}
123-
cli, err := client.New(c.restCfg, client.Options{Scheme: scheme})
153+
cli, err := client.New(c.restCfg, *c.clientOpts)
124154
if err != nil {
125155
return fmt.Errorf("failed to create '%s' cluster client: %w", c.ID(), err)
126156
}
127-
clu, err := cluster.New(c.restCfg, func(o *cluster.Options) { o.Scheme = scheme; o.Cache.Scheme = scheme })
157+
if c.clusterOpts == nil {
158+
c.clusterOpts = []cluster.Option{
159+
func(o *cluster.Options) {
160+
o.Scheme = scheme
161+
o.Cache.Scheme = scheme
162+
},
163+
}
164+
}
165+
clu, err := cluster.New(c.restCfg, c.clusterOpts...)
128166
if err != nil {
129167
return fmt.Errorf("failed to create '%s' cluster Cluster representation: %w", c.ID(), err)
130168
}

0 commit comments

Comments
 (0)