diff --git a/controllers/dscinitialization/suite_test.go b/controllers/dscinitialization/suite_test.go index 3c4508ea2a8..146dcf34f8a 100644 --- a/controllers/dscinitialization/suite_test.go +++ b/controllers/dscinitialization/suite_test.go @@ -79,7 +79,6 @@ func TestDataScienceClusterInitialization(t *testing.T) { var testScheme = runtime.NewScheme() -//nolint:fatcontext var _ = BeforeSuite(func() { // can't use suite's context as the manager should survive the function //nolint:fatcontext diff --git a/controllers/webhook/webhook_suite_test.go b/controllers/webhook/webhook_suite_test.go index feb07a2c761..27c5cd811e6 100644 --- a/controllers/webhook/webhook_suite_test.go +++ b/controllers/webhook/webhook_suite_test.go @@ -73,7 +73,6 @@ func TestAPIs(t *testing.T) { RunSpecs(t, "Webhook Suite") } -//nolint:fatcontext var _ = BeforeSuite(func() { // can't use suite's context as the manager should survive the function //nolint:fatcontext diff --git a/pkg/controller/reconciler/reconciler.go b/pkg/controller/reconciler/reconciler.go index 621898691ec..ec1316ab98c 100644 --- a/pkg/controller/reconciler/reconciler.go +++ b/pkg/controller/reconciler/reconciler.go @@ -108,48 +108,80 @@ func (r *Reconciler[T]) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R return ctrl.Result{}, client.IgnoreNotFound(err) } - dscil := dsciv1.DSCInitializationList{} - if err := r.Client.List(ctx, &dscil); err != nil { - return ctrl.Result{}, err + if !res.GetDeletionTimestamp().IsZero() { + if err := r.delete(ctx, res); err != nil { + return ctrl.Result{}, err + } } - if len(dscil.Items) != 1 { - return ctrl.Result{}, errors.New("unable to find DSCInitialization") + if err := r.apply(ctx, res); err != nil { + return ctrl.Result{}, err } + return ctrl.Result{}, nil +} + +func (r *Reconciler[T]) delete(ctx context.Context, res client.Object) error { + l := log.FromContext(ctx) + l.Info("delete") + rr := types.ReconciliationRequest{ Client: r.Client, Manager: r.m, Instance: res, - DSCI: &dscil.Items[0], Release: r.Release, Manifests: make([]types.ManifestInfo, 0), + + // The DSCI should not be required when deleting a component, if the + // component requires some additional info, then such info should be + // stored as part of the spec/status + DSCI: nil, } - // Handle deletion - if !res.GetDeletionTimestamp().IsZero() { - // Execute finalizers - for _, action := range r.Finalizer { - l.V(3).Info("Executing finalizer", "action", action) - - actx := log.IntoContext( - ctx, - l.WithName(actions.ActionGroup).WithName(action.String()), - ) - - if err := action(actx, &rr); err != nil { - se := odherrors.StopError{} - if !errors.As(err, &se) { - l.Error(err, "Failed to execute finalizer", "action", action) - return ctrl.Result{}, err - } - - l.V(3).Info("detected stop marker", "action", action) - break + // Execute finalizers + for _, action := range r.Finalizer { + l.V(3).Info("Executing finalizer", "action", action) + + actx := log.IntoContext( + ctx, + l.WithName(actions.ActionGroup).WithName(action.String()), + ) + + if err := action(actx, &rr); err != nil { + se := odherrors.StopError{} + if !errors.As(err, &se) { + l.Error(err, "Failed to execute finalizer", "action", action) + return err } + + l.V(3).Info("detected stop marker", "action", action) + break } + } + + return nil +} - return ctrl.Result{}, nil +func (r *Reconciler[T]) apply(ctx context.Context, res client.Object) error { + l := log.FromContext(ctx) + l.Info("apply") + + dscil := dsciv1.DSCInitializationList{} + if err := r.Client.List(ctx, &dscil); err != nil { + return err + } + + if len(dscil.Items) != 1 { + return errors.New("unable to find DSCInitialization") + } + + rr := types.ReconciliationRequest{ + Client: r.Client, + Manager: r.m, + Instance: res, + DSCI: &dscil.Items[0], + Release: r.Release, + Manifests: make([]types.ManifestInfo, 0), } // Execute actions @@ -165,7 +197,7 @@ func (r *Reconciler[T]) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R se := odherrors.StopError{} if !errors.As(err, &se) { l.Error(err, "Failed to execute action", "action", action) - return ctrl.Result{}, err + return err } l.V(3).Info("detected stop marker", "action", action) @@ -173,7 +205,7 @@ func (r *Reconciler[T]) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R } } - err = r.Client.ApplyStatus( + err := r.Client.ApplyStatus( ctx, rr.Instance, client.FieldOwner(r.name), @@ -181,8 +213,8 @@ func (r *Reconciler[T]) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R ) if err != nil { - return ctrl.Result{}, err + return err } - return ctrl.Result{}, err + return nil }