Skip to content

Commit 6066383

Browse files
New Reconciler Option: SkipPrimaryGVKSchemeRegistration (#147)
* Add a new Reconciler option SkipPrimaryGVKSchemeRegistration, which can be used by users who prefer their custom scheme setup over the generic scheme setup currently being done by the reconciler. Signed-off-by: Moritz Clasmeier <[email protected]> * Update pkg/reconciler/reconciler_suite_test.go Co-authored-by: Joe Lanford <[email protected]> Co-authored-by: Joe Lanford <[email protected]>
1 parent f47bc9d commit 6066383

File tree

3 files changed

+761
-628
lines changed

3 files changed

+761
-628
lines changed

pkg/reconciler/reconciler.go

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,16 @@ type Reconciler struct {
6969
preHooks []hook.PreHook
7070
postHooks []hook.PostHook
7171

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
8182

8283
annotSetupOnce sync.Once
8384
annotations map[string]struct{}
@@ -131,7 +132,9 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
131132
controllerName := fmt.Sprintf("%v-controller", strings.ToLower(r.gvk.Kind))
132133

133134
r.addDefaults(mgr, controllerName)
134-
r.setupScheme(mgr)
135+
if !r.skipPrimaryGVKSchemeRegistration {
136+
r.setupScheme(mgr)
137+
}
135138

136139
c, err := controller.New(controllerName, mgr, controller.Options{Reconciler: r, MaxConcurrentReconciles: r.maxConcurrentReconciles})
137140
if err != nil {
@@ -257,6 +260,52 @@ func SkipDependentWatches(skip bool) Option {
257260
}
258261
}
259262

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+
260309
// WithMaxConcurrentReconciles is an Option that configures the number of
261310
// concurrent reconciles that the controller will run.
262311
//

pkg/reconciler/reconciler_suite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var (
4141
cfg *rest.Config
4242

4343
gvk = schema.GroupVersionKind{Group: "example.com", Version: "v1", Kind: "TestApp"}
44+
gv = gvk.GroupVersion()
4445
chrt = testutil.MustLoadChart("../../pkg/internal/testdata/test-chart-1.2.0.tgz")
4546
)
4647

0 commit comments

Comments
 (0)