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

Add support for custom command renaming in Redis. #234

Merged
merged 3 commits into from
Feb 5, 2020
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
43 changes: 25 additions & 18 deletions api/redisfailover/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,33 @@ type RedisFailoverSpec struct {
LabelWhitelist []string `json:"labelWhitelist,omitempty"`
}

// RedisCommandRename defines the specification of a "rename-command" configuration option
type RedisCommandRename struct {
From string `json:"from,omitempty"`
To string `json:"to,omitempty"`
}

// RedisSettings defines the specification of the redis cluster
type RedisSettings struct {
Image string `json:"image,omitempty"`
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
Replicas int32 `json:"replicas,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
CustomConfig []string `json:"customConfig,omitempty"`
Command []string `json:"command,omitempty"`
ShutdownConfigMap string `json:"shutdownConfigMap,omitempty"`
Storage RedisStorage `json:"storage,omitempty"`
Exporter RedisExporter `json:"exporter,omitempty"`
Affinity *corev1.Affinity `json:"affinity,omitempty"`
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`
ServiceAnnotations map[string]string `json:"serviceAnnotations,omitempty"`
HostNetwork bool `json:"hostNetwork,omitempty"`
DNSPolicy corev1.DNSPolicy `json:"dnsPolicy,omitempty"`
Image string `json:"image,omitempty"`
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
Replicas int32 `json:"replicas,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
CustomConfig []string `json:"customConfig,omitempty"`
CustomCommandRenames []RedisCommandRename `json:"customCommandRenames,omitempty"`
Command []string `json:"command,omitempty"`
ShutdownConfigMap string `json:"shutdownConfigMap,omitempty"`
Storage RedisStorage `json:"storage,omitempty"`
Exporter RedisExporter `json:"exporter,omitempty"`
Affinity *corev1.Affinity `json:"affinity,omitempty"`
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`
ServiceAnnotations map[string]string `json:"serviceAnnotations,omitempty"`
HostNetwork bool `json:"hostNetwork,omitempty"`
DNSPolicy corev1.DNSPolicy `json:"dnsPolicy,omitempty"`
}

// SentinelSettings defines the specification of the sentinel cluster
Expand Down
16 changes: 16 additions & 0 deletions example/redisfailover/custom-renames.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: databases.spotahome.com/v1
kind: RedisFailover
metadata:
name: redisfailover
spec:
sentinel:
replicas: 3
redis:
replicas: 3
customCommandRenames:
- from: "monitor"
to: ""
- from: "flushall"
to: "fa"
- from: flushdb
to: xxfd
2 changes: 1 addition & 1 deletion operator/redisfailover/service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (r *RedisFailoverKubeClient) EnsureRedisStatefulset(rf *redisfailoverv1.Red
return r.K8SService.CreateOrUpdateStatefulSet(rf.Namespace, ss)
}

// EnsureRedisConfigMap makes sure the sentinel configmap exists
// EnsureRedisConfigMap makes sure the Redis ConfigMap exists
func (r *RedisFailoverKubeClient) EnsureRedisConfigMap(rf *redisfailoverv1.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) error {

password, err := k8s.GetRedisPassword(r.K8SService, rf)
Expand Down
31 changes: 24 additions & 7 deletions operator/redisfailover/service/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,24 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"bytes"

redisfailoverv1 "github.com/spotahome/redis-operator/api/redisfailover/v1"
"github.com/spotahome/redis-operator/operator/redisfailover/util"
"text/template"
)

const (
redisConfigurationVolumeName = "redis-config"
// Template used to build the Redis configuration
redisConfigTemplate = `slaveof 127.0.0.1 6379
tcp-keepalive 60
save 900 1
save 300 10
{{- range .Spec.Redis.CustomCommandRenames}}
rename-command "{{.From}}" "{{.To}}"
{{- end}}
`
redisShutdownConfigurationVolumeName = "redis-shutdown-config"
redisReadinessVolumeName = "redis-readiness-config"
redisStorageVolumeName = "redis-data"
Expand Down Expand Up @@ -115,13 +126,19 @@ sentinel parallel-syncs mymaster 2`

func generateRedisConfigMap(rf *redisfailoverv1.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference, password string) *corev1.ConfigMap {
name := GetRedisName(rf)
namespace := rf.Namespace

labels = util.MergeLabels(labels, generateSelectorLabels(redisRoleName, rf.Name))
redisConfigFileContent := `slaveof 127.0.0.1 6379
tcp-keepalive 60
save 900 1
save 300 10`

tmpl, err := template.New("redis").Parse(redisConfigTemplate)
if err != nil {
panic(err)
}

var tplOutput bytes.Buffer
if err := tmpl.Execute(&tplOutput, rf); err != nil {
panic(err)
}

redisConfigFileContent := tplOutput.String()

if password != "" {
redisConfigFileContent = fmt.Sprintf("%s\nmasterauth %s\nrequirepass %s", redisConfigFileContent, password, password)
Expand All @@ -130,7 +147,7 @@ save 300 10`
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Namespace: rf.Namespace,
Labels: labels,
OwnerReferences: ownerRefs,
},
Expand Down