@@ -69,15 +69,16 @@ type Reconciler struct {
69
69
preHooks []hook.PreHook
70
70
postHooks []hook.PostHook
71
71
72
- log logr.Logger
73
- gvk * schema.GroupVersionKind
74
- chrt * chart.Chart
75
- selectorPredicate predicate.Predicate
76
- overrideValues map [string ]string
77
- skipDependentWatches bool
78
- maxConcurrentReconciles int
79
- reconcilePeriod time.Duration
80
- maxHistory int
72
+ log logr.Logger
73
+ gvk * schema.GroupVersionKind
74
+ chrt * chart.Chart
75
+ selectorPredicate predicate.Predicate
76
+ overrideValues map [string ]string
77
+ skipDependentWatches bool
78
+ maxConcurrentReconciles int
79
+ reconcilePeriod time.Duration
80
+ maxHistory int
81
+ skipPrimaryGVKSchemeRegistration bool
81
82
82
83
annotSetupOnce sync.Once
83
84
annotations map [string ]struct {}
@@ -131,7 +132,9 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
131
132
controllerName := fmt .Sprintf ("%v-controller" , strings .ToLower (r .gvk .Kind ))
132
133
133
134
r .addDefaults (mgr , controllerName )
134
- r .setupScheme (mgr )
135
+ if ! r .skipPrimaryGVKSchemeRegistration {
136
+ r .setupScheme (mgr )
137
+ }
135
138
136
139
c , err := controller .New (controllerName , mgr , controller.Options {Reconciler : r , MaxConcurrentReconciles : r .maxConcurrentReconciles })
137
140
if err != nil {
@@ -257,6 +260,52 @@ func SkipDependentWatches(skip bool) Option {
257
260
}
258
261
}
259
262
263
+ // SkipPrimaryGVKSchemeRegistration is an Option that allows to disable the default behaviour of
264
+ // registering unstructured.Unstructured as underlying type for the GVK scheme.
265
+ //
266
+ // Disabling this built-in registration is necessary when building operators
267
+ // for which it is desired to have the underlying GVK scheme backed by a
268
+ // custom struct type.
269
+ //
270
+ // Example for using a custom type for the GVK scheme instead of unstructured.Unstructured:
271
+ //
272
+ // // Define custom type for GVK scheme.
273
+ // //+kubebuilder:object:root=true
274
+ // type Custom struct {
275
+ // // [...]
276
+ // }
277
+ //
278
+ // // Register custom type along with common meta types in scheme.
279
+ // scheme := runtime.NewScheme()
280
+ // scheme.AddKnownTypes(SchemeGroupVersion, &Custom{})
281
+ // metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
282
+ //
283
+ // // Create new manager using the controller-runtime, injecting above scheme.
284
+ // options := ctrl.Options{
285
+ // Scheme = scheme,
286
+ // // [...]
287
+ // }
288
+ // mgr, err := ctrl.NewManager(config, options)
289
+ //
290
+ // // Create reconciler with generic scheme registration being disabled.
291
+ // r, err := reconciler.New(
292
+ // reconciler.WithChart(chart),
293
+ // reconciler.SkipPrimaryGVKSchemeRegistration(true),
294
+ // // [...]
295
+ // )
296
+ //
297
+ // // Setup reconciler with above manager.
298
+ // err = r.SetupWithManager(mgr)
299
+ //
300
+ // By default, skipping of the generic scheme setup is disabled, which means that
301
+ // unstructured.Unstructured is used for the GVK scheme.
302
+ func SkipPrimaryGVKSchemeRegistration (skip bool ) Option {
303
+ return func (r * Reconciler ) error {
304
+ r .skipPrimaryGVKSchemeRegistration = skip
305
+ return nil
306
+ }
307
+ }
308
+
260
309
// WithMaxConcurrentReconciles is an Option that configures the number of
261
310
// concurrent reconciles that the controller will run.
262
311
//
0 commit comments