Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions internal/k8sutils/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,12 @@ func CreateMultipleLeaderRedisCommand(ctx context.Context, client kubernetes.Int
PodName: cr.Name + "-leader-" + strconv.Itoa(podCount),
Namespace: cr.Namespace,
}
cmd.AddFlag(getEndpoint(ctx, client, cr, rd))
endpoint := getEndpoint(ctx, client, cr, rd)
if endpoint == "" {
log.FromContext(ctx).Error(errors.New("failed to get endpoint"), "Unable to resolve endpoint for pod", "Pod", rd.PodName)
continue
}
cmd.AddFlag(endpoint)
}
cmd.AddFlag("--cluster-yes")
return cmd
Expand Down Expand Up @@ -272,10 +277,19 @@ func getRedisTLSArgs(tlsConfig *commonapi.TLSConfig, clientHost string) []string
}

// createRedisReplicationCommand will create redis replication creation command
func createRedisReplicationCommand(ctx context.Context, client kubernetes.Interface, cr *rcvb2.RedisCluster, leaderPod RedisDetails, followerPod RedisDetails) []string {
func createRedisReplicationCommand(ctx context.Context, client kubernetes.Interface, cr *rcvb2.RedisCluster, leaderPod RedisDetails, followerPod RedisDetails) ([]string, error) {
followerEndpoint := getEndpoint(ctx, client, cr, followerPod)
if followerEndpoint == "" {
return nil, fmt.Errorf("failed to get endpoint for follower pod %s", followerPod.PodName)
}
leaderEndpoint := getEndpoint(ctx, client, cr, leaderPod)
if leaderEndpoint == "" {
return nil, fmt.Errorf("failed to get endpoint for leader pod %s", leaderPod.PodName)
}

cmd := []string{"redis-cli", "--cluster", "add-node"}
cmd = append(cmd, getEndpoint(ctx, client, cr, followerPod))
cmd = append(cmd, getEndpoint(ctx, client, cr, leaderPod))
cmd = append(cmd, followerEndpoint)
cmd = append(cmd, leaderEndpoint)
cmd = append(cmd, "--cluster-slave")
if cr.Spec.KubernetesConfig.ExistingPasswordSecret != nil {
pass, err := getRedisPassword(ctx, client, cr.Namespace, *cr.Spec.KubernetesConfig.ExistingPasswordSecret.Name, *cr.Spec.KubernetesConfig.ExistingPasswordSecret.Key)
Expand All @@ -286,7 +300,7 @@ func createRedisReplicationCommand(ctx context.Context, client kubernetes.Interf
}
}
cmd = append(cmd, getRedisTLSArgs(cr.Spec.TLS, leaderPod.PodName)...)
return cmd
return cmd, nil
}

// ExecuteRedisReplicationCommand will execute the replication command
Expand Down Expand Up @@ -316,7 +330,12 @@ func ExecuteRedisReplicationCommand(ctx context.Context, client kubernetes.Inter
podIP = getRedisServerIP(ctx, client, followerPod)
if !checkRedisNodePresence(ctx, nodes, podIP) {
log.FromContext(ctx).V(1).Info("Adding node to cluster.", "Node.IP", podIP, "Follower.Pod", followerPod)
cmd := createRedisReplicationCommand(ctx, client, cr, leaderPod, followerPod)
cmd, err := createRedisReplicationCommand(ctx, client, cr, leaderPod, followerPod)
if err != nil {
log.FromContext(ctx).Error(err, "Failed to create replication command")
followerIdx++
continue
}
redisClient := configureRedisClient(ctx, client, cr, followerPod.PodName)
pong, err := redisClient.Ping(ctx).Result()
redisClient.Close()
Expand Down
4 changes: 3 additions & 1 deletion internal/k8sutils/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,10 @@ func TestCreateRedisReplicationCommand(t *testing.T) {
objects = append(objects, secret...)

client := k8sClientFake.NewSimpleClientset(objects...)
cmd := createRedisReplicationCommand(context.TODO(), client, tt.redisCluster, tt.leaderPod, tt.followerPod)
cmd, err := createRedisReplicationCommand(context.TODO(), client, tt.redisCluster, tt.leaderPod, tt.followerPod)

// Assert no error occurred
assert.NoError(t, err)
// Assert the command is as expected using testify
assert.Equal(t, tt.expectedCommand, cmd)
})
Expand Down
Loading