Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

startupConfig to pass custom Startup Probes #541

Merged
merged 4 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/redisfailover/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type RedisSettings struct {
CustomCommandRenames []RedisCommandRename `json:"customCommandRenames,omitempty"`
Command []string `json:"command,omitempty"`
ShutdownConfigMap string `json:"shutdownConfigMap,omitempty"`
StartupConfigMap string `json:"startupConfigMap,omitempty"`
Storage RedisStorage `json:"storage,omitempty"`
InitContainers []corev1.Container `json:"initContainers,omitempty"`
Exporter Exporter `json:"exporter,omitempty"`
Expand Down Expand Up @@ -76,6 +77,7 @@ type SentinelSettings struct {
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
CustomConfig []string `json:"customConfig,omitempty"`
Command []string `json:"command,omitempty"`
StartupConfigMap string `json:"startupConfigMap,omitempty"`
Affinity *corev1.Affinity `json:"affinity,omitempty"`
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
ContainerSecurityContext *corev1.SecurityContext `json:"containerSecurityContext,omitempty"`
Expand Down
32 changes: 32 additions & 0 deletions example/operator/custom-startup-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
apiVersion: v1
kind: ConfigMap
metadata:
name: startup-config
data:
startup.sh: |
#!/bin/bash
redis-cli -h 127.0.0.1 -p ${REDIS_PORT} ping --user pinger --pass pingpass --no-auth-warning
---
apiVersion: databases.spotahome.com/v1
kind: RedisFailover
metadata:
name: redisfailover
spec:
sentinel:
replicas: 3
resources:
requests:
cpu: 100m
limits:
memory: 100Mi
redis:
replicas: 3
startupConfigMap: startup-config
resources:
requests:
cpu: 100m
memory: 200Mi
limits:
cpu: 400m
memory: 500Mi
4 changes: 4 additions & 0 deletions manifests/databases.spotahome.com_redisfailovers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5785,6 +5785,8 @@ spec:
type: object
shutdownConfigMap:
type: string
startupConfigMap:
type: string
storage:
description: RedisStorage defines the structure used to store
the Redis Data
Expand Down Expand Up @@ -12205,6 +12207,8 @@ spec:
additionalProperties:
type: string
type: object
startupConfigMap:
type: string
tolerations:
items:
description: The pod this Toleration is attached to tolerates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5785,6 +5785,8 @@ spec:
type: object
shutdownConfigMap:
type: string
startupConfigMap:
type: string
storage:
description: RedisStorage defines the structure used to store
the Redis Data
Expand Down Expand Up @@ -12205,6 +12207,8 @@ spec:
additionalProperties:
type: string
type: object
startupConfigMap:
type: string
tolerations:
items:
description: The pod this Toleration is attached to tolerates
Expand Down
87 changes: 84 additions & 3 deletions operator/redisfailover/service/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ sentinel down-after-milliseconds mymaster 1000
sentinel failover-timeout mymaster 3000
sentinel parallel-syncs mymaster 2`

redisShutdownConfigurationVolumeName = "redis-shutdown-config"
redisReadinessVolumeName = "redis-readiness-config"
redisStorageVolumeName = "redis-data"
redisShutdownConfigurationVolumeName = "redis-shutdown-config"
redisStartupConfigurationVolumeName = "redis-startup-config"
redisReadinessVolumeName = "redis-readiness-config"
redisStorageVolumeName = "redis-data"
sentinelStartupConfigurationVolumeName = "sentinel-startup-config"

graceTime = 30
)
Expand Down Expand Up @@ -392,6 +394,20 @@ func generateRedisStatefulSet(rf *redisfailoverv1.RedisFailover, labels map[stri
}
}

if rf.Spec.Redis.StartupConfigMap != "" {
ss.Spec.Template.Spec.Containers[0].StartupProbe = &corev1.Probe{
InitialDelaySeconds: graceTime,
TimeoutSeconds: 5,
FailureThreshold: 6,
PeriodSeconds: 15,
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{"/bin/sh", "/redis-startup/startup.sh"},
},
},
}
}

if rf.Spec.Redis.Exporter.Enabled {
exporter := createRedisExporterContainer(rf)
ss.Spec.Template.Spec.Containers = append(ss.Spec.Template.Spec.Containers, exporter)
Expand Down Expand Up @@ -535,6 +551,21 @@ func generateSentinelDeployment(rf *redisfailoverv1.RedisFailover, labels map[st
},
},
}

if rf.Spec.Sentinel.StartupConfigMap != "" {
sd.Spec.Template.Spec.Containers[0].StartupProbe = &corev1.Probe{
InitialDelaySeconds: graceTime,
TimeoutSeconds: 5,
FailureThreshold: 6,
PeriodSeconds: 15,
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{"/bin/sh", "/sentinel-startup/startup.sh"},
},
},
}
}

if rf.Spec.Sentinel.Exporter.Enabled {
exporter := createSentinelExporterContainer(rf)
sd.Spec.Template.Spec.Containers = append(sd.Spec.Template.Spec.Containers, exporter)
Expand Down Expand Up @@ -751,6 +782,15 @@ func getRedisVolumeMounts(rf *redisfailoverv1.RedisFailover) []corev1.VolumeMoun
},
}

if rf.Spec.Redis.StartupConfigMap != "" {
startupVolumeMount := corev1.VolumeMount{
Name: redisStartupConfigurationVolumeName,
MountPath: "/redis-startup",
}

volumeMounts = append(volumeMounts, startupVolumeMount)
}

if rf.Spec.Redis.ExtraVolumeMounts != nil {
volumeMounts = append(volumeMounts, rf.Spec.Redis.ExtraVolumeMounts...)
}
Expand All @@ -766,6 +806,13 @@ func getSentinelVolumeMounts(rf *redisfailoverv1.RedisFailover) []corev1.VolumeM
},
}

if rf.Spec.Sentinel.StartupConfigMap != "" {
startupVolumeMount := corev1.VolumeMount{
Name: "sentinel-startup-config",
MountPath: "/sentinel-startup",
}
volumeMounts = append(volumeMounts, startupVolumeMount)
}
if rf.Spec.Sentinel.ExtraVolumeMounts != nil {
volumeMounts = append(volumeMounts, rf.Spec.Sentinel.ExtraVolumeMounts...)
}
Expand Down Expand Up @@ -814,6 +861,22 @@ func getRedisVolumes(rf *redisfailoverv1.RedisFailover) []corev1.Volume {
},
}

if rf.Spec.Redis.StartupConfigMap != "" {
startupVolumeName := rf.Spec.Redis.StartupConfigMap
startupVolume := corev1.Volume{
Name: redisStartupConfigurationVolumeName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: startupVolumeName,
},
DefaultMode: &executeMode,
},
},
}
volumes = append(volumes, startupVolume)
}

if rf.Spec.Redis.ExtraVolumes != nil {
volumes = append(volumes, rf.Spec.Redis.ExtraVolumes...)
}
Expand All @@ -827,6 +890,8 @@ func getRedisVolumes(rf *redisfailoverv1.RedisFailover) []corev1.Volume {
}

func getSentinelVolumes(rf *redisfailoverv1.RedisFailover, configMapName string) []corev1.Volume {
executeMode := int32(0744)

volumes := []corev1.Volume{
{
Name: "sentinel-config",
Expand All @@ -846,6 +911,22 @@ func getSentinelVolumes(rf *redisfailoverv1.RedisFailover, configMapName string)
},
}

if rf.Spec.Sentinel.StartupConfigMap != "" {
startupVolumeName := rf.Spec.Sentinel.StartupConfigMap
startupVolume := corev1.Volume{
Name: sentinelStartupConfigurationVolumeName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: startupVolumeName,
},
DefaultMode: &executeMode,
},
},
}
volumes = append(volumes, startupVolume)
}

if rf.Spec.Sentinel.ExtraVolumes != nil {
volumes = append(volumes, rf.Spec.Sentinel.ExtraVolumes...)
}
Expand Down
106 changes: 106 additions & 0 deletions operator/redisfailover/service/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1773,3 +1773,109 @@ func TestRedisEnv(t *testing.T) {
assert.Equal(test.expectedRedisEnv, env)
}
}

func TestRedisStartupProbe(t *testing.T) {
mode := int32(0744)
tests := []struct {
name string
expectedVolume corev1.Volume
expectedVolumeMount corev1.VolumeMount
}{
{
name: "startup_config",
expectedVolume: corev1.Volume{
Name: "redis-startup-config",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "startup_config",
},
DefaultMode: &mode,
},
},
},
expectedVolumeMount: corev1.VolumeMount{
Name: "redis-startup-config",
MountPath: "/redis-startup",
},
},
}

for _, test := range tests {
assert := assert.New(t)

var startupVolumes []corev1.Volume
var startupVolumeMounts []corev1.VolumeMount

rf := generateRF()
rf.Spec.Redis.StartupConfigMap = test.name

ms := &mK8SService.Services{}
ms.On("CreateOrUpdatePodDisruptionBudget", namespace, mock.Anything).Once().Return(nil, nil)
ms.On("CreateOrUpdateStatefulSet", namespace, mock.Anything).Once().Run(func(args mock.Arguments) {
s := args.Get(1).(*appsv1.StatefulSet)
startupVolumes = s.Spec.Template.Spec.Volumes
startupVolumeMounts = s.Spec.Template.Spec.Containers[0].VolumeMounts
}).Return(nil)

client := rfservice.NewRedisFailoverKubeClient(ms, log.Dummy, metrics.Dummy)
err := client.EnsureRedisStatefulset(rf, nil, []metav1.OwnerReference{})

assert.NoError(err)
assert.Contains(startupVolumes, test.expectedVolume)
assert.Contains(startupVolumeMounts, test.expectedVolumeMount)
}
}

func TestSentinelStartupProbe(t *testing.T) {
mode := int32(0744)
tests := []struct {
name string
expectedVolume corev1.Volume
expectedVolumeMount corev1.VolumeMount
}{
{
name: "startup_config",
expectedVolume: corev1.Volume{
Name: "sentinel-startup-config",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "startup_config",
},
DefaultMode: &mode,
},
},
},
expectedVolumeMount: corev1.VolumeMount{
Name: "sentinel-startup-config",
MountPath: "/sentinel-startup",
},
},
}

for _, test := range tests {
assert := assert.New(t)

var startupVolumes []corev1.Volume
var startupVolumeMounts []corev1.VolumeMount

rf := generateRF()
rf.Spec.Sentinel.StartupConfigMap = test.name

ms := &mK8SService.Services{}
ms.On("CreateOrUpdatePodDisruptionBudget", namespace, mock.Anything).Once().Return(nil, nil)
ms.On("CreateOrUpdateDeployment", namespace, mock.Anything).Once().Run(func(args mock.Arguments) {
d := args.Get(1).(*appsv1.Deployment)
startupVolumes = d.Spec.Template.Spec.Volumes
startupVolumeMounts = d.Spec.Template.Spec.Containers[0].VolumeMounts
}).Return(nil)

client := rfservice.NewRedisFailoverKubeClient(ms, log.Dummy, metrics.Dummy)
err := client.EnsureSentinelDeployment(rf, nil, []metav1.OwnerReference{})

assert.NoError(err)
assert.Contains(startupVolumes, test.expectedVolume)
assert.Contains(startupVolumeMounts, test.expectedVolumeMount)
}
}