From 88829a2e4cbee7a1aef419431dad850c333d3ff8 Mon Sep 17 00:00:00 2001 From: "Chen.Zhidong" Date: Tue, 22 May 2018 12:37:34 +0800 Subject: [PATCH 1/4] add ConfigMap support --- api/redisfailover/v1alpha2/types.go | 2 ++ operator/redisfailover/service/client.go | 20 ++++++++++++++++---- operator/redisfailover/service/names.go | 6 ++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/api/redisfailover/v1alpha2/types.go b/api/redisfailover/v1alpha2/types.go index cd93ef02c..f1709b508 100644 --- a/api/redisfailover/v1alpha2/types.go +++ b/api/redisfailover/v1alpha2/types.go @@ -42,12 +42,14 @@ type RedisSettings struct { ExporterVersion string `json:"exporterVersion,omitempty"` Image string `json:"image,omitempty"` Version string `json:"version,omitempty"` + ConfigMap string `json:"configMap"` } // SentinelSettings defines the specification of the sentinel cluster type SentinelSettings struct { Replicas int32 `json:"replicas,omitempty"` Resources RedisFailoverResources `json:"resources,omitempty"` + ConfigMap string `json:"configMap"` } // RedisFailoverResources sets the limits and requests for a container diff --git a/operator/redisfailover/service/client.go b/operator/redisfailover/service/client.go index 5b430cdad..3aa62f721 100644 --- a/operator/redisfailover/service/client.go +++ b/operator/redisfailover/service/client.go @@ -52,8 +52,14 @@ func (r *RedisFailoverKubeClient) EnsureSentinelService(rf *redisfailoverv1alpha // EnsureSentinelConfigMap makes sure the sentinel configmap exists func (r *RedisFailoverKubeClient) EnsureSentinelConfigMap(rf *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) error { - cm := generateSentinelConfigMap(rf, labels, ownerRefs) - return r.K8SService.CreateOrUpdateConfigMap(rf.Namespace, cm) + cx, err := r.K8SService.GetConfigMap(rf.Namespace, GetSentinelName(rf)) + if err != nil { + log.Error("cant find sentinel configmap") + cm := generateSentinelConfigMap(rf, labels, ownerRefs) + return r.K8SService.CreateOrUpdateConfigMap(rf.Namespace, cm) + } + log.Infof("%+v", cx) + return nil } // EnsureSentinelDeployment makes sure the sentinel deployment exists in the desired state @@ -76,8 +82,14 @@ func (r *RedisFailoverKubeClient) EnsureRedisStatefulset(rf *redisfailoverv1alph // EnsureRedisConfigMap makes sure the sentinel configmap exists func (r *RedisFailoverKubeClient) EnsureRedisConfigMap(rf *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) error { - cm := generateRedisConfigMap(rf, labels, ownerRefs) - return r.K8SService.CreateOrUpdateConfigMap(rf.Namespace, cm) + cx, err := r.K8SService.GetConfigMap(rf.Namespace, GetSentinelName(rf)) + if err != nil { + log.Error("cant find redis configmap") + cm := generateRedisConfigMap(rf, labels, ownerRefs) + return r.K8SService.CreateOrUpdateConfigMap(rf.Namespace, cm) + } + log.Infof("%+v", cx) + return nil } // EnsureRedisService makes sure the redis statefulset exists diff --git a/operator/redisfailover/service/names.go b/operator/redisfailover/service/names.go index 4ff061e30..b0b0fe1ca 100644 --- a/operator/redisfailover/service/names.go +++ b/operator/redisfailover/service/names.go @@ -8,11 +8,17 @@ import ( // GetRedisName returns the name for redis resources func GetRedisName(rf *redisfailoverv1alpha2.RedisFailover) string { + if rf.Spec.Redis.ConfigMap != "" { + return rf.Spec.Redis.ConfigMap + } return generateName(redisName, rf.Name) } // GetSentinelName returns the name for sentinel resources func GetSentinelName(rf *redisfailoverv1alpha2.RedisFailover) string { + if rf.Spec.Sentinel.ConfigMap != "" { + return rf.Spec.Sentinel.ConfigMap + } return generateName(sentinelName, rf.Name) } From ada6d1b550e0d373c5c023a21dbab2152e54f10f Mon Sep 17 00:00:00 2001 From: "Chen.Zhidong" Date: Tue, 22 May 2018 12:50:55 +0800 Subject: [PATCH 2/4] clean log --- operator/redisfailover/service/client.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/operator/redisfailover/service/client.go b/operator/redisfailover/service/client.go index 3aa62f721..2028559ff 100644 --- a/operator/redisfailover/service/client.go +++ b/operator/redisfailover/service/client.go @@ -52,13 +52,10 @@ func (r *RedisFailoverKubeClient) EnsureSentinelService(rf *redisfailoverv1alpha // EnsureSentinelConfigMap makes sure the sentinel configmap exists func (r *RedisFailoverKubeClient) EnsureSentinelConfigMap(rf *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) error { - cx, err := r.K8SService.GetConfigMap(rf.Namespace, GetSentinelName(rf)) - if err != nil { - log.Error("cant find sentinel configmap") + if _, err := r.K8SService.GetConfigMap(rf.Namespace, GetSentinelName(rf)); err != nil { cm := generateSentinelConfigMap(rf, labels, ownerRefs) return r.K8SService.CreateOrUpdateConfigMap(rf.Namespace, cm) } - log.Infof("%+v", cx) return nil } @@ -82,13 +79,10 @@ func (r *RedisFailoverKubeClient) EnsureRedisStatefulset(rf *redisfailoverv1alph // EnsureRedisConfigMap makes sure the sentinel configmap exists func (r *RedisFailoverKubeClient) EnsureRedisConfigMap(rf *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) error { - cx, err := r.K8SService.GetConfigMap(rf.Namespace, GetSentinelName(rf)) - if err != nil { - log.Error("cant find redis configmap") + if _, err := r.K8SService.GetConfigMap(rf.Namespace, GetSentinelName(rf)); err != nil { cm := generateRedisConfigMap(rf, labels, ownerRefs) return r.K8SService.CreateOrUpdateConfigMap(rf.Namespace, cm) } - log.Infof("%+v", cx) return nil } From a436a0602f70d979d32d085e2364a622445a7ab1 Mon Sep 17 00:00:00 2001 From: "Chen.Zhidong" Date: Wed, 23 May 2018 18:51:00 +0800 Subject: [PATCH 3/4] update according to review 1. add omitempty 2. add GetSentinelConfigMapName() and GetRedisConfigMapName() for getting ConfigMap names 3. revert GetSentinelName() and GetRedisName() --- api/redisfailover/v1alpha2/types.go | 4 ++-- operator/redisfailover/service/client.go | 4 ++-- operator/redisfailover/service/generator.go | 10 ++++++---- operator/redisfailover/service/names.go | 18 ++++++++++++++---- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/api/redisfailover/v1alpha2/types.go b/api/redisfailover/v1alpha2/types.go index f1709b508..7e7ad9e21 100644 --- a/api/redisfailover/v1alpha2/types.go +++ b/api/redisfailover/v1alpha2/types.go @@ -42,14 +42,14 @@ type RedisSettings struct { ExporterVersion string `json:"exporterVersion,omitempty"` Image string `json:"image,omitempty"` Version string `json:"version,omitempty"` - ConfigMap string `json:"configMap"` + ConfigMap string `json:"configMap,omitempty"` } // SentinelSettings defines the specification of the sentinel cluster type SentinelSettings struct { Replicas int32 `json:"replicas,omitempty"` Resources RedisFailoverResources `json:"resources,omitempty"` - ConfigMap string `json:"configMap"` + ConfigMap string `json:"configMap,omitempty"` } // RedisFailoverResources sets the limits and requests for a container diff --git a/operator/redisfailover/service/client.go b/operator/redisfailover/service/client.go index 2028559ff..56a0a427f 100644 --- a/operator/redisfailover/service/client.go +++ b/operator/redisfailover/service/client.go @@ -52,7 +52,7 @@ func (r *RedisFailoverKubeClient) EnsureSentinelService(rf *redisfailoverv1alpha // EnsureSentinelConfigMap makes sure the sentinel configmap exists func (r *RedisFailoverKubeClient) EnsureSentinelConfigMap(rf *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) error { - if _, err := r.K8SService.GetConfigMap(rf.Namespace, GetSentinelName(rf)); err != nil { + if _, err := r.K8SService.GetConfigMap(rf.Namespace, GetSentinelConfigMapName(rf)); err != nil { cm := generateSentinelConfigMap(rf, labels, ownerRefs) return r.K8SService.CreateOrUpdateConfigMap(rf.Namespace, cm) } @@ -79,7 +79,7 @@ func (r *RedisFailoverKubeClient) EnsureRedisStatefulset(rf *redisfailoverv1alph // EnsureRedisConfigMap makes sure the sentinel configmap exists func (r *RedisFailoverKubeClient) EnsureRedisConfigMap(rf *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) error { - if _, err := r.K8SService.GetConfigMap(rf.Namespace, GetSentinelName(rf)); err != nil { + if _, err := r.K8SService.GetConfigMap(rf.Namespace, GetRedisConfigMapName(rf)); err != nil { cm := generateRedisConfigMap(rf, labels, ownerRefs) return r.K8SService.CreateOrUpdateConfigMap(rf.Namespace, cm) } diff --git a/operator/redisfailover/service/generator.go b/operator/redisfailover/service/generator.go index af72a5cc3..34a59f6e6 100644 --- a/operator/redisfailover/service/generator.go +++ b/operator/redisfailover/service/generator.go @@ -76,7 +76,7 @@ func generateRedisService(rf *redisfailoverv1alpha2.RedisFailover, labels map[st } func generateSentinelConfigMap(rf *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) *corev1.ConfigMap { - name := GetSentinelName(rf) + name := GetSentinelConfigMapName(rf) namespace := rf.Namespace labels = util.MergeLabels(labels, generateLabels(sentinelRoleName, rf.Name)) @@ -98,7 +98,7 @@ sentinel parallel-syncs mymaster 2`, } func generateRedisConfigMap(rf *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) *corev1.ConfigMap { - name := GetRedisName(rf) + name := GetRedisConfigMapName(rf) namespace := rf.Namespace labels = util.MergeLabels(labels, generateLabels(redisRoleName, rf.Name)) @@ -119,6 +119,7 @@ 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 @@ -208,7 +209,7 @@ func generateRedisStatefulSet(rf *redisfailoverv1alpha2.RedisFailover, labels ma VolumeSource: corev1.VolumeSource{ ConfigMap: &corev1.ConfigMapVolumeSource{ LocalObjectReference: corev1.LocalObjectReference{ - Name: name, + Name: configMapName, }, }, }, @@ -229,6 +230,7 @@ func generateRedisStatefulSet(rf *redisfailoverv1alpha2.RedisFailover, labels ma func generateSentinelDeployment(rf *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) *appsv1beta2.Deployment { name := GetSentinelName(rf) + configMapName := GetSentinelConfigMapName(rf) namespace := rf.Namespace spec := rf.Spec @@ -347,7 +349,7 @@ func generateSentinelDeployment(rf *redisfailoverv1alpha2.RedisFailover, labels VolumeSource: corev1.VolumeSource{ ConfigMap: &corev1.ConfigMapVolumeSource{ LocalObjectReference: corev1.LocalObjectReference{ - Name: name, + Name: configMapName, }, }, }, diff --git a/operator/redisfailover/service/names.go b/operator/redisfailover/service/names.go index b0b0fe1ca..dcbc27948 100644 --- a/operator/redisfailover/service/names.go +++ b/operator/redisfailover/service/names.go @@ -6,19 +6,29 @@ import ( redisfailoverv1alpha2 "github.com/spotahome/redis-operator/api/redisfailover/v1alpha2" ) -// GetRedisName returns the name for redis resources -func GetRedisName(rf *redisfailoverv1alpha2.RedisFailover) string { +// GetRedisConfigMapName returns the name for redis configmap +func GetRedisConfigMapName(rf *redisfailoverv1alpha2.RedisFailover) string { if rf.Spec.Redis.ConfigMap != "" { return rf.Spec.Redis.ConfigMap } + return GetRedisName(rf) +} + +// GetRedisName returns the name for redis resources +func GetRedisName(rf *redisfailoverv1alpha2.RedisFailover) string { return generateName(redisName, rf.Name) } -// GetSentinelName returns the name for sentinel resources -func GetSentinelName(rf *redisfailoverv1alpha2.RedisFailover) string { +// GetSentinelConfigMapName returns the name for sentinel configmap +func GetSentinelConfigMapName(rf *redisfailoverv1alpha2.RedisFailover) string { if rf.Spec.Sentinel.ConfigMap != "" { return rf.Spec.Sentinel.ConfigMap } + return GetSentinelName(rf) +} + +// GetSentinelName returns the name for sentinel resources +func GetSentinelName(rf *redisfailoverv1alpha2.RedisFailover) string { return generateName(sentinelName, rf.Name) } From 381c0aeae9164bf4c7f6a6bf52ca94f7c5f897e5 Mon Sep 17 00:00:00 2001 From: "Chen.Zhidong" Date: Wed, 23 May 2018 22:41:57 +0800 Subject: [PATCH 4/4] return error if configMap not found --- operator/redisfailover/service/client.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/operator/redisfailover/service/client.go b/operator/redisfailover/service/client.go index 56a0a427f..7fd5ddc1b 100644 --- a/operator/redisfailover/service/client.go +++ b/operator/redisfailover/service/client.go @@ -52,7 +52,11 @@ func (r *RedisFailoverKubeClient) EnsureSentinelService(rf *redisfailoverv1alpha // EnsureSentinelConfigMap makes sure the sentinel configmap exists func (r *RedisFailoverKubeClient) EnsureSentinelConfigMap(rf *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) error { - if _, err := r.K8SService.GetConfigMap(rf.Namespace, GetSentinelConfigMapName(rf)); err != nil { + if rf.Spec.Sentinel.ConfigMap != "" { + if _, err := r.K8SService.GetConfigMap(rf.Namespace, rf.Spec.Sentinel.ConfigMap); err != nil { + return err + } + } else { cm := generateSentinelConfigMap(rf, labels, ownerRefs) return r.K8SService.CreateOrUpdateConfigMap(rf.Namespace, cm) } @@ -79,7 +83,11 @@ func (r *RedisFailoverKubeClient) EnsureRedisStatefulset(rf *redisfailoverv1alph // EnsureRedisConfigMap makes sure the sentinel configmap exists func (r *RedisFailoverKubeClient) EnsureRedisConfigMap(rf *redisfailoverv1alpha2.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) error { - if _, err := r.K8SService.GetConfigMap(rf.Namespace, GetRedisConfigMapName(rf)); err != nil { + if rf.Spec.Redis.ConfigMap != "" { + if _, err := r.K8SService.GetConfigMap(rf.Namespace, rf.Spec.Redis.ConfigMap); err != nil { + return err + } + } else { cm := generateRedisConfigMap(rf, labels, ownerRefs) return r.K8SService.CreateOrUpdateConfigMap(rf.Namespace, cm) }