Skip to content

Commit

Permalink
added unit test-friendly constructor for ReconcilerBase (#58)
Browse files Browse the repository at this point in the history
Signed-off-by: raffaelespazzoli <[email protected]>
  • Loading branch information
raffaelespazzoli authored Dec 29, 2020
1 parent 7c36d9b commit b9ddde6
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 20 deletions.
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 7 additions & 2 deletions pkg/util/lockedresourcecontroller/enforcing-reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{},
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/lockedresourcecontroller/patch-reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/lockedresourcecontroller/resource-reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 21 additions & 11 deletions pkg/util/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit b9ddde6

Please sign in to comment.