From 8afca994a58f36c8f15edd9736cc5b2e21689be9 Mon Sep 17 00:00:00 2001 From: acekingke Date: Fri, 7 Jul 2023 10:22:44 +0800 Subject: [PATCH] *: add maintain mode, for delete leader svc --- controllers/mysqlcluster_controller.go | 31 ++++++++++++++++++++++++-- utils/constants.go | 1 + 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/controllers/mysqlcluster_controller.go b/controllers/mysqlcluster_controller.go index 8b9eed54..71e711f6 100644 --- a/controllers/mysqlcluster_controller.go +++ b/controllers/mysqlcluster_controller.go @@ -137,7 +137,12 @@ func (r *MysqlClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request clustersyncer.NewRoleBindingSyncer(r.Client, instance), clustersyncer.NewServiceAccountSyncer(r.Client, instance), clustersyncer.NewHeadlessSVCSyncer(r.Client, instance), - clustersyncer.NewLeaderSVCSyncer(r.Client, instance), + } + if instance.Unwrap().Labels[utils.LabelMaintain] == "true" { + log.V(1).Info("It has got a maintain label") + r.deletLeaderService(ctx, req, instance.Unwrap()) + } else { + syncers = append(syncers, clustersyncer.NewLeaderSVCSyncer(r.Client, instance)) } if instance.Unwrap().Spec.ReadOnlys != nil { syncers = append(syncers, clustersyncer.NewHeadlessReadOnlySVCSyncer(r.Client, instance), @@ -237,7 +242,29 @@ func (r *MysqlClusterReconciler) deleteFollowerService(ctx context.Context, req } for _, svc := range serviceList.Items { if err := r.Delete(context.TODO(), &svc); err != nil { - log.Error(err, "failed to delete a backup", "backup", svc) + log.Error(err, "failed to delete a service", "service", svc) + } + } + return nil +} + +// For SingleNode, follower service do not need. +func (r *MysqlClusterReconciler) deletLeaderService(ctx context.Context, req ctrl.Request, instance *apiv1alpha1.MysqlCluster) error { + log := log.FromContext(ctx).WithName("controllers").WithName("MysqlCluster") + labelSet := labels.Set{"mysql.radondb.com/service-type": string(utils.LeaderService)} + serviceList := corev1.ServiceList{} + if err := r.List(ctx, + &serviceList, + &client.ListOptions{ + Namespace: instance.Namespace, + LabelSelector: labelSet.AsSelector(), + }, + ); err != nil { + return err + } + for _, svc := range serviceList.Items { + if err := r.Delete(context.TODO(), &svc); err != nil { + log.Error(err, "failed to delete a leader service", "service", svc) } } return nil diff --git a/utils/constants.go b/utils/constants.go index e5836e6a..0326e5d5 100644 --- a/utils/constants.go +++ b/utils/constants.go @@ -196,6 +196,7 @@ const ( const LableRebuild = "rebuild" const LabelRebuildFrom = "rebuild-from" +const LabelMaintain = "maintain" // XenonHttpUrl is a http url corresponding to the xenon instruction. type XenonHttpUrl string