Skip to content

Commit

Permalink
Merge pull request #58 from sillydong/volume
Browse files Browse the repository at this point in the history
add DataVolume to redis
  • Loading branch information
jchanam authored May 25, 2018
2 parents 9310c85 + 5723434 commit d0cbe08
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
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
}

0 comments on commit d0cbe08

Please sign in to comment.