Skip to content

Commit

Permalink
controller,utils: fix the bug for infinite loop radondb#201
Browse files Browse the repository at this point in the history
  • Loading branch information
acekingke committed Aug 30, 2021
1 parent 6d834b7 commit fb79070
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
28 changes: 28 additions & 0 deletions controllers/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllers

import (
"context"
"fmt"
"reflect"

"github.com/presslabs/controller-util/syncer"
Expand All @@ -30,11 +31,13 @@ import (
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"

apiv1alpha1 "github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1"
"github.com/radondb/radondb-mysql-kubernetes/cluster"
clustersyncer "github.com/radondb/radondb-mysql-kubernetes/cluster/syncer"
"github.com/radondb/radondb-mysql-kubernetes/utils"
)

// ClusterReconciler reconciles a Cluster object
Expand Down Expand Up @@ -93,6 +96,31 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}
}()

// if the cluster is not been deleted
if instance.ObjectMeta.DeletionTimestamp.IsZero() {
// if do not have, add it, then update
if !controllerutil.ContainsFinalizer(instance.Unwrap(), string(utils.ClusterFinalizer)) {
controllerutil.AddFinalizer(instance.Unwrap(), string(utils.ClusterFinalizer))
if err := r.Update(ctx, instance.Unwrap()); err != nil {
log.Error(err, "failed to update cluster status")
return ctrl.Result{}, err
}
}
} else { // if the cluster is been deleted
//check the Cluster status is ready
if status.State == apiv1alpha1.ClusterReady || status.State == apiv1alpha1.ClusterError {
controllerutil.RemoveFinalizer(instance.Unwrap(), string(utils.ClusterFinalizer))
if err := r.Update(ctx, instance.Unwrap()); err != nil {
log.Error(err, "failed to update cluster status")
return ctrl.Result{}, err
}
} else {
//return error, reconcile again
return ctrl.Result{}, fmt.Errorf("Is not ready, reconcile again")
}

}

configMapSyncer := clustersyncer.NewConfigMapSyncer(r.Client, instance)
if err = syncer.Sync(ctx, configMapSyncer, r.Recorder); err != nil {
return ctrl.Result{}, err
Expand Down
3 changes: 3 additions & 0 deletions utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ const (

// preUpdate file
FileIndicateUpdate = "PreUpdating"

// ClusterFinalizer Name
ClusterFinalizer ResourceName = "clusers.radondb-mysql-kubernetes"
)

// ResourceName is the type for aliasing resources that will be created.
Expand Down

0 comments on commit fb79070

Please sign in to comment.