Skip to content

Commit

Permalink
fix: forward context in SpaceBindingCleanup reconciler (#897)
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Ilario <filario@redhat.com>
Co-authored-by: Francisc Munteanu <fmuntean@redhat.com>
Co-authored-by: Alexey Kazakov <alkazako@redhat.com>
  • Loading branch information
3 people authored Oct 24, 2023
1 parent 15c9318 commit 44a04c6
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions controllers/spacebindingcleanup/spacebinding_cleanup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.

// Fetch the SpaceBinding instance
spaceBinding := &toolchainv1alpha1.SpaceBinding{}
err := r.Client.Get(context.TODO(), request.NamespacedName, spaceBinding)
err := r.Client.Get(ctx, request.NamespacedName, spaceBinding)
if err != nil {
if errors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
Expand All @@ -77,10 +77,10 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.

spaceName := types.NamespacedName{Namespace: spaceBinding.Namespace, Name: spaceBinding.Spec.Space}
space := &toolchainv1alpha1.Space{}
if err := r.Client.Get(context.TODO(), spaceName, space); err != nil {
if err := r.Client.Get(ctx, spaceName, space); err != nil {
if errors.IsNotFound(err) {
logger.Info("the Space was not found", "Space", spaceBinding.Spec.Space)
requeueAfter, err := r.deleteSpaceBinding(logger, spaceBinding)
requeueAfter, err := r.deleteSpaceBinding(ctx, spaceBinding)
return ctrl.Result{
RequeueAfter: requeueAfter,
}, err
Expand All @@ -91,10 +91,10 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.

murName := types.NamespacedName{Namespace: spaceBinding.Namespace, Name: spaceBinding.Spec.MasterUserRecord}
mur := &toolchainv1alpha1.MasterUserRecord{}
if err := r.Client.Get(context.TODO(), murName, mur); err != nil {
if err := r.Client.Get(ctx, murName, mur); err != nil {
if errors.IsNotFound(err) {
logger.Info("the MUR was not found", "MasterUserRecord", spaceBinding.Spec.MasterUserRecord)
requeueAfter, err := r.deleteSpaceBinding(logger, spaceBinding)
requeueAfter, err := r.deleteSpaceBinding(ctx, spaceBinding)
return ctrl.Result{
RequeueAfter: requeueAfter,
}, err
Expand All @@ -106,7 +106,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
return ctrl.Result{}, nil
}

func (r *Reconciler) deleteSpaceBinding(logger logr.Logger, spaceBinding *toolchainv1alpha1.SpaceBinding) (time.Duration, error) {
func (r *Reconciler) deleteSpaceBinding(ctx context.Context, spaceBinding *toolchainv1alpha1.SpaceBinding) (time.Duration, error) {
logger := log.FromContext(ctx)
logger.Info("deleting the SpaceBinding")

// check if spaceBinding was created from SpaceBindingRequest,
Expand All @@ -116,11 +117,11 @@ func (r *Reconciler) deleteSpaceBinding(logger logr.Logger, spaceBinding *toolch
if err := r.logErrorIfSBRisDisabled(logger, spaceBindingReqeuestAssociated); err != nil {
return norequeue, err
}
return r.deleteSpaceBindingRequest(logger, spaceBindingReqeuestAssociated)
return r.deleteSpaceBindingRequest(ctx, spaceBindingReqeuestAssociated)
}

// otherwise delete the SpaceBinding resource directly ...
return norequeue, r.deleteSpaceBindingResource(spaceBinding)
return norequeue, r.deleteSpaceBindingResource(ctx, spaceBinding)
}

func (r *Reconciler) logErrorIfSBRisDisabled(logger logr.Logger, spaceBindingReqeuestAssociated *SpaceBindingRequestAssociated) error {
Expand All @@ -138,14 +139,16 @@ func (r *Reconciler) logErrorIfSBRisDisabled(logger logr.Logger, spaceBindingReq
return nil
}

func (r *Reconciler) deleteSpaceBindingResource(spaceBinding *toolchainv1alpha1.SpaceBinding) error {
if err := r.Delete(context.TODO(), spaceBinding); err != nil {
func (r *Reconciler) deleteSpaceBindingResource(ctx context.Context, spaceBinding *toolchainv1alpha1.SpaceBinding) error {
if err := r.Delete(ctx, spaceBinding); err != nil {
return errs.Wrapf(err, "unable to delete the SpaceBinding")
}
return nil
}

func (r *Reconciler) deleteSpaceBindingRequest(logger logr.Logger, sbrAssociated *SpaceBindingRequestAssociated) (time.Duration, error) {
func (r *Reconciler) deleteSpaceBindingRequest(ctx context.Context, sbrAssociated *SpaceBindingRequestAssociated) (time.Duration, error) {
logger := log.FromContext(ctx)

spaceBindingRequest := &toolchainv1alpha1.SpaceBindingRequest{}
memberClusterWithSpaceBindingRequest, found, err := cluster.LookupMember(r.MemberClusters, types.NamespacedName{
Namespace: sbrAssociated.namespace,
Expand All @@ -162,11 +165,11 @@ func (r *Reconciler) deleteSpaceBindingRequest(logger logr.Logger, sbrAssociated
// let's just log the info
logger.Info("unable to find SpaceBindingRequest", "SpaceBindingRequest.Name", sbrAssociated.name, "SpaceBindingRequest.Namespace", sbrAssociated.namespace)
// try and delete the SpaceBinding since SBR was not found
return norequeue, r.deleteSpaceBindingResource(sbrAssociated.spaceBinding)
return norequeue, r.deleteSpaceBindingResource(ctx, sbrAssociated.spaceBinding)
}

// delete the SBR
if err := memberClusterWithSpaceBindingRequest.Client.Delete(context.TODO(), spaceBindingRequest); err != nil {
if err := memberClusterWithSpaceBindingRequest.Client.Delete(ctx, spaceBindingRequest); err != nil {
return norequeue, errs.Wrapf(err, "unable to delete the SpaceBindingRequest")
}
return requeueDelay, nil
Expand Down

0 comments on commit 44a04c6

Please sign in to comment.