From f80c8a0ffafc813fbe4b95501d749da15e1ebb78 Mon Sep 17 00:00:00 2001 From: muicoder Date: Wed, 22 Feb 2023 16:29:36 +0800 Subject: [PATCH] RedisPassword for containers, optimize REDIS_ADDR Signed-off-by: muicoder RedisPassword for instance: REDISCLI_AUTH RedisPassword for exporter: REDIS_PASSWORD --- k8sutils/statefulset.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/k8sutils/statefulset.go b/k8sutils/statefulset.go index 58c016a19..f0a064cba 100644 --- a/k8sutils/statefulset.go +++ b/k8sutils/statefulset.go @@ -497,9 +497,7 @@ func getEnvironmentVariables(role string, enabledMetric bool, enabledPassword *b {Name: "SETUP_MODE", Value: role}, } - redisHost := "redis://localhost:6379" if tlsConfig != nil { - redisHost = "rediss://localhost:6379" envVars = append(envVars, GenerateTLSEnvironmentVariables(tlsConfig)...) if enabledMetric { envVars = append(envVars, corev1.EnvVar{ @@ -521,14 +519,28 @@ func getEnvironmentVariables(role string, enabledMetric bool, enabledPassword *b } } - envVars = append(envVars, corev1.EnvVar{ - Name: "REDIS_ADDR", - Value: redisHost, - }) + RedisPassword := "REDISCLI_AUTH" + if enabledMetric { + envVars = append(envVars, corev1.EnvVar{ + Name: "REDIS_ADDR", + Value: func(role string, tls *redisv1beta1.TLSConfig) string { + prefix := "redis://localhost:" + if tls != nil { + prefix = "rediss://localhost:" + } + if role == "sentinel" { + return prefix + strconv.Itoa(sentinelPort) + } else { + return prefix + strconv.Itoa(redisPort) + } + }(role, tlsConfig), + }) + RedisPassword = "REDIS_PASSWORD" + } if enabledPassword != nil && *enabledPassword { envVars = append(envVars, corev1.EnvVar{ - Name: "REDIS_PASSWORD", + Name: RedisPassword, ValueFrom: &corev1.EnvVarSource{ SecretKeyRef: &corev1.SecretKeySelector{ LocalObjectReference: corev1.LocalObjectReference{