Skip to content

Commit

Permalink
Erase the delete conditions and refactor some code to fix the reconci…
Browse files Browse the repository at this point in the history
…le flow

Co-authored-by: Alby Hernández <[email protected]>
  • Loading branch information
sebastian.vargas and achetronic committed Mar 2, 2022
1 parent 7286758 commit 916e726
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 35 deletions.
29 changes: 13 additions & 16 deletions controllers/replika_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package controllers

import (
"context"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"time"
Expand Down Expand Up @@ -62,9 +61,8 @@ func (r *ReplikaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
result = ctrl.Result{}

// 2.1 It does NOT exist: manage removal
if errors.IsNotFound(err) {
if err = client.IgnoreNotFound(err); err == nil {
LogInfof(ctx, "Replika resource not found. Ignoring since object must be deleted.")
err = nil
return result, err
}

Expand All @@ -73,17 +71,8 @@ func (r *ReplikaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
return result, err
}

// 3. Update the status before the requeue
defer func() {
err = r.Status().Update(ctx, replikaManifest)
if err != nil {
LogInfof(ctx, "Failed to update the conditionnnnnnn on replika: %s", req.Name)
}
}()

// 4. Check if the Replika instance is marked to be deleted: indicated by the deletion timestamp being set
isReplikaMarkedToBeDeleted := replikaManifest.GetDeletionTimestamp() != nil
if isReplikaMarkedToBeDeleted {
if !replikaManifest.DeletionTimestamp.IsZero() {
if controllerutil.ContainsFinalizer(replikaManifest, replikaFinalizer) {
// Delete all created targets
err = r.DeleteTargets(ctx, replikaManifest)
Expand All @@ -96,12 +85,20 @@ func (r *ReplikaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
controllerutil.RemoveFinalizer(replikaManifest, replikaFinalizer)
err = r.Update(ctx, replikaManifest)
if err != nil {
return result, err
LogInfof(ctx, "Failed to update finalizer of replika: %s", req.Name)
}
}
return result, err
}

// 3. Update the status before the requeue
defer func() {
err = r.Status().Update(ctx, replikaManifest)
if err != nil {
LogInfof(ctx, "Failed to update the condition on replika: %s", req.Name)
}
}()

// 5. Add finalizer to the Replika CR
if !controllerutil.ContainsFinalizer(replikaManifest, replikaFinalizer) {
controllerutil.AddFinalizer(replikaManifest, replikaFinalizer)
Expand All @@ -114,14 +111,14 @@ func (r *ReplikaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
// 6. The Replika CR already exist: manage the update
err = r.UpdateTargets(ctx, replikaManifest)
if err != nil {
LogInfof(ctx, "Can not update the targets for the Replika: "+replikaManifest.Name)
LogInfof(ctx, "Can not update the targets for the Replika: %s", replikaManifest.Name)
return result, err
}

// 7. Schedule periodical request
RequeueTime, err := r.GetSynchronizationTime(replikaManifest)
if err != nil {
LogInfof(ctx, "Can not requeue the Replika: "+replikaManifest.Name)
LogInfof(ctx, "Can not requeue the Replika: %s", replikaManifest.Name)
}
result.RequeueAfter = RequeueTime

Expand Down
8 changes: 0 additions & 8 deletions controllers/replika_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ const (
ConditionReasonSourceReplicationFailed = "SourceReplicationFailed"
ConditionReasonSourceReplicationFailedMessage = "Error replicating the source on targets"

// Get existent target list
ConditionReasonTargetGetListFailed = "TargetGetListFailed"
ConditionReasonTargetGetListFailedMessage = "Error getting the targets from the cluster"

// Target delete failed
ConditionReasonTargetDeleteFailed = "TargetDeleteFailed"
ConditionReasonTargetDeleteFailedMessage = "Target resource failed on deletion"

// Success
ConditionReasonSourceSynced = "SourceSynced"
ConditionReasonSourceSyncedMessage = "Source was successfully synchronized"
Expand Down
11 changes: 0 additions & 11 deletions controllers/replika_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,24 +257,13 @@ func (r *ReplikaReconciler) DeleteTargets(ctx context.Context, replika *replikav
// Look for the targets inside the cluster
err = r.List(ctx, targets, client.MatchingLabels{resourceReplikaLabelPartOfKey: replika.Name})
if err != nil {
r.UpdateReplikaCondition(replika, r.NewReplikaCondition(ConditionTypeSourceSynced,
metav1.ConditionFalse,
ConditionReasonTargetGetListFailed,
ConditionReasonTargetGetListFailedMessage,
))
return err
}

// Delete the targets
for i := range targets.Items {
err = r.Delete(ctx, &targets.Items[i])
if err != nil {
r.UpdateReplikaCondition(replika, r.NewReplikaCondition(ConditionTypeSourceSynced,
metav1.ConditionFalse,
ConditionReasonTargetDeleteFailed,
ConditionReasonTargetDeleteFailedMessage,
))

return err
}
}
Expand Down

0 comments on commit 916e726

Please sign in to comment.