@@ -19,6 +19,7 @@ package factory
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "strings"
22
23
23
24
etcdaenixiov1alpha1 "github.com/aenix-io/etcd-operator/api/v1alpha1"
24
25
"github.com/aenix-io/etcd-operator/internal/log"
@@ -39,39 +40,16 @@ func CreateOrUpdateClusterStateConfigMap(
39
40
rclient client.Client ,
40
41
) error {
41
42
var err error
42
- initialCluster := ""
43
- clusterService := fmt .Sprintf ("%s.%s.svc:2380" , GetHeadlessServiceName (cluster ), cluster .Namespace )
44
- for i := int32 (0 ); i < * cluster .Spec .Replicas ; i ++ {
45
- if i > 0 {
46
- initialCluster += ","
47
- }
48
- podName := fmt .Sprintf ("%s-%d" , cluster .Name , i )
49
- initialCluster += fmt .Sprintf ("%s=https://%s.%s" ,
50
- podName , podName , clusterService ,
51
- )
52
- }
53
-
54
- configMap := & corev1.ConfigMap {
55
- ObjectMeta : metav1.ObjectMeta {
56
- Namespace : cluster .Namespace ,
57
- Name : GetClusterStateConfigMapName (cluster ),
58
- },
59
- Data : map [string ]string {
60
- "ETCD_INITIAL_CLUSTER_STATE" : "new" ,
61
- "ETCD_INITIAL_CLUSTER" : initialCluster ,
62
- "ETCD_INITIAL_CLUSTER_TOKEN" : cluster .Name + "-" + cluster .Namespace ,
63
- },
43
+ state := "new"
44
+ if isEtcdClusterReady (cluster ) {
45
+ state = "existing"
64
46
}
47
+ configMap := TemplateClusterStateConfigMap (cluster , state , int (* cluster .Spec .Replicas ))
65
48
ctx , err = contextWithGVK (ctx , configMap , rclient .Scheme ())
66
49
if err != nil {
67
50
return err
68
51
}
69
52
70
- if isEtcdClusterReady (cluster ) {
71
- // update cluster state to existing
72
- log .Debug (ctx , "updating cluster state" )
73
- configMap .Data ["ETCD_INITIAL_CLUSTER_STATE" ] = "existing"
74
- }
75
53
log .Debug (ctx , "configmap data generated" , "data" , configMap .Data )
76
54
77
55
if err := ctrl .SetControllerReference (cluster , configMap , rclient .Scheme ()); err != nil {
@@ -88,3 +66,32 @@ func isEtcdClusterReady(cluster *etcdaenixiov1alpha1.EtcdCluster) bool {
88
66
return cond != nil && (cond .Reason == string (etcdaenixiov1alpha1 .EtcdCondTypeStatefulSetReady ) ||
89
67
cond .Reason == string (etcdaenixiov1alpha1 .EtcdCondTypeStatefulSetNotReady ))
90
68
}
69
+ func TemplateClusterStateConfigMap (cluster * etcdaenixiov1alpha1.EtcdCluster , state string , replicas int ) * corev1.ConfigMap {
70
+
71
+ initialClusterMembers := make ([]string , replicas )
72
+ clusterService := fmt .Sprintf ("%s.%s.svc:2380" , GetHeadlessServiceName (cluster ), cluster .Namespace )
73
+ for i := 0 ; i < replicas ; i ++ {
74
+ podName := fmt .Sprintf ("%s-%d" , cluster .Name , i )
75
+ initialClusterMembers [i ] = fmt .Sprintf ("%s=https://%s.%s" ,
76
+ podName , podName , clusterService ,
77
+ )
78
+ }
79
+ initialCluster := strings .Join (initialClusterMembers , "," )
80
+
81
+ configMap := & corev1.ConfigMap {
82
+ TypeMeta : metav1.TypeMeta {
83
+ APIVersion : "v1" ,
84
+ Kind : "ConfigMap" ,
85
+ },
86
+ ObjectMeta : metav1.ObjectMeta {
87
+ Name : fmt .Sprintf ("%s-cluster-state" , cluster .Name ),
88
+ Namespace : cluster .Namespace ,
89
+ },
90
+ Data : map [string ]string {
91
+ "ETCD_INITIAL_CLUSTER_STATE" : state ,
92
+ "ETCD_INITIAL_CLUSTER" : initialCluster ,
93
+ "ETCD_INITIAL_CLUSTER_TOKEN" : fmt .Sprintf ("%s-%s" , cluster .Name , cluster .Namespace ),
94
+ },
95
+ }
96
+ return configMap
97
+ }
0 commit comments