Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
YZ775 committed Aug 1, 2023
1 parent 4e1672d commit 8be729b
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 149 deletions.
9 changes: 5 additions & 4 deletions api/v1beta1/mysqlcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,11 @@ type MySQLClusterStatus struct {
}

const (
ConditionInitialized string = "Initialized"
ConditionAvailable string = "Available"
ConditionHealthy string = "Healthy"
ConditionUpToDate string = "UpToDate"
ConditionInitialized string = "Initialized"
ConditionAvailable string = "Available"
ConditionHealthy string = "Healthy"
ConditionStatefulSetReady string = "StatefulSetReady"
ConditionReconcileSuccess string = "ReconcileSuccess"
)

// BackupStatus represents the status of the last successful backup.
Expand Down
9 changes: 5 additions & 4 deletions api/v1beta2/mysqlcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,11 @@ type MySQLClusterStatus struct {
}

const (
ConditionInitialized string = "Initialized"
ConditionAvailable string = "Available"
ConditionHealthy string = "Healthy"
ConditionUpToDate string = "UpToDate"
ConditionInitialized string = "Initialized"
ConditionAvailable string = "Available"
ConditionHealthy string = "Healthy"
ConditionStatefulSetReady string = "StatefulSetReady"
ConditionReconcileSuccess string = "ReconcileSuccess"
)

// BackupStatus represents the status of the last successful backup.
Expand Down
82 changes: 54 additions & 28 deletions controllers/mysqlcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,23 @@ func (r *MySQLClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request
reconciler = r.reconcileV1
}
}
return reconciler(ctx, req, cluster)
result, err := reconciler(ctx, req, cluster)
if err2 := r.updateReconcileStatus(ctx, cluster, err); err2 != nil {
log.Error(err2, "failed to update reconcile status")
}
return result, err
}

func (r *MySQLClusterReconciler) reconcileV1(ctx context.Context, req ctrl.Request, cluster *mocov1beta2.MySQLCluster) (ctrl.Result, error) {
func (r *MySQLClusterReconciler) reconcileV1(ctx context.Context, req ctrl.Request, cluster *mocov1beta2.MySQLCluster) (result ctrl.Result, err error) {
log := crlog.FromContext(ctx)

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

if cluster.DeletionTimestamp != nil {
if !controllerutil.ContainsFinalizer(cluster, constants.MySQLClusterFinalizer) {
return ctrl.Result{}, nil
Expand Down Expand Up @@ -239,9 +250,6 @@ func (r *MySQLClusterReconciler) reconcileV1(ctx context.Context, req ctrl.Reque
log.Error(err, "failed to reconcile stateful set")
return ctrl.Result{}, err
}
if err := r.UpdateStatusByStatefulSet(ctx, cluster); err != nil {
return ctrl.Result{}, err
}

if err := r.reconcileV1PDB(ctx, req, cluster); err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -1866,7 +1874,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) updateStatusByStatefulSet(ctx context.Context, cluster *mocov1beta2.MySQLCluster) error {
log := crlog.FromContext(ctx)

var sts appsv1.StatefulSet
Expand All @@ -1875,33 +1883,51 @@ func (r *MySQLClusterReconciler) UpdateStatusByStatefulSet(ctx context.Context,
return fmt.Errorf("failed to get StatefulSet %s/%s: %w", cluster.Namespace, cluster.PrefixedName(), err)
}

updated := metav1.ConditionFalse
reason := "UpdateInProgress"
message := "the current state is UpdateInProgress"
if sts.Spec.UpdateStrategy.Type == appsv1.RollingUpdateStatefulSetStrategyType && sts.Spec.UpdateStrategy.RollingUpdate != nil {
if sts.Spec.Replicas != nil && sts.Spec.UpdateStrategy.RollingUpdate.Partition != nil {
message = "UpdateInProgress, Partition is enabled, waiting for the all pods to be updated"
if sts.Status.UpdatedReplicas < (*sts.Spec.Replicas - *sts.Spec.UpdateStrategy.RollingUpdate.Partition) {
message = "UpdateInProgress, Partition is enabled, waiting for partitioned roll out to finish"
}
}
}
if sts.Status.AvailableReplicas != 0 && sts.Status.CurrentRevision == sts.Status.UpdateRevision && sts.Generation == sts.Status.ObservedGeneration {
updated = metav1.ConditionTrue
reason = "UpdateCompleted"
message = "the current state is UpdateCompleted"
stsReady := metav1.ConditionFalse
reason := "StatefulSetNotReady"
if sts.Spec.Replicas == nil {
stsReady = metav1.ConditionFalse
reason = "StatefulSetNotReady"
} else if sts.Status.AvailableReplicas == *sts.Spec.Replicas && sts.Status.CurrentRevision == sts.Status.UpdateRevision && sts.Generation == sts.Status.ObservedGeneration {
stsReady = metav1.ConditionTrue
reason = "AllNewStatefulSetReady"
}

currentConditionUpToDate := meta.FindStatusCondition(cluster.Status.Conditions, mocov1beta2.ConditionUpToDate)
currentConditionStatefulSetReady := meta.FindStatusCondition(cluster.Status.Conditions, mocov1beta2.ConditionStatefulSetReady)
// if current status and new status are different, update status
needUpdate := false
if currentConditionUpToDate == nil || currentConditionUpToDate.Status != updated || currentConditionUpToDate.Message != message {
needUpdate = true
if currentConditionStatefulSetReady == nil || currentConditionStatefulSetReady.Status != stsReady || currentConditionStatefulSetReady.ObservedGeneration != cluster.Generation {
cond := metav1.Condition{
Type: mocov1beta2.ConditionStatefulSetReady,
Status: stsReady,
ObservedGeneration: cluster.Generation,
Reason: reason,
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 reconciliation info")
return err
}
log.Info("update status successfully")
}
if needUpdate {
return nil
}

func (r *MySQLClusterReconciler) updateReconcileStatus(ctx context.Context, cluster *mocov1beta2.MySQLCluster, reconcileErr error) error {
log := crlog.FromContext(ctx)
reason := "ReconcileFailed"
message := "reconcile failed"
success := metav1.ConditionFalse
if reconcileErr == nil {
reason = "ReconcileSuccess"
message = "reconcile successfully"
success = metav1.ConditionTrue
}
currentConditionReconcileSuccess := meta.FindStatusCondition(cluster.Status.Conditions, mocov1beta2.ConditionReconcileSuccess)
if currentConditionReconcileSuccess == nil || currentConditionReconcileSuccess.Status != success || currentConditionReconcileSuccess.ObservedGeneration != cluster.Generation {
cond := metav1.Condition{
Type: mocov1beta2.ConditionUpToDate,
Status: updated,
Type: mocov1beta2.ConditionReconcileSuccess,
Status: success,
ObservedGeneration: cluster.Generation,
Reason: reason,
Message: message,
Expand Down
Loading

0 comments on commit 8be729b

Please sign in to comment.