@@ -28,6 +28,7 @@ import (
28
28
"k8s.io/client-go/kubernetes/scheme"
29
29
"k8s.io/client-go/rest"
30
30
"k8s.io/client-go/tools/record"
31
+ "k8s.io/utils/pointer"
31
32
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
32
33
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
33
34
@@ -82,6 +83,8 @@ type Options struct {
82
83
Scheme * runtime.Scheme
83
84
84
85
// MapperProvider provides the rest mapper used to map go types to Kubernetes APIs
86
+ //
87
+ // Deprecated: Set Cache.Mapper and Client.Mapper directly instead.
85
88
MapperProvider func (c * rest.Config , httpClient * http.Client ) (meta.RESTMapper , error )
86
89
87
90
// Logger is the logger that should be used by this Cluster.
@@ -102,29 +105,54 @@ type Options struct {
102
105
// Note: If a namespace is specified, controllers can still Watch for a
103
106
// cluster-scoped resource (e.g Node). For namespaced resources the cache
104
107
// will only hold objects from the desired namespace.
108
+ //
109
+ // Deprecated: Use Cache.Namespaces instead.
105
110
Namespace string
106
111
107
112
// HTTPClient is the http client that will be used to create the default
108
113
// Cache and Client. If not set the rest.HTTPClientFor function will be used
109
114
// to create the http client.
110
115
HTTPClient * http.Client
111
116
117
+ // Cache is the cache.Options that will be used to create the default Cache.
118
+ // By default, the cache will watch and list requested objects in all namespaces.
119
+ Cache cache.Options
120
+
112
121
// NewCache is the function that will create the cache to be used
113
122
// by the manager. If not set this will use the default new cache function.
123
+ //
124
+ // When using a custom NewCache, the Cache options will be passed to the
125
+ // NewCache function.
126
+ //
127
+ // NOTE: LOW LEVEL PRIMITIVE!
128
+ // Only use a custom NewCache if you know what you are doing.
114
129
NewCache cache.NewCacheFunc
115
130
131
+ // Client is the client.Options that will be used to create the default Client.
132
+ // By default, the client will use the cache for reads and direct calls for writes.
133
+ Client client.Options
134
+
116
135
// NewClient is the func that creates the client to be used by the manager.
117
136
// If not set this will create a Client backed by a Cache for read operations
118
137
// and a direct Client for write operations.
119
- // NOTE: The default client will not cache Unstructured.
138
+ //
139
+ // When using a custom NewClient, the Client options will be passed to the
140
+ // NewClient function.
141
+ //
142
+ // NOTE: LOW LEVEL PRIMITIVE!
143
+ // Only use a custom NewClient if you know what you are doing.
120
144
NewClient client.NewClientFunc
121
145
122
146
// ClientDisableCacheFor tells the client that, if any cache is used, to bypass it
123
147
// for the given objects.
148
+ //
149
+ // Deprecated: Use Client.Cache.DisableFor instead.
124
150
ClientDisableCacheFor []client.Object
125
151
126
152
// DryRunClient specifies whether the client should be configured to enforce
127
153
// dryRun mode.
154
+ //
155
+ // Deprecated: Use Client.DryRun instead.
128
156
DryRunClient bool
129
157
130
158
// EventBroadcaster records Events emitted by the manager and sends them to the Kubernetes API
@@ -171,36 +199,71 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
171
199
}
172
200
173
201
// Create the cache for the cached read client and registering informers
174
- cache , err := options .NewCache (config , cache.Options {
175
- HTTPClient : options .HTTPClient ,
176
- Scheme : options .Scheme ,
177
- Mapper : mapper ,
178
- ResyncEvery : options .SyncPeriod ,
179
- Namespaces : []string {options .Namespace },
180
- })
202
+ cacheOpts := options .Cache
203
+ {
204
+ if cacheOpts .Scheme == nil {
205
+ cacheOpts .Scheme = options .Scheme
206
+ }
207
+ if cacheOpts .Mapper == nil {
208
+ cacheOpts .Mapper = mapper
209
+ }
210
+ if cacheOpts .HTTPClient == nil {
211
+ cacheOpts .HTTPClient = options .HTTPClient
212
+ }
213
+ if cacheOpts .ResyncEvery == nil {
214
+ cacheOpts .ResyncEvery = options .SyncPeriod
215
+ }
216
+ if len (cacheOpts .Namespaces ) == 0 && options .Namespace != "" {
217
+ cacheOpts .Namespaces = []string {options .Namespace }
218
+ }
219
+ }
220
+ cache , err := options .NewCache (config , cacheOpts )
181
221
if err != nil {
182
222
return nil , err
183
223
}
184
224
185
- writeObj , err := options .NewClient (config , client.Options {
186
- HTTPClient : options .HTTPClient ,
187
- Scheme : options .Scheme ,
188
- Mapper : mapper ,
189
- Cache : & client.CacheOptions {
190
- Reader : cache ,
191
- DisableFor : options .ClientDisableCacheFor ,
192
- },
193
- })
225
+ // Create the client, and default its options.
226
+ clientOpts := options .Client
227
+ {
228
+ if clientOpts .Scheme == nil {
229
+ clientOpts .Scheme = options .Scheme
230
+ }
231
+ if clientOpts .Mapper == nil {
232
+ clientOpts .Mapper = mapper
233
+ }
234
+ if clientOpts .HTTPClient == nil {
235
+ clientOpts .HTTPClient = options .HTTPClient
236
+ }
237
+ if clientOpts .Cache == nil {
238
+ clientOpts .Cache = & client.CacheOptions {
239
+ Unstructured : false ,
240
+ }
241
+ }
242
+ if clientOpts .Cache .Reader == nil {
243
+ clientOpts .Cache .Reader = cache
244
+ }
245
+
246
+ // For backward compatibility, the ClientDisableCacheFor option should
247
+ // be appended to the DisableFor option in the client.
248
+ clientOpts .Cache .DisableFor = append (clientOpts .Cache .DisableFor , options .ClientDisableCacheFor ... )
249
+
250
+ if clientOpts .DryRun == nil && options .DryRunClient {
251
+ // For backward compatibility, the DryRunClient (if set) option should override
252
+ // the DryRun option in the client (if unset).
253
+ clientOpts .DryRun = pointer .Bool (true )
254
+ }
255
+ }
256
+ clientWriter , err := options .NewClient (config , clientOpts )
194
257
if err != nil {
195
258
return nil , err
196
259
}
197
260
198
- if options .DryRunClient {
199
- writeObj = client .NewDryRunClient (writeObj )
200
- }
201
-
202
261
// Create the API Reader, a client with no cache.
203
- apiReader , err := client .New (config , client.Options {HTTPClient : options .HTTPClient , Scheme : options .Scheme , Mapper : mapper })
262
+ clientReader , err := client .New (config , client.Options {
263
+ HTTPClient : options .HTTPClient ,
264
+ Scheme : options .Scheme ,
265
+ Mapper : mapper ,
266
+ })
204
267
if err != nil {
205
268
return nil , err
206
269
}
@@ -219,8 +282,8 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
219
282
scheme : options .Scheme ,
220
283
cache : cache ,
221
284
fieldIndexes : cache ,
222
- client : writeObj ,
223
- apiReader : apiReader ,
285
+ client : clientWriter ,
286
+ apiReader : clientReader ,
224
287
recorderProvider : recorderProvider ,
225
288
mapper : mapper ,
226
289
logger : options .Logger ,
0 commit comments