Skip to content

Commit

Permalink
Fix the status update
Browse files Browse the repository at this point in the history
Co-authored-by: Alby Hernández <donfumero@gmail.com>
  • Loading branch information
sebastian.vargas and achetronic committed Mar 10, 2022
1 parent bef9a22 commit beedf19
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 77 deletions.
2 changes: 1 addition & 1 deletion config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ kind: Kustomization
images:
- name: controller
newName: prosimcorp/replika
newTag: v0.2.3
newTag: v0.2.4
57 changes: 22 additions & 35 deletions controllers/replika_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ import (
const (
defaultSyncTimeForExitWithError = 10 * time.Second
scheduleSynchronization = "Schedule synchronization in: %s"
replikaNotFoundError = "Replika resource not found. Ignoring since object must be deleted."
replikaRetrievalError = "Error getting the Replika from the cluster"
targetsDeletionError = "Unable to delete the targets"
replikaFinalizersUpdateError = "Failed to update finalizer of replika: %s"
replikaConditionUpdateError = "Failed to update the condition on replika: %s"
replikaSyncTimeRetrievalError = "Can not get synchronization time from the Replika: %s"
updateTargetsError = "Can not update the targets for the Replika: %s"
)

// ReplikaReconciler reconciles a Replika object
Expand All @@ -51,34 +58,21 @@ type ReplikaReconciler struct {
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.11.0/pkg/reconcile
func (r *ReplikaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, err error) {

//_ = log.FromContext(ctx)

//result = ctrl.Result{
// //RequeueAfter: defaultSyncTimeForExitWithError,
// RequeueAfter: 20 * time.Second,
//}

//LogInfof(ctx, "mensajito")

//log.Log.Info("sdadsadsadsad")
//return ctrl.Result{Requeue: false}, nil

//1. Get the content of the Replika
replikaManifest := &replikav1alpha1.Replika{}
err = r.Get(ctx, req.NamespacedName, replikaManifest)

// 2. Check existance on the cluster
if err != nil {
//result = ctrl.Result{}

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

// 2.2 Failed to get the resource, requeue the request
LogInfof(ctx, "Error getting the Replika from the cluster")
LogInfof(ctx, replikaRetrievalError)
return result, err
}

Expand All @@ -88,15 +82,15 @@ func (r *ReplikaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re
// Delete all created targets
err = r.DeleteTargets(ctx, replikaManifest)
if err != nil {
LogInfof(ctx, "Unable to delete the targets")
LogInfof(ctx, targetsDeletionError)
return result, err
}

// Remove the finalizers on Replika CR
controllerutil.RemoveFinalizer(replikaManifest, replikaFinalizer)
err = r.Update(ctx, replikaManifest)
if err != nil {
LogInfof(ctx, "Failed to update finalizer of replika: %s", req.Name)
LogInfof(ctx, replikaFinalizersUpdateError, req.Name)
}
}
result = ctrl.Result{}
Expand All @@ -115,37 +109,30 @@ func (r *ReplikaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re

// 5. Update the status before the requeue
defer func() {
equal := true
equal, err = r.SameReplikaConditions(ctx, req, replikaManifest)
if equal {
return
}

err = r.Status().Update(ctx, replikaManifest)
if err != nil {
LogInfof(ctx, "Failed to update the condition on replika: %s", req.Name)
LogInfof(ctx, replikaConditionUpdateError, req.Name)
}
}()

// 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: %s", replikaManifest.Name)
LogErrorf(ctx, err, "MEMEMEEM")
return result, err
}

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

// 7. The Replika CR already exist: manage the update
err = r.UpdateTargets(ctx, replikaManifest)
if err != nil {
LogInfof(ctx, updateTargetsError, replikaManifest.Name)

return result, err
}

// 8. Success, update the status
r.UpdateReplikaCondition(replikaManifest, r.NewReplikaCondition(ConditionTypeSourceSynced,
metav1.ConditionTrue,
Expand Down
40 changes: 0 additions & 40 deletions controllers/replika_status.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package controllers

import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
replikav1alpha1 "prosimcorp.com/replika/api/v1alpha1"
ctrl "sigs.k8s.io/controller-runtime"
)

// https://github.com/external-secrets/external-secrets/blob/80545f4f183795ef193747fc959558c761b51c99/apis/externalsecrets/v1alpha1/externalsecret_types.go#L168
Expand Down Expand Up @@ -68,41 +66,3 @@ func (r *ReplikaReconciler) UpdateReplikaCondition(replika *replikav1alpha1.Repl
currentCondition.LastTransitionTime = metav1.Now()
}
}

// SameReplikaConditions compare the conditions of the replika in the cluster with the replica in the reconcile process
func (r *ReplikaReconciler) SameReplikaConditions(ctx context.Context, req ctrl.Request, replika *replikav1alpha1.Replika) (equal bool, err error) {
equal = true

oldReplika := &replikav1alpha1.Replika{}
err = r.Get(ctx, req.NamespacedName, oldReplika)
if err != nil {
LogInfof(ctx, "Error getting the Replika from the cluster")
return equal, err
}

for i := range replika.Status.Conditions {
for j := range oldReplika.Status.Conditions {
sameTypes := replika.Status.Conditions[i].Type == oldReplika.Status.Conditions[j].Type
differentStatus := replika.Status.Conditions[i].Status != oldReplika.Status.Conditions[j].Status
differentReason := replika.Status.Conditions[i].Reason != oldReplika.Status.Conditions[j].Reason
differentMessage := replika.Status.Conditions[i].Message != oldReplika.Status.Conditions[j].Message
if sameTypes && (differentStatus || differentReason || differentMessage) {
equal = false
return equal, err
}
}
}

return equal, err
}

func (r *ReplikaReconciler) UpdateStatus(ctx context.Context, req ctrl.Request, replika *replikav1alpha1.Replika) error {
equal, err := r.SameReplikaConditions(ctx, req, replika)
if equal {
return err
}

err = r.Status().Update(ctx, replika)

return err
}
2 changes: 1 addition & 1 deletion deploy/deployment/replika-controller-manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ spec:
- --leader-elect
command:
- /manager
image: prosimcorp/replika:v0.2.3
image: prosimcorp/replika:v0.2.4
livenessProbe:
httpGet:
path: /healthz
Expand Down

0 comments on commit beedf19

Please sign in to comment.