@@ -61,15 +61,27 @@ func WithClientOptions(opts client.Options) Option {
61
61
}
62
62
}
63
63
64
+ // WithPersistent sets whether the client should persist the underlying client
65
+ // config, REST mapper, and discovery client.
66
+ func WithPersistent (persist bool ) Option {
67
+ return func (c * MemoryRESTClientGetter ) {
68
+ c .persistent = persist
69
+ }
70
+ }
71
+
64
72
// MemoryRESTClientGetter is a resource.RESTClientGetter that uses an
65
- // in-memory REST config, REST mapper, and discovery client. The REST config,
66
- // REST mapper, and discovery client are lazily initialized , and cached for
67
- // subsequent calls.
73
+ // in-memory REST config, REST mapper, and discovery client.
74
+ // If configured, the client config, REST mapper , and discovery client are
75
+ // lazily initialized, and cached for subsequent calls.
68
76
type MemoryRESTClientGetter struct {
69
77
// namespace is the namespace to use for the client.
70
78
namespace string
71
79
// impersonate is the username to use for the client.
72
80
impersonate string
81
+ // persistent indicates whether the client should persist the restMapper,
82
+ // clientCfg, and discoveryClient. Rather than re-initializing them on
83
+ // every call, they will be cached and reused.
84
+ persistent bool
73
85
74
86
cfg * rest.Config
75
87
@@ -124,59 +136,100 @@ func (c *MemoryRESTClientGetter) ToRESTConfig() (*rest.Config, error) {
124
136
// ToDiscoveryClient returns a memory cached discovery client. Calling it
125
137
// multiple times will return the same instance.
126
138
func (c * MemoryRESTClientGetter ) ToDiscoveryClient () (discovery.CachedDiscoveryInterface , error ) {
127
- c .clientCfgMu .Lock ()
128
- defer c .clientCfgMu .Unlock ()
139
+ if c .persistent {
140
+ return c .toPersistentDiscoveryClient ()
141
+ }
142
+ return c .toDiscoveryClient ()
143
+ }
129
144
130
- if c .discoveryClient == nil {
131
- config , err := c .ToRESTConfig ()
132
- if err != nil {
133
- return nil , err
134
- }
145
+ func (c * MemoryRESTClientGetter ) toPersistentDiscoveryClient () (discovery.CachedDiscoveryInterface , error ) {
146
+ c .discoveryMu .Lock ()
147
+ defer c .discoveryMu .Unlock ()
135
148
136
- discoveryClient , err := discovery .NewDiscoveryClientForConfig (config )
149
+ if c .discoveryClient == nil {
150
+ discoveryClient , err := c .toDiscoveryClient ()
137
151
if err != nil {
138
152
return nil , err
139
153
}
140
- c .discoveryClient = memory . NewMemCacheClient ( discoveryClient )
154
+ c .discoveryClient = discoveryClient
141
155
}
142
156
return c .discoveryClient , nil
143
157
}
144
158
159
+ func (c * MemoryRESTClientGetter ) toDiscoveryClient () (discovery.CachedDiscoveryInterface , error ) {
160
+ config , err := c .ToRESTConfig ()
161
+ if err != nil {
162
+ return nil , err
163
+ }
164
+
165
+ discoveryClient , err := discovery .NewDiscoveryClientForConfig (config )
166
+ if err != nil {
167
+ return nil , err
168
+ }
169
+ return memory .NewMemCacheClient (discoveryClient ), nil
170
+ }
171
+
145
172
// ToRESTMapper returns a meta.RESTMapper using the discovery client. Calling
146
173
// it multiple times will return the same instance.
147
174
func (c * MemoryRESTClientGetter ) ToRESTMapper () (meta.RESTMapper , error ) {
148
- c .discoveryMu .Lock ()
149
- defer c .discoveryMu .Unlock ()
175
+ if c .persistent {
176
+ return c .toPersistentRESTMapper ()
177
+ }
178
+ return c .toRESTMapper ()
179
+ }
180
+
181
+ func (c * MemoryRESTClientGetter ) toPersistentRESTMapper () (meta.RESTMapper , error ) {
182
+ c .restMapperMu .Lock ()
183
+ defer c .restMapperMu .Unlock ()
150
184
151
185
if c .restMapper == nil {
152
- discoveryClient , err := c .ToDiscoveryClient ()
186
+ restMapper , err := c .toRESTMapper ()
153
187
if err != nil {
154
188
return nil , err
155
189
}
156
- mapper := restmapper .NewDeferredDiscoveryRESTMapper (discoveryClient )
157
- c .restMapper = restmapper .NewShortcutExpander (mapper , discoveryClient )
190
+ c .restMapper = restMapper
158
191
}
159
192
return c .restMapper , nil
160
193
}
161
194
195
+ func (c * MemoryRESTClientGetter ) toRESTMapper () (meta.RESTMapper , error ) {
196
+ discoveryClient , err := c .ToDiscoveryClient ()
197
+ if err != nil {
198
+ return nil , err
199
+ }
200
+ mapper := restmapper .NewDeferredDiscoveryRESTMapper (discoveryClient )
201
+ return restmapper .NewShortcutExpander (mapper , discoveryClient ), nil
202
+ }
203
+
162
204
// ToRawKubeConfigLoader returns a clientcmd.ClientConfig using
163
205
// clientcmd.DefaultClientConfig. With clientcmd.ClusterDefaults, namespace, and
164
206
// impersonate configured as overwrites.
165
207
func (c * MemoryRESTClientGetter ) ToRawKubeConfigLoader () clientcmd.ClientConfig {
208
+ if c .persistent {
209
+ return c .toPersistentRawKubeConfigLoader ()
210
+ }
211
+ return c .toRawKubeConfigLoader ()
212
+ }
213
+
214
+ func (c * MemoryRESTClientGetter ) toPersistentRawKubeConfigLoader () clientcmd.ClientConfig {
166
215
c .clientCfgMu .Lock ()
167
216
defer c .clientCfgMu .Unlock ()
168
217
169
218
if c .clientCfg == nil {
170
- loadingRules := clientcmd .NewDefaultClientConfigLoadingRules ()
171
- // use the standard defaults for this client command
172
- // DEPRECATED: remove and replace with something more accurate
173
- loadingRules .DefaultClientConfig = & clientcmd .DefaultClientConfig
174
-
175
- overrides := & clientcmd.ConfigOverrides {ClusterDefaults : clientcmd .ClusterDefaults }
176
- overrides .Context .Namespace = c .namespace
177
- overrides .AuthInfo .Impersonate = c .impersonate
178
-
179
- c .clientCfg = clientcmd .NewNonInteractiveDeferredLoadingClientConfig (loadingRules , overrides )
219
+ c .clientCfg = c .toRawKubeConfigLoader ()
180
220
}
181
221
return c .clientCfg
182
222
}
223
+
224
+ func (c * MemoryRESTClientGetter ) toRawKubeConfigLoader () clientcmd.ClientConfig {
225
+ loadingRules := clientcmd .NewDefaultClientConfigLoadingRules ()
226
+ // use the standard defaults for this client command
227
+ // DEPRECATED: remove and replace with something more accurate
228
+ loadingRules .DefaultClientConfig = & clientcmd .DefaultClientConfig
229
+
230
+ overrides := & clientcmd.ConfigOverrides {ClusterDefaults : clientcmd .ClusterDefaults }
231
+ overrides .Context .Namespace = c .namespace
232
+ overrides .AuthInfo .Impersonate = c .impersonate
233
+
234
+ return clientcmd .NewNonInteractiveDeferredLoadingClientConfig (loadingRules , overrides )
235
+ }
0 commit comments