Skip to content

Commit

Permalink
Move initial config to ConfigMap from sts cli flags
Browse files Browse the repository at this point in the history
  • Loading branch information
sircthulhu committed Mar 20, 2024
1 parent 94c538d commit 89f2976
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
29 changes: 15 additions & 14 deletions internal/controller/etcdcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,27 @@ func (r *EtcdClusterReconciler) ensureClusterStateConfigMap(

// configmap does not exist, create with cluster state "new"
if errors.IsNotFound(err) {
initialCluster := ""
for i := uint(0); i < cluster.Spec.Replicas; i++ {
if i > 0 {
initialCluster += ","
}
initialCluster += fmt.Sprintf("%s-%d=https://%s-%d.%s.%s.svc:2380",
cluster.Name, i,
cluster.Name, i, cluster.Name, cluster.Namespace,
)
}

configMap = &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: cluster.Namespace,
Name: r.getClusterStateConfigMapName(cluster),
},
Data: map[string]string{
"ETCD_INITIAL_CLUSTER_STATE": "new",
"ETCD_INITIAL_CLUSTER_STATE": "new",
"ETCD_INITIAL_CLUSTER": initialCluster,
"ETCD_INITIAL_CLUSTER_TOKEN": cluster.Name + "-" + cluster.Namespace,
"ETCD_INITIAL_ADVERTISE_PEER_URLS": "https://$(POD_NAME)." + cluster.Name + ".$(POD_NAMESPACE).svc:2380",
},
}
if err := ctrl.SetControllerReference(cluster, configMap, r.Scheme); err != nil {
Expand Down Expand Up @@ -301,16 +315,6 @@ func (r *EtcdClusterReconciler) ensureClusterStatefulSet(
if errors.IsNotFound(err) {
notFound = true
// prepare initial cluster members
initialCluster := ""
for i := uint(0); i < cluster.Spec.Replicas; i++ {
if i > 0 {
initialCluster += ","
}
initialCluster += fmt.Sprintf("%s-%d=https://%s-%d.%s.%s.svc:2380",
cluster.Name, i,
cluster.Name, i, cluster.Name, cluster.Namespace,
)
}

statefulSet = &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -348,10 +352,7 @@ func (r *EtcdClusterReconciler) ensureClusterStatefulSet(
"--listen-peer-urls=https://0.0.0.0:2380",
// for first version disable TLS for client access
"--listen-client-urls=http://0.0.0.0:2379",
"--initial-advertise-peer-urls=https://$(POD_NAME)." + cluster.Name + ".$(POD_NAMESPACE).svc:2380",
"--data-dir=/var/run/etcd/default.etcd",
"--initial-cluster=" + initialCluster,
fmt.Sprintf("--initial-cluster-token=%s-%s", cluster.Name, cluster.Namespace),
"--auto-tls",
"--peer-auto-tls",
"--advertise-client-urls=http://$(POD_NAME)." + cluster.Name + ".$(POD_NAMESPACE).svc:2379",
Expand Down
4 changes: 1 addition & 3 deletions internal/controller/etcdcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ var _ = Describe("EtcdCluster Controller", func() {
}
err = k8sClient.Get(ctx, cmName, cm)
Expect(err).NotTo(HaveOccurred(), "cluster configmap state should exist")
Expect(cm.Data).To(Equal(map[string]string{
"ETCD_INITIAL_CLUSTER_STATE": "new",
}))
Expect(cm.Data).To(HaveKeyWithValue("ETCD_INITIAL_CLUSTER_STATE", "new"))
// check that Service is created
svc := &v1.Service{}
err = k8sClient.Get(ctx, typeNamespacedName, svc)
Expand Down

0 comments on commit 89f2976

Please sign in to comment.