Skip to content

Commit

Permalink
update pod labels faster
Browse files Browse the repository at this point in the history
Signed-off-by: wkd-woo <[email protected]>
  • Loading branch information
wkd-woo committed May 13, 2024
1 parent b5bf928 commit 001bc1e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 43 deletions.
35 changes: 29 additions & 6 deletions controllers/redisreplication_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
38 changes: 1 addition & 37 deletions k8sutils/redis-replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 001bc1e

Please sign in to comment.