Skip to content

Commit

Permalink
chore: remove generics from Reconcier and use PlatformObject as base …
Browse files Browse the repository at this point in the history
…object type

- remove generics from Reconciler struct as it is not needed
- set PlatformObject as the base obnject that the Reconciler handles to
  ensure the reconciler can access to common objects fields
  • Loading branch information
lburgazzoli committed Jan 17, 2025
1 parent e1457ab commit 6bd5ff2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
28 changes: 14 additions & 14 deletions pkg/controller/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

// Reconciler provides generic reconciliation functionality for ODH objects.
type Reconciler[T common.PlatformObject] struct {
type Reconciler struct {
Client *odhClient.Client
Scheme *runtime.Scheme
Actions []actions.Fn
Expand All @@ -39,25 +39,25 @@ type Reconciler[T common.PlatformObject] struct {

name string
m *odhManager.Manager
instanceFactory func() (T, error)
instanceFactory func() (common.PlatformObject, error)
}

// NewReconciler creates a new reconciler for the given type.
func NewReconciler[T common.PlatformObject](mgr manager.Manager, name string, object T) (*Reconciler[T], error) {
func NewReconciler[T common.PlatformObject](mgr manager.Manager, name string, object T) (*Reconciler, error) {
oc, err := odhClient.NewFromManager(mgr)
if err != nil {
return nil, err
}

cc := Reconciler[T]{
cc := Reconciler{
Client: oc,
Scheme: mgr.GetScheme(),
Log: ctrl.Log.WithName("controllers").WithName(name),
Recorder: mgr.GetEventRecorderFor(name),
Release: cluster.GetRelease(),
name: name,
m: odhManager.New(mgr),
instanceFactory: func() (T, error) {
instanceFactory: func() (common.PlatformObject, error) {
t := reflect.TypeOf(object).Elem()
res, ok := reflect.New(t).Interface().(T)
if !ok {
Expand All @@ -71,31 +71,31 @@ func NewReconciler[T common.PlatformObject](mgr manager.Manager, name string, ob
return &cc, nil
}

func (r *Reconciler[T]) GetRelease() cluster.Release {
func (r *Reconciler) GetRelease() cluster.Release {
return r.Release
}

func (r *Reconciler[T]) GetLogger() logr.Logger {
func (r *Reconciler) GetLogger() logr.Logger {
return r.Log
}

func (r *Reconciler[T]) AddOwnedType(gvk schema.GroupVersionKind) {
func (r *Reconciler) AddOwnedType(gvk schema.GroupVersionKind) {
r.m.AddGVK(gvk, true)
}

func (r *Reconciler[T]) Owns(obj client.Object) bool {
func (r *Reconciler) Owns(obj client.Object) bool {
return r.m.Owns(obj.GetObjectKind().GroupVersionKind())
}

func (r *Reconciler[T]) AddAction(action actions.Fn) {
func (r *Reconciler) AddAction(action actions.Fn) {
r.Actions = append(r.Actions, action)
}

func (r *Reconciler[T]) AddFinalizer(action actions.Fn) {
func (r *Reconciler) AddFinalizer(action actions.Fn) {
r.Finalizer = append(r.Finalizer, action)
}

func (r *Reconciler[T]) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
l := log.FromContext(ctx)
l.Info("reconcile")

Expand All @@ -121,7 +121,7 @@ func (r *Reconciler[T]) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
return ctrl.Result{}, nil
}

func (r *Reconciler[T]) delete(ctx context.Context, res client.Object) error {
func (r *Reconciler) delete(ctx context.Context, res common.PlatformObject) error {
l := log.FromContext(ctx)
l.Info("delete")

Expand Down Expand Up @@ -162,7 +162,7 @@ func (r *Reconciler[T]) delete(ctx context.Context, res client.Object) error {
return nil
}

func (r *Reconciler[T]) apply(ctx context.Context, res client.Object) error {
func (r *Reconciler) apply(ctx context.Context, res common.PlatformObject) error {
l := log.FromContext(ctx)
l.Info("apply")

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/reconciler/reconciler_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (b *ReconcilerBuilder[T]) OwnsGVK(gvk schema.GroupVersionKind, opts ...Watc
return b.Owns(resources.GvkToUnstructured(gvk), opts...)
}

func (b *ReconcilerBuilder[T]) Build(_ context.Context) (*Reconciler[T], error) {
func (b *ReconcilerBuilder[T]) Build(_ context.Context) (*Reconciler, error) {
if b.errors != nil {
return nil, b.errors
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type ReconciliationRequest struct {
*odhClient.Client

Manager *manager.Manager
Instance client.Object
Instance common.PlatformObject
DSCI *dsciv1.DSCInitialization
Release cluster.Release
Manifests []ManifestInfo
Expand Down

0 comments on commit 6bd5ff2

Please sign in to comment.