Skip to content

Commit

Permalink
Separate ConfigMap templating and creation (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
lllamnyp committed Sep 10, 2024
2 parents 51131a1 + cb2a88f commit e00e6cb
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions internal/controller/factory/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package factory
import (
"context"
"fmt"
"strings"

etcdaenixiov1alpha1 "github.com/aenix-io/etcd-operator/api/v1alpha1"
"github.com/aenix-io/etcd-operator/internal/log"
Expand All @@ -39,39 +40,16 @@ func CreateOrUpdateClusterStateConfigMap(
rclient client.Client,
) error {
var err error
initialCluster := ""
clusterService := fmt.Sprintf("%s.%s.svc:2380", GetHeadlessServiceName(cluster), cluster.Namespace)
for i := int32(0); i < *cluster.Spec.Replicas; i++ {
if i > 0 {
initialCluster += ","
}
podName := fmt.Sprintf("%s-%d", cluster.Name, i)
initialCluster += fmt.Sprintf("%s=https://%s.%s",
podName, podName, clusterService,
)
}

configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: cluster.Namespace,
Name: GetClusterStateConfigMapName(cluster),
},
Data: map[string]string{
"ETCD_INITIAL_CLUSTER_STATE": "new",
"ETCD_INITIAL_CLUSTER": initialCluster,
"ETCD_INITIAL_CLUSTER_TOKEN": cluster.Name + "-" + cluster.Namespace,
},
state := "new"
if isEtcdClusterReady(cluster) {
state = "existing"
}
configMap := TemplateClusterStateConfigMap(cluster, state, int(*cluster.Spec.Replicas))
ctx, err = contextWithGVK(ctx, configMap, rclient.Scheme())
if err != nil {
return err
}

if isEtcdClusterReady(cluster) {
// update cluster state to existing
log.Debug(ctx, "updating cluster state")
configMap.Data["ETCD_INITIAL_CLUSTER_STATE"] = "existing"
}
log.Debug(ctx, "configmap data generated", "data", configMap.Data)

if err := ctrl.SetControllerReference(cluster, configMap, rclient.Scheme()); err != nil {
Expand All @@ -88,3 +66,32 @@ func isEtcdClusterReady(cluster *etcdaenixiov1alpha1.EtcdCluster) bool {
return cond != nil && (cond.Reason == string(etcdaenixiov1alpha1.EtcdCondTypeStatefulSetReady) ||
cond.Reason == string(etcdaenixiov1alpha1.EtcdCondTypeStatefulSetNotReady))
}
func TemplateClusterStateConfigMap(cluster *etcdaenixiov1alpha1.EtcdCluster, state string, replicas int) *corev1.ConfigMap {

initialClusterMembers := make([]string, replicas)
clusterService := fmt.Sprintf("%s.%s.svc:2380", GetHeadlessServiceName(cluster), cluster.Namespace)
for i := 0; i < replicas; i++ {
podName := fmt.Sprintf("%s-%d", cluster.Name, i)
initialClusterMembers[i] = fmt.Sprintf("%s=https://%s.%s",
podName, podName, clusterService,
)
}
initialCluster := strings.Join(initialClusterMembers, ",")

configMap := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-cluster-state", cluster.Name),
Namespace: cluster.Namespace,
},
Data: map[string]string{
"ETCD_INITIAL_CLUSTER_STATE": state,
"ETCD_INITIAL_CLUSTER": initialCluster,
"ETCD_INITIAL_CLUSTER_TOKEN": fmt.Sprintf("%s-%s", cluster.Name, cluster.Namespace),
},
}
return configMap
}

0 comments on commit e00e6cb

Please sign in to comment.