Skip to content

Commit 01f3b40

Browse files
committed
Expose Cache/Client options on Cluster
Signed-off-by: Vince Prignano <[email protected]>
1 parent 5c0aa32 commit 01f3b40

File tree

3 files changed

+101
-31
lines changed

3 files changed

+101
-31
lines changed

pkg/client/client.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ type Options struct {
5353
// WarningHandler is used to configure the warning handler responsible for
5454
// surfacing and handling warnings messages sent by the API server.
5555
WarningHandler WarningHandlerOptions
56+
57+
// DryRun instructs the client to only perform dry run requests.
58+
DryRun *bool
5659
}
5760

5861
// WarningHandlerOptions are options for configuring a
@@ -94,8 +97,12 @@ type NewClientFunc func(config *rest.Config, options Options) (Client, error)
9497
// corresponding group, version, and kind for the given type. In the
9598
// case of unstructured types, the group, version, and kind will be extracted
9699
// from the corresponding fields on the object.
97-
func New(config *rest.Config, options Options) (Client, error) {
98-
return newClient(config, options)
100+
func New(config *rest.Config, options Options) (c Client, err error) {
101+
c, err = newClient(config, options)
102+
if err == nil && options.DryRun != nil && *options.DryRun {
103+
c = NewDryRunClient(c)
104+
}
105+
return c, err
99106
}
100107

101108
func newClient(config *rest.Config, options Options) (*client, error) {

pkg/cluster/cluster.go

Lines changed: 87 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"k8s.io/client-go/kubernetes/scheme"
2929
"k8s.io/client-go/rest"
3030
"k8s.io/client-go/tools/record"
31+
"k8s.io/utils/pointer"
3132
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
3233
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
3334

@@ -82,6 +83,8 @@ type Options struct {
8283
Scheme *runtime.Scheme
8384

8485
// MapperProvider provides the rest mapper used to map go types to Kubernetes APIs
86+
//
87+
// Deprecated: Set Cache.Mapper and Client.Mapper directly instead.
8588
MapperProvider func(c *rest.Config, httpClient *http.Client) (meta.RESTMapper, error)
8689

8790
// Logger is the logger that should be used by this Cluster.
@@ -102,29 +105,54 @@ type Options struct {
102105
// Note: If a namespace is specified, controllers can still Watch for a
103106
// cluster-scoped resource (e.g Node). For namespaced resources the cache
104107
// will only hold objects from the desired namespace.
108+
//
109+
// Deprecated: Use Cache.Namespaces instead.
105110
Namespace string
106111

107112
// HTTPClient is the http client that will be used to create the default
108113
// Cache and Client. If not set the rest.HTTPClientFor function will be used
109114
// to create the http client.
110115
HTTPClient *http.Client
111116

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+
112121
// NewCache is the function that will create the cache to be used
113122
// 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.
114129
NewCache cache.NewCacheFunc
115130

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+
116135
// NewClient is the func that creates the client to be used by the manager.
117136
// If not set this will create a Client backed by a Cache for read operations
118137
// 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.
120144
NewClient client.NewClientFunc
121145

122146
// ClientDisableCacheFor tells the client that, if any cache is used, to bypass it
123147
// for the given objects.
148+
//
149+
// Deprecated: Use Client.Cache.DisableFor instead.
124150
ClientDisableCacheFor []client.Object
125151

126152
// DryRunClient specifies whether the client should be configured to enforce
127153
// dryRun mode.
154+
//
155+
// Deprecated: Use Client.DryRun instead.
128156
DryRunClient bool
129157

130158
// 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) {
171199
}
172200

173201
// 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)
181221
if err != nil {
182222
return nil, err
183223
}
184224

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)
194257
if err != nil {
195258
return nil, err
196259
}
197260

198-
if options.DryRunClient {
199-
writeObj = client.NewDryRunClient(writeObj)
200-
}
201-
202261
// 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+
})
204267
if err != nil {
205268
return nil, err
206269
}
@@ -219,8 +282,8 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
219282
scheme: options.Scheme,
220283
cache: cache,
221284
fieldIndexes: cache,
222-
client: writeObj,
223-
apiReader: apiReader,
285+
client: clientWriter,
286+
apiReader: clientReader,
224287
recorderProvider: recorderProvider,
225288
mapper: mapper,
226289
logger: options.Logger,

pkg/manager/manager.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,15 +348,15 @@ func New(config *rest.Config, options Options) (Manager, error) {
348348

349349
cluster, err := cluster.New(config, func(clusterOptions *cluster.Options) {
350350
clusterOptions.Scheme = options.Scheme
351-
clusterOptions.MapperProvider = options.MapperProvider
351+
clusterOptions.MapperProvider = options.MapperProvider //nolint:staticcheck
352352
clusterOptions.Logger = options.Logger
353353
clusterOptions.SyncPeriod = options.SyncPeriod
354-
clusterOptions.Namespace = options.Namespace
354+
clusterOptions.Namespace = options.Namespace //nolint:staticcheck
355355
clusterOptions.NewCache = options.NewCache
356356
clusterOptions.NewClient = options.NewClient
357-
clusterOptions.ClientDisableCacheFor = options.ClientDisableCacheFor
358-
clusterOptions.DryRunClient = options.DryRunClient
359-
clusterOptions.EventBroadcaster = options.EventBroadcaster //nolint:staticcheck
357+
clusterOptions.ClientDisableCacheFor = options.ClientDisableCacheFor //nolint:staticcheck
358+
clusterOptions.DryRunClient = options.DryRunClient //nolint:staticcheck
359+
clusterOptions.EventBroadcaster = options.EventBroadcaster //nolint:staticcheck
360360
})
361361
if err != nil {
362362
return nil, err

0 commit comments

Comments
 (0)