Skip to content

Commit

Permalink
New Reconciler Option: SkipPrimaryGVKSchemeRegistration (#147)
Browse files Browse the repository at this point in the history
* 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 <moritz@stackrox.com>

* Update pkg/reconciler/reconciler_suite_test.go

Co-authored-by: Joe Lanford <joe.lanford@gmail.com>

Co-authored-by: Joe Lanford <joe.lanford@gmail.com>
  • Loading branch information
mtesseract and joelanford committed Mar 21, 2022
1 parent f47bc9d commit 6066383
Show file tree
Hide file tree
Showing 3 changed files with 761 additions and 628 deletions.
69 changes: 59 additions & 10 deletions pkg/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,16 @@ type Reconciler struct {
preHooks []hook.PreHook
postHooks []hook.PostHook

log logr.Logger
gvk *schema.GroupVersionKind
chrt *chart.Chart
selectorPredicate predicate.Predicate
overrideValues map[string]string
skipDependentWatches bool
maxConcurrentReconciles int
reconcilePeriod time.Duration
maxHistory int
log logr.Logger
gvk *schema.GroupVersionKind
chrt *chart.Chart
selectorPredicate predicate.Predicate
overrideValues map[string]string
skipDependentWatches bool
maxConcurrentReconciles int
reconcilePeriod time.Duration
maxHistory int
skipPrimaryGVKSchemeRegistration bool

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

r.addDefaults(mgr, controllerName)
r.setupScheme(mgr)
if !r.skipPrimaryGVKSchemeRegistration {
r.setupScheme(mgr)
}

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

// SkipPrimaryGVKSchemeRegistration is an Option that allows to disable the default behaviour of
// registering unstructured.Unstructured as underlying type for the GVK scheme.
//
// Disabling this built-in registration is necessary when building operators
// for which it is desired to have the underlying GVK scheme backed by a
// custom struct type.
//
// Example for using a custom type for the GVK scheme instead of unstructured.Unstructured:
//
// // Define custom type for GVK scheme.
// //+kubebuilder:object:root=true
// type Custom struct {
// // [...]
// }
//
// // Register custom type along with common meta types in scheme.
// scheme := runtime.NewScheme()
// scheme.AddKnownTypes(SchemeGroupVersion, &Custom{})
// metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
//
// // Create new manager using the controller-runtime, injecting above scheme.
// options := ctrl.Options{
// Scheme = scheme,
// // [...]
// }
// mgr, err := ctrl.NewManager(config, options)
//
// // Create reconciler with generic scheme registration being disabled.
// r, err := reconciler.New(
// reconciler.WithChart(chart),
// reconciler.SkipPrimaryGVKSchemeRegistration(true),
// // [...]
// )
//
// // Setup reconciler with above manager.
// err = r.SetupWithManager(mgr)
//
// By default, skipping of the generic scheme setup is disabled, which means that
// unstructured.Unstructured is used for the GVK scheme.
func SkipPrimaryGVKSchemeRegistration(skip bool) Option {
return func(r *Reconciler) error {
r.skipPrimaryGVKSchemeRegistration = skip
return nil
}
}

// WithMaxConcurrentReconciles is an Option that configures the number of
// concurrent reconciles that the controller will run.
//
Expand Down
1 change: 1 addition & 0 deletions pkg/reconciler/reconciler_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
cfg *rest.Config

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

Expand Down
Loading

0 comments on commit 6066383

Please sign in to comment.