From 001bc1ee03177421ed8c42944e2b79166082fd0a Mon Sep 17 00:00:00 2001 From: wkd-woo Date: Tue, 2 Apr 2024 20:04:09 +0900 Subject: [PATCH] update pod labels faster Signed-off-by: wkd-woo --- controllers/redisreplication_controller.go | 35 ++++++++++++++++---- k8sutils/redis-replication.go | 38 +--------------------- 2 files changed, 30 insertions(+), 43 deletions(-) diff --git a/controllers/redisreplication_controller.go b/controllers/redisreplication_controller.go index 6883ca48d..0bae6b825 100644 --- a/controllers/redisreplication_controller.go +++ b/controllers/redisreplication_controller.go @@ -73,8 +73,23 @@ func (r *RedisReplicationReconciler) Reconcile(ctx context.Context, req ctrl.Req // Check that the Leader and Follower are ready in redis replication if redisReplicationInfo.Status.ReadyReplicas != totalReplicas { - reqLogger.Info("Redis replication nodes are not ready yet", "Ready.Replicas", strconv.Itoa(int(redisReplicationInfo.Status.ReadyReplicas)), "Expected.Replicas", totalReplicas) - return ctrl.Result{RequeueAfter: time.Second * 60}, nil + var realMaster string + masterNodes := k8sutils.GetRedisNodesByRole(ctx, r.K8sClient, r.Log, instance, "master") + slaveNodes := k8sutils.GetRedisNodesByRole(ctx, r.K8sClient, r.Log, instance, "slave") + + if redisReplicationInfo.Status.ReadyReplicas == 0 { + reqLogger.Info("Redis replication nodes are not ready yet", "Ready.Replicas", strconv.Itoa(int(redisReplicationInfo.Status.ReadyReplicas)), "Expected.Replicas", totalReplicas) + return ctrl.Result{RequeueAfter: time.Second * 60}, nil + } + + reqLogger.Info("The number of Redis replication replicas is less than the desired status\n", "Ready.Replicas", strconv.Itoa(int(redisReplicationInfo.Status.ReadyReplicas)), "Expected.Replicas", totalReplicas) + if len(masterNodes) == int(leaderReplicas) && followerReplicas != 0 && len(slaveNodes) != 0 { + realMaster = k8sutils.GetRedisReplicationRealMaster(ctx, r.K8sClient, r.Log, instance, masterNodes) + if err = k8sutils.UpdateRoleLabelPod(ctx, r.K8sClient, r.Log, instance, "master", []string{realMaster}); err != nil { + return ctrl.Result{RequeueAfter: time.Second * 1}, err + } + } + return ctrl.Result{RequeueAfter: time.Second * 10}, nil } var realMaster string @@ -90,16 +105,24 @@ func (r *RedisReplicationReconciler) Reconcile(ctx context.Context, req ctrl.Req if err != nil { return ctrl.Result{RequeueAfter: time.Second * 60}, err } + if err = k8sutils.UpdateRoleLabelPod(ctx, r.K8sClient, r.Log, instance, "master", []string{realMaster}); err != nil { + return ctrl.Result{RequeueAfter: time.Second}, err + } + if err = k8sutils.UpdateRoleLabelPod(ctx, r.K8sClient, r.Log, instance, "slave", slaveNodes); err != nil { + return ctrl.Result{RequeueAfter: time.Second}, err + } } realMaster = k8sutils.GetRedisReplicationRealMaster(ctx, r.K8sClient, r.Log, instance, masterNodes) + slaveNodes := k8sutils.GetRedisNodesByRole(ctx, r.K8sClient, r.Log, instance, "slave") if err = r.UpdateRedisReplicationMaster(ctx, instance, realMaster); err != nil { return ctrl.Result{}, err } - - err = k8sutils.UpdateRoleLabelPod(ctx, r.K8sClient, r.Log, instance) - if err != nil { - return ctrl.Result{RequeueAfter: time.Second * 60}, err + if err = k8sutils.UpdateRoleLabelPod(ctx, r.K8sClient, r.Log, instance, "master", []string{realMaster}); err != nil { + return ctrl.Result{RequeueAfter: time.Second}, err + } + if err = k8sutils.UpdateRoleLabelPod(ctx, r.K8sClient, r.Log, instance, "slave", slaveNodes); err != nil { + return ctrl.Result{RequeueAfter: time.Second}, err } reqLogger.Info("Will reconcile redis operator in again 10 seconds") diff --git a/k8sutils/redis-replication.go b/k8sutils/redis-replication.go index e4b3a9525..9f0e1ae29 100644 --- a/k8sutils/redis-replication.go +++ b/k8sutils/redis-replication.go @@ -3,8 +3,6 @@ package k8sutils import ( "context" - "k8s.io/utils/pointer" - redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2" "github.com/OT-CONTAINER-KIT/redis-operator/pkg/util" "github.com/go-logr/logr" @@ -232,7 +230,7 @@ func IsRedisReplicationReady(ctx context.Context, logger logr.Logger, client kub return true } -func updatePodLabel(ctx context.Context, cl kubernetes.Interface, logger logr.Logger, cr *redisv1beta2.RedisReplication, role string, nodes []string) error { +func UpdateRoleLabelPod(ctx context.Context, cl kubernetes.Interface, logger logr.Logger, cr *redisv1beta2.RedisReplication, role string, nodes []string) error { for _, node := range nodes { pod, err := cl.CoreV1().Pods(cr.Namespace).Get(context.TODO(), node, metav1.GetOptions{}) if err != nil { @@ -250,37 +248,3 @@ func updatePodLabel(ctx context.Context, cl kubernetes.Interface, logger logr.Lo } return nil } - -func UpdateRoleLabelPod(ctx context.Context, cl kubernetes.Interface, logger logr.Logger, cr *redisv1beta2.RedisReplication) error { - // find realMaster, and label this pod: 'redis-role=master' - role := "master" - realMaster := "" - masterPods := GetRedisNodesByRole(ctx, cl, logger, cr, role) - for _, masterPod := range masterPods { - redisClient := configureRedisReplicationClient(cl, logger, cr, masterPod) - numOfConntectedSlaves := checkAttachedSlave(ctx, redisClient, logger, masterPod) - if numOfConntectedSlaves > 0 { - realMaster = masterPod - } - } - if realMaster != "" { - err := updatePodLabel(ctx, cl, logger, cr, role, []string{realMaster}) - if err != nil { - return err - } - } - // when configuring one size replication - if cr.Spec.Size == pointer.Int32(1) { - err := updatePodLabel(ctx, cl, logger, cr, role, masterPods) - if err != nil { - return err - } - } - - role = "slave" - err := updatePodLabel(ctx, cl, logger, cr, role, GetRedisNodesByRole(ctx, cl, logger, cr, role)) - if err != nil { - return err - } - return nil -}