From 57234344b3801db81e6f1e92b9fb2d0c35f4d49c Mon Sep 17 00:00:00 2001 From: "Chen.Zhidong" Date: Fri, 25 May 2018 18:39:25 +0800 Subject: [PATCH] add DataVolume to redis --- api/redisfailover/v1alpha2/types.go | 1 + example/redisfailover.yaml | 3 + operator/redisfailover/service/generator.go | 66 +++++++++++++++------ 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/api/redisfailover/v1alpha2/types.go b/api/redisfailover/v1alpha2/types.go index 7e7ad9e21..7b1127397 100644 --- a/api/redisfailover/v1alpha2/types.go +++ b/api/redisfailover/v1alpha2/types.go @@ -43,6 +43,7 @@ type RedisSettings struct { Image string `json:"image,omitempty"` Version string `json:"version,omitempty"` ConfigMap string `json:"configMap,omitempty"` + DataVolume corev1.Volume `json:"dataVolume,omitempty"` } // SentinelSettings defines the specification of the sentinel cluster diff --git a/example/redisfailover.yaml b/example/redisfailover.yaml index 50303c364..bb21aad70 100644 --- a/example/redisfailover.yaml +++ b/example/redisfailover.yaml @@ -25,3 +25,6 @@ spec: exporter: false # Optional. False by default. Adds a redis-exporter container to export metrics. exporterImage: oliver006/redis_exporter # Optional. oliver006/redis_exporter by default. exporterVersion: v0.11.3 # Optional. v0.11.3 by default. + dataVolume: + name: redis-data + emptyDir: {} diff --git a/operator/redisfailover/service/generator.go b/operator/redisfailover/service/generator.go index eed57df2a..0035a9757 100644 --- a/operator/redisfailover/service/generator.go +++ b/operator/redisfailover/service/generator.go @@ -119,13 +119,14 @@ tcp-keepalive 60`, func generateRedisStatefulSet(rf *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) *appsv1beta2.StatefulSet { name := GetRedisName(rf) - configMapName := GetRedisConfigMapName(rf) namespace := rf.Namespace spec := rf.Spec redisImage := getRedisImage(rf) resources := getRedisResources(spec) labels = util.MergeLabels(labels, generateLabels(redisRoleName, rf.Name)) + volumeMounts := getRedisVolumeMounts(rf) + volumes := getRedisVolumes(rf) ss := &appsv1beta2.StatefulSet{ ObjectMeta: metav1.ObjectMeta{ @@ -164,12 +165,7 @@ func generateRedisStatefulSet(rf *redisfailoverv1alpha2.RedisFailover, labels ma Protocol: corev1.ProtocolTCP, }, }, - VolumeMounts: []corev1.VolumeMount{ - { - Name: "redis-config", - MountPath: "/redis", - }, - }, + VolumeMounts: volumeMounts, Command: []string{ "redis-server", fmt.Sprintf("/redis/%s", redisConfigFileName), @@ -203,18 +199,7 @@ func generateRedisStatefulSet(rf *redisfailoverv1alpha2.RedisFailover, labels ma Resources: resources, }, }, - Volumes: []corev1.Volume{ - { - Name: "redis-config", - VolumeSource: corev1.VolumeSource{ - ConfigMap: &corev1.ConfigMapVolumeSource{ - LocalObjectReference: corev1.LocalObjectReference{ - Name: configMapName, - }, - }, - }, - }, - }, + Volumes: volumes, }, }, }, @@ -534,3 +519,46 @@ func getRedisExporterImage(rf *redisfailoverv1alpha2.RedisFailover) string { return fmt.Sprintf("%s:%s", image, version) } + +func getRedisVolumeMounts(rf *redisfailoverv1alpha2.RedisFailover) []corev1.VolumeMount { + volumeMounts := []corev1.VolumeMount{ + { + Name: "redis-config", + MountPath: "/redis", + }, + } + + // check if data volume is set, if set, mount to /data + if rf.Spec.Redis.DataVolume.Name != "" { + volumeMounts = append(volumeMounts, corev1.VolumeMount{ + Name: rf.Spec.Redis.DataVolume.Name, + MountPath: "/data", + }) + } + + return volumeMounts +} + +func getRedisVolumes(rf *redisfailoverv1alpha2.RedisFailover) []corev1.Volume { + configMapName := GetRedisConfigMapName(rf) + + volumes := []corev1.Volume{ + { + Name: "redis-config", + VolumeSource: corev1.VolumeSource{ + ConfigMap: &corev1.ConfigMapVolumeSource{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: configMapName, + }, + }, + }, + }, + } + + // check if data volume is set, if not set skip it + if rf.Spec.Redis.DataVolume.Name != "" { + volumes = append(volumes, rf.Spec.Redis.DataVolume) + } + + return volumes +}