Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate ConfigMap templating and creation #259

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
lllamnyp marked this conversation as resolved.
Show resolved Hide resolved
}
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
}