diff --git a/pkg/lorry/engines/redis/manager.go b/pkg/lorry/engines/redis/manager.go index d2c8e70737a..0f3d584bc6f 100644 --- a/pkg/lorry/engines/redis/manager.go +++ b/pkg/lorry/engines/redis/manager.go @@ -22,6 +22,8 @@ package redis import ( "context" "fmt" + "os/exec" + "strconv" "strings" "time" @@ -44,7 +46,6 @@ type Manager struct { clientSettings *Settings sentinelClient *redis.SentinelClient - startAt time.Time role string roleSubscribeUpdateTime int64 roleProbePeriod int64 @@ -76,7 +77,14 @@ func NewManager(properties engines.Properties) (engines.DBManager, error) { roleProbePeriod: int64(viper.GetInt(constant.KBEnvRoleProbePeriod)), } - mgr.startAt = time.Now() + majorVersion, err := getRedisMajorVersion() + if err != nil { + return nil, err + } + // The username is supported after 6.0 + if majorVersion < 6 { + redisUser = "" + } defaultSettings := &Settings{ Password: redisPasswd, @@ -120,3 +128,31 @@ func tokenizeCmd2Args(cmd string) []interface{} { } return redisArgs } + +// we get redis version from 'redis-cli --version' +func getRedisMajorVersion() (int, error) { + redisCliCMD, err := exec.LookPath("redis-cli") + if err != nil { + if viper.IsSet("REDIS_VERSION") { + return strconv.Atoi(strings.Split(strings.TrimSpace(viper.GetString("REDIS_VERSION")), ".")[0]) + } + return -1, err + } + + out, err := exec.Command(redisCliCMD, "--version").Output() + if err != nil { + return -1, err + } + + // output eg: redis-cli 7.2.4 + versionInfo := strings.Split(strings.TrimSpace(string(out)), " ") + if len(versionInfo) < 2 { + return -1, fmt.Errorf("invalid redis version info: %s", string(out)) + } + + majorVersion, err := strconv.Atoi(strings.Split(strings.TrimSpace(versionInfo[1]), ".")[0]) + if err != nil { + return -1, err + } + return majorVersion, nil +} diff --git a/pkg/lorry/engines/redis/manager_test.go b/pkg/lorry/engines/redis/manager_test.go index 62556b92ac9..2f41854e8d8 100644 --- a/pkg/lorry/engines/redis/manager_test.go +++ b/pkg/lorry/engines/redis/manager_test.go @@ -42,6 +42,7 @@ var _ = Describe("Redis DBManager", func() { // Set up relevant viper config variables viper.Set(constant.KBEnvServiceUser, "testuser") viper.Set(constant.KBEnvServicePassword, "testpassword") + viper.Set("REDIS_VERSION", "7.2.4") Context("new db manager", func() { It("with right configurations", func() { properties := engines.Properties{