Skip to content

Commit

Permalink
join update status funcion
Browse files Browse the repository at this point in the history
Signed-off-by: YZ775 <[email protected]>
  • Loading branch information
YZ775 committed Aug 9, 2023
1 parent 3ebb2e9 commit 3eecae8
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions controllers/mysqlcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,10 @@ func (r *MySQLClusterReconciler) reconcileV1(ctx context.Context, req ctrl.Reque
log := crlog.FromContext(ctx)

defer func(ctx2 context.Context, cluster2 *mocov1beta2.MySQLCluster) {
if err2 := r.updateStatusByStatefulSet(ctx2, cluster2); err2 != nil {
if err2 := r.updateStatus(ctx2, cluster2, err); err2 != nil {
err = err2
log.Error(err2, "failed to update status")
}
if err2 := r.updateReconcileStatus(ctx, cluster, err); err2 != nil {
log.Error(err2, "failed to exec updateReconcileStatus()")
err = err2
}
}(ctx, cluster)

if cluster.DeletionTimestamp != nil {
Expand Down Expand Up @@ -1874,24 +1870,22 @@ func (r *MySQLClusterReconciler) finalizeV1(ctx context.Context, cluster *mocov1
return nil
}

func (r *MySQLClusterReconciler) updateStatusByStatefulSet(ctx context.Context, cluster *mocov1beta2.MySQLCluster) error {
func (r *MySQLClusterReconciler) updateStatus(ctx context.Context, cluster *mocov1beta2.MySQLCluster, reconcileErr error) error {
log := crlog.FromContext(ctx)

var sts appsv1.StatefulSet
err := r.Get(ctx, client.ObjectKey{Namespace: cluster.Namespace, Name: cluster.PrefixedName()}, &sts)
if err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("failed to get StatefulSet %s/%s: %w", cluster.Namespace, cluster.PrefixedName(), err)
}

needUpdate := false
stsReady := metav1.ConditionFalse
reason := "StatefulSetNotReady"
if sts.Spec.Replicas != nil && sts.Status.AvailableReplicas == *sts.Spec.Replicas && sts.Status.CurrentRevision == sts.Status.UpdateRevision && sts.Generation == sts.Status.ObservedGeneration {
stsReady = metav1.ConditionTrue
reason = "StatefulSetReady"
}

currentConditionStatefulSetReady := meta.FindStatusCondition(cluster.Status.Conditions, mocov1beta2.ConditionStatefulSetReady)
// if current status and new status are different, update status
if currentConditionStatefulSetReady == nil || currentConditionStatefulSetReady.Status != stsReady || currentConditionStatefulSetReady.ObservedGeneration != cluster.Generation {
cond := metav1.Condition{
Type: mocov1beta2.ConditionStatefulSetReady,
Expand All @@ -1901,38 +1895,36 @@ func (r *MySQLClusterReconciler) updateStatusByStatefulSet(ctx context.Context,
Message: "the current state is " + reason,
}
meta.SetStatusCondition(&cluster.Status.Conditions, cond)
if err := r.Status().Update(ctx, cluster); err != nil {
log.Error(err, "failed to update ConditionStatefulSetReady")
return err
}
log.Info("update ConditionStatefulSetReady successfully")
needUpdate = true
}
return nil
}

func (r *MySQLClusterReconciler) updateReconcileStatus(ctx context.Context, cluster *mocov1beta2.MySQLCluster, reconcileErr error) error {
log := crlog.FromContext(ctx)
reason := "ReconcileFailed"
reason = "ReconcileFailed"
message := "reconcile failed"
success := metav1.ConditionFalse
if reconcileErr == nil {
reason = "ReconcileSuccess"
message = "reconcile successfully"
success = metav1.ConditionTrue
}
cond := metav1.Condition{
Type: mocov1beta2.ConditionReconcileSuccess,
Status: success,
ObservedGeneration: cluster.Generation,
Reason: reason,
Message: message,
currentConditionReconcileSuccess := meta.FindStatusCondition(cluster.Status.Conditions, mocov1beta2.ConditionReconcileSuccess)
if currentConditionReconcileSuccess == nil || currentConditionReconcileSuccess.Status != success || currentConditionReconcileSuccess.ObservedGeneration != cluster.Generation {
cond := metav1.Condition{
Type: mocov1beta2.ConditionReconcileSuccess,
Status: success,
ObservedGeneration: cluster.Generation,
Reason: reason,
Message: message,
}
meta.SetStatusCondition(&cluster.Status.Conditions, cond)
needUpdate = true
}
meta.SetStatusCondition(&cluster.Status.Conditions, cond)
if err := r.Status().Update(ctx, cluster); err != nil {
log.Error(err, "failed to update reconcile status")
return err
if needUpdate {
if err := r.Status().Update(ctx, cluster); err != nil {
log.Error(err, "failed to update status")
return err
}
log.Info("update status successfully")
}
log.Info("update reconcile status successfully")
return nil
}

Expand Down

0 comments on commit 3eecae8

Please sign in to comment.