Skip to content

Commit 99ea19c

Browse files
committed
fixed: MIGRATE cmd with auth when password set
1 parent 58182fa commit 99ea19c

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,5 @@ tags
7979
vendor/*
8080
/main
8181
/Dockerfile-withvendor
82-
Dockerfile-TZSH
82+
Dockerfile-TZSH
83+
skaffold.yaml

pkg/redisutil/admin.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,7 @@ func (a *Admin) MigrateKeys(addr string, dest *Node, slots []Slot, batch int, ti
388388
break
389389
}
390390

391-
var args []string
392-
if replace {
393-
args = append([]string{dest.IP, dest.Port, "", "0", timeoutStr, "REPLACE", "KEYS"}, keys...)
394-
} else {
395-
args = append([]string{dest.IP, dest.Port, "", "0", timeoutStr, "KEYS"}, keys...)
396-
}
391+
args := a.migrateCmdArgs(dest, timeoutStr, replace, keys)
397392

398393
resp = c.Cmd("MIGRATE", args)
399394
if err := a.Connections().ValidateResp(resp, addr, "Unable to run command MIGRATE"); err != nil {
@@ -432,12 +427,7 @@ func (a *Admin) MigrateKeysInSlot(addr string, dest *Node, slot Slot, batch int,
432427
break
433428
}
434429

435-
var args []string
436-
if replace {
437-
args = append([]string{dest.IP, dest.Port, "", "0", timeoutStr, "REPLACE", "KEYS"}, keys...)
438-
} else {
439-
args = append([]string{dest.IP, dest.Port, "", "0", timeoutStr, "KEYS"}, keys...)
440-
}
430+
args := a.migrateCmdArgs(dest, timeoutStr, replace, keys)
441431

442432
resp = c.Cmd("MIGRATE", args)
443433
if err := a.Connections().ValidateResp(resp, addr, "Unable to run command MIGRATE"); err != nil {
@@ -448,6 +438,20 @@ func (a *Admin) MigrateKeysInSlot(addr string, dest *Node, slot Slot, batch int,
448438
return keyCount, nil
449439
}
450440

441+
func (a *Admin) migrateCmdArgs(dest *Node, timeoutStr string, replace bool, keys []string) []string {
442+
args := []string{dest.IP, dest.Port, "", "0", timeoutStr}
443+
if password, ok := a.Connections().GetAUTH(); ok {
444+
args = append(args, "AUTH", password)
445+
}
446+
if replace {
447+
args = append(args, "REPLACE", "KEYS")
448+
} else {
449+
args = append(args, "KEYS")
450+
}
451+
args = append(args, keys...)
452+
return args
453+
}
454+
451455
// ForgetNode used to force other redis cluster node to forget a specific node
452456
func (a *Admin) ForgetNode(id string) error {
453457
infos, _ := a.GetClusterInfos()

pkg/redisutil/connections.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ type IAdminConnections interface {
5858
ValidatePipeResp(c IClient, addr, errMessage string) bool
5959
// Reset close all connections and clear the connection map
6060
Reset()
61+
// GetAUTH return password and true if connection password is set, else return false.
62+
GetAUTH() (string, bool)
6163
}
6264

6365
// AdminConnections connection map for redis cluster
@@ -98,6 +100,14 @@ func NewAdminConnections(addrs []string, options *AdminOptions, log logr.Logger)
98100
return cnx
99101
}
100102

103+
// GetAUTH return password and true if connection password is set, else return false.
104+
func (cnx *AdminConnections) GetAUTH() (string, bool) {
105+
if len(cnx.password) > 0 {
106+
return cnx.password, true
107+
}
108+
return "", false
109+
}
110+
101111
// Reconnect force a reconnection on the given address
102112
// is the adress is not part of the map, act like Add
103113
func (cnx *AdminConnections) Reconnect(addr string) error {

0 commit comments

Comments
 (0)