diff --git a/README.md b/README.md index 8db04cb1..d84482f1 100644 --- a/README.md +++ b/README.md @@ -58,9 +58,9 @@ my-xline-cluster - # Get Xline pod $ kubectl get pods NAME READY STATUS RESTARTS AGE -my-xline-cluster-sts-0 1/1 Running 0 - -my-xline-cluster-sts-1 1/1 Running 0 - -my-xline-cluster-sts-2 1/1 Running 0 - +my-xline-cluster-0 1/1 Running 0 - +my-xline-cluster-1 1/1 Running 0 - +my-xline-cluster-2 1/1 Running 0 - ``` ### Delete the xline cluster diff --git a/internal/controller/xlinecluster_controller_test.go b/internal/controller/xlinecluster_controller_test.go index 7528654a..1e39773c 100644 --- a/internal/controller/xlinecluster_controller_test.go +++ b/internal/controller/xlinecluster_controller_test.go @@ -46,8 +46,6 @@ var _ = Describe("XlineCluster controller", func() { const ( XlineClusterName = "test-xline-cluster" XlineClusterNamespace = "default" - XlineClusterStsName = "test-xline-cluster-sts" - XlineClusterSvcName = "test-xline-cluster-svc" timeout = time.Second * 10 duration = time.Second * 10 @@ -92,8 +90,8 @@ var _ = Describe("XlineCluster controller", func() { Stage: xapi.StageComplete, StageStatus: xapi.StageResultSucceeded, Image: image, - StatefulSetRef: xapi.NewNamespacedName(xlineNamespaceName(XlineClusterStsName, XlineClusterNamespace)), - ServiceRef: xapi.NewNamespacedName(xlineNamespaceName(XlineClusterSvcName, XlineClusterNamespace)), + StatefulSetRef: xapi.NewNamespacedName(xlineNamespaceName(XlineClusterName, XlineClusterNamespace)), + ServiceRef: xapi.NewNamespacedName(xlineNamespaceName(XlineClusterName, XlineClusterNamespace)), } Eventually(func() (ExpectClusterStatus, error) { diff --git a/internal/reconciler/cluster_sync.go b/internal/reconciler/cluster_sync.go index 5bfe3a7c..c6c832cb 100644 --- a/internal/reconciler/cluster_sync.go +++ b/internal/reconciler/cluster_sync.go @@ -2,7 +2,6 @@ package reconciler import ( xapi "github.com/xline-kv/xline-operator/api/v1alpha1" - tran "github.com/xline-kv/xline-operator/internal/transformer" appv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" ) @@ -16,25 +15,24 @@ func (r *XlineClusterReconciler) Sync() (xapi.XlineClusterSyncStatus, error) { // sync XlineCluster status func (r *XlineClusterReconciler) syncXlineStatus(xlineStatus *xapi.XlineClusterSyncStatus) error { - svcRef := tran.GetServiceKey(r.CR.ObjKey()) + xcLookupKey := r.CR.ObjKey() svc := &corev1.Service{} - exist, err := r.Exist(svcRef, svc) + exist, err := r.Exist(xcLookupKey, svc) if err != nil { return err } if exist { - xlineStatus.ServiceRef = xapi.NewNamespacedName(svcRef) + xlineStatus.ServiceRef = xapi.NewNamespacedName(xcLookupKey) } - stsRef := tran.GetStatefulSetKey(r.CR.ObjKey()) sts := &appv1.StatefulSet{} - exist, err = r.Exist(stsRef, sts) + exist, err = r.Exist(xcLookupKey, sts) if err != nil { return err } if exist { xlineStatus.Image = *r.CR.Spec.Image - xlineStatus.StatefulSetRef = xapi.NewNamespacedName(stsRef) + xlineStatus.StatefulSetRef = xapi.NewNamespacedName(xcLookupKey) xlineStatus.Conditions = sts.Status.Conditions } diff --git a/internal/transformer/xlinecluster_resource.go b/internal/transformer/xlinecluster_resource.go index 9616e1fa..a037ee3c 100644 --- a/internal/transformer/xlinecluster_resource.go +++ b/internal/transformer/xlinecluster_resource.go @@ -19,29 +19,16 @@ const ( DataDir = "/usr/local/xline/data-dir" ) -func GetServiceKey(xlineClusterName types.NamespacedName) types.NamespacedName { - return types.NamespacedName{ - Namespace: xlineClusterName.Namespace, - Name: fmt.Sprintf("%s-svc", xlineClusterName.Name), - } -} - -func GetStatefulSetKey(xlineClusterName types.NamespacedName) types.NamespacedName { - return types.NamespacedName{ - Namespace: xlineClusterName.Namespace, - Name: fmt.Sprintf("%s-sts", xlineClusterName.Name), - } -} - func GetXlineInstanceLabels(xlineClusterName types.NamespacedName) map[string]string { return MakeResourceLabels(xlineClusterName.Name) } -func GetMemberTopology(stsRef types.NamespacedName, svcName string, replicas int) string { +func GetMemberTopology(cr *xapi.XlineCluster) string { + replicas := int(cr.Spec.Replicas) members := make([]string, replicas) 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) + podName := fmt.Sprintf("%s-%d", cr.Name, i) + dnsName := fmt.Sprintf("%s.%s.%s.svc.cluster.local", podName, cr.Name, cr.Namespace) members[i] = fmt.Sprintf("%s=%s:%d", podName, dnsName, XlinePort) } return strings.Join(members, ",") @@ -80,12 +67,11 @@ func GetAuthSecretEnvVars(auth_sec *xapi.XlineAuthSecret) []corev1.EnvVar { } func MakeService(cr *xapi.XlineCluster, scheme *runtime.Scheme) *corev1.Service { - svcRef := GetServiceKey(cr.ObjKey()) svcLabel := GetXlineInstanceLabels(cr.ObjKey()) service := &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ - Name: svcRef.Name, - Namespace: svcRef.Namespace, + Name: cr.Name, + Namespace: cr.Namespace, }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ @@ -104,9 +90,7 @@ func MakeService(cr *xapi.XlineCluster, scheme *runtime.Scheme) *corev1.Service func MakeStatefulSet(cr *xapi.XlineCluster, scheme *runtime.Scheme) *appv1.StatefulSet { crName := types.NamespacedName{Namespace: cr.Namespace, Name: cr.Name} - stsRef := GetStatefulSetKey(crName) stsLabels := GetXlineInstanceLabels(crName) - svcName := GetServiceKey(cr.ObjKey()).Name initCmd := []string{ "xline", @@ -118,7 +102,7 @@ func MakeStatefulSet(cr *xapi.XlineCluster, scheme *runtime.Scheme) *appv1.State initCmd = append(initCmd, cr.Spec.BootArgs()...) envs := []corev1.EnvVar{ - {Name: "MEMBERS", Value: GetMemberTopology(stsRef, svcName, int(cr.Spec.Replicas))}, + {Name: "MEMBERS", Value: GetMemberTopology(cr)}, {Name: "POD_NAME", ValueFrom: &corev1.EnvVarSource{ FieldRef: &corev1.ObjectFieldSelector{ FieldPath: "metadata.name", @@ -164,13 +148,13 @@ func MakeStatefulSet(cr *xapi.XlineCluster, scheme *runtime.Scheme) *appv1.State // statefulset statefulSet := &appv1.StatefulSet{ ObjectMeta: metav1.ObjectMeta{ - Name: stsRef.Name, - Namespace: stsRef.Namespace, + Name: cr.Name, + Namespace: cr.Namespace, Labels: stsLabels, }, Spec: appv1.StatefulSetSpec{ Replicas: &cr.Spec.Replicas, - ServiceName: svcName, + ServiceName: cr.Name, Selector: &metav1.LabelSelector{MatchLabels: stsLabels}, VolumeClaimTemplates: pvcTemplates, Template: podTemplate, diff --git a/internal/transformer/xlinecluster_resource_test.go b/internal/transformer/xlinecluster_resource_test.go index 86ad5c5a..fabfca37 100644 --- a/internal/transformer/xlinecluster_resource_test.go +++ b/internal/transformer/xlinecluster_resource_test.go @@ -23,34 +23,17 @@ func TestXlineClusterFunc(t *testing.T) { }, } - t.Run("GetServiceKey should work properly", func(t *testing.T) { - xcLookupKey := xlineCluster.ObjKey() - svcObj := GetServiceKey(xcLookupKey) - assert.Equal(t, svcObj.Namespace, "default") - assert.Equal(t, svcObj.Name, "xline-svc") - }) - - t.Run("GetStatefulSetKey should work properly", func(t *testing.T) { - xcLookupKey := xlineCluster.ObjKey() - stsObj := GetStatefulSetKey(xcLookupKey) - assert.Equal(t, stsObj.Namespace, "default") - assert.Equal(t, stsObj.Name, "xline-sts") - }) - t.Run("GetXlineImage should work properly", func(t *testing.T) { xlineImage := *xlineCluster.Spec.Image assert.Equal(t, xlineImage, "xline-img:latest") }) t.Run("GetMemberTopology should work properly", func(t *testing.T) { - xcLookupKey := xlineCluster.ObjKey() - stsRef := GetStatefulSetKey(xcLookupKey) - svcName := GetServiceKey(xcLookupKey).Name - topology := GetMemberTopology(stsRef, svcName, 3) + topology := GetMemberTopology(&xlineCluster) topologyVec := strings.Split(topology, ",") assert.Equal(t, len(topologyVec), 3) for i := 0; i < 3; i++ { - expectRes := fmt.Sprintf("xline-sts-%d=xline-sts-%d.xline-svc.default.svc.cluster.local:2379", i, i) + expectRes := fmt.Sprintf("xline-%d=xline-%d.xline.default.svc.cluster.local:2379", i, i) assert.Equal(t, topologyVec[i], expectRes) } }) diff --git a/tests/e2e/cases/ci.sh b/tests/e2e/cases/ci.sh index 2bd99620..5f3f0ca3 100644 --- a/tests/e2e/cases/ci.sh +++ b/tests/e2e/cases/ci.sh @@ -4,8 +4,6 @@ source "${E2E_TEST_DIR}/common/common.sh" source "${E2E_TEST_DIR}/testenv/testenv.sh" _TEST_CI_CLUSTER_NAME="my-xline-cluster" -_TEST_CI_STS_NAME="$_TEST_CI_CLUSTER_NAME-sts" -_TEST_CI_SVC_NAME="$_TEST_CI_CLUSTER_NAME-svc" _TEST_CI_SECRET_NAME="auth-cred" _TEST_CI_NAMESPACE="default" _TEST_CI_DNS_SUFFIX="svc.cluster.local" @@ -14,9 +12,9 @@ _TEST_CI_STORAGECLASS_NAME="e2e-storage" _TEST_CI_LOG_SYNC_TIMEOUT=30 function test::ci::_mk_endpoints() { - local endpoints="${_TEST_CI_STS_NAME}-0.${_TEST_CI_SVC_NAME}.${_TEST_CI_NAMESPACE}.${_TEST_CI_DNS_SUFFIX}:${_TEST_CI_XLINE_PORT}" + local endpoints="${_TEST_CI_CLUSTER_NAME}-0.${_TEST_CI_CLUSTER_NAME}.${_TEST_CI_NAMESPACE}.${_TEST_CI_DNS_SUFFIX}:${_TEST_CI_XLINE_PORT}" for ((i = 1; i < $1; i++)); do - endpoints="${endpoints},${_TEST_CI_STS_NAME}-${i}.${_TEST_CI_SVC_NAME}.${_TEST_CI_NAMESPACE}.${_TEST_CI_DNS_SUFFIX}:${_TEST_CI_XLINE_PORT}" + endpoints="${endpoints},${_TEST_CI_CLUSTER_NAME}-${i}.${_TEST_CI_CLUSTER_NAME}.${_TEST_CI_NAMESPACE}.${_TEST_CI_DNS_SUFFIX}:${_TEST_CI_XLINE_PORT}" done echo "$endpoints" } @@ -84,8 +82,8 @@ function test::ci::_uninstall_CRD() { function test::ci::wait_all_xline_pod_ready() { for ((i = 0; i < $1; i++)); do - log::info "wait pod/${_TEST_CI_STS_NAME}-${i} to be ready" - if ! k8s::kubectl wait --for=condition=Ready pod/${_TEST_CI_STS_NAME}-${i} --timeout=300s; then + log::info "wait pod/${_TEST_CI_CLUSTER_NAME}-${i} to be ready" + if ! k8s::kubectl wait --for=condition=Ready pod/${_TEST_CI_CLUSTER_NAME}-${i} --timeout=300s; then log::fatal "Failed to wait for util to be ready" fi done @@ -93,8 +91,8 @@ function test::ci::wait_all_xline_pod_ready() { function test::ci::wait_all_xline_pod_deleted() { for ((i = 0; i < $1; i++)); do - log::info "wait pod/${_TEST_CI_STS_NAME}-${i} to be ready" - if ! k8s::kubectl wait --for=delete pod/${_TEST_CI_STS_NAME}-${i} --timeout=300s; then + log::info "wait pod/${_TEST_CI_CLUSTER_NAME}-${i} to be ready" + if ! k8s::kubectl wait --for=delete pod/${_TEST_CI_CLUSTER_NAME}-${i} --timeout=300s; then log::fatal "Failed to wait for util to be ready" fi done @@ -112,7 +110,7 @@ function test::ci::_prepare_pv() { function test::ci::_clean_pvc() { for ((i = 0; i < $1; i++)); do - local pvc_name="xline-storage-${_TEST_CI_STS_NAME}-${i}" + local pvc_name="xline-storage-${_TEST_CI_CLUSTER_NAME}-${i}" log::info "deleting pvc $pvc_name ..." k8s::kubectl delete pvc $pvc_name >/dev/null 2>&1 if ! k8s::kubectl wait --for=delete pvc/${pvc_name} --timeout=300s; then @@ -144,7 +142,7 @@ function test::ci::_start() { test::ci::_prepare_pv log::info "starting xline cluster..." k8s::kubectl apply -f "$(dirname "${BASH_SOURCE[0]}")/manifests/cluster.yaml" >/dev/null 2>&1 - k8s::kubectl::wait_resource_creation sts $_TEST_CI_STS_NAME + k8s::kubectl::wait_resource_creation sts $_TEST_CI_CLUSTER_NAME } @@ -175,7 +173,7 @@ function test::ci::_chaos() { kill=$((RANDOM % fault_tolerance + 1)) log::info "chaos: kill=$kill" for ((j = 0; j < $kill; j++)); do - pod="${_TEST_CI_STS_NAME}-$((RANDOM % size))" + pod="${_TEST_CI_CLUSTER_NAME}-$((RANDOM % size))" log::info "chaos: kill pod=$pod" k8s::kubectl delete pod "$pod" --force --grace-period=0 2>/dev/null done