diff --git a/controllers/mysqlcluster_controller.go b/controllers/mysqlcluster_controller.go index d93b75bc1..c13c2c4c4 100644 --- a/controllers/mysqlcluster_controller.go +++ b/controllers/mysqlcluster_controller.go @@ -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 { @@ -1874,7 +1870,7 @@ 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 @@ -1882,16 +1878,14 @@ func (r *MySQLClusterReconciler) updateStatusByStatefulSet(ctx context.Context, 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, @@ -1901,18 +1895,10 @@ 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 { @@ -1920,19 +1906,25 @@ func (r *MySQLClusterReconciler) updateReconcileStatus(ctx context.Context, clus 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 }