Skip to content

Commit

Permalink
feat: Add support for passing Redis Sentinel username(ACL) and passwo…
Browse files Browse the repository at this point in the history
…rd (#17168)

* Add support for passing Sentinel username and password

Signed-off-by: ShlomiTubul <shlomi.tubul@placer.ai>

* fix align with var naming

Signed-off-by: ShlomiTubul <shlomi.tubul@placer.ai>

* fix align with var naming

Signed-off-by: ShlomiTubul <shlomi.tubul@placer.ai>

---------

Signed-off-by: ShlomiTubul <shlomi.tubul@placer.ai>
Co-authored-by: ShlomiTubul <shlomi.tubul@placer.ai>
  • Loading branch information
shlomitubul and shlomitubul authored Feb 11, 2024
1 parent bb1c1ed commit adceae9
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions util/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const (
envRedisRetryCount = "REDIS_RETRY_COUNT"
// defaultRedisRetryCount holds default number of retries
defaultRedisRetryCount = 3
// envRedisSentinelPassword is an env variable name which stores redis sentinel password
envRedisSentinelPassword = "REDIS_SENTINEL_PASSWORD"
// envRedisSentinelUsername is an env variable name which stores redis sentinel username
envRedisSentinelUsername = "REDIS_SENTINEL_USERNAME"
)

const (
Expand Down Expand Up @@ -57,21 +61,23 @@ func buildRedisClient(redisAddress, password, username string, redisDB, maxRetri
return client
}

func buildFailoverRedisClient(sentinelMaster, password, username string, redisDB, maxRetries int, tlsConfig *tls.Config, sentinelAddresses []string) *redis.Client {
func buildFailoverRedisClient(sentinelMaster, sentinelUsername, sentinelPassword, password, username string, redisDB, maxRetries int, tlsConfig *tls.Config, sentinelAddresses []string) *redis.Client {
opts := &redis.FailoverOptions{
MasterName: sentinelMaster,
SentinelAddrs: sentinelAddresses,
DB: redisDB,
Password: password,
MaxRetries: maxRetries,
TLSConfig: tlsConfig,
Username: username,
MasterName: sentinelMaster,
SentinelAddrs: sentinelAddresses,
DB: redisDB,
Password: password,
MaxRetries: maxRetries,
TLSConfig: tlsConfig,
Username: username,
SentinelUsername: sentinelUsername,
SentinelPassword: sentinelPassword,
}

client := redis.NewFailoverClient(opts)

client.AddHook(redis.Hook(NewArgoRedisHook(func() {
*client = *buildFailoverRedisClient(sentinelMaster, password, username, redisDB, maxRetries, tlsConfig, sentinelAddresses)
*client = *buildFailoverRedisClient(sentinelMaster, sentinelUsername, sentinelPassword, password, username, redisDB, maxRetries, tlsConfig, sentinelAddresses)
})))

return client
Expand Down Expand Up @@ -199,21 +205,30 @@ func AddCacheFlagsToCmd(cmd *cobra.Command, opts ...Options) func() (*Cache, err
}
password := os.Getenv(envRedisPassword)
username := os.Getenv(envRedisUsername)
sentinelUsername := os.Getenv(envRedisSentinelUsername)
sentinelPassword := os.Getenv(envRedisSentinelPassword)
if opt.FlagPrefix != "" {
if val := os.Getenv(opt.getEnvPrefix() + envRedisUsername); val != "" {
username = val
}
if val := os.Getenv(opt.getEnvPrefix() + envRedisPassword); val != "" {
password = val
}
if val := os.Getenv(opt.getEnvPrefix() + envRedisSentinelUsername); val != "" {
sentinelUsername = val
}
if val := os.Getenv(opt.getEnvPrefix() + envRedisSentinelPassword); val != "" {
sentinelPassword = val
}
}

maxRetries := env.ParseNumFromEnv(envRedisRetryCount, defaultRedisRetryCount, 0, math.MaxInt32)
compression, err := CompressionTypeFromString(compressionStr)
if err != nil {
return nil, err
}
if len(sentinelAddresses) > 0 {
client := buildFailoverRedisClient(sentinelMaster, password, username, redisDB, maxRetries, tlsConfig, sentinelAddresses)
client := buildFailoverRedisClient(sentinelMaster, sentinelUsername, sentinelPassword, password, username, redisDB, maxRetries, tlsConfig, sentinelAddresses)
opt.callOnClientCreated(client)
return NewCache(NewRedisCache(client, defaultCacheExpiration, compression)), nil
}
Expand Down

0 comments on commit adceae9

Please sign in to comment.