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 DataVolume to redis #58

Merged
merged 1 commit into from
May 25, 2018
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
1 change: 1 addition & 0 deletions api/redisfailover/v1alpha2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions example/redisfailover.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
66 changes: 47 additions & 19 deletions operator/redisfailover/service/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
},
},
},
Expand Down Expand Up @@ -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
}