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

Elect a new master on sigterm #70

Merged
merged 3 commits into from
Aug 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 10 additions & 9 deletions api/redisfailover/v1alpha2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ type RedisFailoverSpec struct {

// RedisSettings defines the specification of the redis cluster
type RedisSettings struct {
Replicas int32 `json:"replicas,omitempty"`
Resources RedisFailoverResources `json:"resources,omitempty"`
Exporter bool `json:"exporter,omitempty"`
ExporterImage string `json:"exporterImage,omitempty"`
ExporterVersion string `json:"exporterVersion,omitempty"`
Image string `json:"image,omitempty"`
Version string `json:"version,omitempty"`
ConfigMap string `json:"configMap,omitempty"`
Storage RedisStorage `json:"storage,omitempty"`
Replicas int32 `json:"replicas,omitempty"`
Resources RedisFailoverResources `json:"resources,omitempty"`
Exporter bool `json:"exporter,omitempty"`
ExporterImage string `json:"exporterImage,omitempty"`
ExporterVersion string `json:"exporterVersion,omitempty"`
Image string `json:"image,omitempty"`
Version string `json:"version,omitempty"`
ConfigMap string `json:"configMap,omitempty"`
ShutdownConfigMap string `json:"shutdownConfigMap,omitempty"`
Storage RedisStorage `json:"storage,omitempty"`
}

// SentinelSettings defines the specification of the sentinel cluster
Expand Down
14 changes: 14 additions & 0 deletions mocks/operator/redisfailover/service/RedisFailoverClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions operator/redisfailover/ensurer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func (w *RedisFailoverHandler) Ensure(rf *redisfailoverv1alpha2.RedisFailover, l
if err := w.rfService.EnsureSentinelConfigMap(rf, labels, or); err != nil {
return err
}
if err := w.rfService.EnsureRedisShutdownConfigMap(rf, labels, or); err != nil {
return err
}
if err := w.rfService.EnsureRedisConfigMap(rf, labels, or); err != nil {
return err
}
Expand Down
13 changes: 13 additions & 0 deletions operator/redisfailover/service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type RedisFailoverClient interface {
EnsureSentinelDeployment(rFailover *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) error
EnsureRedisStatefulset(rFailover *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) error
EnsureRedisService(rFailover *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) error
EnsureRedisShutdownConfigMap(rFailover *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) error
EnsureRedisConfigMap(rFailover *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) error
EnsureNotPresentRedisService(rFailover *redisfailoverv1alpha2.RedisFailover) error
}
Expand Down Expand Up @@ -94,6 +95,18 @@ func (r *RedisFailoverKubeClient) EnsureRedisConfigMap(rf *redisfailoverv1alpha2
return nil
}

func (r *RedisFailoverKubeClient) EnsureRedisShutdownConfigMap(rf *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) error {
if rf.Spec.Redis.ConfigMap != "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why checking the ConfigMap if the Esure is for the ShutdownConfigMap?

if _, err := r.K8SService.GetConfigMap(rf.Namespace, rf.Spec.Redis.ShutdownConfigMap); err != nil {
return err
}
} else {
cm := generateRedisShutdownConfigMap(rf, labels, ownerRefs)
return r.K8SService.CreateOrUpdateConfigMap(rf.Namespace, cm)
}
return nil
}

// EnsureRedisService makes sure the redis statefulset exists
func (r *RedisFailoverKubeClient) EnsureRedisService(rf *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) error {
svc := generateRedisService(rf, labels, ownerRefs)
Expand Down
1 change: 1 addition & 0 deletions operator/redisfailover/service/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
sentinelConfigFileName = "sentinel.conf"
redisConfigFileName = "redis.conf"
redisName = "r"
redisShutdownName = "shutdown"
redisRoleName = "redis"
redisGroupName = "mymaster"
appLabel = "redis-failover"
Expand Down
51 changes: 49 additions & 2 deletions operator/redisfailover/service/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
)

const (
redisConfigurationVolumeName = "redis-config"
redisStorageVolumeName = "redis-data"
redisConfigurationVolumeName = "redis-config"
redisShutdownConfigurationVolumeName = "redis-shutdown-config"
redisStorageVolumeName = "redis-data"
)

func generateSentinelService(rf *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) *corev1.Service {
Expand Down Expand Up @@ -122,6 +123,28 @@ tcp-keepalive 60`,
}
}

func generateRedisShutdownConfigMap(rf *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) *corev1.ConfigMap {
name := GetRedisShutdownConfigMapName(rf)
namespace := rf.Namespace

labels = util.MergeLabels(labels, generateLabels(redisRoleName, rf.Name))

return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
OwnerReferences: ownerRefs,
},
Data: map[string]string{
"shutdown.sh": `master=$(redis-cli -h ${RFS_REDIS_SERVICE_HOST} -p ${RFS_REDIS_SERVICE_PORT_SENTINEL} --csv SENTINEL get-master-addr-by-name mymaster | tr ',' ' ' | tr -d '\"' |cut -d' ' -f1)
if [[ $master == $(hostname -i) ]]; then
redis-cli -h ${RFS_REDIS_SERVICE_HOST} -p ${RFS_REDIS_SERVICE_PORT_SENTINEL} SENTINEL failover mymaster
fi`,
},
}
}

func generateRedisStatefulSet(rf *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) *appsv1beta2.StatefulSet {
name := GetRedisName(rf)
namespace := rf.Namespace
Expand Down Expand Up @@ -202,6 +225,13 @@ func generateRedisStatefulSet(rf *redisfailoverv1alpha2.RedisFailover, labels ma
},
},
Resources: resources,
Lifecycle: &corev1.Lifecycle{
PreStop: &corev1.Handler{
Exec: &corev1.ExecAction{
Command: []string{"/bin/sh", "-c", "/redis-shutdown/shutdown.sh"},
},
},
},
},
},
Volumes: volumes,
Expand Down Expand Up @@ -541,6 +571,10 @@ func getRedisVolumeMounts(rf *redisfailoverv1alpha2.RedisFailover) []corev1.Volu
Name: redisConfigurationVolumeName,
MountPath: "/redis",
},
{
Name: redisShutdownConfigurationVolumeName,
MountPath: "/redis-shutdown",
},
{
Name: getRedisDataVolumeName(rf),
MountPath: "/data",
Expand All @@ -552,7 +586,9 @@ func getRedisVolumeMounts(rf *redisfailoverv1alpha2.RedisFailover) []corev1.Volu

func getRedisVolumes(rf *redisfailoverv1alpha2.RedisFailover) []corev1.Volume {
configMapName := GetRedisConfigMapName(rf)
shutdownConfigMapName := GetRedisShutdownConfigMapName(rf)

defaultMode := int32(0744)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the mounting mode by default. I'd change this to "executionMode"

volumes := []corev1.Volume{
{
Name: redisConfigurationVolumeName,
Expand All @@ -564,6 +600,17 @@ func getRedisVolumes(rf *redisfailoverv1alpha2.RedisFailover) []corev1.Volume {
},
},
},
{
Name: redisShutdownConfigurationVolumeName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: shutdownConfigMapName,
},
DefaultMode: &defaultMode,
},
},
},
}

dataVolume := getRedisDataVolume(rf)
Expand Down
14 changes: 14 additions & 0 deletions operator/redisfailover/service/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,24 @@ func GetRedisConfigMapName(rf *redisfailoverv1alpha2.RedisFailover) string {
return GetRedisName(rf)
}

// GetRedisShutdownConfigMapName returns the name for redis configmap
func GetRedisShutdownConfigMapName(rf *redisfailoverv1alpha2.RedisFailover) string {
if rf.Spec.Redis.ShutdownConfigMap != "" {
return rf.Spec.Redis.ShutdownConfigMap
}
return GetRedisShowDownName(rf)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

}

// GetRedisName returns the name for redis resources
func GetRedisName(rf *redisfailoverv1alpha2.RedisFailover) string {
return generateName(redisName, rf.Name)
}
// GetRedisName returns the name for redis resources
func GetRedisShowDownName(rf *redisfailoverv1alpha2.RedisFailover) string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

return generateName(redisShutdownName, rf.Name)
Copy link
Collaborator

@jchanam jchanam Aug 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would create "rfshutdown-rfname".

Do you think it's explanatory or it should be in other way?

}



// GetSentinelConfigMapName returns the name for sentinel configmap
func GetSentinelConfigMapName(rf *redisfailoverv1alpha2.RedisFailover) string {
Expand Down