Skip to content

Commit 242ccd5

Browse files
authored
fix: proper order for flags to redis-cli command (#1518)
fix: Extract struct to help properly order redis invocation The command passed to Redis must come after the flags. This struct allows for flags to be set after the command for redis is defined. Signed-off-by: Daniel Goldman <[email protected]>
1 parent aa5373f commit 242ccd5

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

internal/k8sutils/redis.go

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,16 @@ func getRedisHostname(redisInfo RedisDetails, cr *rcvb2.RedisCluster, role strin
7575
}
7676

7777
// CreateSingleLeaderRedisCommand will create command for single leader cluster creation
78-
func CreateSingleLeaderRedisCommand(ctx context.Context, cr *rcvb2.RedisCluster) []string {
79-
cmd := []string{"redis-cli", "CLUSTER", "ADDSLOTS"}
78+
func CreateSingleLeaderRedisCommand(ctx context.Context, cr *rcvb2.RedisCluster) RedisInvocation {
79+
cmd := RedisInvocation{
80+
Command: []string{"redis-cli"},
81+
RedisCommand: []string{"CLUSTER", "ADDSLOTS"},
82+
}
8083
for i := 0; i < 16384; i++ {
81-
cmd = append(cmd, strconv.Itoa(i))
84+
cmd.RedisCommand = append(cmd.RedisCommand, strconv.Itoa(i))
8285
}
8386
log.FromContext(ctx).V(1).Info("Generating Redis Add Slots command for single node cluster",
84-
"BaseCommand", cmd[:3],
87+
"BaseCommand", []string{"redis-cli", "CLUSTER", "ADDSLOTS"},
8588
"SlotsRange", "0-16383",
8689
"TotalSlots", 16384)
8790

@@ -143,8 +146,10 @@ func getMasterHostFromClusterNode(node clusterNodesResponse) (string, error) {
143146
}
144147

145148
// CreateMultipleLeaderRedisCommand will create command for single leader cluster creation
146-
func CreateMultipleLeaderRedisCommand(ctx context.Context, client kubernetes.Interface, cr *rcvb2.RedisCluster) []string {
147-
cmd := []string{"redis-cli", "--cluster", "create"}
149+
func CreateMultipleLeaderRedisCommand(ctx context.Context, client kubernetes.Interface, cr *rcvb2.RedisCluster) RedisInvocation {
150+
cmd := RedisInvocation{
151+
Command: []string{"redis-cli", "--cluster", "create"},
152+
}
148153
replicas := cr.Spec.GetReplicaCounts("leader")
149154

150155
for podCount := 0; podCount < int(replicas); podCount++ {
@@ -155,17 +160,37 @@ func CreateMultipleLeaderRedisCommand(ctx context.Context, client kubernetes.Int
155160
} else {
156161
address = getRedisServerAddress(ctx, client, RedisDetails{PodName: podName, Namespace: cr.Namespace}, *cr.Spec.Port)
157162
}
158-
cmd = append(cmd, address)
163+
cmd.AddFlag(address)
159164
}
160-
cmd = append(cmd, "--cluster-yes")
165+
cmd.AddFlag("--cluster-yes")
161166

162-
log.FromContext(ctx).V(1).Info("Redis cluster creation command", "CommandBase", cmd[:3], "Replicas", replicas)
167+
log.FromContext(ctx).V(1).Info("Redis cluster creation command", "CommandBase", []string{"redis-cli", "cluster", "create"}, "Replicas", replicas)
163168
return cmd
164169
}
165170

171+
// RedisInvocation models an invocation of redis-cli
172+
type RedisInvocation struct {
173+
Command []string // e.g. {"redis-cli", "--cluster", "create"}
174+
Flags []string // e.g. {"-h", "localhost", "-p", "6379"}
175+
RedisCommand []string // e.g. {"CLUSTER", "ADDSLOTS", "1", "2", "3"}
176+
}
177+
178+
// Builds the full argv for executeCommand
179+
func (ri *RedisInvocation) Args() []string {
180+
args := append([]string{}, ri.Command...)
181+
args = append(args, ri.Flags...)
182+
args = append(args, ri.RedisCommand...)
183+
return args
184+
}
185+
186+
func (ri *RedisInvocation) AddFlag(flag ...string) *RedisInvocation {
187+
ri.Flags = append(ri.Flags, flag...)
188+
return ri
189+
}
190+
166191
// ExecuteRedisClusterCommand will execute redis cluster creation command
167192
func ExecuteRedisClusterCommand(ctx context.Context, client kubernetes.Interface, cr *rcvb2.RedisCluster) {
168-
var cmd []string
193+
var cmd RedisInvocation
169194
replicas := cr.Spec.GetReplicaCounts("leader")
170195
switch int(replicas) {
171196
case 1:
@@ -183,12 +208,12 @@ func ExecuteRedisClusterCommand(ctx context.Context, client kubernetes.Interface
183208
if err != nil {
184209
log.FromContext(ctx).Error(err, "Error in getting redis password")
185210
}
186-
cmd = append(cmd, "-a")
187-
cmd = append(cmd, pass)
211+
cmd.AddFlag("-a")
212+
cmd.AddFlag(pass)
188213
}
189-
cmd = append(cmd, getRedisTLSArgs(cr.Spec.TLS, cr.Name+"-leader-0")...)
214+
cmd.AddFlag(getRedisTLSArgs(cr.Spec.TLS, cr.Name+"-leader-0")...)
190215
log.FromContext(ctx).V(1).Info("Redis cluster creation command is", "Command", cmd)
191-
executeCommand(ctx, client, cr, cmd, cr.Name+"-leader-0")
216+
executeCommand(ctx, client, cr, cmd.Args(), cr.Name+"-leader-0")
192217
}
193218

194219
func getRedisTLSArgs(tlsConfig *common.TLSConfig, clientHost string) []string {

internal/k8sutils/redis_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ func TestGetRedisHostname(t *testing.T) {
338338

339339
func TestCreateSingleLeaderRedisCommand(t *testing.T) {
340340
cr := &rcvb2.RedisCluster{}
341-
cmd := CreateSingleLeaderRedisCommand(context.TODO(), cr)
341+
invocation := CreateSingleLeaderRedisCommand(context.TODO(), cr)
342+
cmd := invocation.Args()
342343

343344
assert.Equal(t, "redis-cli", cmd[0])
344345
assert.Equal(t, "CLUSTER", cmd[1])
@@ -404,7 +405,8 @@ func TestCreateMultipleLeaderRedisCommand(t *testing.T) {
404405
t.Run(tt.name, func(t *testing.T) {
405406
client := mock_utils.CreateFakeClientWithPodIPs_LeaderPods(tt.redisCluster)
406407

407-
cmd := CreateMultipleLeaderRedisCommand(context.TODO(), client, tt.redisCluster)
408+
invocation := CreateMultipleLeaderRedisCommand(context.TODO(), client, tt.redisCluster)
409+
cmd := invocation.Args()
408410
assert.Equal(t, tt.expectedCommands, cmd)
409411
})
410412
}

0 commit comments

Comments
 (0)