diff --git a/main.go b/main.go index caba1a8..8332fcd 100644 --- a/main.go +++ b/main.go @@ -71,28 +71,28 @@ func main() { } if err = (&controllers.MyCRDReconciler{ - ReconcilerBase: util.NewReconcilerBase(mgr, mgr.GetEventRecorderFor("MyCRD_controller")), + ReconcilerBase: util.NewFromManager(mgr, mgr.GetEventRecorderFor("MyCRD_controller")), Log: ctrl.Log.WithName("controllers").WithName("MyCRD"), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "MyCRD") os.Exit(1) } if err = (&controllers.EnforcingCRDReconciler{ - EnforcingReconciler: lockedresourcecontroller.NewEnforcingReconciler(mgr, mgr.GetEventRecorderFor("EnforcingCRD_controller"), true), + EnforcingReconciler: lockedresourcecontroller.NewFromManager(mgr, mgr.GetEventRecorderFor("EnforcingCRD_controller"), true), Log: ctrl.Log.WithName("controllers").WithName("EnforcingCRD"), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "EnforcingCRD") os.Exit(1) } if err = (&controllers.EnforcingPatchReconciler{ - EnforcingReconciler: lockedresourcecontroller.NewEnforcingReconciler(mgr, mgr.GetEventRecorderFor("EnforcingPatch_controller"), true), + EnforcingReconciler: lockedresourcecontroller.NewFromManager(mgr, mgr.GetEventRecorderFor("EnforcingPatch_controller"), true), Log: ctrl.Log.WithName("controllers").WithName("EnforcingPatch"), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "EnforcingPatch") os.Exit(1) } if err = (&controllers.TemplatedEnforcingCRDReconciler{ - EnforcingReconciler: lockedresourcecontroller.NewEnforcingReconciler(mgr, mgr.GetEventRecorderFor("TemplatedEnforcingCRD_controller"), true), + EnforcingReconciler: lockedresourcecontroller.NewFromManager(mgr, mgr.GetEventRecorderFor("TemplatedEnforcingCRD_controller"), true), Log: ctrl.Log.WithName("controllers").WithName("TemplatedEnforcingCRD"), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "TemplatedEnforcingCRD") diff --git a/pkg/util/lockedresourcecontroller/enforcing-reconciler.go b/pkg/util/lockedresourcecontroller/enforcing-reconciler.go index 5283cdc..8e0f32d 100644 --- a/pkg/util/lockedresourcecontroller/enforcing-reconciler.go +++ b/pkg/util/lockedresourcecontroller/enforcing-reconciler.go @@ -12,6 +12,7 @@ import ( "github.com/scylladb/go-set/strset" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/rest" "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" @@ -36,9 +37,9 @@ type EnforcingReconciler struct { // clusterWatcher determines whether the created watchers should be at the cluster level or namespace level. // this affects the kind of permissions needed to run the controller // also creating multiple namespace level permissions can create performance issue as one watch per object type per namespace is opened to the API server, if in doubt pass true here. -func NewEnforcingReconciler(mgr manager.Manager, recorder record.EventRecorder, clusterWatchers bool) EnforcingReconciler { +func NewEnforcingReconciler(client client.Client, scheme *runtime.Scheme, restConfig *rest.Config, apireader client.Reader, recorder record.EventRecorder, clusterWatchers bool) EnforcingReconciler { return EnforcingReconciler{ - ReconcilerBase: util.NewReconcilerBase(mgr, recorder), + ReconcilerBase: util.NewReconcilerBase(client, scheme, restConfig, recorder, apireader), lockedResourceManagers: map[string]*LockedResourceManager{}, statusChange: make(chan event.GenericEvent), lockedResourceManagersMutex: sync.Mutex{}, @@ -47,6 +48,10 @@ func NewEnforcingReconciler(mgr manager.Manager, recorder record.EventRecorder, } } +func NewFromManager(mgr manager.Manager, recorder record.EventRecorder, clusterWatchers bool) EnforcingReconciler { + return NewEnforcingReconciler(mgr.GetClient(), mgr.GetScheme(), mgr.GetConfig(), mgr.GetAPIReader(), recorder, clusterWatchers) +} + //GetStatusChangeChannel returns the channel through which status change events can be received func (er *EnforcingReconciler) GetStatusChangeChannel() <-chan event.GenericEvent { return er.statusChange diff --git a/pkg/util/lockedresourcecontroller/locked-resource-manager.go b/pkg/util/lockedresourcecontroller/locked-resource-manager.go index e7fcc0e..4bb6bf9 100644 --- a/pkg/util/lockedresourcecontroller/locked-resource-manager.go +++ b/pkg/util/lockedresourcecontroller/locked-resource-manager.go @@ -274,7 +274,7 @@ func (lrm *LockedResourceManager) IsSamePatches(patches []lockedpatch.LockedPatc } func (lrm *LockedResourceManager) deleteResources(context context.Context) error { - reconcilerBase := util.NewReconcilerBase(lrm.stoppableManager.Manager, lrm.stoppableManager.GetEventRecorderFor("resource-deleter")) + reconcilerBase := util.NewFromManager(lrm.stoppableManager.Manager, lrm.stoppableManager.GetEventRecorderFor("resource-deleter")) for _, resource := range lrm.GetResources() { gvk := resource.Unstructured.GetObjectKind().GroupVersionKind() groupVersion := schema.GroupVersion{Group: gvk.Group, Version: gvk.Version} diff --git a/pkg/util/lockedresourcecontroller/patch-reconciler.go b/pkg/util/lockedresourcecontroller/patch-reconciler.go index 7b0e7a7..50ce432 100644 --- a/pkg/util/lockedresourcecontroller/patch-reconciler.go +++ b/pkg/util/lockedresourcecontroller/patch-reconciler.go @@ -53,7 +53,7 @@ func NewLockedPatchReconciler(mgr manager.Manager, patch lockedpatch.LockedPatch reconciler := &LockedPatchReconciler{ log: ctrl.Log.WithName(controllername).WithName(apis.GetKeyShort(parentObject)).WithName(patch.GetKey()), - ReconcilerBase: util.NewReconcilerBase(mgr, mgr.GetEventRecorderFor(controllername+"_"+patch.GetKey())), + ReconcilerBase: util.NewFromManager(mgr, mgr.GetEventRecorderFor(controllername+"_"+patch.GetKey())), patch: patch, statusChange: statusChange, parentObject: parentObject, diff --git a/pkg/util/lockedresourcecontroller/resource-reconciler.go b/pkg/util/lockedresourcecontroller/resource-reconciler.go index 8da458d..a2ce610 100644 --- a/pkg/util/lockedresourcecontroller/resource-reconciler.go +++ b/pkg/util/lockedresourcecontroller/resource-reconciler.go @@ -51,7 +51,7 @@ func NewLockedObjectReconciler(mgr manager.Manager, object unstructured.Unstruct reconciler := &LockedResourceReconciler{ log: ctrl.Log.WithName(controllername).WithName(apis.GetKeyShort(parentObject)).WithName(apis.GetKeyLong(&object)), - ReconcilerBase: util.NewReconcilerBase(mgr, mgr.GetEventRecorderFor(controllername+"_"+apis.GetKeyLong(&object))), + ReconcilerBase: util.NewFromManager(mgr, mgr.GetEventRecorderFor(controllername+"_"+apis.GetKeyLong(&object))), Resource: object, ExcludePaths: excludePaths, statusChange: statusChange, diff --git a/pkg/util/reconciler.go b/pkg/util/reconciler.go index 7f65c29..67044fc 100644 --- a/pkg/util/reconciler.go +++ b/pkg/util/reconciler.go @@ -50,18 +50,28 @@ import ( // ... other optional fields ... // } type ReconcilerBase struct { - mgr manager.Manager - recorder record.EventRecorder + apireader client.Reader + client client.Client + scheme *runtime.Scheme + restConfig *rest.Config + recorder record.EventRecorder } -// NewReconcilerBase is a contruction function to create a new ReconcilerBase. -func NewReconcilerBase(mgr manager.Manager, recorder record.EventRecorder) ReconcilerBase { +func NewReconcilerBase(client client.Client, scheme *runtime.Scheme, restConfig *rest.Config, recorder record.EventRecorder, apireader client.Reader) ReconcilerBase { return ReconcilerBase{ - mgr: mgr, - recorder: recorder, + apireader: apireader, + client: client, + scheme: scheme, + restConfig: restConfig, + recorder: recorder, } } +// NewReconcilerBase is a contruction function to create a new ReconcilerBase. +func NewFromManager(mgr manager.Manager, recorder record.EventRecorder) ReconcilerBase { + return NewReconcilerBase(mgr.GetClient(), mgr.GetScheme(), mgr.GetConfig(), recorder, mgr.GetAPIReader()) +} + //IsValid determines if a CR instance is valid. this implementation returns always true, should be overridden func (r *ReconcilerBase) IsValid(obj metav1.Object) (bool, error) { return true, nil @@ -79,12 +89,12 @@ func (r *ReconcilerBase) Reconcile(request reconcile.Request) (reconcile.Result, // GetClient returns the underlying client func (r *ReconcilerBase) GetClient() client.Client { - return r.mgr.GetClient() + return r.client } //GetRestConfig returns the undelying rest config func (r *ReconcilerBase) GetRestConfig() *rest.Config { - return r.mgr.GetConfig() + return r.restConfig } // GetRecorder returns the underlying recorder @@ -94,12 +104,12 @@ func (r *ReconcilerBase) GetRecorder() record.EventRecorder { // GetScheme returns the scheme func (r *ReconcilerBase) GetScheme() *runtime.Scheme { - return r.mgr.GetScheme() + return r.scheme } // GetDiscoveryClient returns a discovery client for the current reconciler func (r *ReconcilerBase) GetDiscoveryClient() (*discovery.DiscoveryClient, error) { - return discovery.NewDiscoveryClientForConfig(r.mgr.GetConfig()) + return discovery.NewDiscoveryClientForConfig(r.GetRestConfig()) } // GetDynamicClientOnAPIResource returns a dynamic client on an APIResource. This client can be further namespaced. @@ -468,7 +478,7 @@ func (r *ReconcilerBase) GetDirectClientWithSchemeBuilders(addToSchemes ...func( // GetAPIReader returns a non cached reader func (r *ReconcilerBase) GetAPIReader() client.Reader { - return r.mgr.GetAPIReader() + return r.apireader } // GetOperatorNamespace tries to infer the operator namespace. I first looks for the /var/run/secrets/kubernetes.io/serviceaccount/namespace file.