Skip to content

Commit

Permalink
Merge pull request radondb#581 from acekingke/fixBackupCrDelete
Browse files Browse the repository at this point in the history
controller: delete cluster then delete backup cr radondb#570
  • Loading branch information
acekingke authored Jul 14, 2022
2 parents 6076749 + 8f12a60 commit 4ea1492
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion controllers/mysqlcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import (
"context"
"reflect"

"github.com/presslabs/controller-util/meta"
"github.com/presslabs/controller-util/syncer"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -38,6 +40,8 @@ import (
clustersyncer "github.com/radondb/radondb-mysql-kubernetes/mysqlcluster/syncer"
)

var clusterFinalizer string = "mysqlcluster-finalizer"

// MysqlClusterReconciler reconciles a MysqlCluster object
type MysqlClusterReconciler struct {
client.Client
Expand Down Expand Up @@ -99,7 +103,17 @@ func (r *MysqlClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request
}
}
}()

// Add finalizer if is not added on the resource.
if !meta.HasFinalizer(&instance.ObjectMeta, clusterFinalizer) {
meta.AddFinalizer(&instance.ObjectMeta, clusterFinalizer)
if err = r.Update(ctx, instance.Unwrap()); err != nil {
return ctrl.Result{}, err
}
}
if !instance.ObjectMeta.DeletionTimestamp.IsZero() {
// Delete all the backup cr
return ctrl.Result{}, r.deleteAllBackup(ctx, req, instance.Unwrap())
}
mysqlCMSyncer := clustersyncer.NewMysqlCMSyncer(r.Client, instance)
if err = syncer.Sync(ctx, mysqlCMSyncer, r.Recorder); err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -157,3 +171,36 @@ func (r *MysqlClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
Owns(&policyv1beta1.PodDisruptionBudget{}).
Complete(r)
}

// Delte all backup cr
func (r *MysqlClusterReconciler) deleteAllBackup(ctx context.Context, req ctrl.Request, instance *apiv1alpha1.MysqlCluster) error {
log := log.FromContext(ctx).WithName("controllers").WithName("MysqlCluster")
if !meta.HasFinalizer(&instance.ObjectMeta, clusterFinalizer) {
return nil
}
defer func() {
meta.RemoveFinalizer(&instance.ObjectMeta, clusterFinalizer)
// Update resource so it will remove the finalizer.
if err := r.Update(ctx, instance); err != nil {
log.Error(err, "failed to update cluster")
}
}()
labelSet := labels.Set{"cluster": instance.Name}
backuplist := apiv1alpha1.BackupList{}
if err := r.List(ctx,
&backuplist,
&client.ListOptions{
Namespace: instance.Namespace,
LabelSelector: labelSet.AsSelector(),
},
); err != nil {
return err
}
for _, bcp := range backuplist.Items {
if err := r.Delete(context.TODO(), &bcp); err != nil {
log.Error(err, "failed to delete a backup", "backup", bcp)
}
}

return nil
}

0 comments on commit 4ea1492

Please sign in to comment.