Skip to content

Commit

Permalink
Merge pull request #474 from pych/redis-env-to-extracontainers
Browse files Browse the repository at this point in the history
Redis env to extracontainers
  • Loading branch information
ese authored Sep 15, 2022
2 parents 1d16da7 + 8667fe4 commit 9169915
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 25 deletions.
11 changes: 11 additions & 0 deletions example/redisfailover/sidecars.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
87 changes: 65 additions & 22 deletions operator/redisfailover/service/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,13 @@ 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 {
ss.Spec.Template.Spec.Containers = append(ss.Spec.Template.Spec.Containers, rf.Spec.Redis.ExtraContainers...)
extraContainers := getExtraContainersWithRedisEnv(rf)
ss.Spec.Template.Spec.Containers = append(ss.Spec.Template.Spec.Containers, extraContainers...)
}

if rf.Spec.Auth.SecretPath != "" {
Expand Down Expand Up @@ -616,26 +618,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
}
Expand Down Expand Up @@ -947,3 +931,62 @@ func getTerminationGracePeriodSeconds(rf *redisfailoverv1.RedisFailover) int64 {
}
return 30
}

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{
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_USER",
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",
},
},
})
}

return env
}
13 changes: 10 additions & 3 deletions test/integration/redisfailover/creation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
sentinelSize = int32(3)
authSecretPath = "redis-auth"
testPass = "test-pass"
redisAddr = "redis://localhost:6379"
)

type clients struct {
Expand Down Expand Up @@ -263,7 +264,13 @@ func (c *clients) testAuth(t *testing.T) {
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_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")
assert.Equal(redisSS.Spec.Template.Spec.Containers[1].Env[4].ValueFrom.SecretKeyRef.LocalObjectReference.Name, authSecretPath)
}

0 comments on commit 9169915

Please sign in to comment.