@@ -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
167192func 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
194219func getRedisTLSArgs (tlsConfig * common.TLSConfig , clientHost string ) []string {
0 commit comments