From 6312ca6cb3f75490350b5e609ebab9f4f1e396e8 Mon Sep 17 00:00:00 2001 From: Samuel Vijaykumar M Date: Thu, 8 Sep 2022 18:40:48 +0530 Subject: [PATCH 1/6] WIP: Adding env --- operator/redisfailover/service/generator.go | 45 +++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/operator/redisfailover/service/generator.go b/operator/redisfailover/service/generator.go index e49bb7feb..4efc5071e 100644 --- a/operator/redisfailover/service/generator.go +++ b/operator/redisfailover/service/generator.go @@ -947,3 +947,48 @@ func getTerminationGracePeriodSeconds(rf *redisfailoverv1.RedisFailover) int64 { } return 30 } + +func populateRedisEnvOnExtraContainers(rf *redisfailoverv1.RedisFailover) []corev1.Container { + if len(rf.Spec.Redis.ExtraContainers) > 0 { + for _, ec := range rf.Spec.Redis.ExtraContainers { + env := getRedisEnv(rf) + ec.Env = append(ec.Env, env...) + } + } + + return +} + +func getRedisEnv(rf *redisfailoverv1.RedisFailover) []corev1.EnvVar { + var env []corev1.EnvVar + env = append(env, corev1.EnvVar{ + Name: "REDIS_USERNAME", + Value: "default", + }) + + if rf.Spec.Auth.SecretPath != "" { + env = append(env, corev1.EnvVar{ + Name: "REDIS_PASSWORD", + ValueFrom: &corev1.EnvVarSource{ + SecretKeyRef: &corev1.SecretKeySelector{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: rf.Spec.Auth.SecretPath, + }, + Key: "password", + }, + }, + }) + } + + env = append(env, corev1.EnvVar{ + Name: "REDIS_ADDR", + Value: fmt.Sprintf("redis://localhost:%[1]v", rf.Spec.Redis.Port), + }) + + env = append(env, corev1.EnvVar{ + Name: "REDIS_PORT", + Value: fmt.Sprintf("%[1]v", rf.Spec.Redis.Port), + }) + + return env +} From 9189156545768e3961bb2b1d1aafff97885aaf7e Mon Sep 17 00:00:00 2001 From: Samuel Vijaykumar M Date: Thu, 8 Sep 2022 22:32:58 +0530 Subject: [PATCH 2/6] WIP: Env to extracontainers --- operator/redisfailover/service/generator.go | 39 ++++++--------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/operator/redisfailover/service/generator.go b/operator/redisfailover/service/generator.go index 4efc5071e..c00a93b1e 100644 --- a/operator/redisfailover/service/generator.go +++ b/operator/redisfailover/service/generator.go @@ -401,7 +401,8 @@ func generateRedisStatefulSet(rf *redisfailoverv1.RedisFailover, labels map[stri } if rf.Spec.Redis.ExtraContainers != nil { - ss.Spec.Template.Spec.Containers = append(ss.Spec.Template.Spec.Containers, rf.Spec.Redis.ExtraContainers...) + extraContainers := extraContainersWithRedisEnv(rf) + ss.Spec.Template.Spec.Containers = append(ss.Spec.Template.Spec.Containers, extraContainers...) } if rf.Spec.Auth.SecretPath != "" { @@ -616,26 +617,8 @@ func createRedisExporterContainer(rf *redisfailoverv1.RedisFailover) corev1.Cont Resources: resources, } - if rf.Spec.Auth.SecretPath != "" { - container.Env = append(container.Env, corev1.EnvVar{ - Name: "REDIS_PASSWORD", - ValueFrom: &corev1.EnvVarSource{ - SecretKeyRef: &corev1.SecretKeySelector{ - LocalObjectReference: corev1.LocalObjectReference{ - Name: rf.Spec.Auth.SecretPath, - }, - Key: "password", - }, - }, - }) - } - - if rf.Spec.Redis.Port != 6379 { - container.Env = append(container.Env, corev1.EnvVar{ - Name: "REDIS_ADDR", - Value: fmt.Sprintf("redis://localhost:%[1]v", rf.Spec.Redis.Port), - }) - } + redisEnv := getRedisEnv(rf) + container.Env = append(container.Env, redisEnv...) return container } @@ -948,15 +931,15 @@ func getTerminationGracePeriodSeconds(rf *redisfailoverv1.RedisFailover) int64 { return 30 } -func populateRedisEnvOnExtraContainers(rf *redisfailoverv1.RedisFailover) []corev1.Container { - if len(rf.Spec.Redis.ExtraContainers) > 0 { - for _, ec := range rf.Spec.Redis.ExtraContainers { - env := getRedisEnv(rf) - ec.Env = append(ec.Env, env...) - } +func extraContainersWithRedisEnv(rf *redisfailoverv1.RedisFailover) []corev1.Container { + var extraContainers []corev1.Container + for _, ec := range rf.Spec.Redis.ExtraContainers { + env := getRedisEnv(rf) + ec.Env = append(ec.Env, env...) + extraContainers = append(extraContainers, ec) } - return + return extraContainers } func getRedisEnv(rf *redisfailoverv1.RedisFailover) []corev1.EnvVar { From 71c4ba50287221421f56bbb2df5f65e40c981f78 Mon Sep 17 00:00:00 2001 From: Samuel Vijaykumar M Date: Thu, 8 Sep 2022 23:05:43 +0530 Subject: [PATCH 3/6] Adding the env to initContainers we well --- example/redisfailover/sidecars.yaml | 11 +++++++ operator/redisfailover/service/generator.go | 32 +++++++++++++++------ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/example/redisfailover/sidecars.yaml b/example/redisfailover/sidecars.yaml index 66c7ece68..00fa9996f 100644 --- a/example/redisfailover/sidecars.yaml +++ b/example/redisfailover/sidecars.yaml @@ -4,12 +4,23 @@ kind: Namespace metadata: name: sc --- +apiVersion: v1 +kind: Secret +metadata: + name: redis-auth + namespace: sc +type: Opaque +stringData: + password: pass +--- apiVersion: databases.spotahome.com/v1 kind: RedisFailover metadata: name: sidecars namespace: sc spec: + auth: + secretPath: redis-auth sentinel: initContainers: - name: echo diff --git a/operator/redisfailover/service/generator.go b/operator/redisfailover/service/generator.go index c00a93b1e..ce91e0698 100644 --- a/operator/redisfailover/service/generator.go +++ b/operator/redisfailover/service/generator.go @@ -397,11 +397,12 @@ func generateRedisStatefulSet(rf *redisfailoverv1.RedisFailover, labels map[stri } if rf.Spec.Redis.InitContainers != nil { - ss.Spec.Template.Spec.InitContainers = append(ss.Spec.Template.Spec.InitContainers, rf.Spec.Redis.InitContainers...) + initContainers := getInitContainersWithRedisEnv(rf) + ss.Spec.Template.Spec.InitContainers = append(ss.Spec.Template.Spec.InitContainers, initContainers...) } if rf.Spec.Redis.ExtraContainers != nil { - extraContainers := extraContainersWithRedisEnv(rf) + extraContainers := getExtraContainersWithRedisEnv(rf) ss.Spec.Template.Spec.Containers = append(ss.Spec.Template.Spec.Containers, extraContainers...) } @@ -931,17 +932,30 @@ func getTerminationGracePeriodSeconds(rf *redisfailoverv1.RedisFailover) int64 { return 30 } -func extraContainersWithRedisEnv(rf *redisfailoverv1.RedisFailover) []corev1.Container { - var extraContainers []corev1.Container - for _, ec := range rf.Spec.Redis.ExtraContainers { - env := getRedisEnv(rf) - ec.Env = append(ec.Env, env...) - extraContainers = append(extraContainers, ec) - } +func getExtraContainersWithRedisEnv(rf *redisfailoverv1.RedisFailover) []corev1.Container { + env := getRedisEnv(rf) + extraContainers := getContainersWithRedisEnv(rf.Spec.Redis.ExtraContainers, env) return extraContainers } +func getInitContainersWithRedisEnv(rf *redisfailoverv1.RedisFailover) []corev1.Container { + env := getRedisEnv(rf) + initContainers := getContainersWithRedisEnv(rf.Spec.Redis.InitContainers, env) + + return initContainers +} + +func getContainersWithRedisEnv(cs []corev1.Container, e []corev1.EnvVar) []corev1.Container { + var containers []corev1.Container + for _, c := range cs { + c.Env = append(c.Env, e...) + containers = append(containers, c) + } + + return containers +} + func getRedisEnv(rf *redisfailoverv1.RedisFailover) []corev1.EnvVar { var env []corev1.EnvVar env = append(env, corev1.EnvVar{ From 3059038290ea941dec618eb355f5af13a2cc58df Mon Sep 17 00:00:00 2001 From: Samuel Vijaykumar M Date: Fri, 9 Sep 2022 10:35:25 +0530 Subject: [PATCH 4/6] Test Fix: Pass right reference to asserts --- operator/redisfailover/service/generator.go | 21 ++++++++++--------- .../redisfailover/creation_test.go | 14 ++++++++++--- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/operator/redisfailover/service/generator.go b/operator/redisfailover/service/generator.go index ce91e0698..f53fbca94 100644 --- a/operator/redisfailover/service/generator.go +++ b/operator/redisfailover/service/generator.go @@ -958,6 +958,17 @@ func getContainersWithRedisEnv(cs []corev1.Container, e []corev1.EnvVar) []corev func getRedisEnv(rf *redisfailoverv1.RedisFailover) []corev1.EnvVar { var env []corev1.EnvVar + + env = append(env, corev1.EnvVar{ + Name: "REDIS_ADDR", + Value: fmt.Sprintf("redis://localhost:%[1]v", rf.Spec.Redis.Port), + }) + + env = append(env, corev1.EnvVar{ + Name: "REDIS_PORT", + Value: fmt.Sprintf("%[1]v", rf.Spec.Redis.Port), + }) + env = append(env, corev1.EnvVar{ Name: "REDIS_USERNAME", Value: "default", @@ -977,15 +988,5 @@ func getRedisEnv(rf *redisfailoverv1.RedisFailover) []corev1.EnvVar { }) } - env = append(env, corev1.EnvVar{ - Name: "REDIS_ADDR", - Value: fmt.Sprintf("redis://localhost:%[1]v", rf.Spec.Redis.Port), - }) - - env = append(env, corev1.EnvVar{ - Name: "REDIS_PORT", - Value: fmt.Sprintf("%[1]v", rf.Spec.Redis.Port), - }) - return env } diff --git a/test/integration/redisfailover/creation_test.go b/test/integration/redisfailover/creation_test.go index e428490ed..d28fd493d 100644 --- a/test/integration/redisfailover/creation_test.go +++ b/test/integration/redisfailover/creation_test.go @@ -39,6 +39,7 @@ const ( sentinelSize = int32(3) authSecretPath = "redis-auth" testPass = "test-pass" + redisAddr = "redis://localhost:6379" ) type clients struct { @@ -260,10 +261,17 @@ func (c *clients) testAuth(t *testing.T) { assert.Contains(redisCfg.Data["redis.conf"], "masterauth "+testPass) redisSS, err := c.k8sClient.AppsV1().StatefulSets(namespace).Get(context.Background(), fmt.Sprintf("rfr-%s", name), metav1.GetOptions{}) + redis_addr := fmt.Sprintf("redis://localhost:%[1]v") assert.NoError(err) assert.Len(redisSS.Spec.Template.Spec.Containers, 2) - assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[1].Name, "REDIS_PASSWORD") - assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[1].ValueFrom.SecretKeyRef.Key, "password") - assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[1].ValueFrom.SecretKeyRef.LocalObjectReference.Name, authSecretPath) + assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[1].Name, "REDIS_ADDR") + assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[1].Value, redisAddr) + assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[2].Name, "REDIS_PORT") + assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[2].Value, "6379") + assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[3].Name, "REDIS_USERNAME") + assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[3].Value, "default") + assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[4].Name, "REDIS_PASSWORD") + assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[4].ValueFrom.SecretKeyRef.Key, "password") + assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[4].ValueFrom.SecretKeyRef.LocalObjectReference.Name, authSecretPath) } From 8d9eebe6bce0cab33fd72b06c2a1da622300e43f Mon Sep 17 00:00:00 2001 From: Samuel Vijaykumar M Date: Fri, 9 Sep 2022 10:36:05 +0530 Subject: [PATCH 5/6] Missed to remove a line placed by mistake --- test/integration/redisfailover/creation_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/test/integration/redisfailover/creation_test.go b/test/integration/redisfailover/creation_test.go index d28fd493d..e19900dfa 100644 --- a/test/integration/redisfailover/creation_test.go +++ b/test/integration/redisfailover/creation_test.go @@ -261,7 +261,6 @@ func (c *clients) testAuth(t *testing.T) { assert.Contains(redisCfg.Data["redis.conf"], "masterauth "+testPass) redisSS, err := c.k8sClient.AppsV1().StatefulSets(namespace).Get(context.Background(), fmt.Sprintf("rfr-%s", name), metav1.GetOptions{}) - redis_addr := fmt.Sprintf("redis://localhost:%[1]v") assert.NoError(err) assert.Len(redisSS.Spec.Template.Spec.Containers, 2) From 8667fe4b79193f7ee1205154327861b30518c093 Mon Sep 17 00:00:00 2001 From: Samuel Vijaykumar M Date: Fri, 9 Sep 2022 10:47:22 +0530 Subject: [PATCH 6/6] REDIS_USERNAME -> REDIS_USER more relevant for exporter --- operator/redisfailover/service/generator.go | 2 +- test/integration/redisfailover/creation_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/operator/redisfailover/service/generator.go b/operator/redisfailover/service/generator.go index f53fbca94..83d3521e9 100644 --- a/operator/redisfailover/service/generator.go +++ b/operator/redisfailover/service/generator.go @@ -970,7 +970,7 @@ func getRedisEnv(rf *redisfailoverv1.RedisFailover) []corev1.EnvVar { }) env = append(env, corev1.EnvVar{ - Name: "REDIS_USERNAME", + Name: "REDIS_USER", Value: "default", }) diff --git a/test/integration/redisfailover/creation_test.go b/test/integration/redisfailover/creation_test.go index e19900dfa..00eb5c656 100644 --- a/test/integration/redisfailover/creation_test.go +++ b/test/integration/redisfailover/creation_test.go @@ -268,7 +268,7 @@ func (c *clients) testAuth(t *testing.T) { assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[1].Value, redisAddr) assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[2].Name, "REDIS_PORT") assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[2].Value, "6379") - assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[3].Name, "REDIS_USERNAME") + assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[3].Name, "REDIS_USER") assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[3].Value, "default") assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[4].Name, "REDIS_PASSWORD") assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[4].ValueFrom.SecretKeyRef.Key, "password")