Skip to content

Commit e00e6cb

Browse files
authored
Separate ConfigMap templating and creation (#259)
2 parents 51131a1 + cb2a88f commit e00e6cb

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

internal/controller/factory/configmap.go

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package factory
1919
import (
2020
"context"
2121
"fmt"
22+
"strings"
2223

2324
etcdaenixiov1alpha1 "github.com/aenix-io/etcd-operator/api/v1alpha1"
2425
"github.com/aenix-io/etcd-operator/internal/log"
@@ -39,39 +40,16 @@ func CreateOrUpdateClusterStateConfigMap(
3940
rclient client.Client,
4041
) error {
4142
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"
6446
}
47+
configMap := TemplateClusterStateConfigMap(cluster, state, int(*cluster.Spec.Replicas))
6548
ctx, err = contextWithGVK(ctx, configMap, rclient.Scheme())
6649
if err != nil {
6750
return err
6851
}
6952

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-
}
7553
log.Debug(ctx, "configmap data generated", "data", configMap.Data)
7654

7755
if err := ctrl.SetControllerReference(cluster, configMap, rclient.Scheme()); err != nil {
@@ -88,3 +66,32 @@ func isEtcdClusterReady(cluster *etcdaenixiov1alpha1.EtcdCluster) bool {
8866
return cond != nil && (cond.Reason == string(etcdaenixiov1alpha1.EtcdCondTypeStatefulSetReady) ||
8967
cond.Reason == string(etcdaenixiov1alpha1.EtcdCondTypeStatefulSetNotReady))
9068
}
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

Comments
 (0)