From 82ff60838f77d618aee446b5e15972647d3787b3 Mon Sep 17 00:00:00 2001 From: Phoeniix Zhao Date: Wed, 10 Jan 2024 13:55:29 +0800 Subject: [PATCH] refactor: use constant to replace magic number Signed-off-by: Phoeniix Zhao --- internal/transformer/xlinecluster_resource.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/transformer/xlinecluster_resource.go b/internal/transformer/xlinecluster_resource.go index 8265930e..33226e1d 100644 --- a/internal/transformer/xlinecluster_resource.go +++ b/internal/transformer/xlinecluster_resource.go @@ -13,6 +13,10 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" ) +const ( + XlinePort = 2379 +) + func GetServiceKey(xlineClusterName types.NamespacedName) types.NamespacedName { return types.NamespacedName{ Namespace: xlineClusterName.Namespace, @@ -36,7 +40,7 @@ func GetMemberTopology(stsRef types.NamespacedName, svcName string, replicas int for i := 0; i < replicas; i++ { podName := fmt.Sprintf("%s-%d", stsRef.Name, i) dnsName := fmt.Sprintf("%s.%s.%s.svc.cluster.local", podName, svcName, stsRef.Namespace) - members[i] = fmt.Sprintf("%s=%s:2379", podName, dnsName) + members[i] = fmt.Sprintf("%s=%s:%d", podName, dnsName, XlinePort) } return strings.Join(members, ",") } @@ -53,7 +57,7 @@ func MakeService(cr *xapi.XlineCluster, scheme *runtime.Scheme) *corev1.Service Ports: []corev1.ServicePort{ { Name: "xline-port", - Port: 2379, + Port: XlinePort, }, }, Selector: svcLabel, @@ -76,7 +80,7 @@ func MakeStatefulSet(cr *xapi.XlineCluster, scheme *runtime.Scheme) *appv1.State Image: *cr.Spec.Image, ImagePullPolicy: cr.Spec.ImagePullPolicy, Ports: []corev1.ContainerPort{ - {Name: "xline-port", ContainerPort: 2379}, + {Name: "xline-port", ContainerPort: XlinePort}, }, Env: []corev1.EnvVar{ {Name: "MEMBERS", Value: GetMemberTopology(stsRef, svcName, int(cr.Spec.Replicas))},