Skip to content

Commit

Permalink
fix client
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Gupta <[email protected]>
  • Loading branch information
shubham-cmyk committed Nov 10, 2023
1 parent 36591d5 commit 682f04d
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 167 deletions.
32 changes: 16 additions & 16 deletions controllers/rediscluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,21 @@ func (r *RedisClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request
// check whether the redis is leader or not ?
// if not true then make it leader pod

if !(k8sutils.VerifyLeaderPod(ctx, instance)) {
if !(k8sutils.VerifyLeaderPod(ctx, r.K8sClient, r.Log, instance)) {
// lastLeaderPod is slaving right now Make it the master Pod
// We have to bring a manual failover here to make it a leaderPod
// clusterFailover should also include the clusterReplicate since we have to map the followers to new leader
k8sutils.ClusterFailover(ctx, instance)
k8sutils.ClusterFailover(ctx, r.K8sClient, r.Log, instance)
}

// Step 1 Rehard the Cluster
k8sutils.ReshardRedisCluster(instance)
k8sutils.ReshardRedisCluster(r.K8sClient, r.Log, instance)
// Step 2 Remove the Follower Node
k8sutils.RemoveRedisFollowerNodesFromCluster(ctx, instance)
k8sutils.RemoveRedisFollowerNodesFromCluster(ctx, r.K8sClient, r.Log, instance)
// Step 3 Remove the Leader Node
k8sutils.RemoveRedisNodeFromCluster(ctx, instance)
k8sutils.RemoveRedisNodeFromCluster(ctx, r.K8sClient, r.Log, instance)
// Step 4 Rebalance the cluster
k8sutils.RebalanceRedisCluster(instance)
k8sutils.RebalanceRedisCluster(r.K8sClient, r.Log, instance)
return ctrl.Result{RequeueAfter: time.Second * 100}, nil
}

Expand Down Expand Up @@ -185,34 +185,34 @@ func (r *RedisClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request
}

reqLogger.Info("Creating redis cluster by executing cluster creation commands", "Leaders.Ready", strconv.Itoa(int(redisLeaderInfo.Status.ReadyReplicas)), "Followers.Ready", strconv.Itoa(int(redisFollowerInfo.Status.ReadyReplicas)))
if k8sutils.CheckRedisNodeCount(ctx, instance, "") != totalReplicas {
leaderCount := k8sutils.CheckRedisNodeCount(ctx, instance, "leader")
if k8sutils.CheckRedisNodeCount(ctx, r.K8sClient, r.Log, instance, "") != totalReplicas {
leaderCount := k8sutils.CheckRedisNodeCount(ctx, r.K8sClient, r.Log, instance, "leader")
if leaderCount != leaderReplicas {
reqLogger.Info("Not all leader are part of the cluster...", "Leaders.Count", leaderCount, "Instance.Size", leaderReplicas)
if leaderCount <= 2 {
k8sutils.ExecuteRedisClusterCommand(ctx, instance)
k8sutils.ExecuteRedisClusterCommand(ctx, r.K8sClient, r.Log, instance)
} else {
if leaderCount < leaderReplicas {
// Scale up the cluster
// Step 2 : Add Redis Node
k8sutils.AddRedisNodeToCluster(ctx, instance)
k8sutils.AddRedisNodeToCluster(ctx, r.K8sClient, r.Log, instance)
// Step 3 Rebalance the cluster using the empty masters
k8sutils.RebalanceRedisClusterEmptyMasters(instance)
k8sutils.RebalanceRedisClusterEmptyMasters(r.K8sClient, r.Log, instance)
}
}
} else {
if followerReplicas > 0 && redisFollowerInfo.Status.ReadyReplicas == followerReplicas {
reqLogger.Info("All leader are part of the cluster, adding follower/replicas", "Leaders.Count", leaderCount, "Instance.Size", leaderReplicas, "Follower.Replicas", followerReplicas)
k8sutils.ExecuteRedisReplicationCommand(ctx, instance)
k8sutils.ExecuteRedisReplicationCommand(ctx, r.K8sClient, r.Log, instance)
} else {
reqLogger.Info("no follower/replicas configured, skipping replication configuration", "Leaders.Count", leaderCount, "Leader.Size", leaderReplicas, "Follower.Replicas", followerReplicas)
}
}
} else {
reqLogger.Info("Redis leader count is desired")
if int(totalReplicas) > 1 && k8sutils.CheckRedisClusterState(ctx, instance) >= int(totalReplicas)-1 {
if int(totalReplicas) > 1 && k8sutils.CheckRedisClusterState(ctx, r.K8sClient, r.Log, instance) >= int(totalReplicas)-1 {
reqLogger.Info("Redis leader is not desired, executing failover operation")
err = k8sutils.ExecuteFailoverOperation(ctx, instance)
err = k8sutils.ExecuteFailoverOperation(ctx, r.K8sClient, r.Log, instance)
if err != nil {
return ctrl.Result{RequeueAfter: time.Second * 10}, err
}
Expand All @@ -221,8 +221,8 @@ func (r *RedisClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request
}

// Check If there is No Empty Master Node
if k8sutils.CheckRedisNodeCount(ctx, instance, "") == totalReplicas {
k8sutils.CheckIfEmptyMasters(ctx, instance)
if k8sutils.CheckRedisNodeCount(ctx, r.K8sClient, r.Log, instance, "") == totalReplicas {
k8sutils.CheckIfEmptyMasters(ctx, r.K8sClient, r.Log, instance)
}
reqLogger.Info("Will reconcile redis cluster operator in again 10 seconds")

Expand Down
8 changes: 4 additions & 4 deletions controllers/redisreplication_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ func (r *RedisReplicationReconciler) Reconcile(ctx context.Context, req ctrl.Req

reqLogger.Info("Creating redis replication by executing replication creation commands", "Replication.Ready", strconv.Itoa(int(redisReplicationInfo.Status.ReadyReplicas)))

if len(k8sutils.GetRedisNodesByRole(ctx, instance, "master")) > int(leaderReplicas) {
if len(k8sutils.GetRedisNodesByRole(ctx, r.K8sClient, r.Log, instance, "master")) > int(leaderReplicas) {

masterNodes := k8sutils.GetRedisNodesByRole(ctx, instance, "master")
slaveNodes := k8sutils.GetRedisNodesByRole(ctx, instance, "slave")
err := k8sutils.CreateMasterSlaveReplication(ctx, instance, masterNodes, slaveNodes)
masterNodes := k8sutils.GetRedisNodesByRole(ctx, r.K8sClient, r.Log, instance, "master")
slaveNodes := k8sutils.GetRedisNodesByRole(ctx, r.K8sClient, r.Log, instance, "slave")
err := k8sutils.CreateMasterSlaveReplication(ctx, r.K8sClient, r.Log, instance, masterNodes, slaveNodes)
if err != nil {
return ctrl.Result{RequeueAfter: time.Second * 60}, err
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/redissentinel_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (r *RedisSentinelReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}

// Create Redis Sentinel
err = k8sutils.CreateRedisSentinel(ctx, instance)
err = k8sutils.CreateRedisSentinel(ctx, r.K8sClient, r.Log, instance)
if err != nil {
return ctrl.Result{}, err
}
Expand Down
Loading

0 comments on commit 682f04d

Please sign in to comment.