Skip to content

Commit

Permalink
Separate ConfigMap templating and creation
Browse files Browse the repository at this point in the history
This commit factors out the building of a corev1.ConfigMap struct into a separate function, while leaving the logic of determining key parameters, such as the initial cluster, state, etc, as well as sending the request to the k8s API server in the already existing CreateOrUpdate function. This will be useful in the future, when this logic will be moved out of the factory library and into the reconciliation loop.
  • Loading branch information
lllamnyp committed Sep 10, 2024
1 parent 51131a1 commit cb2a88f
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 cb2a88f

Please sign in to comment.