@@ -168,15 +168,25 @@ func (dfi *DragonflyInstance) masterExists(ctx context.Context) (bool, error) {
168
168
}
169
169
170
170
func (dfi * DragonflyInstance ) getMasterIp (ctx context.Context ) (string , error ) {
171
- dfi .log .Info ("retrieving ip of the master" )
171
+ dfi .log .Info ("retrieving IP of the master" )
172
172
pods , err := dfi .getPods (ctx )
173
173
if err != nil {
174
174
return "" , err
175
175
}
176
176
177
177
for _ , pod := range pods .Items {
178
- if pod .Status .Phase == corev1 .PodRunning && pod .Status .ContainerStatuses [0 ].Ready && pod .Labels [resources .Role ] == resources .Master {
179
- return pod .Status .PodIP , nil
178
+ if pod .Status .Phase == corev1 .PodRunning &&
179
+ pod .Status .ContainerStatuses [0 ].Ready &&
180
+ pod .Labels [resources .Role ] == resources .Master {
181
+
182
+ masterIp , hasMasterIp := pod .Annotations [resources .MasterIp ]
183
+ if hasMasterIp {
184
+ dfi .log .Info ("Retrieved Master IP from annotation" , "masterIp" , masterIp )
185
+ return masterIp , nil
186
+ }
187
+
188
+ masterIp = pod .Status .PodIP
189
+ return masterIp , nil
180
190
}
181
191
}
182
192
@@ -207,7 +217,7 @@ func (dfi *DragonflyInstance) configureReplica(ctx context.Context, pod *corev1.
207
217
// connected to the right master
208
218
func (dfi * DragonflyInstance ) checkReplicaRole (ctx context.Context , pod * corev1.Pod , masterIp string ) (bool , error ) {
209
219
redisClient := redis .NewClient (& redis.Options {
210
- Addr : fmt . Sprintf ( "%s:%d" , pod .Status .PodIP , resources .DragonflyAdminPort ),
220
+ Addr : net . JoinHostPort ( pod .Status .PodIP , strconv . Itoa ( resources .DragonflyAdminPort ) ),
211
221
})
212
222
defer redisClient .Close ()
213
223
@@ -262,7 +272,8 @@ func (dfi *DragonflyInstance) checkAndConfigureReplication(ctx context.Context)
262
272
// check for one master and all replicas
263
273
podRoles := make (map [string ][]string )
264
274
for _ , pod := range pods .Items {
265
- podRoles [pod.Labels [resources.Role ]] = append (podRoles [pod.Labels [resources.Role ]], pod .Name )
275
+ role := pod .Labels [resources .Role ]
276
+ podRoles [role ] = append (podRoles [role ], pod .Name )
266
277
}
267
278
268
279
if len (podRoles [resources .Master ]) != 1 {
@@ -279,6 +290,7 @@ func (dfi *DragonflyInstance) checkAndConfigureReplication(ctx context.Context)
279
290
for _ , pod := range pods .Items {
280
291
if pod .Labels [resources .Role ] == "" {
281
292
if pod .Status .Phase == corev1 .PodRunning && pod .Status .ContainerStatuses [0 ].Ready && pod .Status .PodIP != "" {
293
+ dfi .log .Info ("Marking pod as replica" , "pod" , pod .Name )
282
294
if err := dfi .configureReplica (ctx , & pod ); err != nil {
283
295
return err
284
296
}
@@ -299,17 +311,17 @@ func (dfi *DragonflyInstance) checkAndConfigureReplication(ctx context.Context)
299
311
return err
300
312
}
301
313
302
- // configuring to the right master
314
+ // Configure to the right master if not correct
303
315
if ! ok {
304
- dfi .log .Info ("configuring pod as replica to the right master" , "pod" , pod .Name )
316
+ dfi .log .Info ("Configuring pod as replica to the correct master" , "pod" , pod .Name )
305
317
if err := dfi .configureReplica (ctx , & pod ); err != nil {
306
318
return err
307
319
}
308
320
}
309
321
}
310
322
}
311
323
312
- dfi .log .Info ("all pods are configured correctly" , "dfi" , dfi .df .Name )
324
+ dfi .log .Info ("All pods are configured correctly" , "dfi" , dfi .df .Name )
313
325
return nil
314
326
}
315
327
@@ -335,8 +347,11 @@ func (dfi *DragonflyInstance) replicaOf(ctx context.Context, pod *corev1.Pod, ma
335
347
})
336
348
defer redisClient .Close ()
337
349
350
+ // Sanitize masterIp in case ipv6
351
+ masterIp = strings .Trim (masterIp , "[]" )
352
+
338
353
dfi .log .Info ("Trying to invoke SLAVE OF command" , "pod" , pod .Name , "master" , masterIp , "addr" , redisClient .Options ().Addr )
339
- resp , err := redisClient .SlaveOf (ctx , masterIp , fmt . Sprint (resources .DragonflyAdminPort )).Result ()
354
+ resp , err := redisClient .SlaveOf (ctx , masterIp , strconv . Itoa (resources .DragonflyAdminPort )).Result ()
340
355
if err != nil {
341
356
return fmt .Errorf ("error running SLAVE OF command: %s" , err )
342
357
}
@@ -345,11 +360,14 @@ func (dfi *DragonflyInstance) replicaOf(ctx context.Context, pod *corev1.Pod, ma
345
360
return fmt .Errorf ("response of `SLAVE OF` on replica is not OK: %s" , resp )
346
361
}
347
362
348
- dfi .log .Info ("Marking pod role as replica" , "pod" , pod .Name )
363
+ dfi .log .Info ("Marking pod role as replica" , "pod" , pod .Name , "masterIp" , masterIp )
349
364
pod .Labels [resources .Role ] = resources .Replica
350
- pod .Labels [resources .MasterIp ] = masterIp
365
+ if pod .Annotations == nil {
366
+ pod .Annotations = make (map [string ]string )
367
+ }
368
+ pod .Annotations [resources .MasterIp ] = masterIp
351
369
if err := dfi .client .Update (ctx , pod ); err != nil {
352
- return fmt .Errorf ("could not update replica label" )
370
+ return fmt .Errorf ("could not update replica annotation: %w" , err )
353
371
}
354
372
355
373
return nil
@@ -373,8 +391,14 @@ func (dfi *DragonflyInstance) replicaOfNoOne(ctx context.Context, pod *corev1.Po
373
391
return fmt .Errorf ("response of `SLAVE OF NO ONE` on master is not OK: %s" , resp )
374
392
}
375
393
376
- dfi .log .Info ("Marking pod role as master" , "pod" , pod .Name )
394
+ masterIp := pod .Status .PodIP
395
+
396
+ dfi .log .Info ("Marking pod role as master" , "pod" , pod .Name , "masterIp" , masterIp )
377
397
pod .Labels [resources .Role ] = resources .Master
398
+ if pod .Annotations == nil {
399
+ pod .Annotations = make (map [string ]string )
400
+ }
401
+ pod .Annotations [resources .MasterIp ] = masterIp
378
402
if err := dfi .client .Update (ctx , pod ); err != nil {
379
403
return err
380
404
}
0 commit comments